jk200201 commited on
Commit
d35a9d0
·
verified ·
1 Parent(s): 32e7fbb

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: Qwen/Qwen2.5-Coder-7B-Instruct
4
+ pipeline_tag: text-generation
5
+ library_name: transformers
6
+ language:
7
+ - en
8
+ tags:
9
+ - text-to-sql
10
+ - sql
11
+ - bird
12
+ - chain-of-thought
13
+ - reasoning
14
+ - qwen2.5-coder
15
+ - gguf
16
+ - llama.cpp
17
+ datasets:
18
+ - jk200201/bird-cot-sft
19
+ model-index:
20
+ - name: qwen2.5-coder-7b-bird-cot
21
+ results:
22
+ - task:
23
+ type: text-generation
24
+ name: Text-to-SQL
25
+ dataset:
26
+ type: bird
27
+ name: BIRD (dev)
28
+ metrics:
29
+ - type: accuracy
30
+ name: Result accuracy (greedy)
31
+ value: 52.1
32
+ - type: accuracy
33
+ name: Result accuracy (self-consistency, K=8)
34
+ value: 58.5
35
+ ---
36
+
37
+ # Qwen2.5-Coder-7B — BIRD CoT (Text-to-SQL)
38
+
39
+ A 7B text-to-SQL model that **reasons step-by-step over a database schema, then writes the SQL**. Fine-tuned from `Qwen/Qwen2.5-Coder-7B-Instruct` by distilling *execution-verified* chain-of-thought solutions.
40
+
41
+ On **BIRD dev** (messy, real-world schemas — the hard text-to-SQL benchmark) it reaches **52.1%** result accuracy greedy, and **58.5%** with self-consistency (Best-of-N, K=8) — **surpassing single-shot frontier models at a fraction of the size.**
42
+
43
+ ## Results — BIRD dev (execution result accuracy)
44
+
45
+ | Model | Result accuracy |
46
+ |---|---|
47
+ | Qwen2.5-Coder-7B-Instruct (base) | ~27% |
48
+ | **This model — greedy (1 sample)** | **52.1%** |
49
+ | **This model — self-consistency (K=8)** | **58.5%** |
50
+ | Grok-4 (single-shot) | 55.4% |
51
+ | DeepSeek-V3 (single-shot) | 54.7% |
52
+
53
+ *Result accuracy* = the generated SQL executes to the **same rows** as the gold query (BIRD's official execution metric). Frontier numbers are single-shot; the 58.5% uses K=8 self-consistency (8× inference).
54
+
55
+ ## Usage
56
+
57
+ Prompt the model to reason step-by-step; it returns the reasoning followed by a fenced SQL block. **Take the last ```sql``` block** as the query.
58
+
59
+ ```python
60
+ import re, torch
61
+ from transformers import AutoModelForCausalLM, AutoTokenizer
62
+
63
+ model_id = "jk200201/qwen2.5-coder-7b-bird-cot"
64
+ tok = AutoTokenizer.from_pretrained(model_id)
65
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
66
+
67
+ SYSTEM = ("You are an expert SQLite query writer. Reason step by step about the schema "
68
+ "and the question, then output the final query in a ```sql code block.")
69
+
70
+ def build_prompt(schema, question):
71
+ return ("Given the database schema and question, work out the correct SQLite query step by step.\n\n"
72
+ f"Database Schema:\n{schema}\n\nQuestion: {question}\n\n"
73
+ "Think step by step:\n1. Which tables and columns are relevant?\n"
74
+ "2. What joins, filters, grouping, and ordering are needed?\n3. Handle edge cases.\n\n"
75
+ "Then give the final answer as:\n```sql\n<final query>\n```")
76
+
77
+ schema = "CREATE TABLE singer (Singer_ID INT, Name TEXT, Age INT);"
78
+ question = "How many singers are older than 40?"
79
+ messages = [{"role": "system", "content": SYSTEM},
80
+ {"role": "user", "content": build_prompt(schema, question)}]
81
+
82
+ text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
83
+ inputs = tok(text, return_tensors="pt").to(model.device)
84
+ out = model.generate(**inputs, max_new_tokens=512, do_sample=False)
85
+ resp = tok.decode(out[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
86
+
87
+ sql = re.findall(r"```(?:sql)?\s*(.*?)```", resp, re.DOTALL)[-1].strip()
88
+ print(sql)
89
+ ```
90
+
91
+ **For best accuracy (58.5%):** sample K=8 at `temperature=0.8`, execute each candidate, and take the majority result (self-consistency).
92
+
93
+ ## Run locally (GGUF / Ollama / llama.cpp)
94
+
95
+ Quantized GGUF builds (`Q4_K_M`, `Q5_K_M`, `Q8_0`) are provided for CPU/laptop use:
96
+
97
+ ```bash
98
+ ollama run jk200201/qwen2.5-coder-7b-bird-cot
99
+ # or with llama.cpp:
100
+ ./llama-cli -m qwen2.5-coder-7b-bird-cot.Q4_K_M.gguf -sys "$SYSTEM" -p "$PROMPT"
101
+ ```
102
+
103
+ ## Training
104
+
105
+ - **Method — reasoning distillation (CoT-SFT):** a strong teacher (Qwen3-Coder-480B) generated step-by-step CoT solutions on BIRD train; only **execution-verified-correct** chains were kept (**5,593** examples), then supervised-fine-tuned into the 7B. Distilling the teacher's *reasoning* generalized across BIRD's cross-domain dev databases better than distilling SQL answers directly.
106
+ - **Config:** QLoRA (4-bit NF4, LoRA r=32, α=64), 2 epochs, LR 2e-4 cosine, max seq len 8192.
107
+ - **Data:** [`jk200201/bird-cot-sft`](https://huggingface.co/datasets/jk200201/bird-cot-sft)
108
+
109
+ ## Limitations
110
+
111
+ - Tuned for **BIRD-style** analytic SQL over realistic schemas; unusual dialects/domains may need adaptation. Emits **SQLite** dialect.
112
+ - Greedy (52.1%) is the deployable single-shot; the 58.5% figure requires K=8 self-consistency (8× inference cost).
113
+ - A 7B model — always review generated SQL before running it on production data.
114
+
115
+ ## Citation
116
+
117
+ ```bibtex
118
+ @misc{qwen25coder7b_bird_cot_2026,
119
+ title = {Qwen2.5-Coder-7B BIRD CoT: reasoning distillation for text-to-SQL},
120
+ author = {Jenish Kothari},
121
+ year = {2026},
122
+ howpublished = {\url{https://huggingface.co/jk200201/qwen2.5-coder-7b-bird-cot}}
123
+ }
124
+ ```
125
+
126
+ ## Acknowledgements
127
+ Base: Qwen2.5-Coder (Alibaba Qwen). Teacher: Qwen3-Coder-480B (W&B Inference). Benchmark: [BIRD](https://bird-bench.github.io/).
chat_template.jinja ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0]['role'] == 'system' %}
4
+ {{- messages[0]['content'] }}
5
+ {%- else %}
6
+ {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}
7
+ {%- endif %}
8
+ {{- "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
9
+ {%- for tool in tools %}
10
+ {{- "\n" }}
11
+ {{- tool | tojson }}
12
+ {%- endfor %}
13
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
14
+ {%- else %}
15
+ {%- if messages[0]['role'] == 'system' %}
16
+ {{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }}
17
+ {%- else %}
18
+ {{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }}
19
+ {%- endif %}
20
+ {%- endif %}
21
+ {%- for message in messages %}
22
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %}
23
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
24
+ {%- elif message.role == "assistant" %}
25
+ {{- '<|im_start|>' + message.role }}
26
+ {%- if message.content %}
27
+ {{- '\n' + message.content }}
28
+ {%- endif %}
29
+ {%- for tool_call in message.tool_calls %}
30
+ {%- if tool_call.function is defined %}
31
+ {%- set tool_call = tool_call.function %}
32
+ {%- endif %}
33
+ {{- '\n<tool_call>\n{"name": "' }}
34
+ {{- tool_call.name }}
35
+ {{- '", "arguments": ' }}
36
+ {{- tool_call.arguments | tojson }}
37
+ {{- '}\n</tool_call>' }}
38
+ {%- endfor %}
39
+ {{- '<|im_end|>\n' }}
40
+ {%- elif message.role == "tool" %}
41
+ {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %}
42
+ {{- '<|im_start|>user' }}
43
+ {%- endif %}
44
+ {{- '\n<tool_response>\n' }}
45
+ {{- message.content }}
46
+ {{- '\n</tool_response>' }}
47
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
48
+ {{- '<|im_end|>\n' }}
49
+ {%- endif %}
50
+ {%- endif %}
51
+ {%- endfor %}
52
+ {%- if add_generation_prompt %}
53
+ {{- '<|im_start|>assistant\n' }}
54
+ {%- endif %}
config.json ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen2ForCausalLM"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "bos_token_id": 151643,
7
+ "dtype": "bfloat16",
8
+ "eos_token_id": 151645,
9
+ "hidden_act": "silu",
10
+ "hidden_size": 3584,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 18944,
13
+ "layer_types": [
14
+ "full_attention",
15
+ "full_attention",
16
+ "full_attention",
17
+ "full_attention",
18
+ "full_attention",
19
+ "full_attention",
20
+ "full_attention",
21
+ "full_attention",
22
+ "full_attention",
23
+ "full_attention",
24
+ "full_attention",
25
+ "full_attention",
26
+ "full_attention",
27
+ "full_attention",
28
+ "full_attention",
29
+ "full_attention",
30
+ "full_attention",
31
+ "full_attention",
32
+ "full_attention",
33
+ "full_attention",
34
+ "full_attention",
35
+ "full_attention",
36
+ "full_attention",
37
+ "full_attention",
38
+ "full_attention",
39
+ "full_attention",
40
+ "full_attention",
41
+ "full_attention"
42
+ ],
43
+ "max_position_embeddings": 32768,
44
+ "max_window_layers": 28,
45
+ "model_type": "qwen2",
46
+ "num_attention_heads": 28,
47
+ "num_hidden_layers": 28,
48
+ "num_key_value_heads": 4,
49
+ "pad_token_id": null,
50
+ "rms_norm_eps": 1e-06,
51
+ "rope_parameters": {
52
+ "rope_theta": 1000000.0,
53
+ "rope_type": "default"
54
+ },
55
+ "sliding_window": null,
56
+ "tie_word_embeddings": false,
57
+ "transformers_version": "5.13.1",
58
+ "use_cache": true,
59
+ "use_sliding_window": false,
60
+ "vocab_size": 152064
61
+ }
generation_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 151645,
6
+ 151643
7
+ ],
8
+ "pad_token_id": 151643,
9
+ "repetition_penalty": 1.1,
10
+ "temperature": 0.7,
11
+ "top_k": 20,
12
+ "top_p": 0.8,
13
+ "transformers_version": "5.13.1"
14
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:432a36746704cd4f5bfb157a6e0884c74818de60bfe643cc606d847c246df57e
3
+ size 15231272152
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8
3
+ size 11421892
tokenizer_config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": null,
5
+ "clean_up_tokenization_spaces": false,
6
+ "eos_token": "<|im_end|>",
7
+ "errors": "replace",
8
+ "extra_special_tokens": [
9
+ "<|im_start|>",
10
+ "<|im_end|>",
11
+ "<|object_ref_start|>",
12
+ "<|object_ref_end|>",
13
+ "<|box_start|>",
14
+ "<|box_end|>",
15
+ "<|quad_start|>",
16
+ "<|quad_end|>",
17
+ "<|vision_start|>",
18
+ "<|vision_end|>",
19
+ "<|vision_pad|>",
20
+ "<|image_pad|>",
21
+ "<|video_pad|>"
22
+ ],
23
+ "is_local": false,
24
+ "local_files_only": false,
25
+ "model_max_length": 32768,
26
+ "pad_token": "<|endoftext|>",
27
+ "split_special_tokens": false,
28
+ "tokenizer_class": "Qwen2Tokenizer",
29
+ "unk_token": null
30
+ }