lamini/spider_text_to_sql
Viewer • Updated • 8.03k • 133 • 9
How to use Snehanjan/llama-3.2-3b-spider-text-to-sql with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct")
model = PeftModel.from_pretrained(base_model, "Snehanjan/llama-3.2-3b-spider-text-to-sql")This is a QLoRA fine-tuned LoRA adapter on top of meta-llama/Llama-3.2-3B-Instruct for text-to-SQL generation, trained on the Spider dataset.
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch
base_model_id = "meta-llama/Llama-3.2-3B-Instruct"
adapter_id = "Snehanjan/llama-3.2-3b-spider-text-to-sql"
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
model = AutoModelForCausalLM.from_pretrained(
base_model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
model = PeftModel.from_pretrained(model, adapter_id)
def generate_sql(schema_and_question):
prompt = (
"<|begin_of_text|>"
"<|start_header_id|>system<|end_header_id|>\n\n"
"You are an expert SQL assistant. Given a database schema and a natural language question, "
"generate the correct SQL query. Output ONLY the SQL query, no explanations.<|eot_id|>"
"<|start_header_id|>user<|end_header_id|>\n\n"
f"{schema_and_question}<|eot_id|>"
"<|start_header_id|>assistant<|end_header_id|>\n\n"
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=128,
temperature=0.1,
do_sample=False,
)
response = tokenizer.decode(
outputs[0][inputs.input_ids.shape[1]:],
skip_special_tokens=True
)
return response.strip()
| Parameter | Value |
|---|---|
| Base Model | meta-llama/Llama-3.2-3B-Instruct |
| Dataset | lamini/spider_text_to_sql |
| Method | QLoRA (4-bit NF4 + LoRA) |
| LoRA Rank | 16 |
| LoRA Alpha | 32 |
| Target Modules | all-linear |
| Learning Rate | 2e-4 |
| LR Scheduler | cosine |
| Batch Size | 4 |
| Gradient Accumulation | 2 |
| Effective Batch Size | 8 |
| Epochs | 1 |
| Max Sequence Length | 512 |
| Hardware | Kaggle T4 (15GB VRAM) |
| Step | Training Loss |
|---|---|
| 10 | 1.850 |
| 100 | 0.236 |
| 500 | 0.090 |
| 870 | 0.098 |
Base model
meta-llama/Llama-3.2-3B-Instruct