MrSnurfy commited on
Commit
f35a37d
·
verified ·
1 Parent(s): 90236f2

Upload 7 files

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,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: Qwen/Qwen2.5-Coder-0.5B
3
+ library_name: transformers
4
+ license: apache-2.0
5
+ language:
6
+ - en
7
+ tags:
8
+ - code
9
+ - code-explanation
10
+ - chatml
11
+ - sft
12
+ - trl
13
+ pipeline_tag: text-generation
14
+ datasets:
15
+ - SnurfyAI/Sn-CodeExplainer-15k
16
+ ---
17
+
18
+ # Sn-CodeExplainer-0.5B
19
+
20
+ A 494M parameter code explanation model fine-tuned from [Qwen/Qwen2.5-Coder-0.5B](https://huggingface.co/Qwen/Qwen2.5-Coder-0.5B). Given a function or code snippet, it provides a clear, high-level summary of what the code does.
21
+
22
+ **What it does:** Explains code — purpose, key logic, and edge cases.
23
+ **What it doesn't do:** Write, fix, refactor, or review code.
24
+
25
+ ## Supported Languages
26
+
27
+ Python, JavaScript, TypeScript, Java, C, C++, C#, Go, Rust, PHP
28
+
29
+ ## Usage
30
+
31
+ ```python
32
+ from transformers import AutoModelForCausalLM, AutoTokenizer
33
+ import torch
34
+
35
+ model_id = "SnurfyAI/Sn-CodeExplainer-0.5B"
36
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
37
+ model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16).to("cuda")
38
+
39
+ messages = [
40
+ {"role": "system", "content": "You are a code explanation assistant. Given a piece of code, explain what it does clearly and concisely."},
41
+ {"role": "user", "content": """Can you explain this Python function?
42
+
43
+ def fibonacci(n):
44
+ if n <= 1:
45
+ return n
46
+ a, b = 0, 1
47
+ for _ in range(2, n + 1):
48
+ a, b = b, a + b
49
+ return b"""},
50
+ ]
51
+
52
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
53
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
54
+
55
+ with torch.no_grad():
56
+ output = model.generate(**inputs, max_new_tokens=512, temperature=0.3, do_sample=True, top_p=0.9)
57
+
58
+ response = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
59
+ print(response)
60
+ ```
61
+
62
+ **Example output:**
63
+ > This function computes the nth Fibonacci number iteratively. It handles base cases (0 and 1) directly, then uses two variables to track consecutive Fibonacci values, updating them in a loop until reaching the target index.
64
+
65
+ ## Chat Template
66
+
67
+ This model uses the ChatML format:
68
+
69
+ ```
70
+ <|im_start|>system
71
+ You are a code explanation assistant. Given a piece of code, explain what it does clearly and concisely.<|im_end|>
72
+ <|im_start|>user
73
+ {code and question here}<|im_end|>
74
+ <|im_start|>assistant
75
+ ```
76
+
77
+ The chat template is baked into the tokenizer — `apply_chat_template()` handles formatting automatically.
78
+
79
+ ## Training Details
80
+
81
+ | Parameter | Value |
82
+ |-----------|-------|
83
+ | Base model | [Qwen/Qwen2.5-Coder-0.5B](https://huggingface.co/Qwen/Qwen2.5-Coder-0.5B) (494M params) |
84
+ | Method | Full fine-tune (SFT) |
85
+ | Training data | ~15k synthetic examples |
86
+ | Data format | 1-2 turn ChatML conversations |
87
+ | Data source | DeepSeek V3.2 via OpenRouter |
88
+ | Epochs | 2 |
89
+ | Effective batch size | 32 (batch 4 x grad_accum 8) |
90
+ | Learning rate | 2e-5 (cosine schedule) |
91
+ | Warmup steps | 10 |
92
+ | Max sequence length | 2048 |
93
+ | Precision | bfloat16 |
94
+ | Optimizer | AdamW |
95
+ | Hardware | NVIDIA RTX 5090 (32GB) |
96
+
97
+ ### Training Data
98
+
99
+ The training data covers:
100
+ - **10 programming languages** (Python, JavaScript, TypeScript, Java, C, C++, C#, Go, Rust, PHP)
101
+ - **15 code domains** (algorithms, data structures, string manipulation, file I/O, API handlers, database operations, error handling, math, utilities, OOP, concurrency, recursion, iterators, configuration, data transformation)
102
+ - **3 complexity levels** (simple 5-15 lines, moderate 15-30 lines, complex 30-50 lines)
103
+ - **3 explanation detail levels** (brief, moderate, detailed)
104
+ - ~2% polite refusal examples (redirecting off-topic requests back to code explanation)
105
+
106
+ ### Data Generation Cost
107
+
108
+ ~$3.30 for 15k examples via OpenRouter (DeepSeek V3.2).
109
+
110
+ ## Limitations
111
+
112
+ - **Synthetic training data** — may inherit factual errors or biases from the generator model
113
+ - **English only** — explanations are always in English regardless of the code language
114
+ - **Small model** — 494M parameters limits the depth and nuance of explanations, especially for complex algorithms
115
+ - **Code length** — best with functions under ~50 lines; longer code may exceed the 2048 token context
116
+ - **Not a code generator** — explicitly trained to explain, not to write or modify code
117
+
118
+ ## Citations
119
+
120
+ ### Base Model
121
+
122
+ ```bibtex
123
+ @article{hui2024qwen2,
124
+ title={Qwen2. 5-Coder Technical Report},
125
+ author={Hui, Binyuan and Yang, Jian and Cui, Zeyu and Yang, Jiaxi and Liu, Dayiheng and Zhang, Lei and Liu, Tianyu and Zhang, Jiajun and Yu, Bowen and Dang, Kai and others},
126
+ journal={arXiv preprint arXiv:2409.12186},
127
+ year={2024}
128
+ }
129
+
130
+ @article{qwen2,
131
+ title={Qwen2 Technical Report},
132
+ author={An Yang and Baosong Yang and Binyuan Hui and Bo Zheng and Bowen Yu and Chang Zhou and Chengpeng Li and Chengyuan Li and Dayiheng Liu and Fei Huang and Guanting Dong and Haoran Wei and Huan Lin and Jialong Tang and Jialin Wang and Jian Yang and Jianhong Tu and Jianwei Zhang and Jianxin Ma and Jin Xu and Jingren Zhou and Jinze Bai and Jinzheng He and Junyang Lin and Kai Dang and Keming Lu and Keqin Chen and Kexin Yang and Mei Li and Mingfeng Xue and Na Ni and Pei Zhang and Peng Wang and Ru Peng and Rui Men and Ruize Gao and Runji Lin and Shijie Wang and Shuai Bai and Sinan Tan and Tianhang Zhu and Tianhao Li and Tianyu Liu and Wenbin Ge and Xiaodong Deng and Xiaohuan Zhou and Xingzhang Ren and Xinyu Zhang and Xipin Wei and Xuancheng Ren and Yang Fan and Yang Yao and Yichang Zhang and Yu Wan and Yunfei Chu and Yuqiong Liu and Zeyu Cui and Zhenru Zhang and Zhihao Fan},
133
+ journal={arXiv preprint arXiv:2407.10671},
134
+ year={2024}
135
+ }
136
+ ```
137
+
138
+ ### Training Framework
139
+
140
+ ```bibtex
141
+ @software{vonwerra2020trl,
142
+ title = {{TRL: Transformers Reinforcement Learning}},
143
+ author = {von Werra, Leandro and Belkada, Younes and Tunstall, Lewis and Beeching, Edward and Thrush, Tristan and Lambert, Nathan and Huang, Shengyi and Rasul, Kashif and Gallouédec, Quentin},
144
+ license = {Apache-2.0},
145
+ url = {https://github.com/huggingface/trl},
146
+ year = {2020}
147
+ }
148
+ ```
149
+
150
+ ## License
151
+
152
+ Apache-2.0 (same as the base model)
chat_template.jinja ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {% for message in messages %}<|im_start|>{{ message['role'] }}
2
+ {{ message['content'] }}<|im_end|>
3
+ {% endfor %}{% if add_generation_prompt %}<|im_start|>assistant
4
+ {% endif %}
config.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen2ForCausalLM"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "bos_token_id": null,
7
+ "dtype": "bfloat16",
8
+ "eos_token_id": 151643,
9
+ "hidden_act": "silu",
10
+ "hidden_size": 896,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 4864,
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
+ ],
39
+ "max_position_embeddings": 32768,
40
+ "max_window_layers": 24,
41
+ "model_type": "qwen2",
42
+ "num_attention_heads": 14,
43
+ "num_hidden_layers": 24,
44
+ "num_key_value_heads": 2,
45
+ "pad_token_id": 151643,
46
+ "rms_norm_eps": 1e-06,
47
+ "rope_parameters": {
48
+ "rope_theta": 1000000.0,
49
+ "rope_type": "default"
50
+ },
51
+ "sliding_window": null,
52
+ "tie_word_embeddings": true,
53
+ "transformers_version": "5.3.0",
54
+ "use_cache": false,
55
+ "use_sliding_window": false,
56
+ "vocab_size": 151665
57
+ }
generation_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_sample": false,
3
+ "eos_token_id": [
4
+ 151643
5
+ ],
6
+ "max_new_tokens": 2048,
7
+ "pad_token_id": 151643,
8
+ "transformers_version": "5.3.0"
9
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dec0a38853802707af913a573241b0887e79a023462334396b67dd3b4fd146a0
3
+ size 987612192
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,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "backend": "tokenizers",
4
+ "bos_token": null,
5
+ "clean_up_tokenization_spaces": false,
6
+ "eos_token": "<|endoftext|>",
7
+ "errors": "replace",
8
+ "extra_special_tokens": [
9
+ "<|im_start|>",
10
+ "<|im_end|>"
11
+ ],
12
+ "is_local": false,
13
+ "model_max_length": 32768,
14
+ "pad_token": "<|endoftext|>",
15
+ "split_special_tokens": false,
16
+ "tokenizer_class": "Qwen2Tokenizer",
17
+ "unk_token": null
18
+ }