|
| 1 | +# Copyright 2026 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.arm_pass 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._passes.rewrite_avg_pool2d_pass import RewriteAvgPool2dPass |
| 15 | +from executorch.backends.arm._passes.rewrite_upsample import RewriteUpsamplePass |
| 16 | +from executorch.backends.arm.common.type import ensure_type |
| 17 | +from executorch.backends.arm.tosa.resize_utils import ( |
| 18 | + is_exact_tosa_resize_boundary_downscale_case, |
| 19 | +) |
| 20 | +from executorch.backends.arm.tosa.specification import TosaSpecification |
| 21 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 22 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 23 | +from torch.fx.passes.shape_prop import _extract_tensor_metadata |
| 24 | + |
| 25 | +_EXACT_BOUNDARY_DOWNSCALE = 16 |
| 26 | +_EQUIVALENT_AVG_POOL_KERNEL = 2 |
| 27 | +_EQUIVALENT_AVG_POOL_OFFSET = _EXACT_BOUNDARY_DOWNSCALE // 2 - 1 |
| 28 | + |
| 29 | + |
| 30 | +def is_exact_tosa_boundary_bilinear_downscale( |
| 31 | + node: torch.fx.Node, |
| 32 | + tosa_spec: TosaSpecification, |
| 33 | +) -> bool: |
| 34 | + if ( |
| 35 | + node.op != "call_function" |
| 36 | + or node.target != exir_ops.edge.aten.upsample_bilinear2d.vec |
| 37 | + ): |
| 38 | + return False |
| 39 | + |
| 40 | + input_node = ensure_type(torch.fx.Node, node.args[0]) |
| 41 | + align_corners = ensure_type(bool, node.args[2]) |
| 42 | + if align_corners: |
| 43 | + return False |
| 44 | + input_size_yx = get_first_fake_tensor(input_node).shape[2:] |
| 45 | + output_size_yx = get_first_fake_tensor(node).shape[2:] |
| 46 | + |
| 47 | + scale_y_n, scale_y_d, offset_y, border_y = ( |
| 48 | + RewriteUpsamplePass.get_resize_parameters_1d( |
| 49 | + input_size_yx[0], output_size_yx[0], align_corners |
| 50 | + ) |
| 51 | + ) |
| 52 | + scale_x_n, scale_x_d, offset_x, border_x = ( |
| 53 | + RewriteUpsamplePass.get_resize_parameters_1d( |
| 54 | + input_size_yx[1], output_size_yx[1], align_corners |
| 55 | + ) |
| 56 | + ) |
| 57 | + return is_exact_tosa_resize_boundary_downscale_case( |
| 58 | + input_hw=input_size_yx, |
| 59 | + output_hw=output_size_yx, |
| 60 | + scale=[scale_y_n, scale_y_d, scale_x_n, scale_x_d], |
| 61 | + offset=[offset_y, offset_x], |
| 62 | + border=[border_y, border_x], |
| 63 | + tosa_spec=tosa_spec, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +class DecomposeUnsupportedBilinearResizePass(ArmPass): |
| 68 | + targeted_ops = (exir_ops.edge.aten.upsample_bilinear2d.vec,) |
| 69 | + _passes_required_after: Set[Type[ExportPass]] = {RewriteAvgPool2dPass} |
| 70 | + |
| 71 | + def __init__(self, tosa_spec: TosaSpecification) -> None: |
| 72 | + super().__init__() |
| 73 | + self.tosa_spec = tosa_spec |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def _set_fake_tensor_meta(node: torch.fx.Node, value: torch.Tensor) -> None: |
| 77 | + node.meta["val"] = value |
| 78 | + node.meta["tensor_meta"] = _extract_tensor_metadata(value) |
| 79 | + |
| 80 | + def call(self, graph_module): |
| 81 | + graph = graph_module.graph |
| 82 | + modified = False |
| 83 | + for node in list(graph.nodes): |
| 84 | + if not is_exact_tosa_boundary_bilinear_downscale(node, self.tosa_spec): |
| 85 | + continue |
| 86 | + |
| 87 | + input_node = ensure_type(torch.fx.Node, node.args[0]) |
| 88 | + input_val = input_node.meta["val"] |
| 89 | + input_hw = [int(dim) for dim in get_first_fake_tensor(input_node).shape[2:]] |
| 90 | + |
| 91 | + with graph.inserting_before(node): |
| 92 | + cropped_h = create_node( |
| 93 | + graph, |
| 94 | + op_target=exir_ops.edge.aten.slice_copy.Tensor, |
| 95 | + args=( |
| 96 | + input_node, |
| 97 | + 2, |
| 98 | + _EQUIVALENT_AVG_POOL_OFFSET, |
| 99 | + input_hw[0] - _EQUIVALENT_AVG_POOL_OFFSET, |
| 100 | + 1, |
| 101 | + ), |
| 102 | + from_node=node, |
| 103 | + ) |
| 104 | + cropped_h_val = exir_ops.edge.aten.slice_copy.Tensor( |
| 105 | + input_val, |
| 106 | + 2, |
| 107 | + _EQUIVALENT_AVG_POOL_OFFSET, |
| 108 | + input_hw[0] - _EQUIVALENT_AVG_POOL_OFFSET, |
| 109 | + 1, |
| 110 | + ) |
| 111 | + self._set_fake_tensor_meta(cropped_h, cropped_h_val) |
| 112 | + |
| 113 | + cropped_hw = create_node( |
| 114 | + graph, |
| 115 | + op_target=exir_ops.edge.aten.slice_copy.Tensor, |
| 116 | + args=( |
| 117 | + cropped_h, |
| 118 | + 3, |
| 119 | + _EQUIVALENT_AVG_POOL_OFFSET, |
| 120 | + input_hw[1] - _EQUIVALENT_AVG_POOL_OFFSET, |
| 121 | + 1, |
| 122 | + ), |
| 123 | + from_node=node, |
| 124 | + ) |
| 125 | + cropped_hw_val = exir_ops.edge.aten.slice_copy.Tensor( |
| 126 | + cropped_h_val, |
| 127 | + 3, |
| 128 | + _EQUIVALENT_AVG_POOL_OFFSET, |
| 129 | + input_hw[1] - _EQUIVALENT_AVG_POOL_OFFSET, |
| 130 | + 1, |
| 131 | + ) |
| 132 | + self._set_fake_tensor_meta(cropped_hw, cropped_hw_val) |
| 133 | + |
| 134 | + pooled = create_node( |
| 135 | + graph, |
| 136 | + op_target=exir_ops.edge.aten.avg_pool2d.default, |
| 137 | + args=( |
| 138 | + cropped_hw, |
| 139 | + [_EQUIVALENT_AVG_POOL_KERNEL, _EQUIVALENT_AVG_POOL_KERNEL], |
| 140 | + [_EXACT_BOUNDARY_DOWNSCALE, _EXACT_BOUNDARY_DOWNSCALE], |
| 141 | + ), |
| 142 | + from_node=node, |
| 143 | + ) |
| 144 | + pooled_val = torch.nn.functional.avg_pool2d( |
| 145 | + cropped_hw_val, |
| 146 | + kernel_size=_EQUIVALENT_AVG_POOL_KERNEL, |
| 147 | + stride=_EXACT_BOUNDARY_DOWNSCALE, |
| 148 | + ) |
| 149 | + self._set_fake_tensor_meta(pooled, pooled_val) |
| 150 | + |
| 151 | + node.replace_all_uses_with(pooled) |
| 152 | + graph.erase_node(node) |
| 153 | + modified = True |
| 154 | + |
| 155 | + if modified: |
| 156 | + graph.lint() |
| 157 | + graph_module.recompile() |
| 158 | + graph_module = super().call(graph_module).graph_module |
| 159 | + return PassResult(graph_module, modified) |
0 commit comments