|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# Copyright 2024 Arm Limited and/or its affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +import logging |
| 9 | +import unittest |
| 10 | + |
| 11 | +from typing import Tuple |
| 12 | + |
| 13 | +import torch |
| 14 | +from executorch.backends.arm.quantizer.arm_quantizer import ( |
| 15 | + ArmQuantizer, |
| 16 | + get_symmetric_quantization_config, |
| 17 | +) |
| 18 | +from executorch.backends.arm.test import common |
| 19 | +from executorch.backends.arm.test.tester.arm_tester import ArmTester |
| 20 | + |
| 21 | +from executorch.backends.xnnpack.test.tester.tester import Quantize |
| 22 | +from executorch.exir.backend.backend_details import CompileSpec |
| 23 | +from parameterized import parameterized |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | +logger.setLevel(logging.INFO) |
| 27 | + |
| 28 | +test_data_suite = [ |
| 29 | + # (test_name, test_data, [kernel_size, stride, padding]) |
| 30 | + ("zeros", torch.zeros(1, 1, 4, 8), [2, 2, 1]), |
| 31 | + ("ones", torch.ones(1, 16, 50, 32), [4, 2, 0]), |
| 32 | + ("rand", torch.rand(1, 16, 52, 16), [4, 3, 0]), |
| 33 | +] |
| 34 | + |
| 35 | +test_data_suite_mult_batches = [ |
| 36 | + ("randn", torch.randn(5, 16, 50, 32), [4, 2, 0]), |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +class TestMaxPool2d(unittest.TestCase): |
| 41 | + """Tests MaxPool2d.""" |
| 42 | + |
| 43 | + class MaxPool2d(torch.nn.Module): |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + kernel_size: int | Tuple[int, int], |
| 47 | + stride: int | Tuple[int, int], |
| 48 | + padding: int | Tuple[int, int], |
| 49 | + ): |
| 50 | + super().__init__() |
| 51 | + self.max_pool_2d = torch.nn.MaxPool2d( |
| 52 | + kernel_size=kernel_size, stride=stride, padding=padding |
| 53 | + ) |
| 54 | + |
| 55 | + def forward(self, x): |
| 56 | + return self.max_pool_2d(x) |
| 57 | + |
| 58 | + def _test_maxpool2d_tosa_MI_pipeline( |
| 59 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor] |
| 60 | + ): |
| 61 | + ( |
| 62 | + ArmTester( |
| 63 | + module, |
| 64 | + example_inputs=test_data, |
| 65 | + compile_spec=common.get_tosa_compile_spec(permute_memory_to_nhwc=True), |
| 66 | + ) |
| 67 | + .export() |
| 68 | + .check(["torch.ops.aten.max_pool2d.default"]) |
| 69 | + .check_not(["torch.ops.quantized_decomposed"]) |
| 70 | + .to_edge() |
| 71 | + .partition() |
| 72 | + .check_not(["executorch_exir_dialects_edge__ops_aten_max_pool2d_default"]) |
| 73 | + .check_not( |
| 74 | + [ |
| 75 | + "executorch_exir_dialects_edge__ops_aten_max_pool2d_with_indices_default" |
| 76 | + ] |
| 77 | + ) |
| 78 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 79 | + .to_executorch() |
| 80 | + ) |
| 81 | + |
| 82 | + def _test_maxpool2d_tosa_BI_pipeline( |
| 83 | + self, module: torch.nn.Module, test_data: Tuple[torch.tensor] |
| 84 | + ): |
| 85 | + quantizer = ArmQuantizer().set_io(get_symmetric_quantization_config()) |
| 86 | + ( |
| 87 | + ArmTester( |
| 88 | + module, |
| 89 | + example_inputs=test_data, |
| 90 | + compile_spec=common.get_tosa_compile_spec(permute_memory_to_nhwc=True), |
| 91 | + ) |
| 92 | + .quantize(Quantize(quantizer, get_symmetric_quantization_config())) |
| 93 | + .export() |
| 94 | + .check_count({"torch.ops.aten.max_pool2d.default": 1}) |
| 95 | + .check(["torch.ops.quantized_decomposed"]) |
| 96 | + .to_edge() |
| 97 | + .partition() |
| 98 | + .check_not(["executorch_exir_dialects_edge__ops_aten_max_pool2d_default"]) |
| 99 | + .check_not( |
| 100 | + [ |
| 101 | + "executorch_exir_dialects_edge__ops_aten_max_pool2d_with_indices_default" |
| 102 | + ] |
| 103 | + ) |
| 104 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 105 | + .to_executorch() |
| 106 | + .run_method_and_compare_outputs(inputs=test_data, qtol=1) |
| 107 | + ) |
| 108 | + |
| 109 | + def _test_maxpool2d_tosa_ethos_BI_pipeline( |
| 110 | + self, |
| 111 | + module: torch.nn.Module, |
| 112 | + compile_spec: CompileSpec, |
| 113 | + test_data: Tuple[torch.tensor], |
| 114 | + ): |
| 115 | + quantizer = ArmQuantizer().set_io(get_symmetric_quantization_config()) |
| 116 | + tester = ( |
| 117 | + ArmTester( |
| 118 | + module, |
| 119 | + example_inputs=test_data, |
| 120 | + compile_spec=compile_spec, |
| 121 | + ) |
| 122 | + .quantize(Quantize(quantizer, get_symmetric_quantization_config())) |
| 123 | + .export() |
| 124 | + .check_count({"torch.ops.aten.max_pool2d.default": 1}) |
| 125 | + .check(["torch.ops.quantized_decomposed"]) |
| 126 | + .to_edge() |
| 127 | + .partition() |
| 128 | + .check_not(["executorch_exir_dialects_edge__ops_aten_max_pool2d_default"]) |
| 129 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 130 | + .to_executorch() |
| 131 | + .serialize() |
| 132 | + ) |
| 133 | + |
| 134 | + return tester |
| 135 | + |
| 136 | + @parameterized.expand(test_data_suite) |
| 137 | + def test_maxpool2d_tosa_MI( |
| 138 | + self, |
| 139 | + test_name: str, |
| 140 | + test_data: torch.Tensor, |
| 141 | + model_params: int | Tuple[int, int], |
| 142 | + ): |
| 143 | + self._test_maxpool2d_tosa_MI_pipeline( |
| 144 | + self.MaxPool2d(*model_params), (test_data,) |
| 145 | + ) |
| 146 | + |
| 147 | + @parameterized.expand(test_data_suite) |
| 148 | + def test_maxpool2d_tosa_BI( |
| 149 | + self, |
| 150 | + test_name: str, |
| 151 | + test_data: torch.Tensor, |
| 152 | + model_params: int | Tuple[int, int], |
| 153 | + ): |
| 154 | + self._test_maxpool2d_tosa_BI_pipeline( |
| 155 | + self.MaxPool2d(*model_params), (test_data,) |
| 156 | + ) |
| 157 | + |
| 158 | + @parameterized.expand(test_data_suite) |
| 159 | + def test_maxpool2d_tosa_u55_BI( |
| 160 | + self, |
| 161 | + test_name: str, |
| 162 | + test_data: torch.Tensor, |
| 163 | + model_params: int | Tuple[int, int], |
| 164 | + ): |
| 165 | + tester = self._test_maxpool2d_tosa_ethos_BI_pipeline( |
| 166 | + self.MaxPool2d(*model_params), |
| 167 | + common.get_u55_compile_spec(permute_memory_to_nhwc=True), |
| 168 | + (test_data,), |
| 169 | + ) |
| 170 | + if common.is_option_enabled("corstone300"): |
| 171 | + tester.run_method_and_compare_outputs( |
| 172 | + qtol=1, inputs=(test_data,), target_board="corstone-300" |
| 173 | + ) |
| 174 | + |
| 175 | + @parameterized.expand(test_data_suite) |
| 176 | + def test_maxpool2d_tosa_u85_BI( |
| 177 | + self, |
| 178 | + test_name: str, |
| 179 | + test_data: torch.Tensor, |
| 180 | + model_params: int | Tuple[int, int], |
| 181 | + ): |
| 182 | + tester = self._test_maxpool2d_tosa_ethos_BI_pipeline( |
| 183 | + self.MaxPool2d(*model_params), |
| 184 | + common.get_u85_compile_spec(permute_memory_to_nhwc=True), |
| 185 | + (test_data,), |
| 186 | + ) |
| 187 | + if common.is_option_enabled("corstone300"): |
| 188 | + tester.run_method_and_compare_outputs( |
| 189 | + qtol=1, inputs=(test_data,), target_board="corstone-320" |
| 190 | + ) |
| 191 | + |
| 192 | + @parameterized.expand(test_data_suite_mult_batches) |
| 193 | + def test_maxpool2d_tosa_MI_mult_batches( |
| 194 | + self, |
| 195 | + test_name: str, |
| 196 | + test_data: torch.Tensor, |
| 197 | + model_params: int | Tuple[int, int], |
| 198 | + ): |
| 199 | + self._test_maxpool2d_tosa_MI_pipeline( |
| 200 | + self.MaxPool2d(*model_params), (test_data,) |
| 201 | + ) |
| 202 | + |
| 203 | + @parameterized.expand(test_data_suite_mult_batches) |
| 204 | + def test_maxpool2d_tosa_BI_mult_batches( |
| 205 | + self, |
| 206 | + test_name: str, |
| 207 | + test_data: torch.Tensor, |
| 208 | + model_params: int | Tuple[int, int], |
| 209 | + ): |
| 210 | + self._test_maxpool2d_tosa_BI_pipeline( |
| 211 | + self.MaxPool2d(*model_params), (test_data,) |
| 212 | + ) |
| 213 | + |
| 214 | + @parameterized.expand(test_data_suite_mult_batches) |
| 215 | + @common.expectedFailureOnFVP # TODO: MLETORCH-433 |
| 216 | + def test_maxpool2d_tosa_u55_BI_mult_batches( |
| 217 | + self, |
| 218 | + test_name: str, |
| 219 | + test_data: torch.Tensor, |
| 220 | + model_params: int | Tuple[int, int], |
| 221 | + ): |
| 222 | + tester = self._test_maxpool2d_tosa_ethos_BI_pipeline( |
| 223 | + self.MaxPool2d(*model_params), |
| 224 | + common.get_u55_compile_spec(permute_memory_to_nhwc=True), |
| 225 | + (test_data,), |
| 226 | + ) |
| 227 | + if common.is_option_enabled("corstone300"): |
| 228 | + tester.run_method_and_compare_outputs( |
| 229 | + qtol=1, inputs=(test_data,), target_board="corstone-300" |
| 230 | + ) |
| 231 | + |
| 232 | + @parameterized.expand(test_data_suite_mult_batches) |
| 233 | + @common.expectedFailureOnFVP # TODO: MLETORCH-433 |
| 234 | + def test_maxpool2d_tosa_u85_BI_mult_batches( |
| 235 | + self, |
| 236 | + test_name: str, |
| 237 | + test_data: torch.Tensor, |
| 238 | + model_params: int | Tuple[int, int], |
| 239 | + ): |
| 240 | + tester = self._test_maxpool2d_tosa_ethos_BI_pipeline( |
| 241 | + self.MaxPool2d(*model_params), |
| 242 | + common.get_u85_compile_spec(permute_memory_to_nhwc=True), |
| 243 | + (test_data,), |
| 244 | + ) |
| 245 | + if common.is_option_enabled("corstone300"): |
| 246 | + tester.run_method_and_compare_outputs( |
| 247 | + qtol=1, inputs=(test_data,), target_board="corstone-320" |
| 248 | + ) |
0 commit comments