Repository navigation
Vectorize byte packing for four-bit quantized weights - #34183
Open
Afloat (Afloat16) wants to merge 2 commits into
Open
Afloat (Afloat16) wants to merge 2 commits into
Afloat (Afloat16) wants to merge 2 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the INT4/UINT4 weight-packing optimization.
No open issues 🎉
What changed in this PR
This PR accelerates INT4/UINT4 weight packing in the quantization utilities while preserving nibble order, odd-length padding, return type, and input ownership.
Changes:
- Adds a NumPy fast path for
bytesandbytearray, retaining element-wise packing for other sequences. - Adds seven tests covering packing correctness, buffer compatibility, serialized initializers, and native CPU dequantization with MatMul.
No actionable findings were identified.
| File | Description |
|---|---|
onnxruntime/test/python/quantization/test_four_bit_packing.py |
Adds packing regression tests and native CPU integration coverage. |
onnxruntime/python/tools/quantization/quant_utils.py |
Vectorizes byte-input packing with NumPy bitwise operations. |
Review effort: Balanced
mirounga
self-requested a review
November 13, 2026 16:01
| import unittest | ||
|
|
||
| import numpy as np | ||
| import onnx |
Remove the mixed import forms flagged by CodeQL without changing the test behavior.
This branch has not been deployed
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.
Description
Packing INT4/UINT4 weights currently runs a Python iteration for every pair of elements. Add a NumPy bitwise path for bytes and bytearray inputs, writing the low and high nibbles into the destination bytearray.
The layout, odd-length zero padding, return type and input ownership are unchanged. Other sequences retain the original element-wise path, including noncontiguous and non-byte memoryviews.
Validation
git diff --checkpassed.Exact-source native validation and benchmark: https://github.com/Afloat16/onnxruntime/actions/runs/43401492131
Measured helper performance
One warmup and five timed calls per case, with median time reported. Input construction is outside the measured region. Peak Python/NumPy allocations are measured separately with tracemalloc; packed output hashes match in every case.
The shifted odd-element array adds temporary storage of roughly half the input length. Tiny inputs are slower, as shown above. These results concern this packing helper only, not end-to-end quantization throughput or model accuracy.
The native runtime comes from the repository's official CPU Release build.py/CMake build at 8dea119. Its C++ sources match base a8246c0. The validation installs that built wheel and overlays the complete quantization package from this exact patch, verifying every Python file byte-for-byte and recording a SHA-256 manifest. Tests use normal native imports. This is targeted Linux CPU validation, not GPU execution or the full C++ suite.