Skip to content

Commit 86e9036

Browse files
Arm backend: Canonicalize view-chains (pytorch#22068)
Run FuseViewCopyTransformPass late in the Arm TOSA pipeline, after CanonicalizeViewCopyPermutePass and before constant-shape insertion, so redundant view_copy chains exposed by earlier canonicalization are collapsed before TOSA legalization. cc @digantdesai @freddan80 @per @zingo @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Oscar Andersson <oscar.andersson@arm.com>
1 parent 966bf8c commit 86e9036

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎backends/arm/_passes/arm_pass_manager.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ def _tosa_pipeline(
665665
# permute pairs they expose.
666666
RemoveNoopPass(),
667667
CanonicalizeViewCopyPermutePass(),
668+
# Fuse views again after permutes may have been replaced by views.
669+
FuseViewCopyTransformPass(),
668670
InsertConstShapesPass(),
669671
InsertDataLayoutCastsPass(),
670672
]

‎backends/arm/test/passes/test_fuse_view_copy.py‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
55

66

77
import torch
8+
from executorch.backends.arm._passes import ConvertPermuteSingletonToViewPass
89
from executorch.backends.arm.test import common
910
from executorch.backends.arm.test.tester.test_pipeline import PassPipeline
1011
from executorch.backends.transforms.fuse_view_copy import FuseViewCopyTransform
12+
from executorch.exir.dialects._ops import ops as exir_ops
13+
from executorch.exir.pass_base import ExportPass, PassResult
14+
15+
16+
_VIEW = exir_ops.edge.aten.view_copy.default
17+
_PERMUTE = exir_ops.edge.aten.permute_copy.default
18+
19+
20+
def _count_node(
21+
graph_module: torch.fx.GraphModule, target: torch.fx.node.Target
22+
) -> int:
23+
return sum(
24+
node.op == "call_function" and node.target == target
25+
for node in graph_module.graph.nodes
26+
)
27+
28+
29+
class _AssertAfterInitialFusePass(ExportPass):
30+
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
31+
assert _count_node(graph_module, _VIEW) == 1
32+
assert _count_node(graph_module, _PERMUTE) == 1
33+
return PassResult(graph_module, False)
34+
35+
36+
class _AssertAfterPermuteToViewPass(ExportPass):
37+
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
38+
assert _count_node(graph_module, _VIEW) == 2
39+
assert _count_node(graph_module, _PERMUTE) == 0
40+
return PassResult(graph_module, False)
1141

1242

1343
class FuseSequentialViews(torch.nn.Module):
@@ -62,6 +92,13 @@ def forward(self, x: torch.Tensor):
6292
}
6393

6494

95+
class FuseViewsIntroducedByLaterPass(torch.nn.Module):
96+
def forward(self, x: torch.Tensor):
97+
return x.view((1, 2, 1, 3, 4)).view((2, 1, 3, 4)).permute(0, 2, 3, 1)
98+
99+
data = (torch.randn(2, 1, 3, 4),)
100+
101+
65102
tests = {
66103
"fuse_sequential_views": FuseSequentialViews(),
67104
"fuse_sequential_with_noops_views": FuseSequentialWithNoopsViews(),
@@ -80,3 +117,30 @@ def test_fuse_view_copy_transform_tosa_FP(model):
80117
pass_list=[FuseViewCopyTransform],
81118
)
82119
pipeline.run()
120+
121+
122+
def test_fuse_view_copy_transform_runs_again_after_new_fusable_view_tosa_FP():
123+
model = FuseViewsIntroducedByLaterPass()
124+
pipeline = PassPipeline(
125+
model,
126+
model.data,
127+
quantize=False,
128+
ops_before_pass={
129+
"executorch_exir_dialects_edge__ops_aten_view_copy": 2,
130+
"executorch_exir_dialects_edge__ops_aten_permute_copy_default": 1,
131+
},
132+
ops_after_pass={
133+
"executorch_exir_dialects_edge__ops_aten_view_copy": 1,
134+
},
135+
ops_not_after_pass=[
136+
"executorch_exir_dialects_edge__ops_aten_permute_copy_default",
137+
],
138+
pass_list=[
139+
FuseViewCopyTransform,
140+
_AssertAfterInitialFusePass,
141+
ConvertPermuteSingletonToViewPass,
142+
_AssertAfterPermuteToViewPass,
143+
FuseViewCopyTransform,
144+
],
145+
)
146+
pipeline.run()

0 commit comments

Comments
 (0)