Commit 9567144
Emit GGUF zero points explicitly (#459)
## Summary
This is the clean-main rebuild of the urgent correctness fix from #458.
It contains only the GGUF zero-point fix, based on current
`origin/main`.
GGUF Q4_0 and Q8_0 are symmetric formats, but their dequantization
formulas still require non-zero zero points:
- Q4_0: `(q - 8) * scale`
- Q8_0: `(q - 128) * scale`
Mobius previously marked those GGUF imports as symmetric, so
`QuantizedLinear` and `QuantizedEmbedding` omitted the optional
`zero_points` input for `MatMulNBits` and `GatherBlockQuantized`. That
is not portable: `GatherBlockQuantized` defaults diverge between ORT CPU
and ORT CUDA when `zero_points` is omitted, corrupting CUDA embeddings
before the first decoder layer runs.
This PR makes GGUF Q4_0/Q8_0 emit explicit zero-point initializers, so
both `GatherBlockQuantized` and `MatMulNBits` receive the intended
values instead of relying on EP defaults.
## Regression test
Added a single-node `GatherBlockQuantized` runtime regression:
- qweight nibbles are all `10`
- scales are known constants
- explicit zero point is `8`
- output is asserted against hand-computed `(q - 8) * scale`
- rerunning with zero point `0` is asserted not to match
I also temporarily flipped the test's zero point to `0`; it failed with
a 100% output mismatch, so the test checks the value and not just the
presence of the input.
Synthetic GGUF tests now also assert:
- Q4_0 `MatMulNBits` nodes have the explicit fourth input
- Q4_0 `GatherBlockQuantized` has the explicit fourth input
- Q4_0 embedding zero points are packed as `0x88`
## Quantization types checked
Direct GGUF repackers already produce explicit zero-points for supported
direct formats:
- Q4_0: zp=8
- Q4_1: per-block affine zp
- Q4_K: requantized per-block affine zp
- Q8_0: zp=128
- Q1_0: zp=1; Tencent Q1_0 uses its custom zp path
Q5_0/Q5_1/Q5_K/Q6_K are not direct `MatMulNBits` repack targets here;
when they appear in mixed presets they go through
dequantize+requantize/native-block fallback paths, which produce
explicit zero-points where needed.
## Validation
- `python -m pytest src\mobius\integrations\gguf\_builder_test.py -q` ->
`33 passed`
- `python -m pytest src\mobius\integrations\gguf\ src\mobius\_configs\
src\mobius\_model_package_test.py` -> `304 passed`
- `python -m ruff check src\mobius\integrations\gguf\_builder.py
src\mobius\integrations\gguf\_builder_test.py` -> passed
- `python -m ruff format --check
src\mobius\integrations\gguf\_builder.py
src\mobius\integrations\gguf\_builder_test.py` -> passed
Fresh conversion validated with
`C:\Users\justinchu\dev\models-gguf\qwen2.5-0.5b-instruct-q4_0.gguf`:
```powershell
python -m mobius build-gguf C:\Users\justinchu\dev\models-gguf\qwen2.5-0.5b-instruct-q4_0.gguf --output C:\Users\justinchu\dev\models\qwen2.5-0.5b-q4_0-mobius --keep-quantized --dtype f16 --ep cuda
```
Converted graph verification:
- `GatherBlockQuantized` input count: 4
- all `MatMulNBits` input counts: 4
- zero-point initializers: 170
Generation checks on the freshly converted model, no post-hoc graph
patching:
- ORT CPU via onnx-genai CLI: `The capital of France is Paris. It is the
largest city in`
- ORT CUDA via onnx-genai CLI (`ONNX_GENAI_ORT_LIB_DIR` pointed at the
installed onnxruntime-gpu package, CUDA/cuDNN DLL dirs on PATH): `The
capital of France is Paris. It is the largest city in`
- native CUDA via onnx-genai CLI (`--features native-cuda`, CUDA/cuDNN
DLL dirs on PATH): `The capital of France is Paris. It is the largest
city in`
## Upstream ORT issue
Reported the provider default divergence here:
microsoft/onnxruntime#31692
## Relationship to #458
#458 was accidentally based on another in-flight feature branch and
includes unrelated commits. This PR is the clean-main replacement for
the urgent zero-point correctness fix only.
---------
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>1 parent 1d2a28b commit 9567144
2 files changed
Lines changed: 122 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
528 | 528 | | |
529 | 529 | | |
530 | 530 | | |
531 | | - | |
| 531 | + | |
532 | 532 | | |
533 | 533 | | |
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
538 | 538 | | |
539 | | - | |
540 | | - | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
541 | 547 | | |
542 | 548 | | |
543 | | - | |
| 549 | + | |
544 | 550 | | |
545 | 551 | | |
546 | 552 | | |
| |||
561 | 567 | | |
562 | 568 | | |
563 | 569 | | |
564 | | - | |
565 | | - | |
566 | | - | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
567 | 582 | | |
568 | 583 | | |
569 | 584 | | |
570 | 585 | | |
571 | 586 | | |
572 | 587 | | |
573 | 588 | | |
574 | | - | |
| 589 | + | |
575 | 590 | | |
576 | 591 | | |
577 | 592 | | |
| |||
598 | 613 | | |
599 | 614 | | |
600 | 615 | | |
601 | | - | |
| 616 | + | |
602 | 617 | | |
603 | 618 | | |
604 | 619 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
14 | 77 | | |
15 | 78 | | |
16 | 79 | | |
| |||
311 | 374 | | |
312 | 375 | | |
313 | 376 | | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
314 | 391 | | |
315 | 392 | | |
316 | 393 | | |
| |||
380 | 457 | | |
381 | 458 | | |
382 | 459 | | |
| 460 | + | |
383 | 461 | | |
384 | 462 | | |
385 | 463 | | |
| |||
388 | 466 | | |
389 | 467 | | |
390 | 468 | | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
391 | 473 | | |
392 | 474 | | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
393 | 489 | | |
394 | 490 | | |
395 | 491 | | |
| |||
401 | 497 | | |
402 | 498 | | |
403 | 499 | | |
| 500 | + | |
404 | 501 | | |
405 | 502 | | |
406 | 503 | | |
| |||
479 | 576 | | |
480 | 577 | | |
481 | 578 | | |
482 | | - | |
| 579 | + | |
483 | 580 | | |
484 | 581 | | |
485 | 582 | | |
| |||
0 commit comments