Skip to content

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
pytorch:mainfrom
vijay-kapse:fix/pt2e-observers-reject-complex
Open

vijay-kapse wants to merge 1 commit into
pytorch:mainfrom
vijay-kapse:fix/pt2e-observers-reject-complex

Conversation

@vijay-kapse

Copy link
Copy Markdown

Fixes #4910.

Problem

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 the min/max of the real part alone. calculate_qparams() then hands back a scale and
zero point that look ordinary, with nothing to signal 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.

Measured before the change

observer complex input
MinMaxObserver accepted, scale 0.0117647
MovingAverageMinMaxObserver accepted, scale 0.0117647
PerChannelMinMaxObserver accepted, scale 0.00784314
MovingAveragePerChannelMinMaxObserver accepted, scale 0.00784314
HistogramObserver raises NotImplementedError: "aminmax_cpu" not implemented for 'ComplexFloat'

Change

The four now raise NotImplementedError naming the observer and the dtype, which matches how
this module already reports unsupported configurations (MinMaxObserver's qscheme only support ...) and gives a bug report enough to identify the input:

NotImplementedError: MinMaxObserver does not support complex input, got torch.complex64

HistogramObserver needed no change — it does not perform the cast and already fails through
torch.aminmax. I checked it rather than assuming; the four above now behave the way it
already does.

The numel() == 0 early return is deliberately left ahead of the check, so an empty tensor is
still 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). It
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 unaffected.

5 passed

Reverting only the observer change fails exactly the two complex tests, so the coverage
discriminates rather than passing either way:

FAILED test_observer.py::TestObserverComplexInput::test_per_channel_observers_reject_complex
FAILED test_observer.py::TestObserverComplexInput::test_per_tensor_observers_reject_complex
2 failed, 3 passed

No regressions in the neighbouring suites: test/quantization/test_observer.py 11 passed,
test/quantization/pt2e/test_duplicate_dq.py 4 passed. ruff format --check and ruff check
are clean on both files.

…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.
@pytorch-bot

pytorch-bot Bot commented Oct 3, 2026

Copy link
Copy Markdown

🔗 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 3, 2026

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pt2e observers silently discard the imaginary part of complex tensors

1 participant