Skip to content

Normalize negative axes in blocked quantization helpers - #34181

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

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

Conversation

@Afloat16

Copy link
Copy Markdown

Description

Both compute_scale_zp_blocked and the blocked path of quantize_onnx_initializer accept an axis but use its original value when enumerating the other dimensions. For a rank-2 weight and axis -1 or -2, that enumeration includes both dimensions, so valid input fails while reshaping the weights.

Normalize a negative axis immediately after indexing the selected dimension. This preserves the existing out-of-range-axis validation and makes the remaining layout calculations use the same axis as NumPy. Scale computation, rounding, clipping, per-channel quantization and the rank-2 restriction are unchanged. The main QDQ quantizer already normalizes its axis; this fixes the two lower-level entry points.

Validation

  • Added 7 unittest methods with 50 subcases: positive/negative-axis equality across float16/float32, int8/uint8, symmetric/asymmetric ranges and block sizes; quantized initializer contents; read-only noncontiguous inputs; invalid-axis and existing rank/per-channel controls.
  • Native CPU DequantizeLinear followed by MatMul, with optimization disabled, checks both negative axes against independently reconstructed weights.
  • New and existing test_quant_util.py / test_qdq_block_size.py: 24 methods passed, 193 subtests passed.
  • Restoring the exact original implementation yields 46 failed subtests, 7 completed methods and 4 passing subtests. Failures occur in parameter calculation, initializer packing, noncontiguous-input and native integration cases.
  • Ruff lint/format and git diff --check passed.

Native validation: https://github.com/Afloat16/onnxruntime/actions/runs/43308384299

The runtime was built with the repository's official CPU Release build.py/CMake path from commit 8dea119, whose C++ sources match base a8246c0. This run installs that built wheel, verifies the unchanged native-source baseline, and overlays the complete quantization 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; it does not claim GPU or the full C++ test suite.

Motivation and Context

Negative axes are an existing supported way to identify tensor dimensions. Equivalent positive and negative values should produce identical blocked quantization parameters and initializers, including when these helpers are called directly.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 09:07
@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 block the negative-axis handling fix.

No open issues 🎉

What changed in this PR

Fixes negative-axis handling in two lower-level blocked quantization helpers so equivalent positive and negative axes produce identical results.

Changes:

  • Normalizes negative axes after indexing the selected dimension, preserving existing out-of-range validation.
  • Adds regression tests for parameter and initializer equality, read-only noncontiguous inputs, existing constraints, and native CPU dequantization followed by MatMul.

No actionable findings were identified.

File Description
onnxruntime/​test/​python/​quantization/​test_blocked_negative_axes.py Adds negative-axis regression tests and native CPU integration coverage.
onnxruntime/​python/​tools/​quantization/​quant_utils.py Normalizes negative axes in both blocked quantization helpers.

Review effort: Balanced

@mirounga
mirounga self-requested a review November 13, 2026 16:10
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

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