Preserve node output dtype when lifting scalars to attrs in XNNPACK quantizer - #22065
Preserve node output dtype when lifting scalars to attrs in XNNPACK quantizer#22065shoemoney wants to merge 1 commit into
Conversation
|
Hi @shoemoney! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
|
There was a problem hiding this comment.
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_attrsto create lifted scalar buffers withdtype=n.meta["val"].dtypeinstead of forcingfloat32. - Add a regression test covering an
int64scalar-add index pattern (torch.arange(4) + 0) to ensure dtype preservation and successful execution afterprepare_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.
|
@pytorchbot label "release notes: xnnpack" |
|
/easycla |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Fixes #22062
_convert_scalars_to_attrsinbackends/xnnpack/quantizer/xnnpack_quantizer_utils.pylifts every scalar argument ofaten.add.Tensorandaten.mul.Tensorto a buffer created withtorch.tensor(float(arg)), which is always float32.XNNPACKQuantizer.transform_for_annotationruns this unconditionally, so an integer chain such asx[:, torch.arange(4) + 0](the position-ids pattern emitted by Hugging Face models) has its int64 add promoted to float32, and running theprepare_pt2eoutput fails withIndexError: 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 forfake_mode. Float behavior is unchanged.Added
test_int64_scalar_add_used_as_indextobackends/xnnpack/test/quantizer/test_xnnpack_quantizer.py: it exports the index pattern above, runstransform_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.