|
| 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 typing import Set, Type |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes import ArmPass |
| 10 | +from executorch.backends.arm._passes.arm_pass_utils import ( |
| 11 | + create_node, |
| 12 | + get_first_fake_tensor, |
| 13 | +) |
| 14 | +from executorch.backends.arm.tosa.utils import get_resize_parameters |
| 15 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 16 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 17 | + |
| 18 | + |
| 19 | +class RewriteUpsamplePass(ArmPass): |
| 20 | + """Rewrite upsample2d nodes to TOSA.RESIZE nodes.""" |
| 21 | + |
| 22 | + targeted_ops = ( |
| 23 | + exir_ops.edge.aten.upsample_nearest2d.vec, |
| 24 | + exir_ops.edge.aten.upsample_bilinear2d.vec, |
| 25 | + ) |
| 26 | + |
| 27 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 28 | + |
| 29 | + def call(self, graph_module): |
| 30 | + modified = False |
| 31 | + for node in graph_module.graph.nodes: |
| 32 | + if node.op != "call_function" or node.target not in self.targeted_ops: |
| 33 | + continue |
| 34 | + modified = True |
| 35 | + |
| 36 | + if node.target == exir_ops.edge.aten.upsample_bilinear2d.vec: |
| 37 | + x, output_size, align_corners, scale_factors = node.args |
| 38 | + resize_mode = "bilinear" |
| 39 | + else: |
| 40 | + x, output_size, scale_factors = node.args |
| 41 | + align_corners = False |
| 42 | + resize_mode = "nearest" |
| 43 | + |
| 44 | + with graph_module.graph.inserting_before(node): |
| 45 | + tosa_resize_node = create_node( |
| 46 | + graph_module.graph, |
| 47 | + op_target=exir_ops.backend.tosa.RESIZE.default, |
| 48 | + args=(x, output_size, align_corners, scale_factors), |
| 49 | + kwargs={"resize_mode": resize_mode}, |
| 50 | + from_node=node, |
| 51 | + ) |
| 52 | + node.replace_all_uses_with(tosa_resize_node) |
| 53 | + graph_module.graph.erase_node(node) |
| 54 | + input_dtype = get_first_fake_tensor(x).dtype |
| 55 | + if input_dtype == torch.int8 and resize_mode == "bilinear": |
| 56 | + input_size = get_first_fake_tensor(x).shape |
| 57 | + input_size_xy = input_size[2:] |
| 58 | + output_size = get_first_fake_tensor(node).shape |
| 59 | + output_size_xy = output_size[2:] |
| 60 | + scale_n_yx, _, _, _ = get_resize_parameters( |
| 61 | + input_size_xy=input_size_xy, |
| 62 | + output_size_xy=output_size_xy, |
| 63 | + resize_mode=1, |
| 64 | + align_corners=align_corners, |
| 65 | + ) |
| 66 | + output_dtype = get_first_fake_tensor(node).dtype |
| 67 | + output_scale = float(1 / (scale_n_yx[0] * scale_n_yx[1])) |
| 68 | + with graph_module.graph.inserting_after(tosa_resize_node): |
| 69 | + rescale_node = create_node( |
| 70 | + graph_module.graph, |
| 71 | + exir_ops.backend.tosa.RESCALE.default, |
| 72 | + ) |
| 73 | + tosa_resize_node.replace_all_uses_with(rescale_node) |
| 74 | + rescale_node.args = ( |
| 75 | + tosa_resize_node, |
| 76 | + output_dtype, |
| 77 | + output_scale, |
| 78 | + 0, # zero point |
| 79 | + 0, # zero point |
| 80 | + ) |
| 81 | + |
| 82 | + if modified: |
| 83 | + graph_module = super().call(graph_module).graph_module |
| 84 | + return PassResult(graph_module, modified) |
0 commit comments