Repository navigation
Handle scalar and empty tensors in integer quantization helpers - #34185
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 are reported for the scalar and empty-array quantization changes.
No open issues 🎉
What changed in this PR
This PR fixes scalar and empty-array handling in ONNX Runtime’s integer quantization helpers without changing the FLOAT8 path.
Changes:
- Replace three
len(data)checks withdata.size, allowing scalars to quantize normally and empty tensors to retain identity parameters and their original shape. - Add regression tests for quantization types, overrides, readonly inputs, invalid arguments, and scalar serialization followed by native CPU inference.
| File | Description |
|---|---|
onnxruntime/test/python/quantization/test_quantize_data_shapes.py |
Adds seven test methods covering scalar and empty tensors, controls, and native inference. |
onnxruntime/python/tools/quantization/quant_utils.py |
Uses total element count for integer quantization’s emptiness checks. |
Review effort: Balanced
mirounga
self-requested a review
November 13, 2026 16:06
| 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
Integer
compute_data_quant_paramsuseslen(data)to decide whether an array contains values. This raises on a zero-dimensional tensor and misclassifies empty shapes such as(2, 0)as nonempty, leading to invalid reductions or inconsistent override handling.Use
data.sizefor the three element-count checks. Scalars now follow the same quantization calculation as a one-element vector. Empty integer tensors use the existing identity scale/zero-point defaults regardless of which dimension is empty, while preserving their shape.Validation
DequantizeLinear + Addmodel consumes the scalar quantized initializer after ONNX save/reload.git diff --checkpassed.Exact-source native validation: https://github.com/Afloat16/onnxruntime/actions/runs/43402852485
The runtime was built using the repository's official CPU Release build.py/CMake process at
8dea1194e974fa07b1d2f07a190949c3a92cab80. Its C++ sources match basea8246c0d094f1ab0d6bf236228e5e67683321cd7. This Python-only validation installs that built wheel, overlays the complete quantization package from this exact commit, and verifies byte equality and hashes before normal native imports. It does not claim a separate C++ rebuild for this Python-only change.Motivation and Context
The public array helpers should determine emptiness by total element count. This change is limited to integer quantization; FLOAT8's separate statistics path is unchanged. Some higher-level initializer callers already flatten their inputs, so this is not a claim that every static quantization entry point previously rejected scalars. No model-quality or throughput improvement is claimed.