Skip to content

Commit 15495a5

Browse files
authored
Arm backend: Use shared Conv1d-to-Conv2d transform in Arm backend (pytorch#21975)
### Summary Replace the Arm-specific conversion logic with a thin wrapper around the shared transform while preserving Arm pass-order constraints. Handle the squeeze overload emitted by the shared transform and add dynamic activation-shape coverage. ### Test plan Tests added in PR Signed-off-by: Zingo Andersen <Zingo.Andersen@arm.com>
1 parent 8d21053 commit 15495a5

5 files changed

Lines changed: 32 additions & 95 deletions

File tree

‎backends/arm/_passes/BUCK‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ fbcode_target(
5959
"//executorch/backends/transforms:aten_to_dialect_pass",
6060
"//executorch/backends/arm/tosa:utils",
6161
"//executorch/backends/arm/tosa/dialect:lib",
62+
"//executorch/backends/transforms:convert_conv1d_to_conv2d_pass",
6263
"//executorch/backends/transforms:fuse_view_copy",
6364
"//executorch/backends/transforms:remove_getitem_op",
6465
"//executorch/backends/transforms:replace_scalar_with_tensor",

‎backends/arm/_passes/arm_pass_manager.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def _tosa_pipeline(
578578
DecomposeAdaptiveAvgPool2dPass(),
579579
DecomposeDynamicAdaptiveAvgPool2dPass(),
580580
DecomposeAvgPool2dPass(),
581-
Conv1dUnsqueezePass(),
581+
Conv1dUnsqueezePass(exported_program),
582582
]
583583
)
584584

‎backends/arm/_passes/conv1d_unsqueeze_pass.py‎

Lines changed: 9 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,109 +5,24 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
98
from typing import Set, Type
109

11-
from executorch.backends.arm._passes import ArmOpTargetedPass
12-
10+
from executorch.backends.arm._passes.convert_squeezes_to_view import (
11+
ConvertSqueezesToViewPass,
12+
)
1313
from executorch.backends.arm._passes.rewrite_conv_pass import RewriteConvPass
1414
from executorch.backends.arm._passes.size_adjust_input_pass import SizeAdjustInputPass
15-
16-
from executorch.exir.dialects._ops import ops as exir_ops
15+
from executorch.backends.transforms.convert_conv1d_to_conv2d_pass import (
16+
ConvertConv1dToConv2dPass,
17+
)
1718
from executorch.exir.pass_base import ExportPass
18-
from torch.fx import GraphModule
19-
2019

21-
class Conv1dUnsqueezePass(ArmOpTargetedPass):
22-
"""This pass is used to change conv1d ops into conv2d since TOSA only
23-
supports 2d and 3d convolution.
2420

25-
This is done by modifying the graph to do the
26-
following:
27-
1a) unsqueeze the convolution's input from 3d to 4d
28-
1b) unsqueeze the convolution's weight from 3d to 4d
29-
2) perform a conv2d (with a modified version of the original conv1d args)
30-
3) squeeze the output back down to 3d.
31-
32-
"""
21+
class Conv1dUnsqueezePass(ConvertConv1dToConv2dPass):
22+
"""Arm wrapper for the shared Conv1d-to-Conv2d transform."""
3323

3424
_passes_required_after: Set[Type[ExportPass]] = {
25+
ConvertSqueezesToViewPass,
3526
RewriteConvPass,
3627
SizeAdjustInputPass,
3728
}
38-
target_ops = (exir_ops.edge.aten.convolution.default,)
39-
40-
def should_run_pass(self, graph_module: GraphModule) -> bool:
41-
for node in graph_module.graph.nodes:
42-
if node.op != "call_function" or node.target not in self.target_ops:
43-
continue
44-
if len(node.args) > 3 and len(node.args[3]) == 1:
45-
return True
46-
return any(
47-
isinstance(child, GraphModule) and self.should_run_pass(child)
48-
for child in graph_module.children()
49-
)
50-
51-
def call_operator(self, op, args, kwargs, meta):
52-
if op not in self.target_ops:
53-
return super().call_operator(op, args, kwargs, meta)
54-
stride = list(args[3])
55-
if len(stride) != 1:
56-
return super().call_operator(op, args, kwargs, meta)
57-
58-
x_meta = meta.copy()
59-
x_meta.data["input_qparams"] = {}
60-
x_meta.data["output_qparams"] = {}
61-
62-
x = args[0]
63-
x_unsqueezed_shape = list(x.data.shape[:-1]) + [1] + [x.data.shape[-1]]
64-
x = super().call_operator(
65-
exir_ops.edge.aten.view_copy.default,
66-
(x, x_unsqueezed_shape),
67-
{},
68-
x_meta,
69-
updated=True,
70-
)
71-
72-
w_meta = meta.copy()
73-
w_meta.data["input_qparams"] = {}
74-
w_meta.data["output_qparams"] = {}
75-
76-
w = args[1]
77-
w_unsqueezed_shape = list(w.data.shape[:-1]) + [1] + [w.data.shape[-1]]
78-
w = super().call_operator(
79-
exir_ops.edge.aten.view_copy.default,
80-
(w, w_unsqueezed_shape),
81-
{},
82-
w_meta,
83-
updated=True,
84-
)
85-
86-
new_args = (
87-
x,
88-
w,
89-
args[2],
90-
[1] + args[3], # stride
91-
[0] + args[4], # padding
92-
[1] + args[5], # dilation
93-
args[6],
94-
[0] + args[7],
95-
args[8],
96-
)
97-
x = super().call_operator(
98-
exir_ops.edge.aten.convolution.default, new_args, kwargs, meta, updated=True
99-
)
100-
101-
x_squeezed_meta = meta.copy()
102-
x_squeezed_meta.data["input_qparams"] = {}
103-
x_squeezed_meta.data["output_qparams"] = {}
104-
x_squeezed_shape = list(x.data.shape[:-2]) + [x.data.shape[-1]]
105-
x = super().call_operator(
106-
exir_ops.edge.aten.view_copy.default,
107-
(x, x_squeezed_shape),
108-
{},
109-
x_squeezed_meta,
110-
updated=True,
111-
)
112-
113-
return x

‎backends/arm/_passes/convert_squeezes_to_view.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ConvertSqueezesToViewPass(ArmOpTargetedPass):
2424

2525
_passes_required_after: Set[Type[ExportPass]] = {FuseViewCopyTransformPass}
2626
target_ops = (
27+
exir_ops.edge.aten.squeeze_copy.dim,
2728
exir_ops.edge.aten.squeeze_copy.dims,
2829
exir_ops.edge.aten.unsqueeze_copy.default,
2930
)

‎backends/transforms/test/test_convert_conv1d_to_conv2d_pass.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
@@ -148,6 +149,25 @@ def test_convert_stride_and_dilation():
148149
assert conv.args[3:6] == ([1, 3], [0, 2], [1, 2])
149150

150151

152+
def test_convert_dynamic_activation_shapes():
153+
model = Conv1d().eval()
154+
example_input = torch.randn(2, 2, 8)
155+
batch = torch.export.Dim("batch", min=1, max=4)
156+
length = torch.export.Dim("length", min=4, max=32)
157+
edge = to_edge(
158+
torch.export.export(
159+
model,
160+
(example_input,),
161+
dynamic_shapes={"x": {0: batch, 2: length}},
162+
)
163+
).exported_program()
164+
165+
converted = _transform(edge, ConvertConv1dToConv2dPass(edge))
166+
runtime_input = torch.randn(3, 2, 17)
167+
168+
torch.testing.assert_close(converted.module()(runtime_input), model(runtime_input))
169+
170+
151171
def test_convert_transposed_conv1d():
152172
converted = _convert_and_compare(ConvTranspose1d(), torch.randn(1, 2, 8))
153173
[conv] = [

0 commit comments

Comments
 (0)