QLoRA fine-tune of Llama-2-7B that turns natural-language questions plus a table schema into SQL queries, trained on a free-tier Colab T4.

Dataset token-length distribution

Dataset token-length distribution

I built this to understand fine-tuning from the inside. Text-to-SQL is a strong sandbox: a real, economically valuable task (analysts use it every day), clean evaluation (either the query runs and returns the right rows or it doesn't), and a simple dataset format that lets the focus stay on training mechanics. The whole project was constrained to a 16 GB VRAM + 12-hour free-tier Colab — a constraint that turned out to be useful. It forced sharp choices.

Problem

Base LLMs like Llama-2-7B can continue text but don't follow instructions well, and out of the box they hallucinate SQL that doesn't respect a given schema. Ask a base Llama-2-7B "count the students scoring greater than 85" with a student(marks INTEGER) schema and you'll often get back a plausible-looking query that references student.score, students (plural), or invents a students_table name entirely. That's the instruction-following gap.

Full fine-tuning a 7B model is out of reach on free Colab hardware: you'd need ~80 GB of VRAM just for the optimiser states and gradients in FP16. The goal was a reproducible recipe that fine-tunes Llama-2-7B for text-to-SQL on a single T4 with 16 GB VRAM, and ships the result to the Hugging Face Hub.

Why text-to-SQL is hard

It's easy to underestimate how many implicit decisions a SQL writer makes. A text-to-SQL model has to handle all of them:

Fine-tuning teaches all of these at once, implicitly, by showing the model enough examples of "question + schema -> correct SQL" that it learns the mapping. The model doesn't need to reason about SQL semantics from first principles — it needs to pattern-match to the closest training example.

Why Llama-2-7B (and not 13B or 70B)

VRAM math, plainly: