jerryzhao173985 commited on
Commit
c445217
·
verified ·
1 Parent(s): 5ac355f

Add files using upload-large-folder tool

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,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: burtenshaw/Qwen3-30B-A3B-python-coder
3
+ datasets: burtenshaw/tulu-3-sft-personas-code-no-prompt
4
+ library_name: mlx
5
+ model_name: Qwen3-30B-A3B-python-coder
6
+ tags:
7
+ - generated_from_trainer
8
+ - trl
9
+ - sft
10
+ - mlx
11
+ licence: license
12
+ pipeline_tag: text-generation
13
+ ---
14
+
15
+ # jerryzhao173985/Qwen3-30B-A3B-python-coder-mlx
16
+
17
+ This model [jerryzhao173985/Qwen3-30B-A3B-python-coder-mlx](https://huggingface.co/jerryzhao173985/Qwen3-30B-A3B-python-coder-mlx) was
18
+ converted to MLX format from [burtenshaw/Qwen3-30B-A3B-python-coder](https://huggingface.co/burtenshaw/Qwen3-30B-A3B-python-coder)
19
+ using mlx-lm version **0.24.0**.
20
+
21
+ ## Use with mlx
22
+
23
+ ```bash
24
+ pip install mlx-lm
25
+ ```
26
+
27
+ ```python
28
+ from mlx_lm import load, generate
29
+
30
+ model, tokenizer = load("jerryzhao173985/Qwen3-30B-A3B-python-coder-mlx")
31
+
32
+ prompt = "hello"
33
+
34
+ if tokenizer.chat_template is not None:
35
+ messages = [{"role": "user", "content": prompt}]
36
+ prompt = tokenizer.apply_chat_template(
37
+ messages, add_generation_prompt=True
38
+ )
39
+
40
+ response = generate(model, tokenizer, prompt=prompt, verbose=True)
41
+ ```
added_tokens.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</think>": 151668,
3
+ "</tool_call>": 151658,
4
+ "</tool_response>": 151666,
5
+ "<think>": 151667,
6
+ "<tool_call>": 151657,
7
+ "<tool_response>": 151665,
8
+ "<|box_end|>": 151649,
9
+ "<|box_start|>": 151648,
10
+ "<|endoftext|>": 151643,
11
+ "<|file_sep|>": 151664,
12
+ "<|fim_middle|>": 151660,
13
+ "<|fim_pad|>": 151662,
14
+ "<|fim_prefix|>": 151659,
15
+ "<|fim_suffix|>": 151661,
16
+ "<|im_end|>": 151645,
17
+ "<|im_start|>": 151644,
18
+ "<|image_pad|>": 151655,
19
+ "<|object_ref_end|>": 151647,
20
+ "<|object_ref_start|>": 151646,
21
+ "<|quad_end|>": 151651,
22
+ "<|quad_start|>": 151650,
23
+ "<|repo_name|>": 151663,
24
+ "<|video_pad|>": 151656,
25
+ "<|vision_end|>": 151653,
26
+ "<|vision_pad|>": 151654,
27
+ "<|vision_start|>": 151652
28
+ }
config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen3MoeForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 151643,
8
+ "decoder_sparse_step": 1,
9
+ "eos_token_id": 151645,
10
+ "head_dim": 128,
11
+ "hidden_act": "silu",
12
+ "hidden_size": 2048,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 6144,
15
+ "max_position_embeddings": 40960,
16
+ "max_window_layers": 48,
17
+ "mlp_only_layers": [],
18
+ "model_type": "qwen3_moe",
19
+ "moe_intermediate_size": 768,
20
+ "norm_topk_prob": true,
21
+ "num_attention_heads": 32,
22
+ "num_experts": 128,
23
+ "num_experts_per_tok": 8,
24
+ "num_hidden_layers": 48,
25
+ "num_key_value_heads": 4,
26
+ "output_router_logits": false,
27
+ "quantization": {
28
+ "group_size": 64,
29
+ "bits": 4
30
+ },
31
+ "quantization_config": {
32
+ "group_size": 64,
33
+ "bits": 4
34
+ },
35
+ "rms_norm_eps": 1e-06,
36
+ "rope_scaling": null,
37
+ "rope_theta": 1000000.0,
38
+ "router_aux_loss_coef": 0.001,
39
+ "sliding_window": null,
40
+ "tie_word_embeddings": false,
41
+ "torch_dtype": "bfloat16",
42
+ "transformers_version": "4.51.3",
43
+ "use_cache": false,
44
+ "use_sliding_window": false,
45
+ "vocab_size": 151936
46
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a2bbd7c5fdd8307218e1e3607e6f5b497e5a4ad662ba9dceba618e5092bc4dc
3
+ size 5319507334
model-00002-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:becbd72fa9283968ba3e7760995d6734d6ad1559965e0e1046cead1c012e55f9
3
+ size 5364678700
model-00003-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65ba2116ec4346ebe76fd1fa529020c57570241783e41de3c50012c8277ac116
3
+ size 5274790267
model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a3e33952a2c12628255116e5a55dce40a34501f4c87a87257e834f55cf206496
3
+ size 1215804237
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
special_tokens_map.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>"
16
+ ],
17
+ "eos_token": {
18
+ "content": "<|im_end|>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ },
24
+ "pad_token": {
25
+ "content": "<|endoftext|>",
26
+ "lstrip": false,
27
+ "normalized": false,
28
+ "rstrip": false,
29
+ "single_word": false
30
+ }
31
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aeb13307a71acd8fe81861d94ad54ab689df773318809eed3cbe794b4492dae4
3
+ size 11422654
tokenizer_config.json ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": false,
3
+ "add_prefix_space": false,
4
+ "added_tokens_decoder": {
5
+ "151643": {
6
+ "content": "<|endoftext|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "151644": {
14
+ "content": "<|im_start|>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "151645": {
22
+ "content": "<|im_end|>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ },
29
+ "151646": {
30
+ "content": "<|object_ref_start|>",
31
+ "lstrip": false,
32
+ "normalized": false,
33
+ "rstrip": false,
34
+ "single_word": false,
35
+ "special": true
36
+ },
37
+ "151647": {
38
+ "content": "<|object_ref_end|>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false,
43
+ "special": true
44
+ },
45
+ "151648": {
46
+ "content": "<|box_start|>",
47
+ "lstrip": false,
48
+ "normalized": false,
49
+ "rstrip": false,
50
+ "single_word": false,
51
+ "special": true
52
+ },
53
+ "151649": {
54
+ "content": "<|box_end|>",
55
+ "lstrip": false,
56
+ "normalized": false,
57
+ "rstrip": false,
58
+ "single_word": false,
59
+ "special": true
60
+ },
61
+ "151650": {
62
+ "content": "<|quad_start|>",
63
+ "lstrip": false,
64
+ "normalized": false,
65
+ "rstrip": false,
66
+ "single_word": false,
67
+ "special": true
68
+ },
69
+ "151651": {
70
+ "content": "<|quad_end|>",
71
+ "lstrip": false,
72
+ "normalized": false,
73
+ "rstrip": false,
74
+ "single_word": false,
75
+ "special": true
76
+ },
77
+ "151652": {
78
+ "content": "<|vision_start|>",
79
+ "lstrip": false,
80
+ "normalized": false,
81
+ "rstrip": false,
82
+ "single_word": false,
83
+ "special": true
84
+ },
85
+ "151653": {
86
+ "content": "<|vision_end|>",
87
+ "lstrip": false,
88
+ "normalized": false,
89
+ "rstrip": false,
90
+ "single_word": false,
91
+ "special": true
92
+ },
93
+ "151654": {
94
+ "content": "<|vision_pad|>",
95
+ "lstrip": false,
96
+ "normalized": false,
97
+ "rstrip": false,
98
+ "single_word": false,
99
+ "special": true
100
+ },
101
+ "151655": {
102
+ "content": "<|image_pad|>",
103
+ "lstrip": false,
104
+ "normalized": false,
105
+ "rstrip": false,
106
+ "single_word": false,
107
+ "special": true
108
+ },
109
+ "151656": {
110
+ "content": "<|video_pad|>",
111
+ "lstrip": false,
112
+ "normalized": false,
113
+ "rstrip": false,
114
+ "single_word": false,
115
+ "special": true
116
+ },
117
+ "151657": {
118
+ "content": "<tool_call>",
119
+ "lstrip": false,
120
+ "normalized": false,
121
+ "rstrip": false,
122
+ "single_word": false,
123
+ "special": false
124
+ },
125
+ "151658": {
126
+ "content": "</tool_call>",
127
+ "lstrip": false,
128
+ "normalized": false,
129
+ "rstrip": false,
130
+ "single_word": false,
131
+ "special": false
132
+ },
133
+ "151659": {
134
+ "content": "<|fim_prefix|>",
135
+ "lstrip": false,
136
+ "normalized": false,
137
+ "rstrip": false,
138
+ "single_word": false,
139
+ "special": false
140
+ },
141
+ "151660": {
142
+ "content": "<|fim_middle|>",
143
+ "lstrip": false,
144
+ "normalized": false,
145
+ "rstrip": false,
146
+ "single_word": false,
147
+ "special": false
148
+ },
149
+ "151661": {
150
+ "content": "<|fim_suffix|>",
151
+ "lstrip": false,
152
+ "normalized": false,
153
+ "rstrip": false,
154
+ "single_word": false,
155
+ "special": false
156
+ },
157
+ "151662": {
158
+ "content": "<|fim_pad|>",
159
+ "lstrip": false,
160
+ "normalized": false,
161
+ "rstrip": false,
162
+ "single_word": false,
163
+ "special": false
164
+ },
165
+ "151663": {
166
+ "content": "<|repo_name|>",
167
+ "lstrip": false,
168
+ "normalized": false,
169
+ "rstrip": false,
170
+ "single_word": false,
171
+ "special": false
172
+ },
173
+ "151664": {
174
+ "content": "<|file_sep|>",
175
+ "lstrip": false,
176
+ "normalized": false,
177
+ "rstrip": false,
178
+ "single_word": false,
179
+ "special": false
180
+ },
181
+ "151665": {
182
+ "content": "<tool_response>",
183
+ "lstrip": false,
184
+ "normalized": false,
185
+ "rstrip": false,
186
+ "single_word": false,
187
+ "special": false
188
+ },
189
+ "151666": {
190
+ "content": "</tool_response>",
191
+ "lstrip": false,
192
+ "normalized": false,
193
+ "rstrip": false,
194
+ "single_word": false,
195
+ "special": false
196
+ },
197
+ "151667": {
198
+ "content": "<think>",
199
+ "lstrip": false,
200
+ "normalized": false,
201
+ "rstrip": false,
202
+ "single_word": false,
203
+ "special": false
204
+ },
205
+ "151668": {
206
+ "content": "</think>",
207
+ "lstrip": false,
208
+ "normalized": false,
209
+ "rstrip": false,
210
+ "single_word": false,
211
+ "special": false
212
+ }
213
+ },
214
+ "additional_special_tokens": [
215
+ "<|im_start|>",
216
+ "<|im_end|>",
217
+ "<|object_ref_start|>",
218
+ "<|object_ref_end|>",
219
+ "<|box_start|>",
220
+ "<|box_end|>",
221
+ "<|quad_start|>",
222
+ "<|quad_end|>",
223
+ "<|vision_start|>",
224
+ "<|vision_end|>",
225
+ "<|vision_pad|>",
226
+ "<|image_pad|>",
227
+ "<|video_pad|>"
228
+ ],
229
+ "bos_token": null,
230
+ "chat_template": "{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\\n\\n' }}\n {%- endif %}\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>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\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\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == \"user\" and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {%- set content = message.content %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is defined and message.reasoning_content is not none %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '</think>' in message.content %}\n {%- set content = message.content.split('</think>')[-1].lstrip('\\n') %}\n {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\\n').split('<think>')[-1].lstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\\n<think>\\n' + reasoning_content.strip('\\n') + '\\n</think>\\n\\n' + content.lstrip('\\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- message.content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\\n\\n</think>\\n\\n' }}\n {%- endif %}\n{%- endif %}",
231
+ "clean_up_tokenization_spaces": false,
232
+ "eos_token": "<|im_end|>",
233
+ "errors": "replace",
234
+ "extra_special_tokens": {},
235
+ "model_max_length": 131072,
236
+ "pad_token": "<|endoftext|>",
237
+ "split_special_tokens": false,
238
+ "tokenizer_class": "Qwen2Tokenizer",
239
+ "unk_token": null
240
+ }
train.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "datasets",
5
+ # "httpx",
6
+ # "huggingface - hub",
7
+ # "setuptools",
8
+ # "transformers",
9
+ # "torch",
10
+ # "accelerate",
11
+ # "trl",
12
+ # "peft",
13
+ # "wandb",
14
+ # "torchvision",
15
+ # "torchaudio"
16
+ # ]
17
+ # ///
18
+
19
+
20
+ """## Import libraries"""
21
+
22
+ import torch
23
+ from datasets import load_dataset
24
+ from transformers import AutoModelForCausalLM, AutoTokenizer
25
+ from trl import SFTConfig, SFTTrainer, setup_chat_format
26
+ from peft import LoraConfig
27
+
28
+ """# Load Dataset"""
29
+
30
+ dataset_name = "allenai/tulu-3-sft-personas-code" # Example dataset
31
+
32
+ # Load dataset
33
+ dataset = load_dataset(dataset_name, split="train")
34
+ print(f"Dataset loaded: {dataset}")
35
+
36
+ # Let's look at a sample
37
+ print("\nSample data:")
38
+ print(dataset[0])
39
+
40
+ dataset = dataset.remove_columns("prompt")
41
+ dataset = dataset.train_test_split(test_size=0.2)
42
+
43
+ print(
44
+ f"Train Samples: {len(dataset['train'])}\nTest Samples: {len(dataset['test'])}"
45
+ )
46
+
47
+ """## Configuration
48
+
49
+ Set up the configuration parameters for the fine-tuning process.
50
+ """
51
+
52
+ # Model configuration
53
+ model_name = "Qwen/Qwen3-30B-A3B" # You can change this to any model you want to fine-tune
54
+
55
+ # # Other compatible Qwen3 models
56
+ # model_name = "Qwen/Qwen3-32B"
57
+ # model_name = "Qwen/Qwen3-14B"
58
+ # model_name = "Qwen/Qwen3-8B"
59
+ # model_name = "Qwen/Qwen3-4B"
60
+ # model_name = "Qwen/Qwen3-1.7B"
61
+ # model_name = "Qwen/Qwen3-0.6B"
62
+
63
+ # Training configuration
64
+ output_dir = "./tmp/sft-model"
65
+ num_train_epochs = 1
66
+ per_device_train_batch_size = 1
67
+ gradient_accumulation_steps = 1
68
+ learning_rate = 2e-4
69
+
70
+ """## Load model and tokenizer"""
71
+
72
+ # Load model
73
+ model = AutoModelForCausalLM.from_pretrained(
74
+ model_name,
75
+ torch_dtype=torch.bfloat16,
76
+ use_cache=False, # Disable KV cache during training
77
+ device_map="auto",
78
+ )
79
+
80
+ # Load tokenizer
81
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
82
+
83
+ # # Set up chat formatting (if the model doesn't have a chat template)
84
+ # if tokenizer.chat_template is None:
85
+ # model, tokenizer = setup_chat_format(model, tokenizer, format="chatml")
86
+
87
+ # # Set padding token
88
+ # if tokenizer.pad_token is None:
89
+ # tokenizer.pad_token = tokenizer.eos_token
90
+
91
+ """## Configure PEFT (if enabled)"""
92
+
93
+ # Set up PEFT configuration if enabled
94
+ peft_config = LoraConfig(
95
+ r=32, # Rank
96
+ lora_alpha=16, # Alpha parameter for LoRA scaling
97
+ lora_dropout=0.05,
98
+ bias="none",
99
+ task_type="CAUSAL_LM",
100
+ target_modules="all-linear",
101
+ )
102
+
103
+ """## Configure SFT Trainer"""
104
+
105
+ # Training arguments
106
+ training_args = SFTConfig(
107
+ output_dir=output_dir,
108
+ num_train_epochs=num_train_epochs,
109
+ per_device_train_batch_size=per_device_train_batch_size,
110
+ gradient_accumulation_steps=gradient_accumulation_steps,
111
+ learning_rate=learning_rate,
112
+ gradient_checkpointing=True,
113
+ logging_steps=25,
114
+ save_strategy="epoch",
115
+ optim="adamw_torch",
116
+ lr_scheduler_type="cosine",
117
+ warmup_ratio=0.1,
118
+ max_length=1024,
119
+ packing=True, # Enable packing to increase training efficiency
120
+ eos_token=tokenizer.eos_token,
121
+ bf16=True,
122
+ fp16=False,
123
+ max_steps=1000,
124
+ report_to="wandb", # Disable reporting to avoid wandb prompts
125
+ )
126
+
127
+ """## Initialize and run the SFT Trainer"""
128
+
129
+ # Create SFT Trainer
130
+ trainer = SFTTrainer(
131
+ model=model,
132
+ args=training_args,
133
+ train_dataset=dataset["train"],
134
+ eval_dataset=dataset["test"] if "test" in dataset else None,
135
+ peft_config=peft_config,
136
+ processing_class=tokenizer,
137
+ )
138
+
139
+ # Train the model
140
+ trainer.train()
141
+
142
+ """## Save the fine-tuned model"""
143
+
144
+ # Save the model
145
+ trainer.save_model(output_dir)
146
+
147
+ """## Test the fine-tuned model"""
148
+
149
+ from peft import PeftModel, PeftConfig
150
+
151
+ # Load the base model
152
+ base_model = AutoModelForCausalLM.from_pretrained(
153
+ model_name, trust_remote_code=True, torch_dtype=torch.bfloat16
154
+ )
155
+
156
+ # Load the fine-tuned PEFT model
157
+ model = PeftModel.from_pretrained(base_model, output_dir)
158
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
159
+ # Test the model with an example
160
+ prompt = """Write a function called is_palindrome that takes a single string as input and returns True if the string is a palindrome, and False otherwise.
161
+
162
+ Palindrome Definition:
163
+
164
+ A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.
165
+
166
+ Example:
167
+ ```
168
+ is_palindrome("racecar") # Returns True
169
+ is_palindrome("hello") # Returns False
170
+ is_palindrome("A man, a plan, a canal: Panama") # Returns True
171
+ ```
172
+ """
173
+
174
+ # Format the chat prompt using the tokenizer's chat template
175
+ messages = [
176
+ {"role": "system", "content": "You are a helpful assistant."},
177
+ {"role": "user", "content": prompt},
178
+ ]
179
+ formatted_prompt = tokenizer.apply_chat_template(
180
+ messages, tokenize=False, add_generation_prompt=True
181
+ )
182
+ print(f"Formatted prompt: {formatted_prompt}")
183
+
184
+ # Generate response
185
+ model.eval()
186
+ inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
187
+ with torch.no_grad():
188
+ outputs = model.generate(
189
+ **inputs,
190
+ max_new_tokens=500,
191
+ temperature=0.7,
192
+ top_p=0.9,
193
+ do_sample=True,
194
+ pad_token_id=tokenizer.eos_token_id,
195
+ )
196
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
197
+ print("\nGenerated Response:")
198
+ print(response)
199
+
200
+ model.push_to_hub("burtenshaw/Qwen3-30B-A3B-python-code")
vocab.json ADDED
The diff for this file is too large to render. See raw diff