Registry Mirror
Skip to content

quantize_per_tensor and quantize_per_channel give wrong values on AArch64 for a zero point far from the quantized range聽#25969

Backlog

Description

@AxelNoun

馃悰 Describe the bug

On AArch64, the float to int8/uint8 path of quantize_per_tensor and quantize_per_channel (quantize_arm in kernels/quantized/cpu/op_quantize.cpp) narrows the rounded value to int16 with saturation, then adds the zero point cast to int16. The callers also cast the zero point to int32 first. When the zero point does not fit, or when the value saturates before the addition, the result differs from the scalar path and from the eager op, and the vectorized elements disagree with the scalar tail of the same tensor.

I found this by reading the code while working on #25824; I do not have a model that hits it, since such zero points are far outside the usual range.

With nine float32 values, so that eight go through the vector loop (tf is a Float factory, out a Char tensor):

quantize_per_tensor_out(tf.full({9}, -39990), 1.0, 40000, -128, 127, ScalarType::Char, out);
// eager and x86-64: 10 everywhere. AArch64: -128 x 8, then 10
quantize_per_tensor_out(tf.full({9}, 40000), 1.0, -32768, -128, 127, ScalarType::Char, out);
// eager and x86-64: 127 everywhere. AArch64: -1 x 8, then 127
quantize_per_tensor_out(tf.full({9}, 1), 1.0, 4294967301, -128, 127, ScalarType::Char, out);
// eager and x86-64: 127 everywhere. AArch64: 6 everywhere

quantize_per_channel to uint8 with the first input gives 0 x 8, then 10. I have a small fix that sends such zero points to the scalar path, with a C++ test, and will open a PR linked to this issue.

Versions

ExecuTorch built from main at 9b9fa24, quantized_kernels_test cross-compiled with aarch64-linux-gnu-g++ 13.3.0 and run under qemu-aarch64 8.2.2 (I have no ARM hardware), eager reference from PyTorch 2.15.0+cpu, Ubuntu 24.04 on WSL2.

This issue was written with the help of Claude Code (Anthropic); I verified the reproduction.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions