Skip to content

Precompute quantized constant ops after Q/DQ folding (#24258) - #24258

Open
pssrawat wants to merge 2 commits into
pytorch:mainfrom
pssrawat:export-D128002647
Open

pssrawat wants to merge 2 commits into
pytorch:mainfrom
pssrawat:export-D128002647

Conversation

@pssrawat

@pssrawat pssrawat commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Q/DQ folding (FoldAndAnnotateQParamsPass) always folds the quantize/dequantize pair around a constant-producing op (full, full_like, arange, eye, linspace, scalar_tensor) into the op: it removes the Q node and records the output qparams, but the op itself keeps producing float. The Arm backend follows folding with ComputeConstantOpsAOTPass, which evaluates such ops in float and quantizes the result. Cortex-M did not, so in x + torch.full(shape, 0.5) the int8 quantized_add kernel read a float32 constant, and the output was silently wrong.

Add ComputeQuantizedConstantOpsPass and run it right after folding on both Cortex-M pass lists.

  • a. It only touches nodes with output qparams, so unquantized constant ops are unchanged. A quantized full stays a runtime fill, with its fill value quantized and its dtype set to the qparams' dtype, so a large fill does not turn into a large constant;
  • b.full_like is first converted to full with the Arm backend's ConvertFullLikeToFullPass.
  • c. Since ComputeConstantOpsAOTPass only logs a failed precompute, the new pass raises if a quantized constant op is still in the graph afterwards, as it would feed float data to an int8 kernel.

Differential Revision: D128002647

@pssrawat
pssrawat requested a review from rascani as a code owner October 28, 2026 20:43
@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 4 New Failures

As of commit 46661bf with merge base b831dbe (image):

NEW FAILURES - The following jobs have failed:

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 Oct 28, 2026
@meta-codesync

meta-codesync Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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

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

pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 28, 2026
Summary:

Q/DQ folding (`FoldAndAnnotateQParamsPass`) always folds the quantize/dequantize pair around a constant-producing op (`full`, `full_like`, `arange`, `eye`, `linspace`, `scalar_tensor`) into the op: it removes the Q node and records the output qparams, but the op itself keeps producing float. The Arm backend follows folding with `ComputeConstantOpsAOTPass`, which evaluates such ops in float and quantizes the result. Cortex-M did not, so in `x + torch.full(shape, 0.5)` the int8 `quantized_add` kernel read a float32 constant, and the output was silently wrong.

Add `ComputeQuantizedConstantOpsPass` and run it right after folding on both Cortex-M pass lists. 

- a. It only touches nodes with output qparams, so unquantized constant ops are unchanged. A quantized `full` stays a runtime fill, with its fill value quantized and its dtype set to the qparams' dtype, so a large fill does not turn into a large constant; 
- b.`full_like` is first converted to `full` with the Arm backend's `ConvertFullLikeToFullPass`. 
- c. Since `ComputeConstantOpsAOTPass` only logs a failed precompute, the new pass raises if a quantized constant op is still in the graph afterwards, as it would feed float data to an int8 kernel.

Differential Revision: D128002647
Summary:

Once Q/DQ is folded, the LUT activations (`tanh`, `sigmoid`, `silu`, `gelu`, `log*`, `sqrt`, `rsqrt`) and `div` take int8, but their ATen fake kernels promote it to float. Every pass retraces the graph, so each retrace labels these ops, and every view, permute, clamp and layout copy behind them. The dtype-based checks downstream then decline tensors that are int8 on device: e.g., `ValidateCortexMExplicitLayoutPass` rejects a convolution behind `tanh`.

Fix the dtype during tracing, as the Arm backend does for ops whose fake kernel cannot handle quantized inputs. For these ops, when a node carries output qparams, the retrace takes its output dtype from them instead of from the fake kernel. Every pass that rebuilds the graph recomputes these types, and most passes in the Cortex-M lists are shared passes that don't know about this fix. So CortexMPassManager attaches it to every pass it runs. The one exception is AtenToCortexMPass, which replaces these ops with Cortex-M kernels that already report int8.

The ops stay ATen until `AtenToCortexMPass`, and permute and layout-copy substitution still reads only the fake-tensor dtype, so an int8 graph that was never quantized lowers too.

Authored with an AI coding assistant (Claude).

Differential Revision: D128001102
Summary:

Q/DQ folding (`FoldAndAnnotateQParamsPass`) always folds the quantize/dequantize pair around a constant-producing op (`full`, `full_like`, `arange`, `eye`, `linspace`, `scalar_tensor`) into the op: it removes the Q node and records the output qparams, but the op itself keeps producing float. The Arm backend follows folding with `ComputeConstantOpsAOTPass`, which evaluates such ops in float and quantizes the result. Cortex-M did not, so in `x + torch.full(shape, 0.5)` the int8 `quantized_add` kernel read a float32 constant, and the output was silently wrong.

Add `ComputeQuantizedConstantOpsPass` and run it right after folding on both Cortex-M pass lists. 

- a. It only touches nodes with output qparams, so unquantized constant ops are unchanged. A quantized `full` stays a runtime fill, with its fill value quantized and its dtype set to the qparams' dtype, so a large fill does not turn into a large constant; 
- b.`full_like` is first converted to `full` with the Arm backend's `ConvertFullLikeToFullPass`. 
- c. Since `ComputeConstantOpsAOTPass` only logs a failed precompute, the new pass raises if a quantized constant op is still in the graph afterwards, as it would feed float data to an int8 kernel.

Differential Revision: D128002647
@meta-codesync meta-codesync Bot changed the title Precompute quantized constant ops after Q/DQ folding Precompute quantized constant ops after Q/DQ folding (#24258) Oct 28, 2026

This branch has not been deployed

No deployments
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.

1 participant