|
| 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 CortexMTester, McuTestCase |
| 10 | +from executorch.backends.test.suite.operators.test_add import Model, ModelAlpha |
| 11 | + |
| 12 | + |
| 13 | +class CortexMSelfAdd(torch.nn.Module): |
| 14 | + ops_before_transforms = { |
| 15 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 1, |
| 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 | + } |
| 19 | + |
| 20 | + ops_after_transforms = { |
| 21 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_add_default": 1, |
| 22 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 23 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 24 | + } |
| 25 | + |
| 26 | + def forward(self, x): |
| 27 | + return x + x |
| 28 | + |
| 29 | + |
| 30 | +class CortexMScalarAdd(Model): |
| 31 | + ops_before_transforms = { |
| 32 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 1, |
| 33 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 34 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 35 | + } |
| 36 | + |
| 37 | + ops_after_transforms = { |
| 38 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_add_default": 1, |
| 39 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 40 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +class CortexMTensorAdd(Model): |
| 45 | + ops_before_transforms = { |
| 46 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 1, |
| 47 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 3, |
| 48 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 49 | + } |
| 50 | + |
| 51 | + ops_after_transforms = { |
| 52 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_add_default": 1, |
| 53 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 2, |
| 54 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +class CortexMAlphaAdd(ModelAlpha): |
| 59 | + ops_before_transforms = { |
| 60 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 1, |
| 61 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 3, |
| 62 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 63 | + } |
| 64 | + |
| 65 | + ops_after_transforms = { |
| 66 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_add_default": 1, |
| 67 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 2, |
| 68 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +test_cases = { |
| 73 | + "self_scalar": McuTestCase( |
| 74 | + CortexMSelfAdd(), |
| 75 | + (10.0,), |
| 76 | + ), |
| 77 | + "self_rank_1": McuTestCase( |
| 78 | + CortexMSelfAdd(), |
| 79 | + (torch.linspace(-5, 5, 10),), |
| 80 | + ), |
| 81 | + "self_rank_2_pos": McuTestCase( |
| 82 | + CortexMSelfAdd(), |
| 83 | + (torch.linspace(0, 1000, 10).reshape((10, 1)),), |
| 84 | + ), |
| 85 | + "self_rank_3_neg": McuTestCase( |
| 86 | + CortexMSelfAdd(), |
| 87 | + (torch.linspace(-100, 0, 8).reshape((2, 2, 2)),), |
| 88 | + ), |
| 89 | + "self_rank_4_small": McuTestCase( |
| 90 | + CortexMSelfAdd(), |
| 91 | + (torch.linspace(-0.1, 0.1, 16).reshape(2, 2, 2, 2),), |
| 92 | + ), |
| 93 | + "self_rank_5": McuTestCase( |
| 94 | + CortexMSelfAdd(), |
| 95 | + (torch.linspace(-5, 5, 32).reshape(2, 2, 2, 2, 2),), |
| 96 | + ), |
| 97 | + "scalar_scalar": McuTestCase( |
| 98 | + CortexMScalarAdd(), |
| 99 | + (-0.5, 1.0), |
| 100 | + ), |
| 101 | + "tensor_scalar": McuTestCase( |
| 102 | + CortexMScalarAdd(), |
| 103 | + (torch.ones(2, 2), 1.0), |
| 104 | + ), |
| 105 | + "scalar_tensor": McuTestCase( |
| 106 | + CortexMScalarAdd(), |
| 107 | + (1000.0, torch.ones(2, 2)), |
| 108 | + ), |
| 109 | + "broadcast_1": McuTestCase( |
| 110 | + CortexMTensorAdd(), |
| 111 | + (torch.ones(1), torch.ones(2, 2, 2, 2)), |
| 112 | + ), |
| 113 | + "broadcast_2": McuTestCase( |
| 114 | + CortexMTensorAdd(), |
| 115 | + (torch.ones((2, 1, 1, 1)), torch.ones(1)), |
| 116 | + ), |
| 117 | + "broadcast_3": McuTestCase( |
| 118 | + CortexMTensorAdd(), |
| 119 | + ( |
| 120 | + torch.linspace(-2, 2, 4).reshape(2, 1, 2, 1), |
| 121 | + torch.linspace(-5, 5, 4).reshape(1, 2, 1, 2), |
| 122 | + ), |
| 123 | + ), |
| 124 | + "alpha": McuTestCase( |
| 125 | + CortexMAlphaAdd(0.5), |
| 126 | + ( |
| 127 | + torch.linspace(-10, 10, 20).reshape(4, 5), |
| 128 | + torch.linspace(-20, 20, 20).reshape(4, 5), |
| 129 | + ), |
| 130 | + ), |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +dialect_xfails = { |
| 135 | + "self_scalar": ("'float' object has no attribute 'fake_mode'", AttributeError), |
| 136 | + "self_rank_1": ("Output 0 does not match reference output", AssertionError), |
| 137 | + "self_rank_2_pos": ("Output 0 does not match reference output", AssertionError), |
| 138 | + "self_rank_3_neg": ("Output 0 does not match reference output", AssertionError), |
| 139 | + "self_rank_4_small": ("Output 0 does not match reference output", AssertionError), |
| 140 | + "self_rank_5": ("Output 0 does not match reference output", AssertionError), |
| 141 | + "scalar_scalar": ("'float' object has no attribute 'fake_mode'", AttributeError), |
| 142 | + "broadcast_3": ("Output 0 does not match reference output", AssertionError), |
| 143 | + "alpha": ("Expecting kwargs for aten op IR to be empty", AssertionError), |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +@parametrize("test_case", test_cases, xfails=dialect_xfails) |
| 148 | +def test_dialect_add(test_case): |
| 149 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 150 | + tester.test_dialect( |
| 151 | + test_case.model.ops_before_transforms, test_case.model.ops_after_transforms |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +implementation_xfails = { |
| 156 | + "self_scalar": ("'float' object has no attribute 'fake_mode'", AttributeError), |
| 157 | + "self_rank_1": ("Output 0 does not match reference output", AssertionError), |
| 158 | + "self_rank_2_pos": ("Output 0 does not match reference output", AssertionError), |
| 159 | + "self_rank_3_neg": ("Output 0 does not match reference output", AssertionError), |
| 160 | + "self_rank_4_small": ("Output 0 does not match reference output", AssertionError), |
| 161 | + "self_rank_5": ("Output 0 does not match reference output", AssertionError), |
| 162 | + "scalar_scalar": ("'float' object has no attribute 'fake_mode'", AttributeError), |
| 163 | + "tensor_scalar": ("Output 0 does not match reference output", AssertionError), |
| 164 | + "scalar_tensor": ("Output 0 does not match reference output", AssertionError), |
| 165 | + "broadcast_1": ("Output 0 does not match reference output", AssertionError), |
| 166 | + "broadcast_2": ("Output 0 does not match reference output", AssertionError), |
| 167 | + "broadcast_3": ("Output 0 does not match reference output", AssertionError), |
| 168 | + "alpha": ("Expecting kwargs for aten op IR to be empty", AssertionError), |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +@parametrize("test_case", test_cases, xfails=implementation_xfails) |
| 173 | +def test_implementation_add(test_case): |
| 174 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 175 | + tester.test_implementation() |
0 commit comments