Instructions to use keshavsharma/tinyllama-1.1b-sql-reasoning with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use keshavsharma/tinyllama-1.1b-sql-reasoning with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0") model = PeftModel.from_pretrained(base_model, "keshavsharma/tinyllama-1.1b-sql-reasoning") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,199 +1,175 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
## Model Details
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
-
|
| 36 |
-
## Uses
|
| 37 |
-
|
| 38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 39 |
-
|
| 40 |
-
### Direct Use
|
| 41 |
-
|
| 42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
-
|
| 44 |
-
[More Information Needed]
|
| 45 |
-
|
| 46 |
-
### Downstream Use [optional]
|
| 47 |
-
|
| 48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 49 |
-
|
| 50 |
-
[More Information Needed]
|
| 51 |
-
|
| 52 |
-
### Out-of-Scope Use
|
| 53 |
-
|
| 54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 55 |
-
|
| 56 |
-
[More Information Needed]
|
| 57 |
-
|
| 58 |
-
## Bias, Risks, and Limitations
|
| 59 |
-
|
| 60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 61 |
-
|
| 62 |
-
[More Information Needed]
|
| 63 |
-
|
| 64 |
-
### Recommendations
|
| 65 |
-
|
| 66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 67 |
-
|
| 68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 69 |
-
|
| 70 |
-
## How to Get Started with the Model
|
| 71 |
-
|
| 72 |
-
Use the code below to get started with the model.
|
| 73 |
-
|
| 74 |
-
[More Information Needed]
|
| 75 |
-
|
| 76 |
-
## Training Details
|
| 77 |
-
|
| 78 |
-
### Training Data
|
| 79 |
-
|
| 80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 81 |
-
|
| 82 |
-
[More Information Needed]
|
| 83 |
-
|
| 84 |
-
### Training Procedure
|
| 85 |
-
|
| 86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 87 |
-
|
| 88 |
-
#### Preprocessing [optional]
|
| 89 |
-
|
| 90 |
-
[More Information Needed]
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#### Training Hyperparameters
|
| 94 |
-
|
| 95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 96 |
-
|
| 97 |
-
#### Speeds, Sizes, Times [optional]
|
| 98 |
-
|
| 99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 100 |
-
|
| 101 |
-
[More Information Needed]
|
| 102 |
-
|
| 103 |
-
## Evaluation
|
| 104 |
-
|
| 105 |
-
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 106 |
-
|
| 107 |
-
### Testing Data, Factors & Metrics
|
| 108 |
-
|
| 109 |
-
#### Testing Data
|
| 110 |
-
|
| 111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
| 112 |
-
|
| 113 |
-
[More Information Needed]
|
| 114 |
-
|
| 115 |
-
#### Factors
|
| 116 |
-
|
| 117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 118 |
-
|
| 119 |
-
[More Information Needed]
|
| 120 |
-
|
| 121 |
-
#### Metrics
|
| 122 |
-
|
| 123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 124 |
-
|
| 125 |
-
[More Information Needed]
|
| 126 |
-
|
| 127 |
-
### Results
|
| 128 |
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
|
|
|
| 133 |
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
<
|
| 138 |
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
- **Hours used:** [More Information Needed]
|
| 149 |
-
- **Cloud Provider:** [More Information Needed]
|
| 150 |
-
- **Compute Region:** [More Information Needed]
|
| 151 |
-
- **Carbon Emitted:** [More Information Needed]
|
| 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 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: TinyLlama/TinyLlama-1.1B-Chat-v1.0
|
| 4 |
+
library_name: peft
|
| 5 |
+
tags:
|
| 6 |
+
- text-to-sql
|
| 7 |
+
- lora
|
| 8 |
+
- peft
|
| 9 |
+
- sft
|
| 10 |
+
- reasoning
|
| 11 |
+
datasets:
|
| 12 |
+
- gretelai/synthetic_text_to_sql
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# TinyLlama-1.1B Text-to-SQL β LoRA SFT with Reasoning
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
A parameter-efficient fine-tuned adapter for `TinyLlama/TinyLlama-1.1B-Chat-v1.0` that converts natural-language questions and SQL database schemas into SQL queries, trained to produce an explicit reasoning trace before the final answer.
|
| 18 |
|
| 19 |
+
This is a second-generation adapter in the same series as [keshavsharma/qwen2.5-3b-sql-lora-sft](https://huggingface.co/keshavsharma/qwen2.5-3b-sql-lora-sft) and an earlier plain-SQL TinyLlama adapter. Two changes from the earlier TinyLlama attempt drove a large jump in output quality: switching the training set from `b-mc2/sql-create-context` to `gretelai/synthetic_text_to_sql` (broader task-type and complexity coverage, including joins and analytics-style questions), and training the model to reason step-by-step inside `<reasoning>` tags before committing to a final `<answer>`.
|
| 20 |
|
| 21 |
## Model Details
|
| 22 |
|
| 23 |
+
| Property | Value |
|
| 24 |
+
|---|---|
|
| 25 |
+
| Base model | TinyLlama/TinyLlama-1.1B-Chat-v1.0 |
|
| 26 |
+
| Fine-tuning method | LoRA / PEFT |
|
| 27 |
+
| Task | Text-to-SQL, with reasoning trace |
|
| 28 |
+
| Dataset | gretelai/synthetic_text_to_sql |
|
| 29 |
+
| LoRA rank | 32 |
|
| 30 |
+
| LoRA alpha | 64 |
|
| 31 |
+
| LoRA dropout | 0.1 |
|
| 32 |
+
| Target modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
|
| 33 |
+
| Maximum sequence length | 768 tokens |
|
| 34 |
+
| Training epochs | 2 |
|
| 35 |
+
| Learning rate | 2e-4 |
|
| 36 |
+
| LR scheduler | Cosine |
|
| 37 |
+
| Warmup ratio | 0.03 |
|
| 38 |
+
| Weight decay | 0.01 |
|
| 39 |
+
| Precision | BF16 |
|
| 40 |
+
| Optimizer | AdamW fused |
|
| 41 |
+
| Train/eval split | 90% / 10% |
|
| 42 |
+
| Split seed | 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
## Intended Use
|
| 45 |
|
| 46 |
+
The model takes:
|
| 47 |
+
- A database schema, as CREATE TABLE statements (`SQL Context`).
|
| 48 |
+
- A natural-language question about the database.
|
| 49 |
|
| 50 |
+
It generates a short reasoning trace identifying the relevant tables/columns and query shape, followed by the final SQL query.
|
| 51 |
|
| 52 |
+
## Output Format
|
| 53 |
+
|
| 54 |
+
Unlike a plain text-to-SQL model, this adapter is trained to always respond with a reasoning block followed by an answer block:
|
| 55 |
|
| 56 |
+
```
|
| 57 |
+
<reasoning>...step-by-step derivation of the query...</reasoning><answer>...final SQL query...</answer>
|
| 58 |
+
```
|
| 59 |
|
| 60 |
+
**Downstream code must parse the `<answer>...</answer>` span out of the generation before treating it as executable SQL** β do not execute the raw model output directly.
|
| 61 |
|
| 62 |
+
## Example
|
| 63 |
|
| 64 |
+
Schema:
|
| 65 |
+
```sql
|
| 66 |
+
CREATE TABLE employees (
|
| 67 |
+
employee_id INTEGER,
|
| 68 |
+
name TEXT,
|
| 69 |
+
salary REAL,
|
| 70 |
+
department TEXT
|
| 71 |
+
);
|
| 72 |
+
```
|
| 73 |
|
| 74 |
+
Question: `What is the average salary of employees in the engineering department?`
|
| 75 |
|
| 76 |
+
Generated output:
|
| 77 |
+
```
|
| 78 |
+
<reasoning>This query calculates the average salary of employees in the engineering department by using the AVG function on the salary column, and filtering the employees table for rows where the department is 'Engineering'.</reasoning><answer>SELECT AVG(salary) FROM employees WHERE department = 'Engineering';</answer>
|
| 79 |
+
```
|
| 80 |
|
| 81 |
+
## Training Method
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
Supervised Fine-Tuning (SFT) with LoRA. Instead of updating all parameters of the base model, LoRA adds trainable low-rank matrices to selected transformer layers while keeping the original weights frozen.
|
| 84 |
|
| 85 |
+
### Loss Masking
|
| 86 |
|
| 87 |
+
Loss is computed only on the reasoning + answer tokens (`<reasoning>...</reasoning><answer>...</answer>`). System prompt, schema, and question tokens are masked with `-100` in the labels, so the model is trained to produce the reasoning-and-answer continuation rather than being penalized for reproducing the input prompt.
|
| 88 |
|
| 89 |
+
### Prompt Format
|
| 90 |
|
| 91 |
+
Uses TinyLlama's chat template. The system instruction used during training:
|
| 92 |
|
| 93 |
+
```
|
| 94 |
+
You are a text-to-SQL assistant. Given a database schema (CREATE TABLE statements) and a question, write the SQL query that answers the question. Respond with only the SQL query. Reason with <reasoning></reasoning> blocks. Answer with <answer></answer> blocks.
|
| 95 |
+
```
|
| 96 |
|
| 97 |
+
The user turn follows the format:
|
| 98 |
|
| 99 |
+
```
|
| 100 |
+
SQL Context: {schema}
|
| 101 |
|
| 102 |
+
Question: {question}
|
| 103 |
|
| 104 |
+
Write the SQL query that answers the question.
|
| 105 |
+
```
|
| 106 |
|
| 107 |
+
## Installation
|
| 108 |
|
| 109 |
+
```bash
|
| 110 |
+
pip install torch transformers datasets accelerate peft
|
| 111 |
+
```
|
| 112 |
|
| 113 |
+
## Inference
|
| 114 |
|
| 115 |
+
```python
|
| 116 |
+
import re
|
| 117 |
+
import torch
|
| 118 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 119 |
+
from peft import PeftModel
|
| 120 |
|
| 121 |
+
BASE_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 122 |
+
ADAPTER = "keshavsharma/tinyllama-1.1b-sql-reasoning"
|
| 123 |
+
|
| 124 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 125 |
+
if tokenizer.pad_token is None:
|
| 126 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 127 |
|
| 128 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 129 |
+
BASE_MODEL, torch_dtype=torch.bfloat16, device_map="auto"
|
| 130 |
+
)
|
| 131 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER)
|
| 132 |
+
model.eval()
|
| 133 |
+
model.config.use_cache = True
|
| 134 |
|
| 135 |
+
SYSTEM_PROMPT = (
|
| 136 |
+
"You are a text-to-SQL assistant. Given a database schema (CREATE TABLE "
|
| 137 |
+
"statements) and a question, write the SQL query that answers the question. "
|
| 138 |
+
"Respond with only the SQL query. Reason with <reasoning></reasoning> blocks. "
|
| 139 |
+
"Answer with <answer></answer> blocks."
|
| 140 |
+
)
|
| 141 |
|
| 142 |
+
def generate_sql(schema, question):
|
| 143 |
+
messages = [
|
| 144 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 145 |
+
{"role": "user", "content": f"SQL Context: {schema}\n\nQuestion: {question}\n\nWrite the SQL query that answers the question."},
|
| 146 |
+
]
|
| 147 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 148 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 149 |
+
with torch.no_grad():
|
| 150 |
+
outputs = model.generate(
|
| 151 |
+
**inputs, max_new_tokens=768, do_sample=False, pad_token_id=tokenizer.eos_token_id
|
| 152 |
+
)
|
| 153 |
+
raw = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 154 |
|
| 155 |
+
match = re.search(r"<answer>(.*?)</answer>", raw, re.DOTALL)
|
| 156 |
+
sql = match.group(1).strip() if match else None
|
| 157 |
+
return {"raw": raw, "sql": sql}
|
| 158 |
+
```
|
| 159 |
|
| 160 |
+
Note the reference implementation used during training generated with `do_sample=False` for evaluation, but the training-time sanity-check cell in this repo's notebook used sampling (`top_p=0.95`, `temperature=0.9`) with `do_sample=False` set simultaneously β those sampling parameters are ignored when `do_sample=False`, so generation was effectively greedy/deterministic throughout. Keep `do_sample=False` for reproducible evaluation; only enable sampling deliberately if you want varied reasoning traces.
|
| 161 |
|
| 162 |
+
## Limitations
|
| 163 |
|
| 164 |
+
- Generated SQL should be validated β and ideally checked that every referenced table/column actually exists in the given schema β before execution. Spot testing showed occasional hallucinated column names on harder correlated-subquery questions (e.g. inventing a column that doesn't appear in the schema).
|
| 165 |
+
- Correlated aggregate arithmetic inside `HAVING` (e.g. comparing an average against a per-row-derived threshold computed from another aggregate) remains a weak spot; treat multi-step nested-aggregate questions with extra caution.
|
| 166 |
+
- As a 1.1B-parameter base model, this adapter has materially less headroom than larger Text-to-SQL fine-tunes (e.g. the Qwen2.5-3B version in the same series) on harder multi-table, multi-condition questions, even with reasoning traces.
|
| 167 |
+
- The reasoning trace reflects the model's stated derivation, not a verified execution trace β it can be fluent and plausible while the final SQL is still wrong. Don't treat the presence of correct-sounding reasoning as confirmation the SQL is correct.
|
| 168 |
+
- No claim of production-level Text-to-SQL reliability is made. Outputs should be reviewed or executed against a real schema before use in any automated pipeline.
|
| 169 |
|
| 170 |
+
## Citation / References
|
| 171 |
|
| 172 |
+
- TinyLlama β [TinyLlama: An Open-Source Small Language Model](https://arxiv.org/abs/2401.02385)
|
| 173 |
+
- LoRA β Hu et al., *LoRA: Low-Rank Adaptation of Large Language Models*
|
| 174 |
+
- PEFT β Hugging Face Parameter-Efficient Fine-Tuning
|
| 175 |
+
- Synthetic Text-to-SQL dataset β gretelai/synthetic_text_to_sql
|