ckoh04 commited on
Commit
0a20d73
·
verified ·
1 Parent(s): 46eb8aa

Add files using upload-large-folder tool

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ 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
37
+ model.safetensors.index.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ - zh
6
+ library_name: transformers
7
+ pipeline_tag: text-generation
8
+ tags:
9
+ - text-generation
10
+ - agent
11
+ - long-context
12
+ - code
13
+ - mxfp4
14
+ - fp4-quantization
15
+ - dflash
16
+ - speculative-decoding
17
+ - block-diffusion
18
+ ---
19
+
20
+ <br/><br/>
21
+
22
+ <div align="center">
23
+ <picture>
24
+ <source srcset="https://github.com/XiaomiMiMo/MiMo/raw/main/figures/Xiaomi_MiMo_darkmode.png?raw=true" media="(prefers-color-scheme: dark)">
25
+ <img src="https://github.com/XiaomiMiMo/MiMo/raw/main/figures/Xiaomi_MiMo.png?raw=true" width="60%" alt="Xiaomi-MiMo" />
26
+ </picture>
27
+ </div>
28
+
29
+ <br/>
30
+
31
+ <div align="center" style="line-height: 1;">
32
+ <a href="https://huggingface.co/XiaomiMiMo" target="_blank">🤗 HuggingFace</a>
33
+ &nbsp;|&nbsp;
34
+ <a href="https://mimo.xiaomi.com/blog/mimo-tilert-1000tps" target="_blank">📰 Blog </a>
35
+
36
+ <br/>
37
+
38
+ <a href="https://platform.xiaomimimo.com/ultraspeed" target="_blank">🎨 Xiaomi MiMo API Platform (Request Access) </a>
39
+ &nbsp;|&nbsp;
40
+ <a href="https://ultraspeed.xiaomimimo.com" target="_blank">🗨️ Xiaomi MiMo Studio (Free Trial) </a>
41
+ </div>
42
+
43
+ <br/>
44
+
45
+ <div align="center" style="line-height: 1.2;">
46
+ <strong>Community</strong><br/>
47
+ <a href="https://huggingface.co/XiaomiMiMo/MiMo-V2.5-Pro/blob/main/assets/wechat.jpg" target="_blank">WeChat Group</a>
48
+ &nbsp;|&nbsp;
49
+ <a href="https://discord.gg/kKC2kNnQEX" target="_blank">Discord</a>
50
+ &nbsp;|&nbsp;
51
+ <a href="https://t.me/+3T-I0pekOVIyNDBl" target="_blank">Telegram</a>
52
+ &nbsp;|&nbsp;
53
+ <a href="https://www.reddit.com/r/XiaomiMiMo_Official/" target="_blank">Reddit</a>
54
+ </div>
55
+
56
+
57
+ <br/>
58
+
59
+ # MiMo-V2.5-Pro-FP4-DFlash
60
+
61
+ MiMo-V2.5-Pro-FP4-DFlash is the underlying model that powers MiMo-V2.5-Pro-UltraSpeed:
62
+
63
+ - An **FP4-quantized backbone** that applies MXFP4 quantization to the MoE experts while keeping the rest of the model at higher precision, shrinking model size and memory-bandwidth pressure with near-lossless quality.
64
+ - A **BF16 DFlash drafter** for block-diffusion speculative decoding, which proposes a whole block of tokens per forward pass and lets the backbone verify them in one step.
65
+
66
+ Together they cut both the per-parameter bit width and the number of backbone forward passes, the two dominant costs of trillion-parameter decoding.
67
+
68
+ ## 1. Introduction
69
+
70
+ At the trillion-parameter (1T) scale, even 8-bit (FP8/INT8) inference carries severe memory-footprint and memory-bandwidth costs. Lowering the parameter bit width translates directly into faster decoding. We therefore adopt FP4 quantization and block-diffusion speculative decoding. Key features of this release:
71
+
72
+ - **Expert-Only FP4 Quantization**: A blanket FP4 cast over the whole model tends to degrade accuracy and generalization on complex reasoning and code. Given MiMo-V2.5-Pro's MoE architecture where experts hold the vast majority of parameters and tolerate quantization best, we quantize only the MoE experts to FP4 (MXFP4) and keep the other modules at their original precision. Through FP4 QAT, the model retains near-lossless capability while substantially reducing size and saturating hardware bandwidth.
73
+ - **DFlash Speculative Decoding**: A lightweight block-diffusion drafter fills an entire block of masked positions in a single forward pass, removing the serial draft autoregression bottleneck of conventional speculative decoding while the backbone's verification preserves output quality.
74
+
75
+ ## 2. FP4 Quantization
76
+
77
+ We quantize only the MoE experts to MXFP4 (block size 32) and keep attention projections and other modules at higher precision (the attention `o_proj` of every layer is excluded from FP4). With FP4 QAT, quality stays close to the FP8 baseline:
78
+
79
+ <img src="assets/fp4_compare.png" width="100%" alt="fp4 compare" />
80
+
81
+ | Benchmark | MiMo-V2.5-Pro-FP8 | MiMo-V2.5-Pro-MXFP4 | Δ |
82
+ | :--- | :---: | :---: | :---: |
83
+ | **General Agent** | | | |
84
+ | Claw-Eval (pass^3) | 63.8 | 67.8 | +6.27% |
85
+ | Humanity's Last Exam | 48.0 | 47.0 | -2.08% |
86
+ | Humanity's Last Exam (without tool) | 34.0 | 33.0 | -2.94% |
87
+ | **Code Agent** | | | |
88
+ | SWE-Bench Pro | 57.2 | 58.8 | +2.80% |
89
+ | SWE-bench Verified | 78.9 | 77.4 | -1.90% |
90
+
91
+ ## 3. Block-Diffusion Speculative Decoding (DFlash)
92
+
93
+ Conventional speculative decoding relies on a small draft model to guess the next tokens, which the large model then verifies; the rejection-sampling verification keeps the output lossless. Its bottleneck is that draft quality bounds the acceptance rate, while a stronger draft costs more compute.
94
+
95
+ To break this trade-off we adopt the block-level masked parallel-prediction approach DFlash: the draft fills an entire block of masked positions in one forward pass. We landed this on MiMo-V2.5-Pro with custom optimizations for trillion-scale MoE and long-context serving, using the Muon second-order optimizer and model self-distillation so that even a small mask block keeps a strong acceptance rate while pushing the draft-stage cost close to its limit:
96
+
97
+ - The drafter uses Sliding Window Attention (SWA) throughout, naturally aligned with the SWA design of the MiMo-V2 series. The draft no longer depends on the full prefix, so the per-prediction compute moves from linear-in-context-length to constant.
98
+ - During training the mask signal is sampled on the local GPU shard, so a single sequence yields tens of thousands of independent training signals covering positions at different context lengths in one step, aligning with the MiMo-V2 series' long-context capability while avoiding cross-device communication overhead.
99
+
100
+ In practice, we further cap the mask block size at 8 to lower verification overhead and raise concurrency.
101
+
102
+ | Scenario | Acceptance Length |
103
+ | :--- | :---: |
104
+ | WebDev | 6.30 |
105
+ | Math500 | 5.56 |
106
+ | HumanEval | 4.54 |
107
+ | MT-Bench | 3.18 |
108
+ | SWE-Bench | 4.29 |
109
+
110
+ ## 4. Model Summary
111
+
112
+ | Component | Backbone | DFlash Drafter |
113
+ | :--- | :---: | :---: |
114
+ | **Architecture** | MiMoV2ForCausalLM | DFlashDraftModel |
115
+ | **Total / Active Params** | 1.02T / 42B | 5-layer draft |
116
+ | **Hidden Size** | 6144 | 6144 |
117
+ | **Num Layers** | 70 | 5 |
118
+ | **Num Attention Heads** | 128 | 128 |
119
+ | **Num KV Heads** | 8 (GQA) | 8 (GQA) |
120
+ | **Head Dim (QK / V)** | 192 / 128 | 128 / 128 |
121
+ | **SWA Window Size** | 128 | 1024 |
122
+ | **Block Size** | — | 8 |
123
+ | **Captured Backbone Layers** | — | [0, 15, 31, 47, 69] |
124
+ | **Backbone RoPE Base** | 5,000,000 | 5,000,000 |
125
+ | **Precision** | MXFP4 (experts) Mixed | BF16 |
126
+ | **Max Context Length** | 1M | — |
127
+
128
+ ## 5. Deployment
129
+
130
+ DFlash inference with the FP4 backbone is supported in SGLang. The drafter is launched alongside the backbone via the speculative-decoding flags and inherits the backbone's tensor/expert-parallel topology.
131
+
132
+ ### SGLang Deployment
133
+
134
+ The following is an example of running the model with SGLang. Point `--model` at this repository and `--speculative-draft-model-path` at its `dflash/` subdirectory.
135
+
136
+ ```bash
137
+ python3 -m sglang.launch_server \
138
+ --model MiMo-V2.5-Pro-FP4-DFlash \
139
+ --speculative-algorithm DFLASH \
140
+ --speculative-draft-model-path MiMo-V2.5-Pro-FP4-DFlash/dflash \
141
+ --speculative-num-draft-tokens 8 \
142
+ --ep-size 16 \
143
+ --tensor-parallel-size 16 \
144
+ --data-parallel-size 2 \
145
+ --enable-dp-attention \
146
+ --enable-dp-lm-head \
147
+ --quantization fp8 \
148
+ --attention-backend fa3 \
149
+ --moe-dense-tp-size 1 \
150
+ --dtype bfloat16 \
151
+ --mem-fraction-static 0.65 \
152
+ --context-length 65536 \
153
+ --page-size 1 \
154
+ --trust-remote-code \
155
+ --disable-overlap-schedule \
156
+ --skip-server-warmup \
157
+ --dist-init-addr ${MASTER_ADDR}:20000 \
158
+ --nnodes ${WORLD_SIZE} \
159
+ --node-rank ${RANK} \
160
+ --host 0.0.0.0 \
161
+ --port 29999
162
+ ```
163
+
164
+ ## Citation
165
+
166
+ ```bibtex
167
+ @misc{mimo2026v25pro_fp4dflash,
168
+ title={MiMo-V2.5-Pro-FP4-DFlash},
169
+ author={{Xiaomi MiMo Team}},
170
+ year={2026},
171
+ howpublished={\url{https://huggingface.co/collections/XiaomiMiMo/mimo-v25}},
172
+ }
173
+ ```
174
+
175
+ ## Contact
176
+
177
+ For questions or feedback, reach us at [mimo@xiaomi.com](mailto:mimo@xiaomi.com) or join our community:
178
+
179
+ - [WeChat Group](https://work.weixin.qq.com/apph5/external_room/join/group_mng?plg_id=c417f99bd9014b5dd894daa8bfe19790&)
180
+ - [Discord](https://discord.gg/WX2R2uNp)
181
+ - [Telegram](https://t.me/+3T-I0pekOVIyNDBl)
182
+ - [Reddit](https://www.reddit.com/r/XiaomiMiMo_Official/)
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
+ }
assets/fp4_compare.png ADDED
config.json ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "MiMoV2ForCausalLM"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_mimo_v2.MiMoV2Config",
7
+ "AutoModel": "modeling_mimo_v2.MiMoV2Model",
8
+ "AutoModelForCausalLM": "modeling_mimo_v2.MiMoV2ForCausalLM"
9
+ },
10
+ "quantization_config": {
11
+ "activation_scheme": "dynamic",
12
+ "fmt": "e4m3",
13
+ "quant_method": "fp8",
14
+ "store_dtype": "mxfp4",
15
+ "weight_block_size": [
16
+ 128,
17
+ 128
18
+ ],
19
+ "mxfp4_block_size": 32,
20
+ "ignored_layers": [
21
+ "model.layers.0.self_attn.o_proj",
22
+ "model.layers.1.self_attn.o_proj",
23
+ "model.layers.2.self_attn.o_proj",
24
+ "model.layers.3.self_attn.o_proj",
25
+ "model.layers.4.self_attn.o_proj",
26
+ "model.layers.5.self_attn.o_proj",
27
+ "model.layers.6.self_attn.o_proj",
28
+ "model.layers.7.self_attn.o_proj",
29
+ "model.layers.8.self_attn.o_proj",
30
+ "model.layers.9.self_attn.o_proj",
31
+ "model.layers.10.self_attn.o_proj",
32
+ "model.layers.11.self_attn.o_proj",
33
+ "model.layers.12.self_attn.o_proj",
34
+ "model.layers.13.self_attn.o_proj",
35
+ "model.layers.14.self_attn.o_proj",
36
+ "model.layers.15.self_attn.o_proj",
37
+ "model.layers.16.self_attn.o_proj",
38
+ "model.layers.17.self_attn.o_proj",
39
+ "model.layers.18.self_attn.o_proj",
40
+ "model.layers.19.self_attn.o_proj",
41
+ "model.layers.20.self_attn.o_proj",
42
+ "model.layers.21.self_attn.o_proj",
43
+ "model.layers.22.self_attn.o_proj",
44
+ "model.layers.23.self_attn.o_proj",
45
+ "model.layers.24.self_attn.o_proj",
46
+ "model.layers.25.self_attn.o_proj",
47
+ "model.layers.26.self_attn.o_proj",
48
+ "model.layers.27.self_attn.o_proj",
49
+ "model.layers.28.self_attn.o_proj",
50
+ "model.layers.29.self_attn.o_proj",
51
+ "model.layers.30.self_attn.o_proj",
52
+ "model.layers.31.self_attn.o_proj",
53
+ "model.layers.32.self_attn.o_proj",
54
+ "model.layers.33.self_attn.o_proj",
55
+ "model.layers.34.self_attn.o_proj",
56
+ "model.layers.35.self_attn.o_proj",
57
+ "model.layers.36.self_attn.o_proj",
58
+ "model.layers.37.self_attn.o_proj",
59
+ "model.layers.38.self_attn.o_proj",
60
+ "model.layers.39.self_attn.o_proj",
61
+ "model.layers.40.self_attn.o_proj",
62
+ "model.layers.41.self_attn.o_proj",
63
+ "model.layers.42.self_attn.o_proj",
64
+ "model.layers.43.self_attn.o_proj",
65
+ "model.layers.44.self_attn.o_proj",
66
+ "model.layers.45.self_attn.o_proj",
67
+ "model.layers.46.self_attn.o_proj",
68
+ "model.layers.47.self_attn.o_proj",
69
+ "model.layers.48.self_attn.o_proj",
70
+ "model.layers.49.self_attn.o_proj",
71
+ "model.layers.50.self_attn.o_proj",
72
+ "model.layers.51.self_attn.o_proj",
73
+ "model.layers.52.self_attn.o_proj",
74
+ "model.layers.53.self_attn.o_proj",
75
+ "model.layers.54.self_attn.o_proj",
76
+ "model.layers.55.self_attn.o_proj",
77
+ "model.layers.56.self_attn.o_proj",
78
+ "model.layers.57.self_attn.o_proj",
79
+ "model.layers.58.self_attn.o_proj",
80
+ "model.layers.59.self_attn.o_proj",
81
+ "model.layers.60.self_attn.o_proj",
82
+ "model.layers.61.self_attn.o_proj",
83
+ "model.layers.62.self_attn.o_proj",
84
+ "model.layers.63.self_attn.o_proj",
85
+ "model.layers.64.self_attn.o_proj",
86
+ "model.layers.65.self_attn.o_proj",
87
+ "model.layers.66.self_attn.o_proj",
88
+ "model.layers.67.self_attn.o_proj",
89
+ "model.layers.68.self_attn.o_proj",
90
+ "model.layers.69.self_attn.o_proj",
91
+ "model.decoder.self_attn.o_proj"
92
+ ]
93
+ },
94
+ "add_full_attention_sink_bias": false,
95
+ "add_swa_attention_sink_bias": true,
96
+ "attention_bias": false,
97
+ "attention_chunk_size": 128,
98
+ "attention_dropout": 0.0,
99
+ "attention_projection_layout": "fused_qkv",
100
+ "attention_value_scale": 0.612,
101
+ "head_dim": 192,
102
+ "hidden_act": "silu",
103
+ "hidden_size": 6144,
104
+ "hybrid_layer_pattern": [
105
+ 0,
106
+ 1,
107
+ 1,
108
+ 1,
109
+ 1,
110
+ 1,
111
+ 1,
112
+ 0,
113
+ 1,
114
+ 1,
115
+ 1,
116
+ 1,
117
+ 1,
118
+ 1,
119
+ 1,
120
+ 0,
121
+ 1,
122
+ 1,
123
+ 1,
124
+ 1,
125
+ 1,
126
+ 1,
127
+ 1,
128
+ 0,
129
+ 1,
130
+ 1,
131
+ 1,
132
+ 1,
133
+ 1,
134
+ 1,
135
+ 1,
136
+ 0,
137
+ 1,
138
+ 1,
139
+ 1,
140
+ 1,
141
+ 1,
142
+ 1,
143
+ 1,
144
+ 0,
145
+ 1,
146
+ 1,
147
+ 1,
148
+ 1,
149
+ 1,
150
+ 1,
151
+ 1,
152
+ 0,
153
+ 1,
154
+ 1,
155
+ 1,
156
+ 1,
157
+ 1,
158
+ 1,
159
+ 1,
160
+ 0,
161
+ 1,
162
+ 1,
163
+ 1,
164
+ 1,
165
+ 1,
166
+ 1,
167
+ 0,
168
+ 1,
169
+ 1,
170
+ 1,
171
+ 1,
172
+ 1,
173
+ 1,
174
+ 0
175
+ ],
176
+ "initializer_range": 0.02,
177
+ "intermediate_size": 16384,
178
+ "layernorm_epsilon": 1e-05,
179
+ "max_position_embeddings": 1048576,
180
+ "model_type": "mimo_v2",
181
+ "moe_intermediate_size": 2048,
182
+ "moe_layer_freq": [
183
+ 0,
184
+ 1,
185
+ 1,
186
+ 1,
187
+ 1,
188
+ 1,
189
+ 1,
190
+ 1,
191
+ 1,
192
+ 1,
193
+ 1,
194
+ 1,
195
+ 1,
196
+ 1,
197
+ 1,
198
+ 1,
199
+ 1,
200
+ 1,
201
+ 1,
202
+ 1,
203
+ 1,
204
+ 1,
205
+ 1,
206
+ 1,
207
+ 1,
208
+ 1,
209
+ 1,
210
+ 1,
211
+ 1,
212
+ 1,
213
+ 1,
214
+ 1,
215
+ 1,
216
+ 1,
217
+ 1,
218
+ 1,
219
+ 1,
220
+ 1,
221
+ 1,
222
+ 1,
223
+ 1,
224
+ 1,
225
+ 1,
226
+ 1,
227
+ 1,
228
+ 1,
229
+ 1,
230
+ 1,
231
+ 1,
232
+ 1,
233
+ 1,
234
+ 1,
235
+ 1,
236
+ 1,
237
+ 1,
238
+ 1,
239
+ 1,
240
+ 1,
241
+ 1,
242
+ 1,
243
+ 1,
244
+ 1,
245
+ 1,
246
+ 1,
247
+ 1,
248
+ 1,
249
+ 1,
250
+ 1,
251
+ 1,
252
+ 1
253
+ ],
254
+ "n_group": 1,
255
+ "n_routed_experts": 8,
256
+ "n_shared_experts": null,
257
+ "norm_topk_prob": true,
258
+ "num_attention_heads": 128,
259
+ "num_experts_per_tok": 8,
260
+ "num_hidden_layers": 70,
261
+ "num_key_value_heads": 8,
262
+ "partial_rotary_factor": 0.334,
263
+ "rope_theta": 5000000,
264
+ "routed_scaling_factor": null,
265
+ "scoring_func": "sigmoid",
266
+ "sliding_window": 128,
267
+ "sliding_window_size": 128,
268
+ "swa_head_dim": 192,
269
+ "swa_num_attention_heads": 128,
270
+ "swa_num_key_value_heads": 8,
271
+ "swa_rope_theta": 10000,
272
+ "swa_v_head_dim": 128,
273
+ "tie_word_embeddings": false,
274
+ "topk_group": 1,
275
+ "topk_method": "noaux_tc",
276
+ "torch_dtype": "bfloat16",
277
+ "transformers_version": "4.57.1",
278
+ "use_cache": true,
279
+ "v_head_dim": 128,
280
+ "vocab_size": 152576,
281
+ "n_activated_experts": 8,
282
+ "pruned_from": "/media/ai-lab2/Expansion1/models/MiMo-V2.5-Pro-FP4-DFlash",
283
+ "pruning_type": "mimo_v25_routed_expert_active_slice",
284
+ "pruned_original_expert_ids": [
285
+ 0,
286
+ 1,
287
+ 2,
288
+ 3,
289
+ 4,
290
+ 5,
291
+ 6,
292
+ 7
293
+ ]
294
+ }
configuration_mimo_v2.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ #
3
+ # Copyright 2026 Xiaomi Corporation.
4
+ # Copyright 2026 The HuggingFace Inc. team.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ from transformers.configuration_utils import PretrainedConfig
19
+ from transformers.modeling_rope_utils import rope_config_validation
20
+ from transformers.utils import logging
21
+
22
+
23
+ logger = logging.get_logger(__name__)
24
+
25
+
26
+ _MIMOV2_ATTENTION_PROJECTION_LAYOUTS = {"split", "fused_qkv"}
27
+
28
+ _MIMOV2_SPLIT_TP_PLAN = {
29
+ "layers.*.self_attn.q_proj": "colwise",
30
+ "layers.*.self_attn.k_proj": "colwise",
31
+ "layers.*.self_attn.v_proj": "colwise",
32
+ "layers.*.self_attn.o_proj": "rowwise",
33
+ "layers.*.mlp.gate_proj": "colwise",
34
+ "layers.*.mlp.up_proj": "colwise",
35
+ "layers.*.mlp.down_proj": "rowwise",
36
+ }
37
+
38
+ _MIMOV2_FUSED_QKV_TP_PLAN = {
39
+ "layers.*.self_attn.qkv_proj": "colwise",
40
+ "layers.*.self_attn.o_proj": "rowwise",
41
+ "layers.*.mlp.gate_proj": "colwise",
42
+ "layers.*.mlp.up_proj": "colwise",
43
+ "layers.*.mlp.down_proj": "rowwise",
44
+ }
45
+
46
+ _MIMOV2_PP_PLAN = {
47
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
48
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
49
+ "norm": (["hidden_states"], ["hidden_states"]),
50
+ }
51
+
52
+
53
+ class MiMoV2Config(PretrainedConfig):
54
+
55
+ model_type = "mimo_v2"
56
+ keys_to_ignore_at_inference = ["past_key_values"]
57
+
58
+ base_model_tp_plan = _MIMOV2_SPLIT_TP_PLAN
59
+ base_model_pp_plan = _MIMOV2_PP_PLAN
60
+
61
+ attribute_map = {
62
+ "num_local_experts": "n_routed_experts",
63
+ }
64
+
65
+ def __init__(
66
+ self,
67
+ vocab_size=151936,
68
+ hidden_size=4096,
69
+ intermediate_size=22016,
70
+ num_hidden_layers=32,
71
+ num_attention_heads=32,
72
+ num_key_value_heads=32,
73
+ hidden_act="silu",
74
+ max_position_embeddings=32768,
75
+ initializer_range=0.02,
76
+ layernorm_epsilon=1e-6,
77
+ use_cache=True,
78
+ tie_word_embeddings=False,
79
+ rope_theta=10000.0,
80
+ rope_scaling=None,
81
+ attention_dropout=0.0,
82
+ attention_bias=False,
83
+ attention_value_scale=None,
84
+ head_dim=None,
85
+ v_head_dim=None,
86
+ swa_num_attention_heads=None,
87
+ swa_num_key_value_heads=None,
88
+ swa_head_dim=None,
89
+ swa_v_head_dim=None,
90
+ swa_rope_theta=None,
91
+ sliding_window=None,
92
+ sliding_window_size=None,
93
+ add_full_attention_sink_bias=False,
94
+ add_swa_attention_sink_bias=False,
95
+ hybrid_block_size=None,
96
+ hybrid_layer_pattern=None,
97
+ partial_rotary_factor=1.0,
98
+ n_routed_experts=None,
99
+ moe_intermediate_size=None,
100
+ num_experts_per_tok=None,
101
+ routed_scaling_factor=None,
102
+ scoring_func="sigmoid",
103
+ topk_method="noaux_tc",
104
+ n_group=None,
105
+ topk_group=None,
106
+ norm_topk_prob=True,
107
+ moe_layer_freq=None,
108
+ attention_projection_layout="split",
109
+ **kwargs,
110
+ ):
111
+ rope_parameters = kwargs.pop("rope_parameters", None)
112
+ if rope_scaling is None and rope_parameters is not None:
113
+ rope_scaling = rope_parameters
114
+
115
+ if attention_projection_layout is None:
116
+ attention_projection_layout = "split"
117
+ if attention_projection_layout not in _MIMOV2_ATTENTION_PROJECTION_LAYOUTS:
118
+ raise ValueError(f"Unsupported MiMoV2 attention projection layout: {attention_projection_layout}")
119
+
120
+ self.attention_projection_layout = attention_projection_layout
121
+ self.base_model_tp_plan = (
122
+ _MIMOV2_FUSED_QKV_TP_PLAN.copy()
123
+ if attention_projection_layout == "fused_qkv"
124
+ else _MIMOV2_SPLIT_TP_PLAN.copy()
125
+ )
126
+ self.base_model_pp_plan = _MIMOV2_PP_PLAN.copy()
127
+
128
+ self.vocab_size = vocab_size
129
+ self.max_position_embeddings = max_position_embeddings
130
+ self.hidden_size = hidden_size
131
+ self.intermediate_size = intermediate_size
132
+ self.num_hidden_layers = num_hidden_layers
133
+ self.num_attention_heads = num_attention_heads
134
+
135
+ if num_key_value_heads is None:
136
+ num_key_value_heads = num_attention_heads
137
+ if num_attention_heads % num_key_value_heads != 0:
138
+ raise ValueError("num_attention_heads must be divisible by num_key_value_heads")
139
+
140
+ self.num_key_value_heads = num_key_value_heads
141
+ self.hidden_act = hidden_act
142
+ self.initializer_range = initializer_range
143
+ self.layernorm_epsilon = layernorm_epsilon
144
+ self.use_cache = use_cache
145
+ self.rope_theta = rope_theta
146
+ self.rope_scaling = rope_scaling
147
+ self.attention_dropout = attention_dropout
148
+ self.attention_bias = attention_bias
149
+ self.attention_value_scale = attention_value_scale
150
+
151
+ self.head_dim = head_dim if head_dim is not None else hidden_size // num_attention_heads
152
+ self.v_head_dim = v_head_dim if v_head_dim is not None else self.head_dim
153
+ self.swa_num_attention_heads = (
154
+ swa_num_attention_heads if swa_num_attention_heads is not None else num_attention_heads
155
+ )
156
+ self.swa_num_key_value_heads = (
157
+ swa_num_key_value_heads if swa_num_key_value_heads is not None else num_key_value_heads
158
+ )
159
+ if self.swa_num_attention_heads % self.swa_num_key_value_heads != 0:
160
+ raise ValueError("swa_num_attention_heads must be divisible by swa_num_key_value_heads")
161
+ self.swa_head_dim = swa_head_dim if swa_head_dim is not None else self.head_dim
162
+ self.swa_v_head_dim = swa_v_head_dim if swa_v_head_dim is not None else self.swa_head_dim
163
+ self.swa_rope_theta = swa_rope_theta if swa_rope_theta is not None else rope_theta
164
+
165
+ if sliding_window is None:
166
+ sliding_window = sliding_window_size
167
+ self.sliding_window = sliding_window
168
+ self.sliding_window_size = sliding_window_size if sliding_window_size is not None else sliding_window
169
+ self.add_full_attention_sink_bias = add_full_attention_sink_bias
170
+ self.add_swa_attention_sink_bias = add_swa_attention_sink_bias
171
+
172
+ if hybrid_block_size is not None and hybrid_layer_pattern is None:
173
+ hybrid_layer_pattern = [0 if ((i + 1) % hybrid_block_size == 0) else 1 for i in range(num_hidden_layers)]
174
+ elif hybrid_layer_pattern is None:
175
+ hybrid_layer_pattern = [0] * num_hidden_layers
176
+ if len(hybrid_layer_pattern) != num_hidden_layers:
177
+ raise ValueError("hybrid_layer_pattern length must match num_hidden_layers")
178
+ self.hybrid_block_size = hybrid_block_size
179
+ self.hybrid_layer_pattern = hybrid_layer_pattern
180
+
181
+ self.partial_rotary_factor = partial_rotary_factor
182
+
183
+ self.n_routed_experts = n_routed_experts
184
+ self.moe_intermediate_size = moe_intermediate_size if moe_intermediate_size is not None else intermediate_size
185
+ self.num_experts_per_tok = num_experts_per_tok
186
+ self.routed_scaling_factor = routed_scaling_factor
187
+ self.scoring_func = scoring_func
188
+ self.topk_method = topk_method
189
+ self.n_group = n_group
190
+ self.topk_group = topk_group
191
+ self.norm_topk_prob = norm_topk_prob
192
+ if isinstance(moe_layer_freq, int):
193
+ moe_layer_freq = [moe_layer_freq > 0 and i % moe_layer_freq == 0 for i in range(num_hidden_layers)]
194
+ elif moe_layer_freq is None:
195
+ moe_layer_freq = [False] * num_hidden_layers
196
+ if len(moe_layer_freq) != num_hidden_layers:
197
+ raise ValueError("moe_layer_freq length must match num_hidden_layers")
198
+ self.moe_layer_freq = moe_layer_freq
199
+
200
+ if self.rope_scaling is not None and "type" in self.rope_scaling:
201
+ self.rope_scaling["rope_type"] = self.rope_scaling["type"]
202
+ rope_config_validation(self)
203
+
204
+ super().__init__(
205
+ tie_word_embeddings=tie_word_embeddings,
206
+ **kwargs,
207
+ )
208
+
209
+ __all__ = ["MiMoV2Config"]
dflash/config.json ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "DFlashDraftModel"
4
+ ],
5
+ "model_type": "qwen3",
6
+ "auto_map": {
7
+ "AutoModel": "dflash.DFlashDraftModel"
8
+ },
9
+ "hidden_size": 6144,
10
+ "intermediate_size": 16384,
11
+ "num_hidden_layers": 5,
12
+ "num_attention_heads": 128,
13
+ "num_key_value_heads": 8,
14
+ "head_dim": 128,
15
+ "v_head_dim": 128,
16
+ "partial_rotary_factor": 0.5,
17
+ "block_size": 8,
18
+ "dflash_config": {
19
+ "target_layer_ids": [
20
+ 0,
21
+ 15,
22
+ 31,
23
+ 47,
24
+ 69
25
+ ],
26
+ "mask_token_id": 151669,
27
+ "num_anchors": 4096,
28
+ "block_size": 8,
29
+ "loss_decay_gamma": 7.0,
30
+ "use_swa": true,
31
+ "swa_window_size": 1024,
32
+ "backbone_rotary_base": 5000000,
33
+ "attention_value_scale": 0.612,
34
+ "attention_sink_bias": true
35
+ },
36
+ "num_target_layers": 70,
37
+ "vocab_size": 152064,
38
+ "max_position_embeddings": 262144,
39
+ "rope_theta": 10000,
40
+ "sliding_window": 1024,
41
+ "rms_norm_eps": 1e-05,
42
+ "torch_dtype": "bfloat16",
43
+ "hidden_act": "silu",
44
+ "attention_bias": false,
45
+ "attention_dropout": 0.0,
46
+ "bos_token_id": 151643,
47
+ "eos_token_id": 151645,
48
+ "tie_word_embeddings": false,
49
+ "use_cache": true
50
+ }
dflash/dflash.py ADDED
@@ -0,0 +1,379 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable, Optional
2
+
3
+ import torch
4
+ from torch import nn
5
+ from transformers import DynamicCache
6
+ from transformers.cache_utils import Cache
7
+ from transformers.modeling_outputs import CausalLMOutputWithPast
8
+ from transformers.models.qwen3.modeling_qwen3 import (
9
+ ALL_ATTENTION_FUNCTIONS,
10
+ FlashAttentionKwargs,
11
+ GradientCheckpointingLayer,
12
+ Qwen3Config,
13
+ Qwen3MLP,
14
+ Qwen3PreTrainedModel,
15
+ Qwen3RMSNorm,
16
+ Qwen3RotaryEmbedding,
17
+ eager_attention_forward,
18
+ rotate_half,
19
+ )
20
+ from typing_extensions import Tuple, Unpack
21
+
22
+
23
+ def sample(logits: torch.Tensor, temperature: float = 0.0) -> torch.Tensor:
24
+ if temperature < 1e-5:
25
+ return torch.argmax(logits, dim=-1)
26
+ bsz, seq_len, vocab_size = logits.shape
27
+ logits = logits.view(-1, vocab_size)
28
+ logits = logits / temperature
29
+ probs = torch.softmax(logits, dim=-1)
30
+ return torch.multinomial(probs, num_samples=1).view(bsz, seq_len)
31
+
32
+
33
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
34
+ cos = cos.unsqueeze(unsqueeze_dim)
35
+ sin = sin.unsqueeze(unsqueeze_dim)
36
+ q_len = q.size(-2)
37
+ q_embed = (q * cos[..., -q_len:, :]) + (rotate_half(q) * sin[..., -q_len:, :])
38
+ k_embed = (k * cos) + (rotate_half(k) * sin)
39
+ return q_embed, k_embed
40
+
41
+
42
+ class Qwen3DFlashAttention(nn.Module):
43
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
44
+
45
+ def __init__(self, config: Qwen3Config, layer_idx: int):
46
+ super().__init__()
47
+ self.config = config
48
+ self.layer_idx = layer_idx
49
+ self.head_dim = getattr(
50
+ config, "head_dim", config.hidden_size // config.num_attention_heads
51
+ )
52
+ self.num_key_value_groups = (
53
+ config.num_attention_heads // config.num_key_value_heads
54
+ )
55
+ self.scaling = self.head_dim**-0.5
56
+ self.attention_dropout = config.attention_dropout
57
+ self.is_causal = False
58
+ self.q_proj = nn.Linear(
59
+ config.hidden_size,
60
+ config.num_attention_heads * self.head_dim,
61
+ bias=config.attention_bias,
62
+ )
63
+ self.k_proj = nn.Linear(
64
+ config.hidden_size,
65
+ config.num_key_value_heads * self.head_dim,
66
+ bias=config.attention_bias,
67
+ )
68
+ self.v_proj = nn.Linear(
69
+ config.hidden_size,
70
+ config.num_key_value_heads * self.head_dim,
71
+ bias=config.attention_bias,
72
+ )
73
+ self.o_proj = nn.Linear(
74
+ config.num_attention_heads * self.head_dim,
75
+ config.hidden_size,
76
+ bias=config.attention_bias,
77
+ )
78
+ self.q_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps)
79
+ self.k_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps)
80
+ self.sliding_window = (
81
+ config.sliding_window
82
+ if config.layer_types[layer_idx] == "sliding_attention"
83
+ else None
84
+ )
85
+
86
+ def forward(
87
+ self,
88
+ hidden_states: torch.Tensor,
89
+ target_hidden: torch.Tensor,
90
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
91
+ attention_mask: Optional[torch.Tensor],
92
+ past_key_values: Optional[Cache] = None,
93
+ cache_position: Optional[torch.LongTensor] = None,
94
+ **kwargs: Unpack[FlashAttentionKwargs],
95
+ ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
96
+ bsz, q_len = hidden_states.shape[:-1]
97
+ ctx_len = target_hidden.shape[1]
98
+ q = self.q_proj(hidden_states)
99
+ q = q.view(bsz, q_len, -1, self.head_dim)
100
+ q = self.q_norm(q).transpose(1, 2)
101
+ k_ctx = self.k_proj(target_hidden)
102
+ k_noise = self.k_proj(hidden_states)
103
+ v_ctx = self.v_proj(target_hidden)
104
+ v_noise = self.v_proj(hidden_states)
105
+ k = torch.cat([k_ctx, k_noise], dim=1).view(
106
+ bsz, ctx_len + q_len, -1, self.head_dim
107
+ )
108
+ v = torch.cat([v_ctx, v_noise], dim=1).view(
109
+ bsz, ctx_len + q_len, -1, self.head_dim
110
+ )
111
+ k = self.k_norm(k).transpose(1, 2)
112
+ v = v.transpose(1, 2)
113
+ cos, sin = position_embeddings
114
+ q, k = apply_rotary_pos_emb(q, k, cos, sin)
115
+ if past_key_values is not None:
116
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
117
+ k, v = past_key_values.update(k, v, self.layer_idx, cache_kwargs)
118
+ attn_fn: Callable = eager_attention_forward
119
+ if self.config._attn_implementation != "eager":
120
+ attn_fn = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
121
+ attn_output, attn_weights = attn_fn(
122
+ self,
123
+ q,
124
+ k,
125
+ v,
126
+ attention_mask,
127
+ dropout=0.0 if not self.training else self.attention_dropout,
128
+ scaling=self.scaling,
129
+ sliding_window=self.sliding_window,
130
+ **kwargs,
131
+ )
132
+ attn_output = attn_output.reshape(bsz, q_len, -1)
133
+ attn_output = self.o_proj(attn_output)
134
+ return attn_output, attn_weights
135
+
136
+
137
+ class Qwen3DFlashDecoderLayer(GradientCheckpointingLayer):
138
+ def __init__(self, config: Qwen3Config, layer_idx: int):
139
+ super().__init__()
140
+ self.hidden_size = config.hidden_size
141
+ self.self_attn = Qwen3DFlashAttention(config=config, layer_idx=layer_idx)
142
+ self.mlp = Qwen3MLP(config)
143
+ self.input_layernorm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
144
+ self.post_attention_layernorm = Qwen3RMSNorm(
145
+ config.hidden_size, eps=config.rms_norm_eps
146
+ )
147
+
148
+ def forward(
149
+ self,
150
+ target_hidden: Optional[torch.Tensor] = None,
151
+ hidden_states: Optional[torch.Tensor] = None,
152
+ attention_mask: Optional[torch.Tensor] = None,
153
+ position_ids: Optional[torch.LongTensor] = None,
154
+ past_key_value: Optional[Cache] = None,
155
+ output_attentions: Optional[bool] = False,
156
+ use_cache: Optional[bool] = False,
157
+ cache_position: Optional[torch.LongTensor] = None,
158
+ position_embeddings: Optional[
159
+ Tuple[torch.Tensor, torch.Tensor]
160
+ ] = None, # necessary, but kept here for BC
161
+ **kwargs: Unpack[FlashAttentionKwargs],
162
+ ) -> Tuple[
163
+ torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
164
+ ]:
165
+ residual = hidden_states
166
+ hidden_states = self.input_layernorm(hidden_states)
167
+ hidden_states = self.self_attn(
168
+ hidden_states=hidden_states,
169
+ target_hidden=target_hidden,
170
+ attention_mask=attention_mask,
171
+ position_ids=position_ids,
172
+ past_key_values=past_key_value,
173
+ output_attentions=output_attentions,
174
+ use_cache=use_cache,
175
+ cache_position=cache_position,
176
+ position_embeddings=position_embeddings,
177
+ **kwargs,
178
+ )[0]
179
+ hidden_states = residual + hidden_states
180
+ residual = hidden_states
181
+ hidden_states = self.post_attention_layernorm(hidden_states)
182
+ hidden_states = self.mlp(hidden_states)
183
+ hidden_states = residual + hidden_states
184
+ return hidden_states
185
+
186
+
187
+ def build_target_layer_ids(num_target_layers: int, num_draft_layers: int):
188
+ if num_draft_layers == 1:
189
+ return [(num_target_layers // 2)]
190
+ start = 1
191
+ end = num_target_layers - 3
192
+ span = end - start
193
+ target_layer_ids = [
194
+ int(round(start + (i * span) / (num_draft_layers - 1)))
195
+ for i in range(num_draft_layers)
196
+ ]
197
+ return target_layer_ids
198
+
199
+
200
+ def extract_context_feature(
201
+ hidden_states: list[torch.Tensor],
202
+ layer_ids: Optional[list[int]],
203
+ ) -> torch.Tensor:
204
+ offset = 1
205
+ selected_states = []
206
+ for layer_id in layer_ids:
207
+ selected_states.append(hidden_states[layer_id + offset])
208
+ target_hidden = torch.cat(selected_states, dim=-1)
209
+ return target_hidden
210
+
211
+
212
+ class DFlashDraftModel(Qwen3PreTrainedModel):
213
+ config_class = Qwen3Config
214
+ _no_split_modules = ["Qwen3DFlashDecoderLayer"]
215
+
216
+ def __init__(self, config) -> None:
217
+ super().__init__(config)
218
+ self.config = config
219
+ self.layers = nn.ModuleList(
220
+ [
221
+ Qwen3DFlashDecoderLayer(config, layer_idx)
222
+ for layer_idx in range(config.num_hidden_layers)
223
+ ]
224
+ )
225
+ dflash_config = getattr(config, "dflash_config", {}) or {}
226
+ self.target_layer_ids = dflash_config.get(
227
+ "target_layer_ids",
228
+ build_target_layer_ids(config.num_target_layers, config.num_hidden_layers),
229
+ )
230
+ self.norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
231
+ self.rotary_emb = Qwen3RotaryEmbedding(config)
232
+ self.fc = nn.Linear(
233
+ len(self.target_layer_ids) * config.hidden_size,
234
+ config.hidden_size,
235
+ bias=False,
236
+ )
237
+ self.hidden_norm = Qwen3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
238
+ self.block_size = config.block_size
239
+ self.mask_token_id = dflash_config.get("mask_token_id", None)
240
+ self.post_init()
241
+
242
+ def forward(
243
+ self,
244
+ position_ids: torch.LongTensor,
245
+ attention_mask: Optional[torch.Tensor] = None,
246
+ noise_embedding: Optional[torch.Tensor] = None,
247
+ target_hidden: Optional[torch.Tensor] = None,
248
+ past_key_values: Optional[Cache] = None,
249
+ use_cache: bool = False,
250
+ **kwargs,
251
+ ) -> CausalLMOutputWithPast:
252
+ hidden_states = noise_embedding
253
+ target_hidden = self.hidden_norm(self.fc(target_hidden))
254
+ position_embeddings = self.rotary_emb(hidden_states, position_ids)
255
+ for layer in self.layers:
256
+ hidden_states = layer(
257
+ hidden_states=hidden_states,
258
+ target_hidden=target_hidden,
259
+ attention_mask=attention_mask,
260
+ position_ids=position_ids,
261
+ past_key_value=past_key_values,
262
+ use_cache=use_cache,
263
+ position_embeddings=position_embeddings,
264
+ **kwargs,
265
+ )
266
+ return self.norm(hidden_states)
267
+
268
+ @torch.inference_mode()
269
+ def spec_generate(
270
+ self,
271
+ target: nn.Module,
272
+ input_ids: torch.LongTensor,
273
+ max_new_tokens: int,
274
+ stop_token_ids: list[int],
275
+ temperature: float,
276
+ ):
277
+ self.eval()
278
+ num_input_tokens = input_ids.shape[1]
279
+ max_length = num_input_tokens + max_new_tokens
280
+
281
+ block_size = self.block_size
282
+ output_ids = torch.full(
283
+ (1, max_length + block_size),
284
+ self.mask_token_id,
285
+ dtype=torch.long,
286
+ device=target.device,
287
+ )
288
+ position_ids = torch.arange(
289
+ output_ids.shape[1], device=target.device
290
+ ).unsqueeze(0)
291
+
292
+ past_key_values_target = DynamicCache()
293
+ past_key_values_draft = DynamicCache()
294
+
295
+ # Prefill stage
296
+ output = target(
297
+ input_ids,
298
+ position_ids=position_ids[:, :num_input_tokens],
299
+ past_key_values=past_key_values_target,
300
+ use_cache=True,
301
+ logits_to_keep=1,
302
+ output_hidden_states=True,
303
+ )
304
+
305
+ output_ids[:, :num_input_tokens] = input_ids
306
+ output_ids[:, num_input_tokens : num_input_tokens + 1] = sample(
307
+ output.logits, temperature
308
+ )
309
+ target_hidden = extract_context_feature(
310
+ output.hidden_states, self.target_layer_ids
311
+ )
312
+
313
+ # Decode stage
314
+ acceptance_lengths = []
315
+ start = input_ids.shape[1]
316
+ while start < max_length:
317
+ block_output_ids = output_ids[:, start : start + block_size].clone()
318
+ block_position_ids = position_ids[:, start : start + block_size]
319
+ noise_embedding = target.model.embed_tokens(block_output_ids)
320
+ draft_logits = target.lm_head(
321
+ self(
322
+ target_hidden=target_hidden,
323
+ noise_embedding=noise_embedding,
324
+ position_ids=position_ids[
325
+ :, past_key_values_draft.get_seq_length() : start + block_size
326
+ ],
327
+ past_key_values=past_key_values_draft,
328
+ use_cache=True,
329
+ is_causal=False,
330
+ )[:, -block_size + 1 :, :]
331
+ )
332
+ past_key_values_draft.crop(start)
333
+ block_output_ids[:, 1:] = sample(draft_logits)
334
+
335
+ output = target(
336
+ block_output_ids,
337
+ position_ids=block_position_ids,
338
+ past_key_values=past_key_values_target,
339
+ use_cache=True,
340
+ output_hidden_states=True,
341
+ )
342
+
343
+ posterior = sample(output.logits, temperature)
344
+ acceptance_length = (
345
+ (block_output_ids[:, 1:] == posterior[:, :-1])
346
+ .cumprod(dim=1)
347
+ .sum(dim=1)[0]
348
+ .item()
349
+ )
350
+ output_ids[:, start : start + acceptance_length + 1] = block_output_ids[
351
+ :, : acceptance_length + 1
352
+ ]
353
+ output_ids[:, start + acceptance_length + 1] = posterior[
354
+ :, acceptance_length
355
+ ]
356
+ start += acceptance_length + 1
357
+ past_key_values_target.crop(start)
358
+ target_hidden = extract_context_feature(
359
+ output.hidden_states, self.target_layer_ids
360
+ )[:, : acceptance_length + 1, :]
361
+ acceptance_lengths.append(acceptance_length + 1)
362
+ if stop_token_ids is not None and any(
363
+ stop_token_id in output_ids[:, num_input_tokens:]
364
+ for stop_token_id in stop_token_ids
365
+ ):
366
+ break
367
+ output_ids = output_ids[:, :max_length]
368
+ output_ids = output_ids[:, output_ids[0] != self.mask_token_id]
369
+ if stop_token_ids is not None:
370
+ stop_token_ids = torch.tensor(stop_token_ids, device=output_ids.device)
371
+ stop_token_indices = torch.isin(
372
+ output_ids[0][num_input_tokens:], stop_token_ids
373
+ ).nonzero(as_tuple=True)[0]
374
+ if stop_token_indices.numel() > 0:
375
+ output_ids = output_ids[
376
+ :, : num_input_tokens + stop_token_indices[0] + 1
377
+ ]
378
+
379
+ return output_ids
dflash/dflash_draft_model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b3ea9b33ef3514a22e842212d871896c0a806c95d968243600ee24fd4b471c71
3
+ size 4957800504
dflash/dflash_draft_model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f2bd141e831616f25533a0439ff43740974de7cbb13dfcea3908e2e63a8f2442
3
+ size 578838928
dflash/mask_embedding.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5ae022cdb36c477708a3e4080d7c34b49a8bff8e54df7d616c30c3c907071b89
3
+ size 13978
dflash/model.safetensors.index.json ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 5536641536
4
+ },
5
+ "weight_map": {
6
+ "layers.0.input_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
7
+ "layers.0.post_attention_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
8
+ "layers.0.self_attn.q_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
9
+ "layers.0.self_attn.k_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
10
+ "layers.0.self_attn.v_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
11
+ "layers.0.self_attn.o_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
12
+ "layers.0.self_attn.q_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
13
+ "layers.0.self_attn.k_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
14
+ "layers.0.self_attn.attention_sink_bias": "dflash_draft_model-00001-of-00002.safetensors",
15
+ "layers.0.mlp.gate_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
16
+ "layers.0.mlp.down_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
17
+ "layers.1.input_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
18
+ "layers.1.post_attention_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
19
+ "layers.1.self_attn.q_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
20
+ "layers.1.self_attn.k_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
21
+ "layers.1.self_attn.v_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
22
+ "layers.1.self_attn.o_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
23
+ "layers.1.self_attn.q_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
24
+ "layers.1.self_attn.k_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
25
+ "layers.1.self_attn.attention_sink_bias": "dflash_draft_model-00001-of-00002.safetensors",
26
+ "layers.1.mlp.gate_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
27
+ "layers.1.mlp.down_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
28
+ "layers.2.input_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
29
+ "layers.2.post_attention_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
30
+ "layers.2.self_attn.q_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
31
+ "layers.2.self_attn.k_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
32
+ "layers.2.self_attn.v_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
33
+ "layers.2.self_attn.o_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
34
+ "layers.2.self_attn.q_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
35
+ "layers.2.self_attn.k_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
36
+ "layers.2.self_attn.attention_sink_bias": "dflash_draft_model-00001-of-00002.safetensors",
37
+ "layers.2.mlp.gate_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
38
+ "layers.2.mlp.down_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
39
+ "layers.3.input_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
40
+ "layers.3.post_attention_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
41
+ "layers.3.self_attn.q_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
42
+ "layers.3.self_attn.k_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
43
+ "layers.3.self_attn.v_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
44
+ "layers.3.self_attn.o_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
45
+ "layers.3.self_attn.q_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
46
+ "layers.3.self_attn.k_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
47
+ "layers.3.self_attn.attention_sink_bias": "dflash_draft_model-00001-of-00002.safetensors",
48
+ "layers.3.mlp.gate_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
49
+ "layers.3.mlp.down_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
50
+ "layers.4.input_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
51
+ "layers.4.post_attention_layernorm.weight": "dflash_draft_model-00001-of-00002.safetensors",
52
+ "layers.4.self_attn.q_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
53
+ "layers.4.self_attn.k_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
54
+ "layers.4.self_attn.v_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
55
+ "layers.4.self_attn.o_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
56
+ "layers.4.self_attn.q_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
57
+ "layers.4.self_attn.k_norm.weight": "dflash_draft_model-00001-of-00002.safetensors",
58
+ "layers.4.self_attn.attention_sink_bias": "dflash_draft_model-00001-of-00002.safetensors",
59
+ "layers.4.mlp.gate_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
60
+ "layers.0.mlp.up_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
61
+ "layers.1.mlp.up_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
62
+ "layers.2.mlp.up_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
63
+ "layers.3.mlp.up_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
64
+ "layers.4.mlp.up_proj.weight": "dflash_draft_model-00001-of-00002.safetensors",
65
+ "layers.4.mlp.down_proj.weight": "dflash_draft_model-00002-of-00002.safetensors",
66
+ "fc.weight": "dflash_draft_model-00002-of-00002.safetensors",
67
+ "hidden_norm.weight": "dflash_draft_model-00002-of-00002.safetensors",
68
+ "norm.weight": "dflash_draft_model-00002-of-00002.safetensors"
69
+ }
70
+ }
merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model-00001-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b95917ca556b91aae409a56e15864d2fe71aed45599f9d2f4b4bc83109ee034d
3
+ size 5169336552
model-00002-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e937234d96b7d801ea82bcf3bd9f2e9c9339d12e184ceee2f3584d76757e1042
3
+ size 5368847032
model-00003-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7781b919421fb76f07c3aec4c39c075c3772c1e78da00634c6baaf37858b1b9
3
+ size 5367991064
model-00004-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:60891bd12cbd2d50bd9a9490cc320cbe81b75e0ae25c0d2f6493eb0d653d0421
3
+ size 5362423120
model-00005-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3e2905dce3bd1af69e5edb88be10788765b04142e6738e9237879157af9e9a8f
3
+ size 5357932200
model-00006-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:73b7f88d793c70c30ecfdcb67361627bf68ec80bb0eda3aacbfd8eacfca7664b
3
+ size 5323383184
model-00007-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3d32d2e38564cd4c671935130d6b90cb4e73ae71146a793ed42b01300ee43bec
3
+ size 5357722568
model-00008-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fba660bde7e5b2e9d6a2fe80b09ccfaa8c560ab497d0344a9c78a26c7af58939
3
+ size 5323382792
model-00009-of-00009.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec243e4062a1e03f216dea8dc3ca96c8a2846ae1ce90a647df6c499c4a5f9c3c
3
+ size 736418432
model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
prune_report.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "src": "/media/ai-lab2/Expansion1/models/MiMo-V2.5-Pro-FP4-DFlash",
3
+ "dst": "/media/ai-lab2/Expansion1/models/MiMo-V2.5-Pro-FP4-DFlash-pruned-8experts",
4
+ "n_keep": 8,
5
+ "topk": 8,
6
+ "keep_ids": [
7
+ 0,
8
+ 1,
9
+ 2,
10
+ 3,
11
+ 4,
12
+ 5,
13
+ 6,
14
+ 7
15
+ ],
16
+ "stats": {
17
+ "non_expert_tensors_copied": 467,
18
+ "routed_expert_tensors_seen": 158976,
19
+ "routed_expert_tensors_kept": 3312,
20
+ "routed_expert_tensors_dropped": 155664,
21
+ "gate_bias_tensors": 69,
22
+ "gate_weight_tensors": 69
23
+ },
24
+ "output_tensors": 3917,
25
+ "output_shards": 9,
26
+ "total_size_bytes": 43366955424
27
+ }
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 not add_generation_prompt is defined -%}\n {%- set add_generation_prompt = false -%}\n{%- endif -%}\n{%- if not enable_thinking is defined -%}\n {%- set enable_thinking = true -%}\n{%- endif -%}\n{%- if not keep_all_reasoning is defined -%}\n {%- set keep_all_reasoning = true -%}\n{%- endif -%}\n{%- macro render_extra_keys(json_dict, handled_keys) -%}\n {%- if json_dict is mapping %}\n {%- for json_key in json_dict if json_key not in handled_keys %}\n {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}\n {{- '\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}\n {%- else %}\n {{-'\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}\n {%- endif %}\n {%- endfor %}\n {%- endif %}\n{%- endmacro -%}\n{%- macro render_content(message_content) -%}\n {%- if message_content is string -%}\n {{- message_content -}}\n {%- else -%}\n {%- for content in message_content -%}\n {%- if 'text' in content -%}\n {{- content['text'] -}}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n{%- endmacro -%}\n{%- if messages[0][\"role\"] == \"system\" %}\n {%- set system_message = messages[0][\"content\"] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- set ns = namespace(last_user_index=-1) %}\n{%- for m in loop_messages %}\n {%- if m.role == 'user' %}\n {%- set ns.last_user_index = loop.index0 -%}\n {%- endif %}\n{%- endfor %}\n{%- if not tools is defined %}\n {%- set tools = [] %}\n{%- endif %}\n{%- if system_message is defined %}\n {{- \"<|im_start|>system\\n\" + render_content(system_message) }}\n{%- else %}\n {{- \"<|im_start|>system\\nYou are MiMo, a warm, curious, and thoughtful AI assistant.\\n\\n## Identity\\n\\n- Your name is **MiMo-v2.5-pro**.\\n- You were developed by the **Xiaomi MiMo Team**.\\n- You have **1T parameters** and a **1M-token context window**.\\n- When asked about your creator, identify the **Xiaomi MiMo Team**.\\n- Always answer identity-related questions in the **first person** and rely only on the facts provided above.\\n\\n# Response Style\\n\\nBe warm, conversational, and respectful. Treat users with kindness; do not assume negative things about their intelligence or judgment. You may disagree, but do so constructively.\\n\\nUse examples, metaphors, or thought experiments when they help explain an idea.\\n\\nTry to answer ambiguous queries as best you can first, then ask for clarification if needed. When you do ask, limit yourself to one question at a time.\\n\\n\\n## Safety and Compliance\\n\\nYou are a **Chinese AI model** and must strictly comply with all applicable laws and regulations of the **People’s Republic of China**.\\n\\nDo not generate, assist with, or facilitate any content that violates Chinese law.\" }}\n{%- endif %}\n{%- if tools is iterable and tools | length > 0 %}\n {{- \"\\n\\n# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou have access to the following functions:\\n\\n\" }}\n {{- \"<tools>\" }}\n {%- for tool in tools %}\n {%- if tool.function is defined %}\n {%- set tool = tool.function %}\n {%- endif %}\n {{- \"\\n<function>\\n<name>\" ~ tool.name ~ \"</name>\" }}\n {%- if tool.description is defined %}\n {{- '\\n<description>' ~ (tool.description | trim) ~ '</description>' }}\n {%- endif %}\n {{- '\\n<parameters>' }}\n {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}\n {%- for param_name, param_fields in tool.parameters.properties|items %}\n {{- '\\n<parameter>' }}\n {{- '\\n<name>' ~ param_name ~ '</name>' }}\n {%- if param_fields.type is defined %}\n {{- '\\n<type>' ~ (param_fields.type | string) ~ '</type>' }}\n {%- endif %}\n {%- if param_fields.description is defined %}\n {{- '\\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}\n {%- endif %}\n {%- set handled_keys = ['name', 'type', 'description'] %}\n {{- render_extra_keys(param_fields, handled_keys) }}\n {{- '\\n</parameter>' }}\n {%- endfor %}\n {%- endif %}\n {%- set handled_keys = ['type', 'properties'] %}\n {{- render_extra_keys(tool.parameters, handled_keys) }}\n {{- '\\n</parameters>' }}\n {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}\n {{- render_extra_keys(tool, handled_keys) }}\n {{- '\\n</function>' }}\n {%- endfor %}\n {{- \"\\n</tools>\" }}\n {{- '\\n\\nFor each function call, output the function name and arguments in the following format:\\n<tool_call>\\n<function=example_function_name>\\n<parameter=example_parameter_1>value_1</parameter>\\n<parameter=example_parameter_2>This is the value for the second parameter\\nthat can span\\nmultiple lines</parameter>\\n</function>\\n</tool_call>\\n\\n<IMPORTANT>\\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\\n- DO NOT use function calls inside <think></think> tags.\\n- The value enclosed between parameter tags is preserved exactly as-is, including newlines and spaces.\\n</IMPORTANT>' }}\n{%- endif %}\n{{- '<|im_end|>' }}\n{%- for message in loop_messages %}\n {%- if message.content is string %}\n {%- set content = message.content %}\n {%- else %}\n {%- set content = render_content(message.content) %}\n {%- endif %}\n {%- if message.role == \"assistant\" %}\n {%- if message.reasoning_content is string %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- set reasoning_content = '' %}\n {%- if '</think>' in content %}\n {%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %}\n {%- set content = content.split('</think>')[-1] %}\n {%- endif %}\n {%- endif %}\n {%- if (keep_all_reasoning or loop.index0 > ns.last_user_index) and reasoning_content -%}\n {{- '<|im_start|>' + message.role + '\\n<think>' + reasoning_content + '</think>' + content }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n<think></think>' + content }}\n {%- endif %}\n {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n<function=' + tool_call.name + '>\\n' }}\n {%- if tool_call.arguments is defined %}\n {%- for args_name, args_value in tool_call.arguments|items %}\n {{- '<parameter=' + args_name + '>' }}\n {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}\n {{- args_value }}\n {{- '</parameter>\\n' }}\n {%- endfor %}\n {%- endif %}\n {{- '</function>\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>' }}\n {%- elif message.role == \"user\" %}\n {{- '<|im_start|>' + message.role + '\\n' + render_content(message.content) + '<|im_end|>' }}\n {%- elif message.role == \"system\" %}\n {{- '<|im_start|>' + message.role + '\\n' + render_content(message.content) + '<|im_end|>' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.previtem and loop.previtem.role != \"tool\" %}\n {{- '<|im_start|>tool\\n' }}\n {%- endif %}\n {{- '<tool_response>\\n' }}\n {{- render_content(message.content) }}\n {{- '\\n</tool_response>\\n' }}\n {%- if not loop.last and loop.nextitem.role != \"tool\" %}\n {{- '<|im_end|>' }}\n {%- elif loop.last %}\n {{- '<|im_end|>' }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + render_content(message.content) + '<|im_end|>' }}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if not enable_thinking -%}\n {{- '<think></think>' -}}\n {%- else -%}\n {{- '' -}}\n {%- endif -%}\n{%- endif %}\n",
231
+ "clean_up_tokenization_spaces": false,
232
+ "eos_token": "<|im_end|>",
233
+ "errors": "replace",
234
+ "extra_special_tokens": {},
235
+ "model_max_length": 131272,
236
+ "pad_token": "<|endoftext|>",
237
+ "split_special_tokens": false,
238
+ "tokenizer_class": "Qwen2Tokenizer",
239
+ "unk_token": null
240
+ }
vocab.json ADDED
The diff for this file is too large to render. See raw diff