|
5 | 5 | # This source code is licensed under the BSD-style license found in the |
6 | 6 | # LICENSE file in the root directory of this source tree. |
7 | 7 |
|
8 | | - |
9 | 8 | from typing import Set, Type |
10 | 9 |
|
11 | | -from executorch.backends.arm._passes import ArmOpTargetedPass |
12 | | - |
| 10 | +from executorch.backends.arm._passes.convert_squeezes_to_view import ( |
| 11 | + ConvertSqueezesToViewPass, |
| 12 | +) |
13 | 13 | from executorch.backends.arm._passes.rewrite_conv_pass import RewriteConvPass |
14 | 14 | 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 | +) |
17 | 18 | from executorch.exir.pass_base import ExportPass |
18 | | -from torch.fx import GraphModule |
19 | | - |
20 | 19 |
|
21 | | -class Conv1dUnsqueezePass(ArmOpTargetedPass): |
22 | | - """This pass is used to change conv1d ops into conv2d since TOSA only |
23 | | - supports 2d and 3d convolution. |
24 | 20 |
|
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.""" |
33 | 23 |
|
34 | 24 | _passes_required_after: Set[Type[ExportPass]] = { |
| 25 | + ConvertSqueezesToViewPass, |
35 | 26 | RewriteConvPass, |
36 | 27 | SizeAdjustInputPass, |
37 | 28 | } |
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 |
0 commit comments