Repository navigation
Prevent range overflow in blocked quantization - #34182
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 blocked-quantization range overflow fix.
No open issues 🎉
What changed in this PR
This PR prevents intermediate range overflow in blocked quantization, allowing large finite float16 and float32 weights to produce finite scales when those scales fit in the original dtype.
Changes:
- Promote range endpoints to float64 before subtraction while retaining the final scale dtype and existing quantization rules.
- Add regression tests for large and control ranges, input layouts, native CPU dequantization, and static QDQ quantization.
No actionable findings were identified.
| File | Description |
|---|---|
onnxruntime/test/python/quantization/test_blocked_quantization_range.py |
Adds parameter-level regression tests and native CPU integration coverage. |
onnxruntime/python/tools/quantization/quant_utils.py |
Promotes both range endpoints before subtraction to prevent overflow. |
Review effort: Balanced
mirounga
self-requested a review
November 13, 2026 16:09
| from pathlib import Path | ||
|
|
||
| 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
Blocked quantization computes the difference between the maximum and minimum weights in the input dtype. Opposite-sign float16 weights near 60000, or float32 weights near 2e38, overflow that subtraction even though the resulting scale is representable. Infinite scales then corrupt the quantized weights.
Promote both range endpoints to float64 before subtracting them. The final scale still uses the weight dtype, and the existing zero-point, symmetry, clipping, block layout and degenerate-range rules are retained.
Validation
git diff --checkpassed.Native validation: https://github.com/Afloat16/onnxruntime/actions/runs/43400845218
The runtime was built with the official CPU Release build.py/CMake path from commit 8dea119, whose C++ sources match base a8246c0. This run installs that built wheel, verifies the matching native-source baseline, and overlays the complete quantization Python package from this exact patch with byte-for-byte checks and a SHA-256 manifest. It uses ordinary native imports. This is targeted Linux CPU validation, not a GPU or full C++ test-suite result.
Motivation and Context
Finite weight ranges should produce finite scales when those scales fit in the original dtype. This fixes the blocked helper's intermediate range subtraction; it does not change the quantization algorithm or claim a general model-accuracy improvement.