File size: 14,618 Bytes
b42a566
 
 
 
 
 
 
 
4afa667
 
b42a566
 
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
 
 
4afa667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42a566
4afa667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
 
 
b42a566
4afa667
b42a566
4afa667
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42a566
 
 
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
 
b42a566
4afa667
 
 
 
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
b42a566
4afa667
 
 
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
b42a566
4afa667
 
b42a566
4afa667
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
 
 
 
 
b42a566
4afa667
 
 
 
 
b42a566
4afa667
b42a566
4afa667
 
 
 
 
 
 
 
 
b42a566
4afa667
b42a566
4afa667
 
 
b42a566
4afa667
 
b42a566
4afa667
 
b42a566
4afa667
 
 
 
b42a566
4afa667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42a566
4afa667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42a566
4afa667
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
---
base_model: Qwen/Qwen2.5-3B-Instruct
library_name: peft
pipeline_tag: text-generation
tags:
- base_model:adapter:Qwen/Qwen2.5-3B-Instruct
- lora
- transformers
datasets:
- xlangai/spider
---

# Distill β€” Fine-tuned Qwen2.5-3B for Text-to-SQL

Distill is a **QLoRA fine-tuned LoRA adapter** based on [`Qwen/Qwen2.5-3B-Instruct`](https://huggingface.co/Qwen/Qwen2.5-3B-Instruct), specialized for **natural-language-to-SQL generation**.

The model takes a database schema and a natural-language question and generates a SQL query.

The model was fine-tuned on the `sql-create-context` dataset using 4-bit QLoRA on a single NVIDIA T4 GPU. The final LoRA adapter is approximately **114 MB** with approximately **21M trainable parameters**.

## Model Details

* **Developed by:** MahboobAlam0
* **Model type:** LoRA / QLoRA adapter
* **Base model:** Qwen/Qwen2.5-3B-Instruct
* **Task:** Natural language β†’ SQL
* **Fine-tuning method:** QLoRA
* **Dataset:** sql-create-context
* **Training examples:** 28,000
* **Validation examples:** 7,858
* **LoRA rank:** 16
* **LoRA alpha:** 32
* **Quantization:** 4-bit NF4 with double quantization
* **Trainable parameters:** ~21M
* **Adapter size:** ~114 MB
* **Hardware:** 1 Γ— NVIDIA T4
* **Training platform:** Kaggle
* **Programming language:** Python 3.12

## Model Description

Distill adapts Qwen2.5-3B-Instruct for text-to-SQL generation using parameter-efficient fine-tuning.

The base model is loaded using 4-bit quantization and kept frozen while LoRA adapters are trained on selected attention and MLP projection layers.

The LoRA adapters target:

* `q_proj`
* `k_proj`
* `v_proj`
* `o_proj`
* `gate_proj`
* `up_proj`
* `down_proj`

The model is designed to generate SQL from a database schema and a natural-language question.

### Architecture

```text
Natural Language Question
          +
    CREATE TABLE Schema
          β”‚
          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Qwen2.5-3B-Instruct                 β”‚
β”‚ Frozen 4-bit NF4 quantized weights β”‚
β”‚                                     β”‚
β”‚ LoRA adapters (~21M parameters)     β”‚
β”‚                                     β”‚
β”‚ Attention: q, k, v, o              β”‚
β”‚ MLP: gate, up, down                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
      Generated SQL
```

## Intended Use

This model is intended for:

* Natural-language database querying
* Text-to-SQL applications
* SQL generation
* Database assistants
* Analytics interfaces
* Business intelligence prototypes
* SQL education and experimentation
* Research into parameter-efficient fine-tuning

A typical input contains:

1. A SQL database schema
2. A natural-language question

The model then generates the corresponding SQL query.

### Example

**Schema**

```sql
CREATE TABLE orders (
    id INTEGER,
    customer VARCHAR,
    amount FLOAT,
    status VARCHAR
);
```

**Question**

```text
What is the total revenue from completed orders?
```

**Generated SQL**

```sql
SELECT SUM(amount)
FROM orders
WHERE status = 'completed';
```

## Out-of-Scope Use

This model should not be treated as an unrestricted autonomous database agent.

It should not be used without appropriate validation for:

* Destructive database operations
* Production database migrations
* Database authorization or access-control decisions
* Security-sensitive operations
* Financial decisions
* Medical decisions
* Legal decisions
* Other high-stakes applications

Generated SQL should always be treated as untrusted model output.

## Limitations

### Dataset limitations

The model was trained on `sql-create-context`, which primarily contains single-table schema examples.

Performance may therefore decrease on:

* Complex multi-table schemas
* Multiple joins
* Nested queries
* Large enterprise schemas
* Highly domain-specific databases

### SQL dialect limitations

The evaluation uses SQLite.

The model should not be assumed to produce correct syntax for PostgreSQL, MySQL, SQL Server, Oracle, or other SQL dialects without additional testing.

### SQL correctness

The model can generate SQL that is syntactically valid but semantically incorrect.

Generated SQL should be validated against the actual database schema and, where appropriate, tested before execution.

### Model size

The base model contains approximately 3B parameters. While this makes the model relatively lightweight, it also limits its ability to handle highly complex SQL generation compared with substantially larger models.

### Evaluation limitations

Execution accuracy was measured using schema-seeded in-memory SQLite databases with empty tables. Consequently, execution accuracy primarily measures structural and semantic SQL equivalence under the evaluation setup rather than correctness against realistic database contents.

## Recommendations

For production applications:

* Validate generated SQL before execution.
* Prefer read-only database credentials.
* Restrict permitted SQL operations.
* Validate referenced tables and columns.
* Use query timeouts and resource limits.
* Test queries against representative data.
* Do not expose unrestricted production database credentials to the model.
* Consider SQL parsing and allowlisting before execution.

## Training

### Training Dataset

The model was fine-tuned on the `sql-create-context` dataset.

| Split      | Examples |
| ---------- | -------: |
| Training   |   28,000 |
| Validation |    7,858 |

The dataset provides database schemas, natural-language questions, and corresponding SQL queries.

### Fine-Tuning Method

The model was trained using **QLoRA**.

The base Qwen2.5-3B-Instruct model was loaded using 4-bit NF4 quantization with double quantization. The base weights were frozen while LoRA adapters were trained.

### Training Configuration

| Parameter            | Value                    |
| -------------------- | ------------------------ |
| Base model           | Qwen/Qwen2.5-3B-Instruct |
| Quantization         | 4-bit NF4                |
| Double quantization  | Enabled                  |
| LoRA rank            | 16                       |
| LoRA alpha           | 32                       |
| Learning rate        | 2e-4                     |
| Scheduler            | Cosine decay             |
| Warmup               | 5%                       |
| Optimizer            | Paged AdamW 8-bit        |
| Effective batch size | 16                       |
| Hardware             | NVIDIA T4                |
| Platform             | Kaggle                   |

The repository reports that LoRA rank 8 underfit during initial experiments, while rank 32 did not provide sufficient improvement to justify its additional adapter memory. Rank 16 was selected for the final training configuration.

### Checkpoint Selection

The best validation checkpoint was reported at approximately step 200.

| Checkpoint     | Step | Validation Loss | Result                  |
| -------------- | ---: | --------------: | ----------------------- |
| checkpoint-200 |  200 |           0.685 | Best checkpoint         |
| checkpoint-800 |  800 |           0.829 | Overfitting / discarded |

The best checkpoint was restored for the final model.

## Evaluation

The model was evaluated on **7,858 held-out examples** from the validation data.

The base-model comparison was performed on a **300-example subset** using the repository's SQL extraction procedure.

### Metrics

#### Exact Match

Exact Match compares normalized generated SQL with the normalized reference SQL.

```text
normalise(prediction) == normalise(reference)
```

This is a strict metric. Semantically equivalent SQL queries can receive different scores if their textual formulation differs.

#### BLEU

BLEU measures token-level n-gram overlap between generated and reference SQL.

#### Execution Accuracy

Generated and reference SQL queries are executed against schema-seeded in-memory SQLite databases.

```text
execution(prediction) == execution(reference)
```

This provides a measure of semantic equivalence that is less sensitive to different SQL formulations.

## Results

| Metric                | Base Qwen2.5-3B |    Distill |
| --------------------- | --------------: | ---------: |
| Exact Match Accuracy  |          2.33%* | **30.67%** |
| BLEU Score            |         51.87%* | **59.74%** |
| Execution Accuracy    |               β€” | **55.03%** |
| Validation Perplexity |               β€” |   **1.98** |

* Base-model measurements were obtained on a 300-example subset.

The repository reports a **13Γ— improvement in exact-match accuracy** compared with its measured base-model result.

### Result Interpretation

The difference between exact-match accuracy and execution accuracy is important:

```text
Exact Match Accuracy: 30.67%
Execution Accuracy:   55.03%
```

This indicates that some generated SQL queries differ from the reference query text while still producing equivalent results under the evaluation setup.

Therefore, exact match alone should not be considered a complete measure of SQL generation quality.

## Base Model Comparison

The repository reports the following comparison:

```text
Qwen2.5-3B-Instruct:
    Exact Match: 2.33%
    BLEU:        51.87%

Distill:
    Exact Match: 30.67%
    BLEU:        59.74%
```

The repository also notes that the base model can return SQL together with Markdown code blocks or explanatory text. SQL extraction methodology therefore has a significant effect on the measured baseline.

## How to Use

This repository contains a PEFT adapter. The compatible Qwen2.5-3B-Instruct base model is required.

### Installation

```bash
pip install torch transformers peft bitsandbytes accelerate
```

### Load the Adapter

```python
import torch
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
)
from peft import PeftModel

base_model = "Qwen/Qwen2.5-3B-Instruct"
adapter_path = "path/to/adapter"

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.float16,
)

tokenizer = AutoTokenizer.from_pretrained(base_model)

model = AutoModelForCausalLM.from_pretrained(
    base_model,
    quantization_config=quantization_config,
    device_map="auto",
)

model = PeftModel.from_pretrained(
    model,
    adapter_path,
)
```

### Generate SQL

```python
schema = """
CREATE TABLE orders (
    id INTEGER,
    customer VARCHAR,
    amount FLOAT,
    status VARCHAR
);
"""

question = "What is the total revenue from completed orders?"

prompt = f"""
Schema:
{schema}

Question:
{question}

SQL:
"""

inputs = tokenizer(
    prompt,
    return_tensors="pt"
).to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=128,
)

response = tokenizer.decode(
    outputs[0],
    skip_special_tokens=True,
)

print(response)
```

## Repository

The complete training and application code is available in the project repository:

https://github.com/MahboobAlam0/Distill---Fine-tuned-Qwen2.5-3B

The repository includes:

* QLoRA training code
* Evaluation scripts
* Inference wrapper
* FastAPI API
* Streamlit interface
* Kaggle training notebook
* Training and validation data
* Docker configuration
* Tests

## Serving

The project includes a FastAPI REST API and Streamlit interface.

### FastAPI

```bash
uvicorn api.main:app --reload --port 8000
```

Example request:

```bash
curl -X POST http://localhost:8000/generate \
  -H "Content-Type: application/json" \
  -d '{
    "schema": "CREATE TABLE products (id INTEGER, name VARCHAR, price FLOAT, stock INTEGER)",
    "question": "Which products are out of stock?"
  }'
```

### Streamlit

```bash
streamlit run app/streamlit_app.py
```

### Docker

```bash
docker-compose up
```

The repository exposes the API on port `8000` and the Streamlit interface on port `8501`.

## Reproducing Training

The repository includes a self-contained Kaggle notebook.

```text
1. Upload kaggle/train_notebook.ipynb to Kaggle.
2. Select an NVIDIA T4 GPU.
3. Run the notebook.
4. Export the resulting LoRA adapter.
```

The repository reports approximately **2 hours** for the documented training run on a single Kaggle T4 GPU.

## Technical Specifications

### Hardware

* 1 Γ— NVIDIA T4
* Approximately 15 GB VRAM

### Software

* Python 3.12
* PyTorch
* Transformers
* PEFT
* BitsAndBytes
* TRL
* NLTK
* SQLite
* FastAPI
* Uvicorn
* Streamlit
* Docker

### Adapter

* LoRA rank: 16
* LoRA alpha: 32
* Approximately 21M trainable parameters
* Approximately 114 MB adapter size

## Environmental Impact

Training was performed on a single NVIDIA T4 GPU on Kaggle.

The project does not provide a measured carbon-emissions value.

| Property         | Value        |
| ---------------- | ------------ |
| Hardware         | NVIDIA T4    |
| GPU count        | 1            |
| Training time    | ~2 hours     |
| Platform         | Kaggle       |
| Compute region   | Not reported |
| Carbon emissions | Not measured |

## License

The GitHub project is released under the **MIT License**.

However, this model is a fine-tuned adapter based on `Qwen/Qwen2.5-3B-Instruct`. The upstream Qwen model is listed under the **Qwen Research License Agreement**, so the upstream model's license and terms must also be respected.

For the complete upstream license terms, see:

https://huggingface.co/Qwen/Qwen2.5-3B-Instruct

For the project's MIT license, see the GitHub repository:

https://github.com/MahboobAlam0/Distill---Fine-tuned-Qwen2.5-3B

## Citation

If you use this model or the associated project, please cite:

```bibtex
@software{mahboobalam_distill_qwen25_3b,
  author = {MahboobAlam0},
  title = {Distill - Fine-tuned Qwen2.5-3B},
  url = {https://github.com/MahboobAlam0/Distill---Fine-tuned-Qwen2.5-3B},
  license = {MIT}
}
```

## Acknowledgements

This project builds upon:

* Qwen2.5-3B-Instruct
* Hugging Face Transformers
* PEFT
* TRL
* BitsAndBytes
* PyTorch
* `sql-create-context`
* SQLite

## Model Card Contact

For questions, issues, or contributions, please use the GitHub repository's issue tracker.

**Project:** Distill β€” Fine-tuned Qwen2.5-3B
**Task:** Natural language β†’ SQL
**Base model:** Qwen2.5-3B-Instruct
**Fine-tuning:** QLoRA
**Training data:** 28,000 examples
**Validation data:** 7,858 examples
**Exact Match:** 30.67%
**Execution Accuracy:** 55.03%
**Adapter size:** ~114 MB
**Hardware:** 1 Γ— NVIDIA T4