Skip to content

Fix portable dequantize_per_channel misreading Int zero points - #21773

Open
karan1508 wants to merge 1 commit into
pytorch:mainfrom
karan1508:export-D115185783
Open

Fix portable dequantize_per_channel misreading Int zero points#21773
karan1508 wants to merge 1 commit into
pytorch:mainfrom
karan1508:export-D115185783

Conversation

@karan1508

Copy link
Copy Markdown
Contributor

Summary:

Why?

A quantized tensor stores small integers plus two numbers that convert them
back to real values:

real_value = (stored_integer - zero_point) x scale

"Per-channel" means each output channel gets its own zero point and scale, so
those are little arrays - one entry per channel.

The zero-point array is allowed to be stored as either 32-bit or 64-bit
integers. The dequantization operator checked for exactly that and accepted
both, and then read the array as if it were always 64-bit. So whenever the
array really was 32-bit, the operator read memory at the wrong stride and two
things went wrong at once.

It glued neighbouring channels together. Say the real zero points are
[30, 50, 60, 90] stored as 32-bit. Reading them as 64-bit pairs them up:

channel 0  ->  30 + 50 x 2^32  ~  214,748,364,830
channel 1  ->  60 + 90 x 2^32  ~  386,547,056,700

It ran off the end. Four 64-bit reads need 32 bytes, but a 4-entry 32-bit
array only holds 16. Channels 2 and 3 read whatever memory happened to sit
after the array - random garbage.

Plug a nonsense zero point into the formula and everything downstream is wrong:

(bias - 214,748,364,830) x scale  ->  78.7  instead of  0.047

The giveaway while debugging was an all-zero bias dequantizing to 37.7. Zero
minus garbage isn't zero.

Weights were never affected - their zero-point arrays happen to be stored as
64-bit. Biases are the ones stored as 32-bit, so every model with quantized
biases was hitting this. It only became visible in deep models, where one bad
bias per layer compounds through ~50 stacked convolutions until the activations
slam into the int16 ceiling and the output is pure noise. Shallower models
stayed inside their test tolerances and looked fine, which is why this went
unnoticed for so long.

What?

Read the array at whatever width it actually is. The helper sits next to the
existing one for the scale array, which already did exactly this - the
zero-point path had just never been given the same treatment:

int64_t get_zero_point(const Tensor& zero_points, size_t channel_ix) {
  if (zero_points.scalar_type() == ScalarType::Int) {
    return static_cast<int64_t>(
        zero_points.const_data_ptr<int32_t>()[channel_ix]);   // 32-bit
  }
  return zero_points.const_data_ptr<int64_t>()[channel_ix];   // 64-bit
}

Both places that used to hardcode the 64-bit read now call it, so the general
and optimized code paths can no longer disagree. That is the entire fix - no
logic change, just reading the right number of bytes.

The new regression test covers the 32-bit width across input types and both the
single- and multi-dimensional shapes. Every pre-existing test for this operator
happened to build its zero-point array as 64-bit, so the accepted-but-broken
case was never once exercised.

Reviewed By: digantdesai

Differential Revision: D115185783

Summary:
## Why?

A quantized tensor stores small integers plus two numbers that convert them
back to real values:

    real_value = (stored_integer - zero_point) x scale

"Per-channel" means each output channel gets its own zero point and scale, so
those are little arrays - one entry per channel.

The zero-point array is allowed to be stored as either 32-bit or 64-bit
integers. The dequantization operator checked for exactly that and accepted
both, and then read the array as if it were always 64-bit. So whenever the
array really was 32-bit, the operator read memory at the wrong stride and two
things went wrong at once.

**It glued neighbouring channels together.** Say the real zero points are
[30, 50, 60, 90] stored as 32-bit. Reading them as 64-bit pairs them up:

    channel 0  ->  30 + 50 x 2^32  ~  214,748,364,830
    channel 1  ->  60 + 90 x 2^32  ~  386,547,056,700

**It ran off the end.** Four 64-bit reads need 32 bytes, but a 4-entry 32-bit
array only holds 16. Channels 2 and 3 read whatever memory happened to sit
after the array - random garbage.

Plug a nonsense zero point into the formula and everything downstream is wrong:

    (bias - 214,748,364,830) x scale  ->  78.7  instead of  0.047

The giveaway while debugging was an all-zero bias dequantizing to 37.7. Zero
minus garbage isn't zero.

Weights were never affected - their zero-point arrays happen to be stored as
64-bit. Biases are the ones stored as 32-bit, so every model with quantized
biases was hitting this. It only became visible in deep models, where one bad
bias per layer compounds through ~50 stacked convolutions until the activations
slam into the int16 ceiling and the output is pure noise. Shallower models
stayed inside their test tolerances and looked fine, which is why this went
unnoticed for so long.

## What?

Read the array at whatever width it actually is. The helper sits next to the
existing one for the scale array, which already did exactly this - the
zero-point path had just never been given the same treatment:

    int64_t get_zero_point(const Tensor& zero_points, size_t channel_ix) {
      if (zero_points.scalar_type() == ScalarType::Int) {
        return static_cast<int64_t>(
            zero_points.const_data_ptr<int32_t>()[channel_ix]);   // 32-bit
      }
      return zero_points.const_data_ptr<int64_t>()[channel_ix];   // 64-bit
    }

Both places that used to hardcode the 64-bit read now call it, so the general
and optimized code paths can no longer disagree. That is the entire fix - no
logic change, just reading the right number of bytes.

The new regression test covers the 32-bit width across input types and both the
single- and multi-dimensional shapes. Every pre-existing test for this operator
happened to build its zero-point array as 64-bit, so the accepted-but-broken
case was never once exercised.

Reviewed By: digantdesai

Differential Revision: D115185783
@pytorch-bot

pytorch-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21773

Note: Links to docs will display an error until the docs builds have been completed.

❌ 6 Awaiting Approval, 2 New Failures

As of commit 7bb6c2e with merge base d189b45 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

NEW FAILURES - The following jobs have failed:

  • Cadence Build & Test / hifi-build / hifi4 (gh)
    ##[error]Refusing to check out fork pull request code from a 'pull_request_target' workflow. This workflow runs with the base repository's GITHUB_TOKEN, secrets, default-branch cache scope, and runner access. Fetching and executing a fork's code in that trusted context commonly leads to "pwn request" vulnerabilities. To opt in, review the risks at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' on the actions/checkout step.
  • Cadence Build & Test / vision-build / vision (gh)
    ##[error]Refusing to check out fork pull request code from a 'pull_request_target' workflow. This workflow runs with the base repository's GITHUB_TOKEN, secrets, default-branch cache scope, and runner access. Fetching and executing a fork's code in that trusted context commonly leads to "pwn request" vulnerabilities. To opt in, review the risks at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' on the actions/checkout step.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@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 Aug 12, 2026
@meta-codesync

meta-codesync Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@karan1508 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D115185783.

@digantdesai digantdesai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants