1212
1313from executorch .backends .arm ._passes .arm_pass_utils import (
1414 create_node ,
15+ expand_around_channel ,
1516 get_first_fake_tensor ,
1617 get_param_tensor ,
1718 is_buffer ,
2930from torch .export .graph_signature import InputKind
3031
3132
32- class RewriteConv2dPass (ArmPass ):
33+ class RewriteConvPass (ArmPass ):
3334 """Rewrites aten.convolution to tosa.CONV2D or tosa.DEPTHWISE_CONV2D."""
3435
3536 def __init__ (self , exported_program : torch .export .ExportedProgram ):
@@ -88,11 +89,27 @@ def _is_depthwise_conv2d(self, node: torch.fx.Node) -> bool:
8889 or node .target != exir_ops .edge .aten .convolution .default
8990 ):
9091 return False
92+ input_tensor = get_first_fake_tensor (node .all_input_nodes [0 ])
93+ if len (input_tensor .shape ) != 4 :
94+ return False
9195 groups = node .args [- 1 ]
92- in_channels = get_first_fake_tensor ( node . all_input_nodes [ 0 ]) .shape [1 ]
96+ in_channels = input_tensor .shape [1 ]
9397 out_channels = get_first_fake_tensor (node ).shape [1 ]
9498 return (in_channels == groups ) and (out_channels % in_channels ) == 0
9599
100+ def _is_conv3d (self , rank , groups ) -> bool :
101+ if rank == 5 :
102+ # A Conv3D is considered depthwise if Group == InChannels and
103+ # Group * N == OutChannels, where N is a possitive integer.
104+ # Currently we do not support depthwise or grouped conv3d.
105+ # @TODO Add grouped/depthwise conv3d support or reject in partitioner.
106+ if groups != 1 :
107+ raise RuntimeError (
108+ "CONV3D with groups != 1 is not supported in the Arm backend."
109+ )
110+ return True
111+ return False
112+
96113 def _reshape_weights (self , weight_node : torch .fx .Node , in_channels : int ) -> None :
97114 """Reshape the weights for depthwise convolution such that when serialized to TOSA,
98115 the weights are in the format [H, W, in_channels, m_length] where
@@ -201,7 +218,7 @@ def insert_output_rescale(self, graph_module, node):
201218 )
202219 return rescale_node
203220
204- def call (self , graph_module : torch .fx .GraphModule ) -> PassResult :
221+ def call (self , graph_module : torch .fx .GraphModule ) -> PassResult : # noqa: C901
205222 modified = False
206223 for node in graph_module .graph .nodes :
207224 if (
@@ -224,30 +241,40 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
224241 group ,
225242 ) = node .args
226243
227- pad = [val for val in pad for _ in (0 , 1 )]
228244 input_fake_tensor = get_first_fake_tensor (x )
229245 weight_fake_tensor = get_first_fake_tensor (weight )
230- # Adjust the pad value if needed to meet the
231- # strict convolution output shape calculation.
232- pad [1 ] = self ._adjust_pad_if_needed (
233- input_fake_tensor .shape [2 ],
234- weight_fake_tensor .shape [2 ],
235- stride [0 ],
236- pad [1 ],
237- dilation [0 ],
238- )
239- pad [3 ] = self ._adjust_pad_if_needed (
240- input_fake_tensor .shape [3 ],
241- weight_fake_tensor .shape [3 ],
242- stride [1 ],
243- pad [3 ],
244- dilation [1 ],
245- )
246+ input_shape = input_fake_tensor .shape
247+ weight_shape = weight_fake_tensor .shape
248+ spatial_rank = len (input_shape ) - 2
249+ stride_list = expand_around_channel (stride , spatial_rank )
250+ dilation_list = expand_around_channel (dilation , spatial_rank )
251+ pad_list = expand_around_channel (pad , spatial_rank )
252+
253+ pad_attr : list [int ] = []
254+ for value in pad_list :
255+ pad_attr .extend ([value , value ]) # duplicate pad before/after per axis
256+
257+ for axis_index in range (spatial_rank ):
258+ pad_index = axis_index * 2 + 1 # adjust trailing pad entry
259+ pad_attr [pad_index ] = self ._adjust_pad_if_needed (
260+ input_shape [axis_index + 2 ],
261+ weight_shape [axis_index + 2 ],
262+ stride_list [axis_index ],
263+ pad_attr [pad_index ],
264+ dilation_list [axis_index ],
265+ )
266+
267+ stride = tuple (stride_list )
268+ dilation = tuple (dilation_list )
269+ pad = pad_attr
270+
246271 has_bias = bias is not None
247272 if not has_bias :
248273 bias = self ._add_bias (graph_module , node , weight )
249274
250- if self ._is_depthwise_conv2d (node ):
275+ if self ._is_conv3d (len (input_shape ), group ):
276+ target_op = exir_ops .backend .tosa .CONV3D .default
277+ elif self ._is_depthwise_conv2d (node ):
251278 target_op = exir_ops .backend .tosa .DEPTHWISE_CONV2D .default
252279 # If there are any TOSA.DEPTHWISE_CONV2D nodes using the weights, we've already reshaped them.
253280 if all (user .target != target_op for user in weight .users ):
@@ -256,7 +283,7 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
256283 else :
257284 target_op = exir_ops .backend .tosa .CONV2D .default
258285
259- conv2d_args = (
286+ conv_args = (
260287 x ,
261288 weight ,
262289 bias ,
@@ -272,7 +299,7 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
272299 tosa_op = create_node (
273300 graph = graph_module .graph ,
274301 op_target = target_op ,
275- args = conv2d_args ,
302+ args = conv_args ,
276303 from_node = node ,
277304 inherit_qparams = True ,
278305 )
@@ -281,7 +308,7 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
281308 input_fake_tensor ,
282309 weight_fake_tensor ,
283310 bias_fake_tensor ,
284- * conv2d_args [3 :],
311+ * conv_args [3 :],
285312 )
286313
287314 if (
0 commit comments