Skip to content

Conversation

martinlsm
Copy link
Collaborator

@martinlsm martinlsm commented Sep 10, 2025

Introduce a mechanism to enforce required ordering of passes in ArmPassManager. Each ArmPass must now declare which passes are required to run after it, ensuring ordering constraints are always upheld.

This prevents accidental breakage when modifying pass ordering in the manager.

Ordering constraints are verified by the new method ArmPass.validate_constraints_mandatory. We considered reusing torch.fx.passes.infra.pass_manager.PassManager.validate_constraints, but that utility only checks pairwise ordering and cannot enforce that a pass is actually run, which did not meet our needs.

This patch only implements the mechanism and tests for it. Defining the actual pass orderings are done in a later patch.

Test plan

The change comes with added unit tests in backends/arm/test/misc/test_pass_required_order.py

cc @digantdesai @freddan80 @per @zingo @oscarandersson8218

Copy link

pytorch-bot bot commented Sep 10, 2025

🔗 Helpful Links

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

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

❌ 3 New Failures, 2 Unrelated Failures

As of commit 243d006 with merge base 5fd66ee (image):

NEW FAILURES - The following jobs have 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 Sep 10, 2025
@martinlsm
Copy link
Collaborator Author

@pytorchbot label ciflow/trunk

@martinlsm
Copy link
Collaborator Author

@pytorchbot label "partner: arm"

@pytorch-bot pytorch-bot bot added the partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm label Sep 10, 2025
Copy link

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.

