Skip to content

fix(cadence): fix or-precedence bug in check_out_zero_point_is_min_range#20608

Open
durvesh1992 wants to merge 1 commit into
pytorch:mainfrom
durvesh1992:fix/cadence-uint-zero-point-or-precedence
Open

fix(cadence): fix or-precedence bug in check_out_zero_point_is_min_range#20608
durvesh1992 wants to merge 1 commit into
pytorch:mainfrom
durvesh1992:fix/cadence-uint-zero-point-or-precedence

Conversation

@durvesh1992

Copy link
Copy Markdown

Summary

check_out_zero_point_is_min_range (backends/cadence/aot/quantizer/utils.py) contained an operator-precedence bug:

elif out_dtype == torch.uint8 or torch.uint16:
    return out_zero_point == 0
return False

Python parses this as (out_dtype == torch.uint8) or (torch.uint16). Since torch.uint16 is a (truthy) dtype object, the elif is effectively always True for any dtype that isn't int8/int16, and the trailing return False is dead code.

Effect: for unrelated dtypes such as float32 or int32, the function returned out_zero_point == 0 instead of False.

check_out_zero_point_is_min_range(0, torch.float32)  # was True, should be False
check_out_zero_point_is_min_range(0, torch.int32)    # was True, should be False

The function gates Add+ReLU fusion in quantizer/patterns.py (AddReluPattern.get_anchors), so an output whose dtype is not a supported quant type but whose zero point happens to be 0 would wrongly pass the check.

Fix: compare out_dtype against each dtype explicitly:

elif out_dtype == torch.uint8 or out_dtype == torch.uint16:

Test plan

Adds backends/cadence/aot/tests/test_quantizer_utils.py covering signed (int8/int16), unsigned (uint8/uint16), and non-quant (float32/int32) dtypes.

  • Before the fix, test_non_quant_dtype_is_false fails (float32/int32 with zero point 0 return True).
  • After the fix, all tests pass.

Run:

python -m unittest backends.cadence.aot.tests.test_quantizer_utils -v

check_out_zero_point_is_min_range had:

    elif out_dtype == torch.uint8 or torch.uint16:

which Python parses as `(out_dtype == torch.uint8) or (torch.uint16)`.
torch.uint16 is always truthy, so the branch matched for every dtype
that is not int8/int16 and the trailing `return False` became dead code.
As a result the function returned `out_zero_point == 0` for unrelated
dtypes (e.g. float32, int32) instead of False.

This gates Add+ReLU fusion in quantizer/patterns.py, so an output whose
dtype is not a supported quant type but whose zero point is 0 would
wrongly pass the check. Compare each dtype explicitly.

Add a unit test covering signed, unsigned, and non-quant dtypes.
@pytorch-bot

pytorch-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 11 Awaiting Approval

As of commit 11fb5e7 with merge base b331ebd (image):

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

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 Jun 29, 2026
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Missing ID

  • ❌ The email address for the commit (11fb5e7) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please visit our EasyCLA portal and chat with our support bot.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant