Skip to content

Prevent range overflow in blocked quantization - #34182

Open
Afloat (Afloat16) wants to merge 2 commits into
microsoft:mainfrom
Afloat16:fix/blocked-quantization-range-overflow-20261112
Open

Afloat (Afloat16) wants to merge 2 commits into
microsoft:mainfrom
Afloat16:fix/blocked-quantization-range-overflow-20261112

Conversation

@Afloat16

Copy link
Copy Markdown

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

  • Added 8 unittest methods with 40 subcases covering float16/float32, both axes, int8/uint8, symmetric/asymmetric ranges, same-sign symmetric ranges, read-only noncontiguous weights, and ordinary/small/zero-range controls.
  • An independent Python-float calculation checks every block's scale and zero point.
  • Native CPU blocked DequantizeLinear and actual static QDQ quantization followed by MatMul verify finite persisted scales and inference results. Graph optimization is disabled for these integration checks.
  • New tests plus existing quantization utility and block-size tests: 25 methods passed, 183 subtests passed.
  • Restoring the original implementation yields 28 failing subtests, 8 completed methods and 12 passing subtests.
  • Ruff lint/format and git diff --check passed.

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.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 09:32
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants