Results
500 held-out questions (489 scored; 11 skipped because
their reference SQL does not run), same prompt and greedy decoding for every model:
Table with columns: Model, Params trained, Execution accuracy, Exact match, Valid SQL| Model | Params trained | Execution accuracy | Exact match | Valid SQL |
|---|
| Qwen2.5-0.5B-Instruct (no fine-tuning) | 0 | 50.7% | 44.6% | 94.3% |
| Qwen2.5-3B-Instruct (no fine-tuning, 6x larger) | 0 | 57.3% | 57.7% | 97.1% |
| This adapter | 8.8M (1.75%) | 84.9% | 74.0% | 98.8% |
Execution accuracy runs the predicted and reference queries on three differently seeded SQLite databases built
from the schema, and counts a prediction correct only if the results match on all three. Exact string match
undercounts correct SQL, so it is shown for reference only.
What the numbers do and don't mean. Part of the gain is the model learning this dataset's conventions (for
example copying literal values exactly as written in the question), which transfers less to databases with
different conventions. Many remaining "failures" are noise in the WikiSQL-derived reference SQL. Error analysis
is in the GitHub README.
Use it
import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizer base = "Qwen/Qwen2.5-0.5B-Instruct"tok = AutoTokenizer.from_pretrained(base)model = AutoModelForCausalLM.from_pretrained(base, torch_dtype=torch.float16, device_map="auto")model = PeftModel.from_pretrained(model, "khush-ahuja/qwen2.5-0.5b-text2sql-lora").eval() schema = "CREATE TABLE employees (name VARCHAR, dept VARCHAR, salary INTEGER)"question = "Which department has the highest average salary?"system = ("You translate questions into SQLite queries. " "Reply with a single SQL query and nothing else.")messages = [ {"role": "system", "content": system}, {"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},]prompt = tok.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)enc = tok(prompt, return_tensors="pt").to(model.device)out = model.generate(**enc, max_new_tokens=128, do_sample=False)print(tok.decode(out[0, enc["input_ids"].shape[1]:], skip_special_tokens=True))
Use the same system and user prompt format as above; the adapter was trained on it.
Training
- Data: b-mc2/sql-create-context, 20,000 training
examples; train/test split by hashing question + schema, so no example appears in both
- LoRA rank 16, alpha 32, on all attention and MLP projections; base model frozen
- 1 epoch, learning rate 0.0002, cosine schedule; loss on the SQL answer only (prompt tokens masked)
- fp32 master weights with fp16 autocast and gradient checkpointing on a single T4 GPU; 18.7 minutes;
final train loss 0.072
Limitations
Trained on single-table questions with short schemas. It has not been evaluated on multi-table joins, long
schemas or other SQL dialects, and it should not be pointed at a production database without review of the
generated SQL.