smalinin commited on
Commit
cd7fceb
·
1 Parent(s): a8ff93b

Upload Q2_K/Q2_REPAIR_REPORT.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. Q2_K/Q2_REPAIR_REPORT.md +341 -0
Q2_K/Q2_REPAIR_REPORT.md ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DeepSeek V4.1 Flash Q2_K Repair Report
2
+
3
+ ## Summary
4
+
5
+ The original seven-shard Q2_K model required two distinct classes of repair:
6
+
7
+ 1. Its Engram metadata was incompatible with the `deepseek41` runtime and was incomplete.
8
+ 2. Eighty-four architecture-critical tensors had been quantized to Q2_K even though their roles require substantially higher precision.
9
+
10
+ The final quality artifact contains:
11
+
12
+ - Correct and complete `deepseek41.engram.*` metadata.
13
+ - Four official BF16 Engram gate tensors.
14
+ - Eighty official F32 multi-stream Hyper-Connection (mHC) tensors.
15
+ - The original Q2/Q3 backbone and MoE weights everywhere else.
16
+
17
+ The model was repaired selectively. It was not converted or requantized as a whole.
18
+
19
+ ## 1. Engram metadata repair
20
+
21
+ The original first shard contained only four Engram metadata values, and they used the wrong architecture namespace:
22
+
23
+ ```text
24
+ deepseek4.engram.head_count
25
+ deepseek4.engram.key_length
26
+ deepseek4.engram.layer_ids
27
+ deepseek4.engram.max_ngram_size
28
+ ```
29
+
30
+ The runtime resolves architecture-dependent GGUF keys as `{arch}.{key}`. Because the model declares `general.architecture = deepseek41`, the loader looks for `deepseek41.engram.*`, not `deepseek4.engram.*`.
31
+
32
+ The four existing keys were renamed to:
33
+
34
+ ```text
35
+ deepseek41.engram.head_count
36
+ deepseek41.engram.key_length
37
+ deepseek41.engram.layer_ids
38
+ deepseek41.engram.max_ngram_size
39
+ ```
40
+
41
+ Five required values were also missing entirely and were added:
42
+
43
+ ```text
44
+ deepseek41.engram.multipliers
45
+ deepseek41.engram.primes
46
+ deepseek41.engram.offsets
47
+ deepseek41.engram.token_map
48
+ deepseek41.engram.pad_id
49
+ ```
50
+
51
+ The metadata count in shard 1 changed from 63 to 68: four old keys were replaced by four correctly namespaced keys, and five new values were added.
52
+
53
+ ### Why this was necessary
54
+
55
+ Without this repair, the current loader cannot initialize the Engram subsystem. The namespace mismatch prevents it from finding even the four values present in the file, while the missing arrays prevent reproduction of the checkpoint's token compression and hash calculation.
56
+
57
+ The restored data covers:
58
+
59
+ - A 129,280-entry INT32 token map.
60
+ - 8 UINT64 hash multipliers.
61
+ - 48 UINT64 primes.
62
+ - 48 UINT64 offsets.
63
+ - Compressed vocabulary size 99,092.
64
+ - Compressed padding ID 2.
65
+
66
+ The constants were regenerated using the converter algorithm with the official configuration and tokenizer from checkpoint revision:
67
+
68
+ ```text
69
+ dba1be0a40aa45a94ad051997016db3960a90277
70
+ ```
71
+
72
+ They matched the independent JigSawPT GGUF representation exactly.
73
+
74
+ ## 2. Engram gate tensor restoration
75
+
76
+ The original Q2 model stored these tensors in Q2_K:
77
+
78
+ ```text
79
+ blk.1.engram_q.weight
80
+ blk.1.engram_k.weight
81
+ blk.14.engram_q.weight
82
+ blk.14.engram_k.weight
83
+ ```
84
+
85
+ Each tensor has GGUF shape:
86
+
87
+ ```text
88
+ [5120, 4]
89
+ ```
90
+
91
+ They were replaced with the original BF16 checkpoint payloads:
92
+
93
+ ```text
94
+ engram_q/k: Q2_K -> BF16
95
+ ```
96
+
97
+ ### Why this was necessary
98
+
99
+ These small tensors control the Engram gate. They behave as sensitive scale-like parameters rather than ordinary large backbone matrices. Q2_K heavily distorts their relative magnitudes and therefore changes how Engram table output is injected into the model state.
100
+
101
+ The practical effect was visible in full-model testing. The source Q2 with quantized gate tensors produced a weak, factually incorrect continuation. Replacing only these four tensors with their official BF16 values changed the deterministic trajectory and produced the expected `Au` token for a gold-symbol prompt.
102
+
103
+ The four payloads were extracted with HTTP range requests from the official safetensors shards. Each payload was independently compared with the corresponding JigSawPT tensor and matched byte-for-byte.
104
+
105
+ ## 3. mHC tensor restoration
106
+
107
+ DeepSeek V4.1 Flash has two multi-stream Hyper-Connection parameter matrices in every one of its 40 layers:
108
+
109
+ ```text
110
+ blk.N.hc_attn_fn.weight
111
+ blk.N.hc_ffn_fn.weight
112
+ ```
113
+
114
+ This gives:
115
+
116
+ ```text
117
+ 40 layers x 2 tensors = 80 tensors
118
+ ```
119
+
120
+ Each tensor has GGUF shape:
121
+
122
+ ```text
123
+ [20480, 24]
124
+ ```
125
+
126
+ The community Q2 model stored all 80 tensors in Q2_K. They were replaced with official F32 payloads:
127
+
128
+ ```text
129
+ hc_attn_fn: Q2_K -> F32
130
+ hc_ffn_fn: Q2_K -> F32
131
+ ```
132
+
133
+ ### Why this was necessary
134
+
135
+ The mHC matrices generate coefficients that mix the model's four residual streams around every attention and feed-forward sublayer. Their values are small and numerically sensitive. An error in these parameters changes residual routing twice per layer and can accumulate through all 40 layers.
136
+
137
+ The official reference, vLLM, and SGLang implementations keep these parameters in FP32. Operation-level validation also demonstrated a substantial precision difference:
138
+
139
+ ```text
140
+ BF16 projection error: up to 1.16e-2
141
+ F32 projection error: up to 1.43e-6
142
+ ```
143
+
144
+ For this reason, restoring the mHC tensors only to BF16 was not sufficient for close agreement with the reference. All 80 were restored to F32.
145
+
146
+ The payloads were extracted from the official checkpoint revision listed above. They were distributed across official safetensors shards 3 through 42. The complete extracted payload size was 157,286,400 bytes (150 MiB).
147
+
148
+ ## 4. What was not changed
149
+
150
+ The complete model was not requantized. The following data was preserved:
151
+
152
+ - MoE expert weights.
153
+ - Attention and feed-forward backbone weights.
154
+ - Token embeddings.
155
+ - Indexer weights.
156
+ - Compressed-attention weights.
157
+ - Both large Engram tables.
158
+ - Every Q2_K, Q3_K, Q6_K, BF16, and F32 tensor not explicitly listed for replacement.
159
+ - Tokenizer data and chat template.
160
+ - Existing non-Engram metadata.
161
+
162
+ Only 84 tensors were replaced:
163
+
164
+ ```text
165
+ 80 mHC tensors + 4 Engram q/k tensors = 84 tensors
166
+ ```
167
+
168
+ This targeted approach preserved the original community quantization for the large backbone while restoring reference precision only where ordinary low-bit quantization was unsuitable.
169
+
170
+ ## 5. Tensor type changes
171
+
172
+ The complete seven-shard model contains 1,046 unique tensors. Tensor type counts changed as follows:
173
+
174
+ | Tensor type | Original Q2 | Repaired Q2 |
175
+ |---|---:|---:|
176
+ | Q2_K | 476 | 392 |
177
+ | F32 | 449 | 529 |
178
+ | BF16 | 40 | 44 |
179
+ | Q3_K | 80 | 80 |
180
+ | Q6_K | 1 | 1 |
181
+
182
+ The repair occurred in two tensor-replacement steps:
183
+
184
+ 1. Restoring the four Engram gate tensors changed Q2_K from 476 to 472 and BF16 from 40 to 44.
185
+ 2. Restoring the 80 mHC tensors changed Q2_K from 472 to 392 and F32 from 449 to 529.
186
+
187
+ The complete size changed as follows:
188
+
189
+ ```text
190
+ Original Q2: 264,514,761,248 bytes
191
+ Repaired Q2: 264,659,800,416 bytes
192
+ Growth: 145,039,168 bytes (approximately 138.3 MiB)
193
+ ```
194
+
195
+ The total tensor count and every tensor shape remained unchanged.
196
+
197
+ ## 6. Rewritten shards
198
+
199
+ Critical tensors were present in all seven Q2 shards:
200
+
201
+ ```text
202
+ 1, 2, 3, 4, 5, 6, and 7
203
+ ```
204
+
205
+ Their distribution was:
206
+
207
+ | Shard | mHC tensors | Engram q/k tensors | Total replaced |
208
+ |---:|---:|---:|---:|
209
+ | 1 | 4 | 2 | 6 |
210
+ | 2 | 18 | 0 | 18 |
211
+ | 3 | 6 | 0 | 6 |
212
+ | 4 | 4 | 2 | 6 |
213
+ | 5 | 18 | 0 | 18 |
214
+ | 6 | 18 | 0 | 18 |
215
+ | 7 | 12 | 0 | 12 |
216
+ | **Total** | **80** | **4** | **84** |
217
+
218
+ Shard 1 also required the complete Engram metadata repair because it contains the model-level GGUF metadata.
219
+
220
+ During the first quality-repair step, only shards 1 and 4 required tensor rewriting because they contain `engram_q/k`; shard 1 was also rewritten for metadata. Restoring all 80 mHC tensors in the final step required rewriting every shard in the seven-file split set.
221
+
222
+ Because the replacement payloads are larger than their Q2_K versions, each affected rewrite recalculated:
223
+
224
+ - GGUF tensor type entries.
225
+ - Tensor payload sizes.
226
+ - Tensor offsets.
227
+ - 32-byte alignment padding.
228
+ - The data-section offset.
229
+ - The final shard size.
230
+
231
+ ## 7. Integrity and post-write validation
232
+
233
+ The source files were never modified in place. Both repair stages used streaming rewrites into new files and checked:
234
+
235
+ - Full source-file SHA-256 hashes.
236
+ - Official replacement-payload SHA-256 hashes.
237
+ - Preservation of the tensor count.
238
+ - Preservation of all tensor shapes.
239
+ - Correct output types for replacement tensors.
240
+ - Unchanged types and payloads for all other tensors.
241
+ - Correct offsets, sizes, and GGUF alignment.
242
+ - Complete and correctly typed Engram metadata.
243
+ - Absence of the obsolete `deepseek4.engram.*` namespace.
244
+ - Successful parsing of the complete seven-shard output set.
245
+ - Exactly 1,046 unique tensor names with no duplicates.
246
+
247
+ The final manifest confirms:
248
+
249
+ ```text
250
+ general.architecture = deepseek41
251
+ GGUF version = 3
252
+ split.count = 7
253
+ tensor count = 1046
254
+ Engram q/k = 4 x BF16
255
+ mHC tensors = 80 x F32
256
+ ```
257
+
258
+ Relevant artifacts:
259
+
260
+ - [`stage1/quality-gguf-manifest.json`](stage1/quality-gguf-manifest.json): metadata and BF16 Engram repair.
261
+ - [`stage14/quality-f32-mhc-gguf-manifest.json`](stage14/quality-f32-mhc-gguf-manifest.json): final complete model manifest.
262
+ - [`stage14/quality-f32-mhc-checksums.sha256`](stage14/quality-f32-mhc-checksums.sha256): final shard hashes.
263
+ - [`tools/rewrite_q2_mhc_shard.py`](tools/rewrite_q2_mhc_shard.py): streaming F32 mHC rewrite tool.
264
+
265
+ ## 8. Runtime validation
266
+
267
+ The repaired seven-shard model successfully loaded and executed across six CUDA GPUs. The broader bring-up validation completed 27 out of 27 checks, including:
268
+
269
+ - Discovery and loading of all seven GGUF shards.
270
+ - Loading all 1,046 tensors.
271
+ - Offloading all 41 model layers.
272
+ - Lazy loading of both large Engram tables.
273
+ - Prompt execution at several supported context sizes.
274
+ - Deterministic repeated generation.
275
+ - Server health, completion, and metrics endpoints.
276
+ - Clean shutdown and GPU-memory release.
277
+ - No NaN/Inf logits, allocation loop, or first-token hang.
278
+
279
+ See [`STAGE_9_REPORT.md`](STAGE_9_REPORT.md) for the complete bring-up validation.
280
+
281
+ ## 9. Paired quality validation
282
+
283
+ A single greedy continuation is highly sensitive to small numeric changes and was not used as the final quality gate for the F32 mHC repair. Instead, the Engram-repaired model with Q2_K mHC and the final model with F32 mHC were compared on the same 32 Wikitext-2 chunks.
284
+
285
+ Test parameters:
286
+
287
+ ```text
288
+ Context: 512
289
+ Chunks: 32
290
+ Scored tokens/chunk: 255
291
+ Total scored tokens: 8160
292
+ Batch: 512
293
+ Micro-batch: 128
294
+ ```
295
+
296
+ Results:
297
+
298
+ | Model set | mHC precision | Perplexity | Uncertainty |
299
+ |---|---|---:|---:|
300
+ | Previous Engram quality overlay | Q2_K | 6.3085 | 0.17503 |
301
+ | Final quality artifact | F32 | **6.2046** | 0.17046 |
302
+
303
+ The F32-mHC model:
304
+
305
+ - Improved perplexity by 1.647%.
306
+ - Was better on 25 of the 32 paired chunks.
307
+ - Achieved a mean paired NLL gain of 0.016607.
308
+ - Produced a paired t-statistic of 3.326.
309
+
310
+ This limited paired test supports keeping the official mHC tensors in F32. It is a selection and regression gate, not a complete published benchmark against the official FP8 implementation.
311
+
312
+ See [`STAGE_14_REPORT.md`](STAGE_14_REPORT.md) and [`stage14/ppl-pair-c512-chunk32.json`](stage14/ppl-pair-c512-chunk32.json) for details.
313
+
314
+ ## 10. Final artifact
315
+
316
+ The recommended logical path is:
317
+
318
+ ```text
319
+ /home/sexxxx/_my_deepseek41/artifacts/quality-f32-mhc/DeepSeek-V4.1-Flash-Q2_K-00001-of-00007.gguf
320
+ ```
321
+
322
+ The physical files are stored at:
323
+
324
+ ```text
325
+ /media/sexxxx/WW2T/models/vcruz305/DeepSeek-V4.1-Flash-Q2_K-quality-f32-mhc/
326
+ ```
327
+
328
+ The logical artifact directory contains symbolic links to the seven physical shards.
329
+
330
+ ## Result
331
+
332
+ The final model keeps the low-bit community quantization for the large backbone and expert weights while using reference precision for architecture-critical parameters:
333
+
334
+ ```text
335
+ Backbone and experts: Q2_K/Q3_K
336
+ Engram q/k: BF16
337
+ mHC parameters: F32
338
+ Engram metadata: complete deepseek41 namespace
339
+ ```
340
+
341
+ This is the recommended Q2 development and validation artifact. It corrects both the loader-blocking metadata defects and the quality-critical tensor quantization defects without requantizing the complete model.