|
| 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 | +"""Provide TOSA support checks for upsample operators.""" |
| 6 | + |
| 7 | +import torch.fx as fx |
| 8 | +from executorch.backends.arm._passes.arm_pass_utils import get_first_fake_tensor |
| 9 | +from executorch.backends.arm._passes.rewrite_upsample import RewriteUpsamplePass |
| 10 | +from executorch.backends.arm.common.type import ensure_type |
| 11 | +from executorch.backends.arm.operator_support.tosa_supported_operators import ( |
| 12 | + register_tosa_support_check, |
| 13 | + SupportedTOSAOperatorCheck, |
| 14 | +) |
| 15 | +from executorch.backends.arm.tosa import TosaSpecification |
| 16 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 17 | + |
| 18 | + |
| 19 | +@register_tosa_support_check |
| 20 | +class UpsampleNearest2dSupported(SupportedTOSAOperatorCheck): |
| 21 | + """Provide the explicit TOSA support gate for nearest upsample.""" |
| 22 | + |
| 23 | + targets = [exir_ops.edge.aten.upsample_nearest2d.vec] |
| 24 | + |
| 25 | + def is_node_tosa_supported( |
| 26 | + self, _node: fx.Node, _tosa_spec: TosaSpecification |
| 27 | + ) -> bool: # type: ignore[override, misc] |
| 28 | + return True |
| 29 | + |
| 30 | + |
| 31 | +@register_tosa_support_check |
| 32 | +class UpsampleBilinear2dSupported(SupportedTOSAOperatorCheck): |
| 33 | + """Reject bilinear upsample cases that cannot lower to a valid TOSA |
| 34 | + RESIZE. |
| 35 | + """ |
| 36 | + |
| 37 | + targets = [exir_ops.edge.aten.upsample_bilinear2d.vec] |
| 38 | + |
| 39 | + def is_node_tosa_supported( |
| 40 | + self, node: fx.Node, _tosa_spec: TosaSpecification |
| 41 | + ) -> bool: # type: ignore[override, misc] |
| 42 | + input_node = ensure_type(fx.Node, node.args[0]) |
| 43 | + align_corners = ensure_type(bool, node.args[2]) |
| 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 | + try: |
| 48 | + scale_y_n, scale_y_d, _, _ = RewriteUpsamplePass.get_resize_parameters_1d( |
| 49 | + input_size_yx[0], output_size_yx[0], align_corners |
| 50 | + ) |
| 51 | + scale_x_n, scale_x_d, _, _ = RewriteUpsamplePass.get_resize_parameters_1d( |
| 52 | + input_size_yx[1], output_size_yx[1], align_corners |
| 53 | + ) |
| 54 | + except RuntimeError as err: |
| 55 | + self.reporter.report_reject(node, str(err)) |
| 56 | + return False |
| 57 | + |
| 58 | + # get_resize_parameters_1d() returns the TOSA RESIZE scale fraction for |
| 59 | + # each spatial dimension. For align_corners=False, this is the effective |
| 60 | + # output_size / input_size ratio, so the 1/16 boundary is checked |
| 61 | + # directly in the same representation that RESIZE lowering will use. |
| 62 | + if scale_y_d >= 16 * scale_y_n or scale_x_d >= 16 * scale_x_n: |
| 63 | + self.reporter.report_reject( |
| 64 | + node, |
| 65 | + "Bilinear RESIZE downscale must be strictly greater than 1/16", |
| 66 | + ) |
| 67 | + return False |
| 68 | + |
| 69 | + return True |
0 commit comments