|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from copy import copy |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | +from executorch.exir.pass_base import ExportPass |
| 11 | + |
| 12 | + |
| 13 | +class DecomposeGroupedConv(ExportPass): |
| 14 | + """ |
| 15 | + Splits a grouped convolution which is not supported by TOSA into multiple |
| 16 | + convolutions using slice->conv->cat. |
| 17 | +
|
| 18 | + Before pass: |
| 19 | + x = conv(input, weight, bias, groups = 2) |
| 20 | +
|
| 21 | + After pass: |
| 22 | + input1 = slice(input) |
| 23 | + weight1 = slice(weight) |
| 24 | + bias1 = slice(bias) |
| 25 | + x1 = conv(input1, weight1, bias1) |
| 26 | +
|
| 27 | + input2 = slice(input) |
| 28 | + weight2 = slice(weight) |
| 29 | + bias2 = slice(bias) |
| 30 | + x2 = conv(input2, weight2, bias2) |
| 31 | +
|
| 32 | + x = cat(x1, x2) |
| 33 | + """ |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def _get_decomposition(op): |
| 37 | + match op: |
| 38 | + case exir_ops.edge.aten.convolution.default: |
| 39 | + return ( |
| 40 | + exir_ops.edge.aten.slice_copy.Tensor, |
| 41 | + exir_ops.edge.aten.convolution.default, |
| 42 | + exir_ops.edge.aten.cat.default, |
| 43 | + ) |
| 44 | + case torch.ops.aten.conv2d.default: |
| 45 | + return ( |
| 46 | + torch.ops.aten.slice_copy.Tensor, |
| 47 | + torch.ops.aten.conv2d.default, |
| 48 | + torch.ops.aten.cat.default, |
| 49 | + ) |
| 50 | + case _: |
| 51 | + raise RuntimeError("Unvalid op for grouped conv decomposition.") |
| 52 | + |
| 53 | + def call_operator(self, op, args, kwargs, meta): |
| 54 | + if op == exir_ops.edge.aten.convolution.default: |
| 55 | + groups = args[8] |
| 56 | + transposed = args[6] |
| 57 | + elif op == torch.ops.aten.conv2d.default: |
| 58 | + groups = args[6] |
| 59 | + transposed = False |
| 60 | + else: |
| 61 | + return super().call_operator(op, args, kwargs, meta) |
| 62 | + |
| 63 | + if groups == 1 or transposed: |
| 64 | + return super().call_operator(op, args, kwargs, meta) |
| 65 | + |
| 66 | + input_node = args[0] |
| 67 | + if input_node.data.shape[1] == groups: |
| 68 | + # This is a depthwise convolution which is handled elsewhere |
| 69 | + return super().call_operator(op, args, kwargs, meta) |
| 70 | + |
| 71 | + weight_node = args[1] |
| 72 | + bias_node = args[2] |
| 73 | + |
| 74 | + input_slice_size = weight_node.data.shape[1] |
| 75 | + output_slice_size = weight_node.data.shape[0] // groups |
| 76 | + |
| 77 | + no_q_dq_meta = copy(meta) |
| 78 | + no_q_dq_meta.data = {} |
| 79 | + no_q_dq_meta.data = {} |
| 80 | + |
| 81 | + slice_op, conv_op, cat_op = DecomposeGroupedConv._get_decomposition(op) |
| 82 | + |
| 83 | + input_slices = [] |
| 84 | + for i in range(groups): |
| 85 | + start_index = i * input_slice_size |
| 86 | + stop_index = (i + 1) * input_slice_size |
| 87 | + slice_args = (input_node, 1, start_index, stop_index) |
| 88 | + |
| 89 | + input_slices.append( |
| 90 | + super().call_operator(slice_op, slice_args, kwargs, no_q_dq_meta) |
| 91 | + ) |
| 92 | + |
| 93 | + filter_slices = [] |
| 94 | + for i in range(groups): |
| 95 | + start_index = i * output_slice_size |
| 96 | + stop_index = (i + 1) * output_slice_size |
| 97 | + slice_args = (weight_node, 0, start_index, stop_index) |
| 98 | + |
| 99 | + filter_slices.append( |
| 100 | + super().call_operator(slice_op, slice_args, kwargs, no_q_dq_meta) |
| 101 | + ) |
| 102 | + |
| 103 | + bias_slices = [] |
| 104 | + for i in range(groups): |
| 105 | + if bias_node is None: |
| 106 | + bias_slices.append(None) |
| 107 | + else: |
| 108 | + |
| 109 | + start_index = i * output_slice_size |
| 110 | + stop_index = (i + 1) * output_slice_size |
| 111 | + slice_args = (bias_node, 0, start_index, stop_index) |
| 112 | + |
| 113 | + bias_slices.append( |
| 114 | + super().call_operator(slice_op, slice_args, kwargs, no_q_dq_meta) |
| 115 | + ) |
| 116 | + |
| 117 | + output_slices = [] |
| 118 | + for input_slice, filter_slice, bias_slice in zip( |
| 119 | + input_slices, filter_slices, bias_slices |
| 120 | + ): |
| 121 | + |
| 122 | + if op == exir_ops.edge.aten.convolution.default: |
| 123 | + conv_args = (input_slice, filter_slice, bias_slice, *args[3:8], 1) |
| 124 | + elif op == torch.ops.aten.conv2d.default: |
| 125 | + conv_args = (input_slice, filter_slice, bias_slice, *args[3:6], 1) |
| 126 | + else: |
| 127 | + raise RuntimeError("Unvalid op for grouped conv decomposition.") |
| 128 | + |
| 129 | + output_slices.append( |
| 130 | + super().call_operator(conv_op, conv_args, kwargs, meta) |
| 131 | + ) |
| 132 | + |
| 133 | + cat_args = (output_slices, 1) |
| 134 | + return super().call_operator(cat_op, cat_args, kwargs, no_q_dq_meta) |
0 commit comments