1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5BASE_MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
6ADAPTER = "jk200201/qwen2.5-coder-7b-bird-dpo"
7
8tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
9
10bnb = BitsAndBytesConfig(
11 load_in_4bit=True, bnb_4bit_quant_type="nf4",
12 bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True,
13)
14model = AutoModelForCausalLM.from_pretrained(
15 BASE_MODEL, quantization_config=bnb, device_map="auto", trust_remote_code=True
16)
17model = PeftModel.from_pretrained(model, ADAPTER)
18model.eval()
19
20schema = "CREATE TABLE users (id INT, name TEXT, country TEXT);"
21question = "How many users are from Japan?"
22evidence = "" # optional domain hint for BIRD-style queries
23
24prompt = f"""Convert the following natural language question into a valid SQL query.
25
26Database Schema:
27{schema}
28
29{f'External Knowledge:{chr(10)}{evidence}{chr(10)}{chr(10)}' if evidence.strip() else ''}Question: {question}
30
31Return only the SQL query with no explanation."""
32
33inputs = tokenizer.apply_chat_template(
34 [{"role": "user", "content": prompt}],
35 return_tensors="pt", add_generation_prompt=True
36).to(model.device)
37
38out = model.generate(inputs, max_new_tokens=256, do_sample=False, pad_token_id=tokenizer.eos_token_id)
39sql = tokenizer.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True).strip()
40print(sql)
Northeastern Discovery HPC — single NVIDIA H200 80GB. Training time: ~1h 15min.
Initially trained with 4,677 pairs (1,219 clear-preference + 3,458 judge-resolved style pairs). This regressed to 40.7% (-9.6pp).
1@misc{kothari2026qwenbirddpo,
2 author = {Kothari, Jenish},
3 title = {Qwen2.5-Coder-7B BIRD-DPO: Frontier-Disagreement DPO for Real-World Text-to-SQL},
4 year = {2026},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/jk200201/qwen2.5-coder-7b-bird-dpo}},
7}