You can fine-tune a 7B-parameter model on Text-to-SQL without renting an A100.
The package wraps the whole pipeline: loading and parsing Spider data, formatting prompts, running QLoRA training, and evaluating results. Everything is modular, so you can swap out the base model, tweak the LoRA config, or bring your own dataset without touching the rest.
Clone the repo and install:
git clone git@github.com:dinis-a/qlora-text2sql.git
cd qlora-text2sql
pip install -e .Grab the Spider dataset:
python scripts/download_dataset.py --output ./data/spiderTrain a model:
from qlora_text2sql import QLoRAText2SQL, create_qlora_config, SpiderDataset
dataset = SpiderDataset("./data/spider", split="train")
cfg = create_qlora_config(base_model_id="mistralai/Mistral-7B-Instruct-v0.2")
model = QLoRAText2SQL(cfg)
model.setup()
model.train(dataset, output_dir="./checkpoints", num_epochs=3)
model.save("./trained_adapter")Run inference:
cfg = create_qlora_config(base_model_id="mistralai/Mistral-7B-Instruct-v0.2")
model = QLoRAText2SQL(cfg)
model.load_adapter("mistralai/Mistral-7B-Instruct-v0.2", "./trained_adapter")
sql = model.predict_sql(
question="What is the average salary by department?",
schema="""
CREATE TABLE employees (id INT, name TEXT, department TEXT, salary REAL);
CREATE TABLE departments (id INT, name TEXT);
""",
)
print(sql)Or use the CLI:
python scripts/inference.py \
--base_model mistralai/Mistral-7B-Instruct-v0.2 \
--adapter ./trained_adapter \
--question "List all employees hired after 2020"qlora_text2sql/
├── src/qlora_text2sql/ # Python package
│ ├── __init__.py # Lazy imports so you can use the light parts without torch
│ ├── data/
│ │ ├── schema_utils.py # Prompt formatting, Spider parsing (pure Python, no ML deps)
│ │ └── spider_dataset.py # PyTorch Dataset with tokenization
│ ├── model/
│ │ └── qlora_model.py # QLoRA setup, training loop, and inference
│ └── utils/
│ └── metrics.py # Exact set match and execution accuracy
├── notebooks/
│ └── train_qlora_text2sql_kaggle.ipynb # Kaggle notebook with MLflow + HuggingFace Hub
├── scripts/
│ ├── download_dataset.py # Downloads Spider via KaggleHub
│ └── inference.py # Interactive and one-shot inference
├── configs/
│ └── default.yaml # Training defaults you can override
├── tests/
│ └── test_core.py # Offline smoke tests (no GPU needed)
├── .github/workflows/
│ └── ci.yml # GitHub Actions: tests, lint, type check
├── requirements.txt
├── pyproject.toml
└── README.md
| Parameter | Default | What it does |
|---|---|---|
lora_r |
16 | Rank of the LoRA adaptation matrices |
lora_alpha |
32 | Scaling factor — higher means stronger adaptation signal |
lora_dropout |
0.05 | Dropout applied to LoRA layers during training |
bits |
4 | 4-bit NormalFloat quantization (the "Q" in QLoRA) |
bnb_4bit_quant_type |
nf4 |
NormalFloat4 — information-theoretically optimal for normal weights |
use_double_quant |
true |
Nested quantization, saves another ~0.4 bits per parameter |
| Parameter | Value |
|---|---|
| Epochs | 3 |
| Batch size (per device) | 4 |
| Gradient accumulation | 4 (effective batch = 16) |
| Learning rate | 2e-4 |
| Scheduler | Cosine decay with 3% warmup |
| Max sequence length | 2048 |
| Gradient clipping | 0.3 |
| Model | Parameters | VRAM (4-bit) | Access |
|---|---|---|---|
| Mistral-7B-Instruct-v0.2 | 7B | ~14 GB | No auth needed |
| Meta-Llama-3-8B-Instruct | 8B | ~15 GB | HuggingFace auth required |
| Gemma-7B-it | 7B | ~14 GB | HuggingFace auth required |
| Phi-2 | 2.7B | ~6 GB | No auth needed |
Mistral is the default because it hits a sweet spot: strong SQL generation without needing any access tokens.
I track two metrics, following the Spider benchmark:
Exact Set Match tokenizes each SQL query into components (SELECT columns, FROM tables, WHERE conditions, etc.) and checks if the sets match between prediction and reference. Ordering within a clause doesn't matter — SELECT a, b matches SELECT b, a.
Execution Accuracy runs both the predicted and reference SQL against the actual database and compares the result sets. This is the one that counts — a query can look different from the reference but still produce the correct answer. It is also the primary Spider benchmark metric.
The Spider dataset has:
- 7,000 training examples across 166 databases
- 1,034 dev examples across 20 databases
- Four difficulty levels: Easy through Extra Hard
- Queries with GROUP BY, ORDER BY, HAVING, nested subqueries, and multiple JOINs
Downloading is handled through Jerome Blanchet's Kaggle mirror via kagglehub, so you don't need to deal with the original Spider distribution.
Install dev dependencies and the pre-commit hooks:
pip install -e ".[dev]"
pre-commit installRun what you need:
make test # Run the test suite
make lint # Ruff linter
make format # Black formatter
make typecheck # Mypy
make test-cov # Tests with HTML coverage reportAll of these run in CI on every push and pull request.
MIT — see LICENSE.
- The Spider dataset from Yale's LILY Lab
- Tim Dettmers' QLoRA paper and the
bitsandbyteslibrary - The HuggingFace PEFT team for making LoRA dead simple