Qwen3-4B Text-to-SQL QLoRA Adapter

Model Description

This repository contains a QLoRA (4-bit NF4) adapter fine-tuned from Qwen/Qwen3-4B for Text-to-SQL generation. Given a relational database schema (CREATE TABLE ...) and a natural language question in English, the model outputs a single clean, executable SQL query without markdown fences, explanations, or extra commentary.

  • Developed by: Vladyslav Bondar (ipz235bvv, Zhytomyr Polytechnic State University, group Π†ΠŸΠ—-23-5)
  • Model type: Causal Language Model (LoRA adapter via PEFT)
  • Language(s): English (SQL generation from English queries)
  • License: Apache 2.0 (base model) / CC-BY-SA-4.0 (training dataset)
  • Finetuned from model: Qwen/Qwen3-4B

Uses

Direct Use

Automated translation of natural language analytical questions into SQL queries based on a provided CREATE TABLE DDL schema for read-only database engines and BI pipelines.

Out-of-Scope Use

  • Executing generated queries with write/DDL privileges (INSERT, UPDATE, DELETE, DROP) without validation.
  • Complex multi-database dialectical SQL outside standard SQLite/ANSI syntax covered by WikiSQL and Spider.

How to Get Started with the Model

import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig

base_model_id = "Qwen/Qwen3-4B"
adapter_id = "ipz235bvv/qwen3-4b-sql-qlora-adapter"

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

tokenizer = AutoTokenizer.from_pretrained(adapter_id)
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_id,
    quantization_config=bnb_config,
    device_map="auto",
)
model = PeftModel.from_pretrained(base_model, adapter_id)
model.eval()

messages = [
    {
        "role": "system",
        "content": "Translate the user question into a valid SQL query using the provided schema. Output SQL only.",
    },
    {
        "role": "user",
        "content": "Schema:\nCREATE TABLE head (age INTEGER)\n\nQuestion:\nHow many heads of the departments are older than 56?",
    },
]

prompt = tokenizer.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

with torch.no_grad():
    outputs = model.generate(**inputs, max_new_tokens=128, do_sample=False)

sql = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
print(sql)

Training Details

Training Data

  • Dataset: b-mc2/sql-create-context (built from WikiSQL and Spider).
  • Split (deduplicated, seed=42): 2,000 training examples, 200 validation examples, and 300 isolated test examples formatted in ChatML with enable_thinking=False.

Training Procedure & Hyperparameters

  • Method: Supervised Fine-Tuning (TRL SFTTrainer) with QLoRA (4-bit NF4, double quantization, fp16 compute).
  • LoRA Config: r=16, lora_alpha=32, lora_dropout=0.05, target_modules="all-linear" (33,030,144 trainable parameters β€” 0.8145% of total).
  • Optimizer & Schedule: learning_rate=1e-4, cosine scheduler, warmup_steps=10, effective batch size 16 (4 Γ— 4), max_steps=125 (1 epoch), max_length=512, assistant_only_loss=True.
  • Hardware & Duration: NVIDIA Tesla T4 (15.6 GB VRAM), training time 14.90 min, peak GPU memory 3.91 GB, adapter size 132.2 MB.

Evaluation

Evaluated on 300 held-out test examples (comparing the base model with a 96-token detailed system prompt vs. the fine-tuned QLoRA adapter with an 18-token short system prompt):

Metric Base Qwen3-4B (96-tok prompt) Fine-tuned QLoRA (18-tok prompt) Change (Ξ”)
Clean SQL (no fences/extra text), % 96.67% 100.00% +3.33%
Valid SQLite (EXPLAIN), % 97.67% 98.67% +1.00%
Normalized Exact Match, % 5.00% 69.67% +64.67%
Exact Match (quote-insensitive), % 53.00% 69.67% +16.67%
Downloads last month
27
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for ipz235bvv/qwen3-4b-sql-qlora-adapter

Finetuned
Qwen/Qwen3-4B
Adapter
(1,279)
this model

Dataset used to train ipz235bvv/qwen3-4b-sql-qlora-adapter