onnx: keep asymmetric MatMulNBits weights block-quantized - #2648
Open
czoli1976 wants to merge 2 commits into
Open
onnx: keep asymmetric MatMulNBits weights block-quantized#2648czoli1976 wants to merge 2 commits into
czoli1976 wants to merge 2 commits into
Conversation
…symmetric, so the asymmetric int4 the ORT-GenAI exports emit fell back to a dense f32 weight — eight times the memory, and enough to put a 1.7B model out of reach. Q4_0 fixes the zero point at 8, so split the weight as (q - z) * s == (q - 8) * s + (8 - z) * s: the first term is exactly Q4_0, and the second is constant within a block, contributing the block sums of the activations against an [N, K/32] constant. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…matching the bias add.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MatMulNBitsonly kept its weight block-quantized when the export was symmetric. Every ORT-GenAI int4 export is asymmetric — all 197 nodes in a Bonsai/Qwen3 1.7B export carry zero points — so they all fell back to a dense f32 weight: eight times the memory, and enough to put a 1.7B model out of reach on a laptop.Approach
No new block-quant format is needed. Q4_0 fixes the zero point at 8, so the weight splits as
The first term is exactly Q4_0, packed as today. The second is constant within a block, so its whole contribution is the block sums of the activations against an
[N, K/32]constant — one thirty-second of the weight, and a matmul of one thirty-second the size. Both terms use the f16-rounded scale the packer already stores, so the split is exact against the rounded weight rather than introducing a second approximation.The dense f32 fallback stays for the shapes that need it (other block sizes, a partial last block), and is now built only when that branch is actually taken instead of always.
Effect
On
onnx-community/Bonsai-1.7B-ONNX/model_q4.onnx(Qwen3, 28 layers, asymmetric int4) the weights stayQ4_0in memory: peak RSS 1.11 GB for load, optimize and an 8-token prefill, against roughly 7 GB of expanded f32 weights before. Logits match onnxruntime to 1e-4 absolute on values up to ±12.5 (4.4e-6 relative), with identical argmax on every position.Testing
Checked against onnxruntime across M/K/N shapes with and without zero points, and for block sizes on and off the fast path: relative error ~3e-4, the same order as the pre-existing symmetric path, which is the f16 scale rounding rather than the split.
onnx/test_cases/run_all.shshows no new failures.🍍