chabab commited on
Commit
1ecd6e2
·
verified ·
1 Parent(s): 7fe014a

Add model card with eval results

Browse files
Files changed (1) hide show
  1. README.md +111 -37
README.md CHANGED
@@ -1,59 +1,133 @@
1
  ---
2
  base_model: google/gemma-3-270m-it
 
 
3
  library_name: transformers
4
- model_name: gemma-3-270m-text2sql-oracle-postgres
 
5
  tags:
6
- - generated_from_trainer
7
- - sft
8
- - trl
9
- - hf_jobs
10
- licence: license
 
 
11
  ---
12
 
13
- # Model Card for gemma-3-270m-text2sql-oracle-postgres
14
 
15
- This model is a fine-tuned version of [google/gemma-3-270m-it](https://huggingface.co/google/gemma-3-270m-it).
16
- It has been trained using [TRL](https://github.com/huggingface/trl).
 
17
 
18
- ## Quick start
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ```python
21
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"
24
- generator = pipeline("text-generation", model="chabab/gemma-3-270m-text2sql-oracle-postgres", device="cuda")
25
- output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
26
- print(output["generated_text"])
 
 
 
 
 
27
  ```
28
 
29
- ## Training procedure
 
 
 
 
 
 
 
30
 
31
-
 
32
 
 
33
 
34
- This model was trained with SFT.
 
35
 
36
- ### Framework versions
 
 
 
 
 
 
 
 
37
 
38
- - TRL: 0.24.0
39
- - Transformers: 4.57.1
40
- - Pytorch: 2.13.0
41
- - Datasets: 5.0.1
42
- - Tokenizers: 0.22.2
43
 
44
- ## Citations
45
 
 
 
 
 
46
 
 
47
 
48
- Cite TRL as:
49
-
50
- ```bibtex
51
- @misc{vonwerra2022trl,
52
- title = {{TRL: Transformer Reinforcement Learning}},
53
- author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
54
- year = 2020,
55
- journal = {GitHub repository},
56
- publisher = {GitHub},
57
- howpublished = {\url{https://github.com/huggingface/trl}}
58
- }
59
- ```
 
1
  ---
2
  base_model: google/gemma-3-270m-it
3
+ datasets:
4
+ - chabab/text2sql-oracle-postgres
5
  library_name: transformers
6
+ license: apache-2.0
7
+ pipeline_tag: text-generation
8
  tags:
9
+ - text-to-sql
10
+ - sql
11
+ - oracle
12
+ - postgresql
13
+ - gemma3
14
+ - trl
15
+ - sft
16
  ---
17
 
18
+ # gemma-3-270m-text2sql-oracle-postgres
19
 
20
+ [`google/gemma-3-270m-it`](https://huggingface.co/google/gemma-3-270m-it) fine-tuned to turn a
21
+ schema + a natural-language question into **one** dialect-correct SQL statement — Oracle or
22
+ PostgreSQL — with no markdown fences and no commentary.
23
 
24
+ At 270M parameters it runs on CPU and quantizes to ~290 MB.
25
+
26
+ ## Results
27
+
28
+ Held-out test split (60 examples), greedy decoding, normalized exact match against gold SQL:
29
+
30
+ | Slice | Exact match |
31
+ |---|---|
32
+ | **Overall** | **78.3%** (47/60) |
33
+ | Oracle | 86.7% (39/45) |
34
+ | PostgreSQL | 53.3% (8/15) |
35
+ | easy | 93.3% (14/15) |
36
+ | medium | 69.2% (27/39) |
37
+ | hard | 100% (6/6) |
38
+
39
+ Per-example predictions are in [`eval_results.json`](./eval_results.json).
40
+
41
+ Caveats worth knowing before you rely on these numbers:
42
+
43
+ - The test split is skewed 45 Oracle / 15 PostgreSQL, so the PostgreSQL figure rests on 15
44
+ examples and has a wide error bar.
45
+ - Exact match is strict. Several "failures" are valid SQL that differs from gold — an extra
46
+ `LIMIT`, a different but equivalent predicate. Real semantic accuracy is higher than 78.3%.
47
+ - The most common genuine error is dialect leakage: emitting `LIKE` where PostgreSQL gold uses
48
+ `ILIKE`. If case-insensitive matching matters to you, check that specific pattern.
49
+ - Only the 7 schemas in the training set (hr, sales, banking, inventory, tickets, university,
50
+ logistics) are represented. Generalization to unseen schemas is untested.
51
+
52
+ ## Usage
53
+
54
+ The model expects the system prompt naming the dialect, then a `Schema:` block and a `Question:`
55
+ block — the same shape as the training data.
56
 
57
  ```python
58
+ import torch
59
+ from transformers import AutoModelForCausalLM, AutoTokenizer
60
+
61
+ model_id = "chabab/gemma-3-270m-text2sql-oracle-postgres"
62
+ tok = AutoTokenizer.from_pretrained(model_id)
63
+ model = AutoModelForCausalLM.from_pretrained(
64
+ model_id,
65
+ dtype=torch.bfloat16,
66
+ attn_implementation="eager", # Gemma-3 needs eager attention for correct generation
67
+ device_map="auto",
68
+ )
69
+
70
+ messages = [
71
+ {"role": "system", "content": "You convert natural language into PostgreSQL SQL. Use only tables and columns from the provided schema. Reply with one SQL statement and nothing else. No markdown fences. No commentary."},
72
+ {"role": "user", "content": """Schema:
73
+ employees(
74
+ employee_id INTEGER PK,
75
+ first_name VARCHAR(50),
76
+ last_name VARCHAR(50),
77
+ hire_date DATE,
78
+ salary NUMERIC(12,2),
79
+ department_id INTEGER FK->departments.department_id
80
+ )
81
 
82
+ Question:
83
+ Show the five employees with the largest salary. Return only the SQL."""},
84
+ ]
85
+
86
+ ids = tok.apply_chat_template(messages, add_generation_prompt=True,
87
+ return_tensors="pt", return_dict=True).to(model.device)
88
+ out = model.generate(**ids, max_new_tokens=256, do_sample=False)
89
+ print(tok.decode(out[0][ids["input_ids"].shape[-1]:], skip_special_tokens=True).strip())
90
+ # SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC LIMIT 5;
91
  ```
92
 
93
+ Two things matter for output quality:
94
+
95
+ - **Use greedy decoding** (`do_sample=False`). The task has one right answer; sampling only adds
96
+ drift.
97
+ - **Use `attn_implementation="eager"`.** Gemma-3 generates degenerate repeated tokens under the
98
+ default SDPA path in some configurations.
99
+
100
+ ## GGUF / local inference
101
 
102
+ Quantized builds for Ollama, LM Studio, and llama.cpp:
103
+ [`chabab/gemma-3-270m-text2sql-oracle-postgres-GGUF`](https://huggingface.co/chabab/gemma-3-270m-text2sql-oracle-postgres-GGUF)
104
 
105
+ ## Training
106
 
107
+ Full-parameter SFT (no LoRA — the model is small enough to tune end to end) with TRL `SFTTrainer`
108
+ on one L4 GPU, about 10 minutes.
109
 
110
+ | | |
111
+ |---|---|
112
+ | Base | `google/gemma-3-270m-it` |
113
+ | Data | `chabab/text2sql-oracle-postgres` — 684 train / 60 validation / 60 test |
114
+ | Epochs | 5 |
115
+ | Effective batch size | 16 (4 × 4 grad accum) |
116
+ | Learning rate | 5e-5, cosine, 10 warmup steps |
117
+ | Max sequence length | 1024 |
118
+ | Precision | bf16 |
119
 
120
+ Final metrics: train loss **0.0256**, eval loss **0.0683**, eval token accuracy **98.5%**.
121
+ Eval loss fell monotonically through training with no divergence.
 
 
 
122
 
123
+ ## Limitations
124
 
125
+ Generated SQL is not validated against a live database. The model can produce syntactically valid
126
+ statements that reference the wrong table or misread the intent — two of the observed test
127
+ failures do exactly that. Review output before executing it, and never run generated SQL against
128
+ production with write permissions.
129
 
130
+ ## License
131
 
132
+ Apache 2.0, inheriting the [Gemma terms of use](https://ai.google.dev/gemma/terms) from the base
133
+ model.