File size: 4,993 Bytes
2b3c3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
license: apache-2.0
base_model: Qwen/Qwen2.5-Coder-1.5B-Instruct
datasets:
- xlangai/spider
language:
- en
library_name: transformers
pipeline_tag: text-generation
tags:
- text-to-sql
- sql
- spider
- sqlite
- qwen2
- lora
model-index:
- name: Qwen2.5-Coder-1.5B-Spider-SQL
  results:
  - task:
      type: text-to-sql
      name: Text-to-SQL
    dataset:
      type: xlangai/spider
      name: Spider (dev)
      split: validation
    metrics:
    - type: execution_accuracy
      value: 69.8
      name: Execution accuracy
    - type: exact_match
      value: 62.2
      name: Exact-set match
---

# Qwen2.5-Coder-1.5B-Spider-SQL

A text-to-SQL model: give it a SQLite schema (`CREATE TABLE` statements) and a question in English, and it replies with one SQL query.

It is [Qwen/Qwen2.5-Coder-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-1.5B-Instruct) fine-tuned with LoRA on the [Spider](https://yale-lily.github.io/spider) training set, with the adapter merged into the weights. It loads like any `transformers` model, no PEFT needed.

## Results (Spider dev, 1034 questions)

Scored with the official Spider evaluator ([test-suite-sql-eval](https://github.com/taoyds/test-suite-sql-eval), `--etype all`), greedy decoding, same prompt for both models.

**Execution accuracy** (predicted and gold query return the same rows):

| | easy (248) | medium (446) | hard (174) | extra (166) | **all** |
|---|---|---|---|---|---|
| Qwen2.5-Coder-1.5B-Instruct (base) | 81.5 | 61.0 | 42.5 | 34.9 | **58.6** |
| **This model** | 87.5 | 72.9 | 56.9 | 48.8 | **69.8** |

**Exact-set match** (SQL structure matches gold, values ignored):

| | easy | medium | hard | extra | **all** |
|---|---|---|---|---|---|
| Base | 76.6 | 33.9 | 25.3 | 12.7 | 39.3 |
| **This model** | 83.1 | 63.5 | 46.0 | 44.6 | **62.2** |

Execution was checked against the original Spider dev databases, not the larger test-suite databases, so the test-suite number may be slightly lower. The dev set was also used to pick training settings.

## How to use

The model was trained on one exact prompt format. Use the same system prompt and the `Schema:` / `Question:` layout below, or results get worse.

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

repo = "HideIron/Qwen2.5-Coder-1.5B-Spider-SQL"
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, dtype="bfloat16", device_map="auto")

SYSTEM = ("You are a text-to-SQL assistant. Given a SQLite database schema and a "
          "question, reply with a single SQL query and nothing else.")

schema = """CREATE TABLE singer (singer_id INT PRIMARY KEY, name TEXT, country TEXT, age INT);
CREATE TABLE concert (concert_id INT PRIMARY KEY, concert_name TEXT, year TEXT);
CREATE TABLE singer_in_concert (concert_id INT, singer_id INT,
  FOREIGN KEY (concert_id) REFERENCES concert(concert_id),
  FOREIGN KEY (singer_id) REFERENCES singer(singer_id));"""
question = "How many singers from France performed in a concert in 2014?"

messages = [
    {"role": "system", "content": SYSTEM},
    {"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},
]
inputs = tok.apply_chat_template(messages, add_generation_prompt=True,
                                 return_tensors="pt", return_dict=True).to(model.device)
out = model.generate(**inputs, max_new_tokens=256, do_sample=False)
print(tok.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True))
```

Tips:
- Put the real `CREATE TABLE` statements in the schema, with primary and foreign keys. The model uses the keys to work out joins.
- Use greedy decoding (`do_sample=False`), as in the evaluation.

## Training

| | |
|---|---|
| Base model | Qwen/Qwen2.5-Coder-1.5B-Instruct |
| Data | Spider `train_spider` (7000 question/SQL pairs, 140 databases) |
| Method | LoRA, r=16, alpha=32, dropout 0.05, on q/k/v/o projections (0.28 % of parameters trained), then merged |
| Loss | Cross-entropy on the SQL answer tokens only (prompt tokens masked) |
| Schedule | 2 epochs, 1750 steps, effective batch 8, LR 1e-4, cosine with 3 % warmup, bf16 |
| Max length | 2048 tokens (no training example was truncated) |
| Hardware | 1x NVIDIA A10 (24 GB), about 1 hour |

## Limitations

- **SQLite only.** Trained on SQLite schemas and gold queries; other dialects (Postgres, MySQL, T-SQL) may get SQLite-flavoured SQL.
- **Spider-style questions.** Spider covers 200 small databases with clean names. Large real-world schemas, ambiguous column names, or questions that need outside knowledge are harder; the model has not been evaluated on BIRD or Spider 2.0.
- **One query per answer**, no explanation, no multi-turn clarification.
- **It does not run or check its SQL.** Treat its output as a draft. Never run generated SQL against a production database with write permissions.

## License

The model weights are released under Apache 2.0, the license of the base model. The training data, Spider, is licensed CC BY-SA 4.0.