@martinlsm martinlsm force-pushed the marlin-organize-pass-manager branch from 132f407 to b9a0d81 Compare September 15, 2025 14:57
Returns the list of passes that must be run after this pass, sorted by name.
"""
if hasattr(pass_, "_passes_required_after"):
return sorted([ArmPass.get_name(p) for p in pass_._passes_required_after])
Copy link
Contributor

Choose a reason for hiding this comment

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

One issue I can see with this approach is transitive dependency and running into weird errors.

At a high level, do we see many cases where we run same pass multiple times in certain order for a given backend, or do something different, in terms of pass ordering, based on some flag or a model architecture?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not fully understanding the case of transitive dependencies. With this implementation, if we got the following scenario:

PassA:
    _passes_required_after = {PassB}

PassB:
    _passes_required_after = {PassC}

PassC:
    _passes_required_after = set()

Then PassA will transitively depend on PassC.

Then if create a pipeline where we remove PassB like this:

[
    PassA,
    PassC,
]

Then we will get the following error:

The following constraints for passes are not met:
    PassB must run after PassA

Is that not an reasonable way to handle it? I cannot think of a case where a missing transitive dependency will slip through here and not get caught by validate_constraints_mandatory.

Then regarding your second point: We have a long-term plan to change the passes such that they will always be added in the same order, but let themselves check whether they should run or not. This means that the order will always remain the same no matter which state/hardware profile we are in. Does that answer your question or maybe I'm not fully following?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is that not an reasonable way to handle it?

Yeah I guess. Thanks. I was also thinking like,

PassA --(needs)--> Pass B
PassB --(needs)--> Pass C
PassC --(needs)--> Pass A

More broadly,

let themselves check whether they should run or not

I am failing to see the gain from this added complexity over a list[Pass] for a given backend. Mainly because this is not a user facing surface.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you are saying we don't have to maintain "per backend + config list of passe" perhaps that is a good enough reason to embrace this complexity.. But I doubt if we can get rid of backend "specialized" logic from the PassManager but happy to be proven wrong :)

Copy link
Collaborator Author

@martinlsm martinlsm Sep 17, 2025

Choose a reason for hiding this comment

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

Thanks @digantdesai for the thorough review and feedback!

Regarding the cycles, yes, this implementation would not be able to handle that. But we also want to avoid those kind of loops, so having circular dependencies such as that is something we want to avoid. I cannot say it's 100% future proof but it's unlikely that we would ever need cyclic dependencies I'm thinking.

Regarding the added complexity, perhaps it adds a bit yes; the idea is to uphold intended ordering of the passes in cases where a developer wants to change the ordering. It's not obvious what should come before/after what when there's so many of them. One could say that the unit tests should protect against improper reordering, but this feature gives us quicker and clearer feedback in those cases.

I see your argument about special cases might work against this design. In the worst case we would have to revisit this feature and try to extend/change it somehow. But we do hope to eliminate special cases and always list the passes in the same order at least.

Copy link
Contributor

@digantdesai digantdesai Sep 17, 2025

Choose a reason for hiding this comment

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

_passes_required_after being empty for almost all existing passes implies that a simple (ordered) list is sufficient :)
I don't want to block you here, and since this is inside Arm dir, I will let you make the call. Thanks for the discussion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! I see your point, but the actual dependencies are added in a later patch that is on its way.

Copy link
Contributor

@digantdesai digantdesai left a comment

Choose a reason for hiding this comment

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

stamping to unblock from merging if/when ready. Thanks @martinlsm

AdrianLundell and others added 2 commits September 19, 2025 08:00
Introduce a mechanism to enforce required ordering of passes in
ArmPassManager. Each ArmPass must now declare which passes are required
to run after it, ensuring ordering constraints are always upheld.

This prevents accidental breakage when modifying pass ordering in the
manager.

Ordering constraints are verified by the new method
ArmPass.validate_constraints_mandatory. We considered reusing
torch.fx.passes.infra.pass_manager.PassManager.validate_constraints, but
that utility only checks pairwise ordering and cannot enforce that a
pass is actually run, which did not meet our needs.

This patch only implements the mechanism and tests for it. Defining the
actual pass orderings are done in a later patch.

Change-Id: I6f822ec4192b0c8dd19b70d85905adcd08ca502f
Signed-off-by: Adrian Lundell <adrian.lundell@arm.com>
Co-authored-by: Martin Lindström <martin.lindstroem@arm.com>
DecomposeLinearVectorNormPass is listed in both the
transform_for_annotation_pipeline and _tosa_INT_pipeline stages. The
latter is redundant because torch.linalg.vector_norm can only run with
floating point inputs, i.e., we should always be in a quantized setting
when we enter _tosa_INT_pipeline if the operator is present and
_transform_for_annotation_pipeline will always run; therefore, remove
DecomposeLinearVectorNormPass from _tosa_INT_pipeline.

Signed-off-by: Martin Lindström <Martin.Lindstroem@arm.com>
Change-Id: I647687b51298bbb98087914fbbee053436ffb79f
@martinlsm martinlsm force-pushed the marlin-organize-pass-manager branch from b9a0d81 to 243d006 Compare September 19, 2025 06:01
@zingo
Copy link
Collaborator

zingo commented Sep 19, 2025

Test fails seems unrelated

@zingo zingo merged commit 8da822c into pytorch:main Sep 19, 2025
267 of 272 checks passed
StrycekSimon pushed a commit to nxp-upstream/executorch that referenced this pull request Sep 23, 2025
Introduce a mechanism to enforce required ordering of passes in
ArmPassManager. Each ArmPass must now declare which passes are required
to run after it, ensuring ordering constraints are always upheld.

This prevents accidental breakage when modifying pass ordering in the
manager.

Ordering constraints are verified by the new method
ArmPass.validate_constraints_mandatory. We considered reusing
torch.fx.passes.infra.pass_manager.PassManager.validate_constraints, but
that utility only checks pairwise ordering and cannot enforce that a
pass is actually run, which did not meet our needs.

This patch only implements the mechanism and tests for it. Defining the
actual pass orderings are done in a later patch.

### Test plan
The change comes with added unit tests in
backends/arm/test/misc/test_pass_required_order.py


Signed-off-by: Adrian Lundell <adrian.lundell@arm.com>
Signed-off-by: Martin Lindström <Martin.Lindstroem@arm.com>
Co-authored-by: Adrian Lundell <adrian.lundell@arm.com>
Co-authored-by: Martin Lindström <martin.lindstroem@arm.com>
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants