Skip to content

Conversation

@Erik-Lundell
Copy link
Collaborator

@Erik-Lundell Erik-Lundell commented Nov 17, 2025

Arm backend: Support quantized while_loop

- Add annotation logic.
- Extend cond handling in q-dq folding to while
- Extend InsertCondRescale pass to handle while.

Arm backend: Add initial while_loop support.

- Refactor CondSupported to also test while, move to own file
  and split into one check for submodule nodes, and one for ops.
- Add node visitor
- Add tests

Arm backend: Initial quantization support for conditional

The standard prepare/convert_pt2 does not seem to support
quantization out of the box. Instead, a quantization call
is introduced in the TOSAQuantizer, that does the neccessary
steps to get correct quantization on submodules. A custom
Quantize step is needed in the ArmTester to make this work
in testing.

Additionally, getting correct quantization parameters needs
some delicate handling. The model is calibrated twice,
once for each code path. Because of this, the observers outside
the if/else submodules see different data than the observers
inside the submodules. Rescales need to be inserted to handle
this. To get a correctly traced graph at all times, we
  1. Fold the outmost quant ops in the submodules at the same
     time as the cond is folded. Add qparam meta to folded
     nodes inside submodule.
  2. Use this meta in the InsertCondRescale pass to
     insert a tosa.RESCALE to handle the different qparams.
  3. After this, the submodule's q-dq nodes can be folded normally.

cc @freddan80 @per @zingo @oscarandersson8218 @digantdesai

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 17, 2025

🔗 Helpful Links

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

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

❌ 1 New Failure, 3 Unrelated Failures

