You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit aa93ab6
Browse filesBrowse the repository at this point in the historyBrowse files
Fail closed on lossy GGUF quantization preservation (#609)
## Summary
Fixes the preserved Q4_K_M/MatMulNBits correctness divergence exposed by
#607.
Q4_K_M is a model-level mixed preset, not a GGML tensor type. The pinned
SmolLM artifact contains Q5_0=166, Q4_K=16, Q8_0=15, Q6_K=14, and F32=61
tensors. Mobius correctly decoded Q4_K's 144-byte/256-value layout,
six-bit scale/min fields, nibble order, orientation, and signed minimum
subtraction, and ORT correctly consumed the emitted affine packing. The
divergence came earlier: the importer normalized the mixed Q5/Q4_K/Q6/Q8
source tensors into one four-bit MatMulNBits contract, adding a second
lossy quantization.
This change:
- records which affine repacks preserve dequantized values and rejects
lossy or incompatible routes under `keep_quantized=True`;
- fails closed for unsupported projections, mixed float projections,
embeddings/heads, multimodal builders, MTP sidecars/native blocks, and
packed fused expert gate/up tensors;
- retains explicit `keep_quantized=False` / `--dequantize` as the opt-in
float route;
- adds independent asymmetric Q4_K and ORT MatMulNBits value tests plus
real SmolLM2 Q4_K_M prefill/decode/generation coverage.
## Root-cause evidence
The first divergence is already present in layer 0 before ORT execution:
| Tensor role | Source qtype | Relative L2 error after normalization |
|---|---:|---:|
| Q projection | Q5_0 | 0.09408 |
| K projection | Q5_0 | 0.09410 |
| V projection | Q8_0 -> Q4 | 0.08793 |
| FFN down | Q6_K -> Q4 | 0.08310 |
The Q4_K formula is `value = d * sub_scale * q - dmin * sub_min`.
Although Q4_K itself can be represented by four-bit MatMulNBits with
floating zero points, the full Q4_K_M mixture cannot be represented
losslessly by one four-bit graph contract. ORT 1.29 also advertises
8-bit MatMulNBits in schema but the tested CPU unpacked path rejects it
with `Only 2b and 4b quantization is supported for unpacked compute for
now`.
## Validation
- Final focused GGUF/quantization/MatMulNBits suite: **745 passed**
- Real pinned SmolLM2 Q4_K_M test: **1 passed**
- default API and CLI preservation reject before producing a model;
- explicit dequantization builds, saves, reloads, and runs;
- full prefill and ten cached-decode logits match the same-artifact
Transformers GGUF reference;
- maximum prefill absolute difference: **9.0599e-05**;
- ten greedy tokens match exactly: `198, 198, 18, 504, 2388, 13685, 284,
5208, 28, 198`.
- Earlier broad run: **7058 passed, 52 skipped**; the two remaining
`glm_moe_dsa` shape/checker failures reproduce on the base and are
unrelated.
- Changed-file Ruff check and format check pass.
- Two independent final review passes completed; all findings were
addressed and the final MoE fix was re-reviewed with no findings.
## Verdict
This is an importer preservation-policy bug, not an ORT Q4 packing bug.
Preservation now succeeds only for value-equivalent routes and otherwise
fails closed rather than emitting wrong logits.
---------
Signed-off-by: Justin Chu <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Copy file name to clipboardExpand all lines: docs/api/build_from_gguf.md
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,7 @@ def build_from_gguf(
181
181
|`gguf_path`|`str \| Path`| (required) | Local `.gguf` path or `owner/repo:filename.gguf` Hub reference. |
182
182
|`task`|`str \| None`|`None`| Override the model task (e.g. `"text-generation"`). When `None`, the task is auto-detected from the model type. |
183
183
|`dtype`|`str \| None`|`None`| Override model dtype (e.g. `"f16"`). When `None`, defaults to float32. |
184
-
|`keep_quantized`|`bool`|`True`| Preserve quantization when present. Supported affine blocks are repacked as `MatMulNBits`; in text-only builds, supported native IQ/MXFP4 projection blocks retain their bytes. Multimodal and mixed presets may require dequantization/requantization. Set to `False` to dequantize all weights. |
184
+
|`keep_quantized`|`bool`|`True`| Preserve quantization when present. Supported affine blocks are repacked as `MatMulNBits`; in text-only builds, supported native IQ/MXFP4 projection blocks retain their bytes. Projection tensors that would require lossy requantization are rejected. Set to `False` to dequantize all weights. |
185
185
|`execution_provider`|`str`|`"default"`| Target EP for EP-aware graph optimization. |
0 commit comments