Skip to content

Preserve node output dtype when lifting scalars to attrs in XNNPACK quantizer - #22065

Open
shoemoney wants to merge 1 commit into
pytorch:mainfrom
shoemoney:fix/xnnpack-scalar-dtype-promotion
Open

Preserve node output dtype when lifting scalars to attrs in XNNPACK quantizer#22065
shoemoney wants to merge 1 commit into
pytorch:mainfrom
shoemoney:fix/xnnpack-scalar-dtype-promotion

Conversation

@shoemoney

Copy link
Copy Markdown

Fixes #22062

_convert_scalars_to_attrs in backends/xnnpack/quantizer/xnnpack_quantizer_utils.py lifts every scalar argument of aten.add.Tensor and aten.mul.Tensor to a buffer created with torch.tensor(float(arg)), which is always float32. XNNPACKQuantizer.transform_for_annotation runs this unconditionally, so an integer chain such as x[:, torch.arange(4) + 0] (the position-ids pattern emitted by Hugging Face models) has its int64 add promoted to float32, and running the prepare_pt2e output fails with IndexError: tensors used as indices must be long, int, byte or bool tensors. The fix creates the lifted constant with the node's own output dtype, torch.tensor(args[i], dtype=n.meta["val"].dtype), which the function already relies on for fake_mode. Float behavior is unchanged.

Added test_int64_scalar_add_used_as_index to backends/xnnpack/test/quantizer/test_xnnpack_quantizer.py: it exports the index pattern above, runs transform_for_annotation, asserts the lifted constant keeps dtype int64, and executes the prepared module. The test fails with the float32 dtype assertion before this change and passes after. Neighboring scalar tests (test_add_mul_scalar, test_add_mul_long, test_mul_float32_max) still pass. flake8 and ufmt clean on both files.

Written in conjunction with my pair programmer Claude.

Copilot AI lite review requested due to automatic review settings August 23, 2026 18:20
@shoemoney
shoemoney requested a review from digantdesai as a code owner August 23, 2026 18:20
@pytorch-bot

pytorch-bot Bot commented Aug 23, 2026

Copy link
Copy Markdown

@meta-cla

meta-cla Bot commented Aug 23, 2026

Copy link
Copy Markdown

Hi @shoemoney!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 23, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: shoemoney / name: Jeremy Schoemaker (1f22872)

Copilot AI 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.

Pull request overview

This pull request fixes an XNNPACK quantizer graph-rewrite bug where scalar constants lifted into module buffers were always created as float32, unintentionally promoting integer subgraphs (notably index expressions) and causing prepare_pt2e runtime failures. The fix preserves the original node’s output dtype when materializing the lifted scalar tensor, keeping integer index chains intact.

Changes:

  • Update _convert_scalars_to_attrs to create lifted scalar buffers with dtype=n.meta["val"].dtype instead of forcing float32.
  • Add a regression test covering an int64 scalar-add index pattern (torch.arange(4) + 0) to ensure dtype preservation and successful execution after prepare_pt2e.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
backends/xnnpack/quantizer/xnnpack_quantizer_utils.py Preserve node output dtype when lifting scalar constants into _tensor_constant_* buffers.
backends/xnnpack/test/quantizer/test_xnnpack_quantizer.py Add regression test ensuring lifted scalar constants used in indexing remain int64 and prepared module runs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@shoemoney

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: xnnpack"

@pytorch-bot pytorch-bot Bot added the release notes: xnnpack Changes to the XNNPack backend delegate label Aug 23, 2026
@shoemoney

Copy link
Copy Markdown
Author

/easycla

@shoemoney shoemoney closed this Aug 23, 2026
@shoemoney shoemoney reopened this Aug 23, 2026
@meta-cla

meta-cla Bot commented Aug 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 23, 2026

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

thanks for the fix!

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. release notes: xnnpack Changes to the XNNPack backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_convert_scalars_to_attrs writes every lifted scalar as float32, retyping integer subgraphs and breaking prepare_pt2e

4 participants