|  | 
|  | 1 | +# Copyright 2024 Arm Limited and/or its affiliates. | 
|  | 2 | +# All rights reserved. | 
|  | 3 | +# | 
|  | 4 | +# This source code is licensed under the BSD-style license found in the | 
|  | 5 | +# LICENSE file in the root directory of this source tree. | 
|  | 6 | + | 
|  | 7 | +import numpy as np | 
|  | 8 | +from executorch.backends.arm._passes.arm_pass_utils import ( | 
|  | 9 | +    create_node, | 
|  | 10 | +    get_first_fake_tensor, | 
|  | 11 | +) | 
|  | 12 | +from executorch.backends.arm.tosa_quant_utils import dq_op, q_op | 
|  | 13 | +from executorch.exir.dialects._ops import ops as exir_ops | 
|  | 14 | +from executorch.exir.pass_base import ExportPass, PassResult | 
|  | 15 | + | 
|  | 16 | + | 
|  | 17 | +class DecomposeLinearPass(ExportPass): | 
|  | 18 | +    """ | 
|  | 19 | +    This pass decomposes linear into a Conv2D with the required view operations. | 
|  | 20 | +    linear(x, weights, bias) becomes: | 
|  | 21 | +        x_reshaped       = view(x) | 
|  | 22 | +        weights_reshaped = view(weights) | 
|  | 23 | +        conv2d           = conv2d(x_reshaped, weights_reshaped, bias) | 
|  | 24 | +        output           = view(conv2d) | 
|  | 25 | +    It also inserts q/dq pairs if the linear node was quantized. | 
|  | 26 | +    """ | 
|  | 27 | + | 
|  | 28 | +    def call(self, graph_module): | 
|  | 29 | +        for node in graph_module.graph.nodes: | 
|  | 30 | +            if node.op != "call_function": | 
|  | 31 | +                continue | 
|  | 32 | +            if node.target != exir_ops.edge.aten.linear.default: | 
|  | 33 | +                continue | 
|  | 34 | +            args = node.args | 
|  | 35 | +            input = args[0] | 
|  | 36 | +            weights = args[1] | 
|  | 37 | +            bias = args[2] if len(args) > 2 else None | 
|  | 38 | +            output_shape = get_first_fake_tensor(node).shape | 
|  | 39 | +            input_shape = get_first_fake_tensor(input).shape | 
|  | 40 | +            weights_shape = get_first_fake_tensor(weights).shape | 
|  | 41 | +            batches = int(np.prod(input_shape[:-1])) if len(input_shape) > 1 else 1 | 
|  | 42 | +            # input has shape (..., Ci) | 
|  | 43 | +            input_reshaped_shape = [batches, input_shape[-1], 1, 1] | 
|  | 44 | +            # weights have shape (Co, Ci) | 
|  | 45 | +            weights_reshaped_shape = [weights_shape[0], weights_shape[1], 1, 1] | 
|  | 46 | + | 
|  | 47 | +            with graph_module.graph.inserting_before(node): | 
|  | 48 | +                quantize = input.op == "call_function" and input.target == dq_op | 
|  | 49 | +                q_params = input.args[1:] if quantize else None | 
|  | 50 | +                # Reshape input to 4D with shape (N, Ci, 1, 1) | 
|  | 51 | +                input_reshaped = create_node( | 
|  | 52 | +                    graph=graph_module.graph, | 
|  | 53 | +                    op_target=exir_ops.edge.aten.view_copy.default, | 
|  | 54 | +                    args=(input, input_reshaped_shape), | 
|  | 55 | +                    kwargs={}, | 
|  | 56 | +                    quantize=quantize, | 
|  | 57 | +                    q_params=q_params, | 
|  | 58 | +                ) | 
|  | 59 | + | 
|  | 60 | +                quantize = weights.op == "call_function" and weights.target == dq_op | 
|  | 61 | +                q_params = weights.args[1:] if quantize else None | 
|  | 62 | +                # Reshape weights to 4D with shape (Co, Ci, 1, 1) | 
|  | 63 | +                weights_reshaped = create_node( | 
|  | 64 | +                    graph=graph_module.graph, | 
|  | 65 | +                    op_target=exir_ops.edge.aten.view_copy.default, | 
|  | 66 | +                    args=(weights, weights_reshaped_shape), | 
|  | 67 | +                    kwargs={}, | 
|  | 68 | +                    quantize=quantize, | 
|  | 69 | +                    q_params=q_params, | 
|  | 70 | +                ) | 
|  | 71 | + | 
|  | 72 | +                consumer_node = list(node.users)[0] | 
|  | 73 | +                quantize = ( | 
|  | 74 | +                    consumer_node.op == "call_function" and consumer_node.target == q_op | 
|  | 75 | +                ) | 
|  | 76 | +                q_params = consumer_node.args[1:] if quantize else None | 
|  | 77 | +                conv = create_node( | 
|  | 78 | +                    graph=graph_module.graph, | 
|  | 79 | +                    op_target=exir_ops.edge.aten.convolution.default, | 
|  | 80 | +                    args=( | 
|  | 81 | +                        input_reshaped, | 
|  | 82 | +                        weights_reshaped, | 
|  | 83 | +                        bias, | 
|  | 84 | +                        [1, 1],  # strides | 
|  | 85 | +                        [0, 0],  # padding | 
|  | 86 | +                        [1, 1],  # dilation | 
|  | 87 | +                        False,  # transposed | 
|  | 88 | +                        [0, 0],  # output padding | 
|  | 89 | +                        1,  # groups | 
|  | 90 | +                    ), | 
|  | 91 | +                    kwargs={}, | 
|  | 92 | +                    quantize=quantize, | 
|  | 93 | +                    q_params=q_params, | 
|  | 94 | +                ) | 
|  | 95 | + | 
|  | 96 | +            with graph_module.graph.inserting_after(conv): | 
|  | 97 | +                # Reshape output to same rank as original input with shape (..., Co) | 
|  | 98 | +                # No need to insert q/dq pair as Conv2D node above has inserted them if | 
|  | 99 | +                # required. | 
|  | 100 | +                output = create_node( | 
|  | 101 | +                    graph=graph_module.graph, | 
|  | 102 | +                    op_target=exir_ops.edge.aten.view_copy.default, | 
|  | 103 | +                    args=(conv, list(output_shape)), | 
|  | 104 | +                    kwargs={}, | 
|  | 105 | +                ) | 
|  | 106 | + | 
|  | 107 | +            node.replace_all_uses_with(output) | 
|  | 108 | +            graph_module.graph.erase_node(node) | 
|  | 109 | +            graph_module.graph.eliminate_dead_code() | 
|  | 110 | +        graph_module.recompile() | 
|  | 111 | +        graph_module = super().call(graph_module).graph_module | 
|  | 112 | +        return PassResult(graph_module, True) | 
0 commit comments