onnx: import GatherBlockQuantized without widening the table - #2647
Open
czoli1976 wants to merge 1 commit into
Open
onnx: import GatherBlockQuantized without widening the table#2647czoli1976 wants to merge 1 commit into
czoli1976 wants to merge 1 commit into
Conversation
…tized, which tract had no handler for, so those models could not be imported. Gather the rows of the int4 table first and dequantize only those, keeping the table 4-bit in memory rather than paying eight times its size to widen a vocabulary-sized embedding up front. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Adds the
com.microsoft.GatherBlockQuantizedimporter. ORT-GenAI exports look up their embedding through it, so without a handler those models cannot be imported at all.Approach
The rows are gathered before being dequantized, so the table stays 4-bit in memory and only the selected rows are widened. Dequantizing the table up front — the straightforward lowering — would cost eight times its size for a vocabulary-sized embedding: on a 1.7B Qwen3 export that is 155 MB of int4 turning into 1.24 GB of f32, for a step that only ever reads a handful of rows.
Everything is expressed with existing ops, so there is no new eval kernel:
Gatheron the packed table, a nibble split (the values are integral and below 256, so/16and the remainder are exact in f32), a per-block broadcast of the scales, and(value - zero) * scale.Scope
Rank-2 table,
bits=4, uint8 storage,gather_axis0 (which the operator requires for uint8 anyway) and the last axis asquantize_axis— what the exports emit. Anything else is rejected with a specific message rather than silently mis-lowered. Absentzero_points, the zero defaults to 8 as the spec states.Testing
Checked against onnxruntime 1.28 across block sizes 16 / 32 / 128, with and without zero points, and for 1-D and 2-D index shapes: bit-exact in every case (the arithmetic is exact, so this is equality, not a tolerance). A case is added under
onnx/test_cases/with onnxruntime-generated expectations, passing all four passes including the NNEF round-trip.Note that onnxruntime only registers this op in reasonably recent builds — 1.19 is too old to run
generate_io.py.🍍