Skip to content

Commit 6cbdc03

Browse files
authored
Let callers gate permutability of extra elementwise ops (pytorch#22937)
Summary: RemovePermutesAroundElementwiseOps only reasons about per-tensor quantization. A numel-1 qparam broadcasts identically under any permutation, so it is left wired directly to the op; that is why the pass adds quantize_per_tensor / dequantize_per_tensor and not their per-channel counterparts. extra_permutable_ops bypasses that reasoning: it is unioned straight into the permutable set with no predicate, so a caller-supplied quantized op is treated as permutable regardless of its qparams. A per-channel qparam has numel > 1, misses the layout-invariant branch, and is handled as a constant input -- the pass inserts a compensating permute on it. That both moves an axis the pass does not remap and stops the qparam being a plain placeholder, which lowering requires. Add an optional target_filters mapping, mirroring the one PropagateSlice already takes, so a caller adding quantized ops can restrict them to the cases the pass actually handles. Authored with assistance from Claude Code. Differential Revision: D120724474 Pull Request resolved: pytorch#22937
1 parent a00e2a6 commit 6cbdc03

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎backends/transforms/remove_permutes_around_elementwise_ops.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# pyre-unsafe
99

10+
from collections.abc import Callable
1011
from dataclasses import dataclass, field
1112
from typing import cast
1213

@@ -18,6 +19,7 @@
1819
)
1920
from executorch.backends.transforms.permute_pass_utils import get_arg, set_arg
2021
from executorch.exir.dialects._ops import ops as exir_ops
22+
from executorch.exir.dialects.edge._ops import EdgeOpOverload
2123
from executorch.exir.pass_base import ExportPass, PassResult
2224

2325

@@ -71,7 +73,13 @@ class Subgraph:
7173
default_factory=set
7274
)
7375

74-
def __init__(self, extra_permutable_ops: set | None = None) -> None:
76+
def __init__(
77+
self,
78+
extra_permutable_ops: set | None = None,
79+
target_filters: (
80+
dict[EdgeOpOverload, Callable[[torch.fx.Node], bool]] | None
81+
) = None,
82+
) -> None:
7583
super().__init__()
7684
self._permutable_ops = {
7785
exir_ops.edge.aten.add.Tensor,
@@ -99,6 +107,13 @@ def __init__(self, extra_permutable_ops: set | None = None) -> None:
99107
pass
100108
if extra_permutable_ops:
101109
self._permutable_ops |= extra_permutable_ops
110+
# Per-op predicates gating permutability. The pass only reasons about
111+
# per-tensor quantization: a numel-1 qparam broadcasts identically under
112+
# any permutation, while a per-channel one is tied to an axis this pass
113+
# does not remap. Callers adding quantized ops supply a filter here.
114+
self._target_filters: dict[EdgeOpOverload, Callable[[torch.fx.Node], bool]] = (
115+
target_filters or {}
116+
)
102117
self._sq_unsq_cache: dict[torch.fx.Node, bool] = {}
103118
self._interleave_cache: dict[
104119
torch.fx.Node,
@@ -657,6 +672,9 @@ def _is_pointwise(target) -> bool:
657672
def is_node_permutable(self, node: torch.fx.Node) -> bool:
658673
if node.target in self._PAD_OPS and not self._is_constant_pad(node):
659674
return False
675+
target_filter = self._target_filters.get(node.target)
676+
if target_filter is not None and not target_filter(node):
677+
return False
660678
if node.target in self._permutable_ops:
661679
if node.target in (
662680
exir_ops.edge.aten.amax.default,

‎backends/transforms/test/test_permute_optimization_passes.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,50 @@ def test_permute_view_squeeze_elementwise_view_unsqueeze_permute(self) -> None:
922922
"RemovePermutesAcrossView",
923923
)
924924

925+
def test_target_filter_blocks_permutation(self) -> None:
926+
"""A target_filter that rejects a node keeps its permutes in place."""
927+
928+
def build() -> tuple[torch.fx.GraphModule, torch.Tensor]:
929+
builder = GraphBuilder()
930+
x_data = torch.randn(1, 128, 16)
931+
x = builder.placeholder("x", x_data)
932+
p1 = builder.call_operator(
933+
op=exir_ops.edge.aten.permute_copy.default, args=(x, [0, 2, 1])
934+
)
935+
mul = builder.call_operator(op=exir_ops.edge.aten.mul.Tensor, args=(p1, p1))
936+
p2 = builder.call_operator(
937+
op=exir_ops.edge.aten.permute_copy.default, args=(mul, [0, 2, 1])
938+
)
939+
builder.output([p2])
940+
return builder.get_graph_module(), x_data
941+
942+
# Baseline: with no filter the pass cancels both permutes.
943+
unfiltered = cast(PassResult, RemovePermutesAroundElementwiseOps()(build()[0]))
944+
self.assertEqual(
945+
count_node(
946+
unfiltered.graph_module, exir_ops.edge.aten.permute_copy.default
947+
),
948+
0,
949+
)
950+
951+
original, x_data = build()
952+
gm_before = copy.deepcopy(original)
953+
p = RemovePermutesAroundElementwiseOps(
954+
target_filters={exir_ops.edge.aten.mul.Tensor: lambda node: False}
955+
)
956+
result = cast(PassResult, p(original))
957+
958+
self.assertFalse(result.modified)
959+
self.assertEqual(
960+
count_node(result.graph_module, exir_ops.edge.aten.permute_copy.default), 2
961+
)
962+
validate_numerics(
963+
gm_before,
964+
result.graph_module,
965+
[x_data],
966+
"RemovePermutesAroundElementwiseOps",
967+
)
968+
925969
def test_4d_permute_squeeze_clamp_3d_permute(self) -> None:
926970
"""Cascade detector conv→LN boundary: permute_4D([0,3,1,2]) →
927971
view(squeeze) → hardtanh → permute_3D([0,2,1]).

0 commit comments

Comments
 (0)