|
| 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 unittest |
| 8 | + |
| 9 | +from typing import Optional, Tuple |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.arm.test import common |
| 13 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 14 | +from parameterized import parameterized |
| 15 | + |
| 16 | + |
| 17 | +test_data_suite = [ |
| 18 | + # (test_name, test_data, size, scale_factor, compare_outputs) |
| 19 | + ("rand_double_scale", torch.rand(2, 4, 8, 3), None, 2.0, True), |
| 20 | + ("rand_double_scale_one_dim", torch.rand(2, 4, 8, 3), None, (1.0, 2.0), True), |
| 21 | + ("rand_double_size", torch.rand(2, 4, 8, 3), (16, 6), None, True), |
| 22 | + ("rand_one_double_scale", torch.rand(2, 4, 1, 1), None, 2.0, True), |
| 23 | + ("rand_one_double_size", torch.rand(2, 4, 1, 1), (2, 2), None, True), |
| 24 | + ("rand_one_same_scale", torch.rand(2, 4, 1, 1), None, 1.0, True), |
| 25 | + ("rand_one_same_size", torch.rand(2, 4, 1, 1), (1, 1), None, True), |
| 26 | + # Can't compare outputs as the rounding when selecting the nearest pixel is |
| 27 | + # different between PyTorch and TOSA. Just check the legalization went well. |
| 28 | + # TODO Improve the test infrastructure to support more in depth verification |
| 29 | + # of the TOSA legalization results. |
| 30 | + ("rand_half_scale", torch.rand(2, 4, 8, 6), None, 0.5, False), |
| 31 | + ("rand_half_size", torch.rand(2, 4, 8, 6), (4, 3), None, False), |
| 32 | + ("rand_one_and_half_scale", torch.rand(2, 4, 8, 3), None, 1.5, False), |
| 33 | + ("rand_one_and_half_size", torch.rand(2, 4, 8, 3), (12, 4), None, False), |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +class TestUpsampleNearest2d(unittest.TestCase): |
| 38 | + class UpsamplingNearest2d(torch.nn.Module): |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + size: Optional[Tuple[int]], |
| 42 | + scale_factor: Optional[float | Tuple[float]], |
| 43 | + ): |
| 44 | + super().__init__() |
| 45 | + self.upsample = torch.nn.UpsamplingNearest2d( # noqa: TOR101 |
| 46 | + size=size, scale_factor=scale_factor |
| 47 | + ) |
| 48 | + |
| 49 | + def forward(self, x): |
| 50 | + return self.upsample(x) |
| 51 | + |
| 52 | + class Upsample(torch.nn.Module): |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + size: Optional[Tuple[int]], |
| 56 | + scale_factor: Optional[float | Tuple[float]], |
| 57 | + ): |
| 58 | + super().__init__() |
| 59 | + self.upsample = torch.nn.Upsample( |
| 60 | + size=size, scale_factor=scale_factor, mode="nearest" |
| 61 | + ) |
| 62 | + |
| 63 | + def forward(self, x): |
| 64 | + return self.upsample(x) |
| 65 | + |
| 66 | + class Interpolate(torch.nn.Module): |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + size: Optional[Tuple[int]], |
| 70 | + scale_factor: Optional[float | Tuple[float]], |
| 71 | + ): |
| 72 | + super().__init__() |
| 73 | + self.upsample = lambda x: torch.nn.functional.interpolate( |
| 74 | + x, size=size, scale_factor=scale_factor, mode="nearest" |
| 75 | + ) |
| 76 | + |
| 77 | + def forward(self, x): |
| 78 | + return self.upsample(x) |
| 79 | + |
| 80 | + def _test_upsample_nearest_2d_tosa_MI_pipeline( |
| 81 | + self, |
| 82 | + module: torch.nn.Module, |
| 83 | + test_data: Tuple[torch.tensor], |
| 84 | + compare_outputs: bool, |
| 85 | + ): |
| 86 | + tester = ( |
| 87 | + ArmTester( |
| 88 | + module, |
| 89 | + example_inputs=test_data, |
| 90 | + compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+MI"), |
| 91 | + ) |
| 92 | + .export() |
| 93 | + .check(["torch.ops.aten.upsample_nearest2d.vec"]) |
| 94 | + .check_not(["torch.ops.quantized_decomposed"]) |
| 95 | + .to_edge_transform_and_lower() |
| 96 | + .check_not(["torch.ops.aten.upsample_nearest2d.vec"]) |
| 97 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 98 | + .to_executorch() |
| 99 | + ) |
| 100 | + |
| 101 | + if compare_outputs: |
| 102 | + tester.run_method_and_compare_outputs(inputs=test_data) |
| 103 | + |
| 104 | + def _test_upsample_nearest_2d_tosa_BI_pipeline( |
| 105 | + self, |
| 106 | + module: torch.nn.Module, |
| 107 | + test_data: Tuple[torch.tensor], |
| 108 | + compare_outputs: bool, |
| 109 | + ): |
| 110 | + tester = ( |
| 111 | + ArmTester( |
| 112 | + module, |
| 113 | + example_inputs=test_data, |
| 114 | + compile_spec=common.get_tosa_compile_spec("TOSA-0.80.0+BI"), |
| 115 | + ) |
| 116 | + .quantize() |
| 117 | + .export() |
| 118 | + .check(["torch.ops.aten.upsample_nearest2d.vec"]) |
| 119 | + .check(["torch.ops.quantized_decomposed"]) |
| 120 | + .to_edge_transform_and_lower() |
| 121 | + .check_not(["torch.ops.aten.upsample_nearest2d.vec"]) |
| 122 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 123 | + .to_executorch() |
| 124 | + ) |
| 125 | + |
| 126 | + if compare_outputs: |
| 127 | + tester.run_method_and_compare_outputs(inputs=test_data) |
| 128 | + |
| 129 | + @parameterized.expand(test_data_suite) |
| 130 | + def test_upsample_nearest_2d_tosa_MI( |
| 131 | + self, |
| 132 | + test_name: str, |
| 133 | + test_data: torch.Tensor, |
| 134 | + size: Optional[Tuple[int]], |
| 135 | + scale_factor: Optional[float | Tuple[float]], |
| 136 | + compare_outputs: bool, |
| 137 | + ): |
| 138 | + self._test_upsample_nearest_2d_tosa_MI_pipeline( |
| 139 | + self.UpsamplingNearest2d(size, scale_factor), (test_data,), compare_outputs |
| 140 | + ) |
| 141 | + self._test_upsample_nearest_2d_tosa_MI_pipeline( |
| 142 | + self.Upsample(size, scale_factor), (test_data,), compare_outputs |
| 143 | + ) |
| 144 | + self._test_upsample_nearest_2d_tosa_MI_pipeline( |
| 145 | + self.Interpolate(size, scale_factor), (test_data,), compare_outputs |
| 146 | + ) |
| 147 | + |
| 148 | + @parameterized.expand(test_data_suite) |
| 149 | + def test_upsample_nearest_2d_tosa_BI( |
| 150 | + self, |
| 151 | + test_name: str, |
| 152 | + test_data: torch.Tensor, |
| 153 | + size: Optional[Tuple[int]], |
| 154 | + scale_factor: Optional[float | Tuple[float]], |
| 155 | + compare_outputs: bool, |
| 156 | + ): |
| 157 | + self._test_upsample_nearest_2d_tosa_BI_pipeline( |
| 158 | + self.UpsamplingNearest2d(size, scale_factor), (test_data,), compare_outputs |
| 159 | + ) |
| 160 | + self._test_upsample_nearest_2d_tosa_BI_pipeline( |
| 161 | + self.Upsample(size, scale_factor), (test_data,), compare_outputs |
| 162 | + ) |
| 163 | + self._test_upsample_nearest_2d_tosa_BI_pipeline( |
| 164 | + self.Interpolate(size, scale_factor), (test_data,), compare_outputs |
| 165 | + ) |
0 commit comments