Repository navigation
Fix numerical stability of distribution calibration moments - #34179
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 block the numerical-stability improvements to distribution-calibration moments.
No open issues 🎉
What changed in this PR
Improves numerical stability of distribution-calibration moments, helping prevent invalid calibration ranges and FP8 quantization scales for float16 and float32 histograms.
Changes:
- Promotes bin edges to float64 before computing midpoints, signed powers, and moments, then restores the output dtype.
- Computes variance directly from centered values to avoid cancellation and intermediate overflow or underflow.
- Adds numerical regression tests, CPU calibration checks, and static FP8 quantization coverage.
| File | Description |
|---|---|
onnxruntime/test/python/quantization/test_distribution_moments.py |
Adds moment regression tests and CPU calibration and FP8 integration checks. |
onnxruntime/python/tools/quantization/calibrate.py |
Uses float64 intermediates and centered variance for stable histogram moments. |
Review effort: Balanced
mirounga
self-requested a review
November 13, 2026 16:13
| 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
Compute distribution-calibration histogram moments in float64 before converting the resulting mean and standard deviation back to the histogram dtype:
E[x²] - E[x]².The change preserves the histogram's output dtype, signed-power convention, empty-histogram behavior, and zero variance of a single occupied bin.
Motivation and Context
Finite float16/float32 histograms can currently produce infinite, zero, or NaN standard deviations even when the correct result is representable. This propagates into distribution calibration ranges and FP8 quantization scales.
The regression tests compare histogram moments with independently expanded observations and exercise real CPU MatMul calibration, persisted calibration data, and static FP8 quantization. They cover midpoint overflow, squared-moment overflow/underflow, cancellation, signed powers, and control cases.
Validation
tools/ci_build/build.pyentry point on Ubuntu 24.04 / Python 3.12. The source-build job completed compilation and produced the wheel.calibrate.pybytes, and runs the new tests together withtest_calibration.py: 31 passed, 15 subtests passed.calibrate.pyinto the same native runtime reproduces 7 failing test methods and 6 failing subtests; the remaining controls pass. The initial build job's test-collection path was corrected in the linked follow-up validation.git diff --checkpass.Validation is limited to the CPU calibration/quantization paths above. This does not claim support for arbitrary extreme float64 powers or a general model-accuracy improvement; the full C++ and GPU suites were not run.