|
| 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 | + |
| 7 | +import torch |
| 8 | +from executorch.backends.arm.test.common import parametrize |
| 9 | +from executorch.backends.cortex_m.test.tester import ( |
| 10 | + CortexMTester, |
| 11 | + McuTestCase, |
| 12 | + ramp_tensor, |
| 13 | +) |
| 14 | + |
| 15 | +OPS_BEFORE_PASSES = { |
| 16 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 17 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 2, |
| 18 | + "executorch_exir_dialects_edge__ops_aten_permute_copy_default": 1, |
| 19 | +} |
| 20 | + |
| 21 | +OPS_AFTER_PASSES = { |
| 22 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 23 | + "executorch_exir_dialects_edge__ops_cortex_m_transpose_default": 1, |
| 24 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class CortexMPermute(torch.nn.Module): |
| 29 | + ops_before_transforms = OPS_BEFORE_PASSES |
| 30 | + ops_after_transforms = OPS_AFTER_PASSES |
| 31 | + |
| 32 | + def __init__(self, perms): |
| 33 | + super().__init__() |
| 34 | + self.perms = perms |
| 35 | + |
| 36 | + def forward(self, x): |
| 37 | + return x.permute(self.perms) |
| 38 | + |
| 39 | + |
| 40 | +class CortexMTranspose(torch.nn.Module): |
| 41 | + ops_before_transforms = OPS_BEFORE_PASSES |
| 42 | + ops_after_transforms = OPS_AFTER_PASSES |
| 43 | + |
| 44 | + def __init__(self, dim0, dim1): |
| 45 | + super().__init__() |
| 46 | + self.dim0 = dim0 |
| 47 | + self.dim1 = dim1 |
| 48 | + |
| 49 | + def forward(self, x): |
| 50 | + return x.transpose(self.dim0, self.dim1) |
| 51 | + |
| 52 | + |
| 53 | +class CortexMT(torch.nn.Module): |
| 54 | + ops_before_transforms = OPS_BEFORE_PASSES |
| 55 | + ops_after_transforms = OPS_AFTER_PASSES |
| 56 | + |
| 57 | + def forward(self, x): |
| 58 | + return x.t() |
| 59 | + |
| 60 | + |
| 61 | +test_cases = { |
| 62 | + "permute_nhwc_to_nchw": McuTestCase( |
| 63 | + CortexMPermute((0, 3, 1, 2)), |
| 64 | + (ramp_tensor(-0.5, 0.5, (2, 3, 4, 2)),), |
| 65 | + ), |
| 66 | + "permute_nchw_to_nhwc_neg_index": McuTestCase( |
| 67 | + CortexMPermute((0, -2, -1, -3)), |
| 68 | + (ramp_tensor(10, 100, (2, 3, 4, 2)),), |
| 69 | + ), |
| 70 | + "permute_rank_1": McuTestCase( |
| 71 | + CortexMPermute((0,)), |
| 72 | + (ramp_tensor(10, 100, (3)),), |
| 73 | + ), |
| 74 | + "transpose_1_2": McuTestCase( |
| 75 | + CortexMTranspose(1, 2), |
| 76 | + (ramp_tensor(-1.0, 1.0, (1, 3, 4)),), |
| 77 | + ), |
| 78 | + "transpose_0_1": McuTestCase( |
| 79 | + CortexMTranspose(0, 1), |
| 80 | + (ramp_tensor(-2.0, 2.0, (2, 3, 4, 3)),), |
| 81 | + ), |
| 82 | + "t_operator": McuTestCase( |
| 83 | + CortexMT(), |
| 84 | + (ramp_tensor(-0.5, 0.5, (4, 2)),), |
| 85 | + ), |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +@parametrize("test_case", test_cases) |
| 90 | +def test_dialect_transpose(test_case): |
| 91 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 92 | + tester.test_dialect( |
| 93 | + test_case.model.ops_before_transforms, |
| 94 | + test_case.model.ops_after_transforms, |
| 95 | + qtol=1, |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@parametrize("test_case", test_cases) |
| 100 | +def test_implementation_transpose(test_case): |
| 101 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 102 | + tester.test_implementation(qtol=1) |
0 commit comments