Skip to content

dequantize_per_channel writes float into a Half or Double out tensor when out_dtype is not given #25877

Backlog

Description

@AxelNoun

🐛 Describe the bug

can_use_optimized_dequantize_per_channel (kernels/quantized/cpu/op_dequantize.cpp) only rejects a non-float output when out_dtype is given; it never looks at out.scalar_type(). With a contiguous int8 input, no out_dtype and a Half or Double out, the kernel takes the optimized path, which writes through out.mutable_data_ptr<float>(). In portable mode that write is not type-checked: a Half output gets 4 bytes per element in a 2-byte buffer, and a Double output gets garbage. The generic path, which dispatches on out.scalar_type(), would return the right values. I found this by reading the code while working on #25824.

Minimal C++ reproduction (added to kernels/quantized/test/op_dequantize_test.cpp):

TensorFactory<ScalarType::Char> tf;
TensorFactory<ScalarType::Double> tf_double;
TensorFactory<ScalarType::Long> tf_long;
TensorFactory<ScalarType::Half> tf_half;
Tensor out = tf_half.zeros({2, 8});
dequantize_per_channel_out(
    tf.full({2, 8}, 100), tf_double.make({2}, {0.5, 0.5}),
    tf_long.make({2}, {30, 30}), /*axis=*/0, -128, 127,
    ScalarType::Char, std::optional<ScalarType>(), out);
// expected 35 everywhere
Actual: ETensor(sizes={2, 8}, dtype=Half, data={0, 3.02344, 0, 3.02344, ...})

With a Double out the values are 1.50324e+10 and 0, and the test binary then crashes. Through quantized_ops_aot_lib (ATen mode), the same call raises RuntimeError: expected scalar type Float but found Half instead. A program exported the usual way allocates a float output when out_dtype is not given, so it does not hit this path; a direct kernel call or a program with a non-float output does.

I would expect the optimized path to be used only for a float out. I have a one-condition fix with a test and will open a PR linked to this issue.

Versions

ExecuTorch built from main at d34221b, PyTorch 2.15.0+cpu, Ubuntu 24.04 on WSL2, x86_64.

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