Repository navigation
Reject complex input in the pt2e min/max observers instead of silently dropping the imaginary part - #4970
Open
vijay-kapse wants to merge 1 commit into
Open
Reject complex input in the pt2e min/max observers instead of silently dropping the imaginary part#4970vijay-kapse wants to merge 1 commit into
vijay-kapse wants to merge 1 commit into
Conversation
…y dropping the imaginary part
MinMaxObserver, MovingAverageMinMaxObserver, PerChannelMinMaxObserver and
MovingAveragePerChannelMinMaxObserver cast the incoming tensor to the dtype of
their min/max buffers:
x = x.to(self.min_val.dtype)
For a complex tensor that buffer is real, so the imaginary part is dropped and
the observer records min/max of the real part alone. calculate_qparams() then
returns a scale and zero point that look ordinary, with nothing to indicate that
most of the input was discarded:
x = torch.tensor([1+100j, 2+200j, 3+300j])
obs = MinMaxObserver(); obs(x)
obs.min_val, obs.max_val # 1.0, 3.0 -- the 100..300 imaginary range is gone
obs.calculate_qparams() # scale 0.0117647, zero_point 0
The quantize kernels reject the input a step later anyway:
PerTensorAffineQuantizer::quantize and PerChannelAffineFloatQParamsQuantizer::quantize
in ATen's Quantizer.cpp both require a float tensor. So the observer and the
kernel disagreed about whether complex input is allowed, and the observer was the
one that failed quietly.
These four now raise NotImplementedError naming the observer and the dtype, which
matches how the module already reports unsupported configurations and gives a
report enough information to identify the input.
HistogramObserver needed no change: it does not perform the cast, and already
fails on complex input through torch.aminmax
("aminmax_cpu" not implemented for 'ComplexFloat'). The four above now behave the
same way as it does.
The numel() == 0 early return is deliberately left ahead of the check, so an
empty tensor is still returned unobserved whatever its dtype.
Adds test/quantization/pt2e/test_observer.py, which covers all five observers over
complex64 and complex128, asserts the message names both the observer and the
dtype, and pins that real input and the empty-tensor path are unchanged.
Reverting the observer change fails exactly the two complex tests.
Fixes pytorch#4910.
vijay-kapse
requested review from
andrewor14,
jerryzh168 and
vkuzo
as code owners
October 3, 2026 05:48
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4970
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
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.
Fixes #4910.
Problem
MinMaxObserver,MovingAverageMinMaxObserver,PerChannelMinMaxObserverandMovingAveragePerChannelMinMaxObservercast the incoming tensor to the dtype of theirmin/max buffers:
For a complex tensor that buffer is real, so the imaginary part is dropped and the observer
records the min/max of the real part alone.
calculate_qparams()then hands back a scale andzero point that look ordinary, with nothing to signal that most of the input was discarded:
The quantize kernels reject the input a step later anyway —
PerTensorAffineQuantizer::quantizeand
PerChannelAffineFloatQParamsQuantizer::quantizein ATen'sQuantizer.cppboth require afloat tensor. So the observer and the kernel disagreed about whether complex input is allowed,
and the observer was the one that failed quietly.
Measured before the change
MinMaxObserverMovingAverageMinMaxObserverPerChannelMinMaxObserverMovingAveragePerChannelMinMaxObserverHistogramObserverNotImplementedError: "aminmax_cpu" not implemented for 'ComplexFloat'Change
The four now raise
NotImplementedErrornaming the observer and the dtype, which matches howthis module already reports unsupported configurations (
MinMaxObserver's qscheme only support ...) and gives a bug report enough to identify the input:HistogramObserverneeded no change — it does not perform the cast and already fails throughtorch.aminmax. I checked it rather than assuming; the four above now behave the way italready does.
The
numel() == 0early return is deliberately left ahead of the check, so an empty tensor isstill returned unobserved whatever its dtype. That path is pinned by a test.
Tests
Adds
test/quantization/pt2e/test_observer.py(there was no pt2e observer test module). Itcovers all five observers over
complex64andcomplex128, asserts the message names both theobserver and the dtype, and pins that real input and the empty-tensor path are unaffected.
Reverting only the observer change fails exactly the two complex tests, so the coverage
discriminates rather than passing either way:
No regressions in the neighbouring suites:
test/quantization/test_observer.py11 passed,test/quantization/pt2e/test_duplicate_dq.py4 passed.ruff format --checkandruff checkare clean on both files.