-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add partitioner pre-decomposition transform hook #22141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
YufengShi-dudu
merged 2 commits into
pytorch:main
from
YufengShi-dudu:add-partitioner-pre-decomposition-hook
Aug 27, 2026
+91
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright 2026 Arm Limited and/or its affiliates. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import torch | ||
| from executorch.exir import to_edge_transform_and_lower | ||
| from executorch.exir.backend.partitioner import Partitioner, PartitionResult | ||
| from torch.export import export, ExportedProgram | ||
|
|
||
|
|
||
| class _SDPA(torch.nn.Module): | ||
| def forward( | ||
| self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor | ||
| ) -> torch.Tensor: | ||
| return torch.nn.functional.scaled_dot_product_attention(query, key, value) | ||
|
|
||
|
|
||
| class _RecordingPartitioner(Partitioner): | ||
| def __init__(self, name: str, calls: list[str]) -> None: | ||
| super().__init__() | ||
| self.name = name | ||
| self.calls = calls | ||
| self.saw_sdpa = False | ||
|
|
||
| def transform_for_pre_decomposition( | ||
| self, exported_program: ExportedProgram | ||
| ) -> ExportedProgram: | ||
| self.calls.append(self.name) | ||
| self.saw_sdpa = any( | ||
| node.target == torch.ops.aten.scaled_dot_product_attention.default | ||
| for node in exported_program.graph.nodes | ||
| ) | ||
| return exported_program | ||
|
|
||
| def partition(self, exported_program: ExportedProgram) -> PartitionResult: | ||
| return PartitionResult(exported_program, {}) | ||
|
|
||
|
|
||
| def test_partitioner_transforms_run_before_decomposition_in_order() -> None: | ||
| inputs = tuple(torch.randn(1, 3, 4, 5) for _ in range(3)) | ||
| exported_program = export(_SDPA(), inputs, strict=True) | ||
| calls: list[str] = [] | ||
| first = _RecordingPartitioner("first", calls) | ||
| second = _RecordingPartitioner("second", calls) | ||
|
|
||
| to_edge_transform_and_lower( | ||
| exported_program, | ||
| partitioner=[first, second], | ||
| ) | ||
|
|
||
| assert calls == ["first", "second"] | ||
| assert first.saw_sdpa | ||
| assert second.saw_sdpa |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC do_not_decomp will complain if a partitioner asked for something to be not decomposed and then it didn't partition, is this true for this as well? I can imagine that as a good thing if we are planning to do some transforms here in this pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your comment. No, we don't record the same info to check whether the generated ops remain undelegated after the following decomposition and partitioning. Tracking them may require a new metedata field.
transform_for_annotation_pipeline() also applies transforms without tracking generated nodes. Do you think we should introduce such tracking for this hook?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah quantizer being backend specific yet no direct enforcement for its actions is something I am not very happy about.
The reason I am a bit nervous this is, now we are modifying the graph in the paritioner and without any consequences if this hook misbehaves.
That said, with your hook, since its a pass, writing a '_sanity_check_graph_for_non_decomp_ops` like fn can get tricky if multiple passes or even multiple partitioners have worked on the graph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok let's hope this doesn't get out of control, I will stamp this as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, we need to be more cautious when adding new passes to this hook.