As of commit 332375f with merge base 9952aef (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Nov 17, 2025
@Erik-Lundell Erik-Lundell added partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm ciflow/trunk release notes: arm Changes to the ARM backend delegate and removed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Nov 17, 2025
@Erik-Lundell
Copy link
Collaborator Author

Erik-Lundell commented Nov 17, 2025

@metascroy Hey Scott, this PR is rather large so probably best for you to test internally first. 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 Nov 17, 2025
@mergennachin mergennachin requested a review from Copilot November 17, 2025 16:30
Copilot finished reviewing on behalf of mergennachin November 17, 2025 16:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for quantized while_loop and cond operations to the Arm backend. The implementation extends the TOSA backend to handle control flow constructs with proper quantization handling through specialized passes and annotation logic.

Key changes:

  • Introduces while_loop operator support with validation, node visitors, and tests
  • Adds quantization infrastructure for control flow operations via InsertControlFlowRescalesPass and updated annotation logic
  • Refactors control flow support into dedicated module (control_flow_support.py) for better code organization

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
backends/arm/tosa/partitioner.py Renames get_control_flow_submodules to get_cond_while_submodules
backends/arm/tosa/backend.py Adds containing_graph_module parameter and output node normalization
backends/arm/test/tester/test_pipeline.py Switches to custom ArmQuantize class
backends/arm/test/tester/quantize.py New custom quantize stage for Arm-specific quantization with submodule support
backends/arm/test/tester/arm_tester.py Updates quantize stage to use ArmQuantize
backends/arm/test/ops/test_while.py New comprehensive test suite for while_loop operations
backends/arm/test/ops/test_cond.py Adds tests for quantized conditionals and new test cases
backends/arm/scripts/parse_test_names.py Adds while_loop.default to custom edge ops list
backends/arm/quantizer/quantization_annotator.py Extends annotation logic to handle cond and while_loop operations
backends/arm/quantizer/arm_quantizer.py Adds quantize_with_submodules method for proper submodule quantization
backends/arm/process_node.py Handles placeholder processing for submodule inputs
backends/arm/operators/operator_validation_utils.py Adds reusable control flow extension validation
backends/arm/operators/op_while.py New visitor for while_loop operator serialization
backends/arm/operators/op_cond_if.py Refactors to use shared validation utility
backends/arm/operators/init.py Imports new op_while module
backends/arm/operator_support/tosa_supported_operators.py Moves control flow support to dedicated module
backends/arm/operator_support/control_flow_support.py New dedicated module for control flow partitioning logic
backends/arm/operator_support/init.py Imports new control flow support module
backends/arm/_passes/insert_rescales_pass.py Adds InsertControlFlowRescalesPass for quantization parameter handling
backends/arm/_passes/fold_qdq_with_annotated_qparams_pass.py Extends to handle control flow node quantization folding
backends/arm/_passes/arm_pass_manager.py Integrates new control flow rescale pass
backends/arm/_passes/init.py Exports new control flow rescale pass
Comments suppressed due to low confidence (1)

backends/arm/process_node.py:249

  • Unnecessary 'pass' statement.
        pass

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 22 out of 22 changed files in this pull request and generated 11 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@metascroy
Copy link
Contributor

@metascroy Hey Scott, this PR is rather large so probably best for you to test internally first. Thanks!

Importing now

@metascroy
Copy link
Contributor

I need to wait for the previous Arm PRs to commit to the internal code base before importing this one due to merge conflicts. They are landing right now (#15828, #15869).

The standard prepare/convert_pt2 does not seem to support
quantization out of the box. Instead, a quantization call
is introduced in the TOSAQuantizer, that does the neccessary
steps to get correct quantization on submodules. A custom
Quantize step is needed in the ArmTester to make this work
in testing.

Additionally, getting correct quantization parameters needs
some delicate handling. The model is calibrated twice,
once for each code path. Because of this, the observers outside
the if/else submodules see different data than the observers
inside the submodules. Rescales need to be inserted to handle
this. To get a correctly traced graph at all times, we
  1. Fold the outmost quant ops in the submodules at the same
     time as the cond is folded. Add qparam meta to folded
     nodes inside submodule.
  2. Use this meta in the InsertCondRescale pass to
     insert a tosa.RESCALE to handle the different qparams.
  3. After this, the submodule's q-dq nodes can be folded normally.

Signed-off-by: Erik Lundell <[email protected]>
Change-Id: Ia37c4478a661d1156f537ca6d4e1e17f0f130d24
- Refactor CondSupported to also test while, move to own file
  and split into one check for submodule nodes, and one for ops.
- Add node visitor
- Add tests
Signed-off-by: Erik Lundell <[email protected]>
Change-Id: I932c2065fe3e9ff8956bc336c62624522d9ba0f0
- Add annotation logic.
- Extend cond handling in q-dq folding to while
- Extend InsertCondRescale pass to handle while.

Signed-off-by: Erik Lundell <[email protected]>
Change-Id: Ia3b2ad04a2d1c298f09b0d15a5e6969ed46db66b
Signed-off-by: Erik Lundell <[email protected]>
Change-Id: I89b908d821b3d718fc86597fad95a32b12097bdc
@Erik-Lundell
Copy link
Collaborator Author

I need to wait for the previous Arm PRs to commit to the internal code base before importing this one due to merge conflicts. They are landing right now (#15828, #15869).

Strange, I rebased now and did not get any merge conflicts. Anyways, just pushed a rebased version + fixes from copilot's review.

@metascroy
Copy link
Contributor

I need to wait for the previous Arm PRs to commit to the internal code base before importing this one due to merge conflicts. They are landing right now (#15828, #15869).

Strange, I rebased now and did not get any merge conflicts. Anyways, just pushed a rebased version + fixes from copilot's review.

It's not the OSS code base that had a merge conflict, but the internal codebase (which can be 10-20 commits behind main in the OSS one due to the import process). Anyway, I was able to import this morning and tests are running.

For my own reference: D87349540

@metascroy
Copy link
Contributor

The internal tests pass on this, so I think we're safe to merge

@meta-codesync
Copy link

meta-codesync bot commented Nov 19, 2025

@metascroy has imported this pull request. If you are a Meta employee, you can view this in D87349540.

@zingo
Copy link
Collaborator

zingo commented Nov 20, 2025

Fails are unrelated to Arm changes in this PR

@zingo zingo merged commit ee7e95e into pytorch:main Nov 20, 2025
297 of 301 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm release notes: arm Changes to the ARM backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants