Skip to content

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

Open
pssrawat wants to merge 1 commit into
pytorch:mainfrom
pssrawat:export-D128002647
Open

pssrawat wants to merge 1 commit 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 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.

This also fixes a test that was already failing. surrounded_quantized_op_constant in misc/test_quantization.py was marked as an expected failure. Its torch.ones(2, 2) constant is lowered to a full op, so the int8 add read float data, and every output element was off by about 0.49. With this change the case matches the quantized reference exactly, so its xfail is removed. The other constants in that file are stored tensors, which folding already quantized correctly.

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.

✅ No Failures

As of commit e2356ac with merge base 7586c16 (image):
💚 Looks good so far! There are no failures yet. 💚

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.

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
@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
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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
@pssrawat pssrawat added the release notes: none Do not include this in the release notes label Oct 29, 2026
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 29, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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 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`.

Differential Revision: D128002647
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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 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`.

Differential Revision: D128002647
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 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`.

This also fixes a test that was already failing. `surrounded_quantized_op_constant` in `misc/test_quantization.py` was marked as an expected failure. Its `torch.ones(2, 2)` constant is lowered to a `full` op, so the int8 add read float data, and every output element was off by about 0.49. With this change the case matches the quantized reference exactly, so its `xfail` is removed. The other constants in that file are stored tensors, which folding already quantized correctly.

Differential Revision: D128002647
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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 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`.

This also fixes a test that was already failing. `surrounded_quantized_op_constant` in `misc/test_quantization.py` was marked as an expected failure. Its `torch.ones(2, 2)` constant is lowered to a `full` op, so the int8 add read float data, and every output element was off by about 0.49. With this change the case matches the quantized reference exactly, so its `xfail` is removed. The other constants in that file are stored tensors, which folding already quantized correctly.

Differential Revision: D128002647
pssrawat added a commit to pssrawat/executorch that referenced this pull request Oct 30, 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 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`.

This also fixes a test that was already failing. `surrounded_quantized_op_constant` in `misc/test_quantization.py` was marked as an expected failure. Its `torch.ones(2, 2)` constant is lowered to a `full` op, so the int8 add read float data, and every output element was off by about 0.49. With this change the case matches the quantized reference exactly, so its `xfail` is removed. The other constants in that file are stored tensors, which folding already quantized correctly.

Differential Revision: D128002647

@AdrianLundell AdrianLundell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Small nit, the full -> fill change does not seem strictly related to the compute constant ops AOT pass to me, maybe that should be it's own pass and we keep the arm passes as they are for clarity? LGTM otherwise

@pssrawat

Copy link
Copy Markdown
Contributor Author

Small nit, the full -> fill change does not seem strictly related to the compute constant ops AOT pass to me, maybe that should be it's own pass and we keep the arm passes as they are for clarity? LGTM otherwise

I'll do it as a followup

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 release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants