Repository navigation
Preserve zero batches in histogram calibration - #2369
MrCapricornLiu wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Thanks for the contribution! This PR now has merge conflicts with We've added the |
|
Thanks for the contribution! This PR adds its We've added the |
Fixes #2654. ## What changed Make the entropy calibration helper copy its input histogram before applying its existing first-bin adjustment. A NumPy slice is a view, so the previous implementation modified the collector's persistent counts. Subsequent percentile/MSE queries and continued collection could then use the altered distribution. The regression uses the public `HistogramCalibrator` with both histogram backends. It checks unchanged counts and edges, repeated entropy queries, unchanged percentile thresholds, and equivalence with an untouched reference collector after another batch. ## Validation ```sh python -m pytest -q tests/unit/torch/quantization/test_calibrator.py tests/unit/torch/quantization/test_tensor_quantizer_cpu.py tests/unit/torch/quantization/test_print.py ``` 79 passed, 1 existing skip on CPU. The focused regression on unchanged upstream gives 1 failure for NumPy histograms and 1 pass for PyTorch histograms. Repository pre-commit checks pass for the changed files. No new dependencies or public API changes. CUDA execution was not run. This is independent of #2369: its zero-prefix changes affect collection, while this patch changes only the entropy calculation. The changelog is updated. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed entropy-based calibration so repeated calculations preserve collected histogram counts and bin edges. Percentile and entropy results remain consistent, and collecting additional data after a calculation now produces calibration results consistent with uninterrupted collection. The fix applies to both NumPy- and Torch-based histograms, ensuring calibration state remains stable across calculation and collection steps. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Sylvester Kaczmarek <[email protected]>
Keep the counts of an all-zero prefix in the first bin when the first nonzero batch defines the histogram range, so those samples are not lost from the calibrated distribution. Squashed the branch onto current main and moved the CHANGELOG entry to the 0.49.0 section. Signed-off-by: Chenghao Liu <[email protected]>
800c3f1 to
89438fa
Compare
|
Rebased onto current Local verification with torch 2.15 on CPU:
I did not run the rest of |
What does this PR do?
Type of change: Bug fix
Histogram calibration can fail when its first batch is all zeros and a later batch contains nonzero values. The initial bin edges are all zero, so growing the range divides by a zero bin width and raises
OverflowError.torch.histcalso places zeros in the middle of its automatic range when both bounds are zero.Keep zero-only batches in the first bin, then rebuild the range when the first nonzero batch arrives and carry the previous zero counts into that bin. This preserves the calibrated distribution without imposing a minimum range that would lose resolution for small activations. Ordinary range growth and the NumPy collector are unchanged.
Usage
No API change. Existing histogram-calibrated
TensorQuantizercalls accept initial zero batches.Testing
TensorQuantizer.This covers the default
torch_hist=True, skip_zeros=Falsecollector. Empty input after filtering withskip_zeros=True, nonfinite inputs, distributed calibration and full-model accuracy/performance were not tested or changed.Before your PR is "Ready for review"
Additional Information
The histogram collector is separate from the NVFP4 activation headroom algorithm in #2028.
Summary by CodeRabbit
Bug Fixes
Tests