|
| 1 | +# Copyright 2023-2024 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 typing import cast, List |
| 7 | + |
| 8 | +import executorch.backends.arm.tosa_quant_utils as tqutils |
| 9 | +import executorch.backends.arm.tosa_utils as tutils |
| 10 | + |
| 11 | +import serializer.tosa_serializer as ts |
| 12 | +from executorch.backends.arm.operators.node_visitor import ( |
| 13 | + NodeVisitor, |
| 14 | + register_node_visitor, |
| 15 | +) |
| 16 | +from executorch.backends.arm.tosa_mapping import TosaArg |
| 17 | +from serializer.tosa_serializer import TosaOp |
| 18 | +from torch.fx import Node |
| 19 | + |
| 20 | + |
| 21 | +@register_node_visitor |
| 22 | +class AddVisitor(NodeVisitor): |
| 23 | + target = "aten.sum.dim_IntList" |
| 24 | + |
| 25 | + def __init__(self, *args): |
| 26 | + super().__init__(*args) |
| 27 | + |
| 28 | + def define_node( |
| 29 | + self, |
| 30 | + node: Node, |
| 31 | + tosa_graph: ts.TosaSerializer, |
| 32 | + inputs: List[TosaArg], |
| 33 | + output: TosaArg, |
| 34 | + is_quant_node: bool, |
| 35 | + ) -> None: |
| 36 | + input_node = inputs[0] |
| 37 | + input_shape = list(input_node.shape) |
| 38 | + dim_list = cast(list[int], inputs[1].special) |
| 39 | + dim_list = [dim % len(input_node.shape) for dim in dim_list] |
| 40 | + keep_dim = cast(bool, inputs[2].number if len(inputs) > 2 else False) |
| 41 | + assert keep_dim, "This case should be handled by InsertSqueezeAfterSumPass" |
| 42 | + |
| 43 | + if is_quant_node: |
| 44 | + |
| 45 | + # Rescale input to 32 bit |
| 46 | + rescaled_inputs, scale = tqutils.rescale_nodes_to_int32( |
| 47 | + [node.all_input_nodes[0]], tosa_graph |
| 48 | + ) |
| 49 | + |
| 50 | + prev_node = rescaled_inputs[0] |
| 51 | + reduced_shape = input_shape |
| 52 | + |
| 53 | + # Reduce all dims in dim_list one-by-one. |
| 54 | + for dim in dim_list: |
| 55 | + # When reduced, the size of the dim becomes 1. |
| 56 | + reduced_shape[dim] = 1 |
| 57 | + |
| 58 | + attr = ts.TosaSerializerAttribute() |
| 59 | + attr.AxisAttribute(input_node.dim_order.index(dim)) |
| 60 | + |
| 61 | + next_node = tosa_graph.addIntermediate( |
| 62 | + tutils.tosa_shape(reduced_shape, input_node.dim_order), |
| 63 | + dtype=ts.DType.INT32, |
| 64 | + ) |
| 65 | + |
| 66 | + tosa_graph.addOperator( |
| 67 | + TosaOp.Op().REDUCE_SUM, [prev_node.name], [next_node.name], attr |
| 68 | + ) |
| 69 | + |
| 70 | + prev_node = next_node |
| 71 | + tqutils.rescale_node_back_to_int8(node, prev_node, scale, tosa_graph) |
| 72 | + else: |
| 73 | + input_name = input_node.name |
| 74 | + reduced_shape = input_shape |
| 75 | + |
| 76 | + # Reduce all dims in dim_list one-by-one. |
| 77 | + for dim in dim_list: |
| 78 | + # When reduced, the size of the dim becomes 1 |
| 79 | + reduced_shape[dim] = 1 |
| 80 | + |
| 81 | + attr = ts.TosaSerializerAttribute() |
| 82 | + attr.AxisAttribute(input_node.dim_order.index(dim)) |
| 83 | + |
| 84 | + if dim == dim_list[-1]: |
| 85 | + output_name = output.name |
| 86 | + else: |
| 87 | + output_name = tosa_graph.addIntermediate( |
| 88 | + tutils.tosa_shape(reduced_shape, input_node.dim_order), |
| 89 | + dtype=ts.DType.FP32, |
| 90 | + ).name |
| 91 | + |
| 92 | + tosa_graph.addOperator( |
| 93 | + TosaOp.Op().REDUCE_SUM, [input_name], [output_name], attr |
| 94 | + ) |
| 95 | + |
| 96 | + input_name = output_name |
0 commit comments