|
| 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 | + |
| 16 | +class CortexMMm(torch.nn.Module): |
| 17 | + def forward(self, x, y): |
| 18 | + return torch.mm(x, y) |
| 19 | + |
| 20 | + ops_before_transforms = { |
| 21 | + "executorch_exir_dialects_edge__ops_aten_mm_default": 1, |
| 22 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 23 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 24 | + } |
| 25 | + |
| 26 | + ops_after_transforms = { |
| 27 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, |
| 28 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 29 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +class CortexMBmm(torch.nn.Module): |
| 34 | + def forward(self, x, y): |
| 35 | + return torch.bmm(x, y) |
| 36 | + |
| 37 | + ops_before_transforms = { |
| 38 | + "executorch_exir_dialects_edge__ops_aten_bmm_default": 1, |
| 39 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 40 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 41 | + } |
| 42 | + |
| 43 | + ops_after_transforms = { |
| 44 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, |
| 45 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 46 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +class CortexMAddmm(torch.nn.Module): |
| 51 | + def forward(self, x, y, z, alpha=None, beta=None): |
| 52 | + return torch.addmm(beta, x, alpha, y, z) |
| 53 | + |
| 54 | + ops_before_transforms = { |
| 55 | + "executorch_exir_dialects_edge__ops_aten_addmm_default": 1, |
| 56 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, |
| 57 | + "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, |
| 58 | + } |
| 59 | + |
| 60 | + ops_after_transforms = { |
| 61 | + "executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, |
| 62 | + "executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, |
| 63 | + "executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +class CortexMAt(CortexMMm): |
| 68 | + def forward(self, x, y): |
| 69 | + return x @ y |
| 70 | + |
| 71 | + |
| 72 | +class CortexMMatmul(CortexMMm): |
| 73 | + def forward(self, x, y): |
| 74 | + return torch.matmul(x, y) |
| 75 | + |
| 76 | + |
| 77 | +class CortexMLinear(CortexMMatmul): |
| 78 | + def __init__(self, *args, **kwargs): |
| 79 | + super().__init__() |
| 80 | + self.linear = torch.nn.Linear(*args, bias=False) |
| 81 | + |
| 82 | + def forward(self, x): |
| 83 | + return self.linear(x) |
| 84 | + |
| 85 | + |
| 86 | +class CortexMLinearBias(CortexMAddmm): |
| 87 | + def __init__(self, *args, **kwargs): |
| 88 | + super().__init__() |
| 89 | + self.linear = torch.nn.Linear(*args, bias=True) |
| 90 | + |
| 91 | + def forward(self, x): |
| 92 | + return self.linear(x) |
| 93 | + |
| 94 | + |
| 95 | +test_cases = { |
| 96 | + "mm": McuTestCase( |
| 97 | + model=CortexMMm(), |
| 98 | + example_inputs=( |
| 99 | + ramp_tensor(0, 10, (1, 16)), |
| 100 | + ramp_tensor(0, 10, (16, 16)), |
| 101 | + ), |
| 102 | + ), |
| 103 | + "bmm": McuTestCase( |
| 104 | + model=CortexMBmm(), |
| 105 | + example_inputs=( |
| 106 | + ramp_tensor(0, 10, (1, 16, 16)), |
| 107 | + ramp_tensor(0, 10, (1, 16, 16)), |
| 108 | + ), |
| 109 | + ), |
| 110 | + "addmm": McuTestCase( |
| 111 | + model=CortexMAddmm(), |
| 112 | + example_inputs=( |
| 113 | + ramp_tensor(0, 10, (1, 16)), |
| 114 | + ramp_tensor(0, 10, (16, 16)), |
| 115 | + ramp_tensor(0, 10, (16, 16)), |
| 116 | + 2, |
| 117 | + 4, |
| 118 | + ), |
| 119 | + ), |
| 120 | + "addmm_scalars": McuTestCase( |
| 121 | + model=CortexMAddmm(), |
| 122 | + example_inputs=( |
| 123 | + ramp_tensor(0, 10, (1, 16)), |
| 124 | + ramp_tensor(0, 10, (16, 16)), |
| 125 | + ramp_tensor(0, 10, (16, 16)), |
| 126 | + ), |
| 127 | + ), |
| 128 | + "@-operator": McuTestCase( |
| 129 | + model=CortexMAt(), |
| 130 | + example_inputs=( |
| 131 | + ramp_tensor(0, 10, (1, 16)), |
| 132 | + ramp_tensor(0, 10, (16, 16)), |
| 133 | + ), |
| 134 | + ), |
| 135 | + "matmul": McuTestCase( |
| 136 | + model=CortexMMatmul(), |
| 137 | + example_inputs=( |
| 138 | + ramp_tensor(0, 10, (1, 16)), |
| 139 | + ramp_tensor(0, 10, (16, 16)), |
| 140 | + ), |
| 141 | + ), |
| 142 | + "linear_rank1": McuTestCase( |
| 143 | + model=CortexMLinear(2, 3), |
| 144 | + example_inputs=(ramp_tensor(-1, 1, (2,)),), |
| 145 | + ), |
| 146 | + "linear_rank2_pos": McuTestCase( |
| 147 | + model=CortexMLinear(8, 3), |
| 148 | + example_inputs=(ramp_tensor(0, 10, (2, 8)),), |
| 149 | + ), |
| 150 | + "linear_rank3_neg": McuTestCase( |
| 151 | + model=CortexMLinear(5, 3), |
| 152 | + example_inputs=(ramp_tensor(-40, 0, (4, 2, 5)),), |
| 153 | + ), |
| 154 | + "linear_rank4": McuTestCase( |
| 155 | + model=CortexMLinear(16, 32), |
| 156 | + example_inputs=(ramp_tensor(-100, 100, (2, 1, 2, 16)),), |
| 157 | + ), |
| 158 | + "linear_rank5": McuTestCase( |
| 159 | + model=CortexMLinear(4, 3), |
| 160 | + example_inputs=(ramp_tensor(-2, 2, (5, 2, 1, 2, 4)),), |
| 161 | + ), |
| 162 | + "linear_bias": McuTestCase( |
| 163 | + model=CortexMLinearBias(61, 37), |
| 164 | + example_inputs=(ramp_tensor(0, 10, (8, 61)),), |
| 165 | + ), |
| 166 | +} |
| 167 | + |
| 168 | +dialect_xfails = { |
| 169 | + "mm": ("torch.mm ops are currently not quantized", RuntimeError), |
| 170 | + "bmm": ("torch.bmm ops are currently not quantized", RuntimeError), |
| 171 | + "addmm": ("torch.addmm ops are currently not quantized", RuntimeError), |
| 172 | + "addmm_scalars": ("torch.addmm ops are currently not quantized", RuntimeError), |
| 173 | + "matmul": ("torch.matmul ops are currently not quantized", RuntimeError), |
| 174 | + "@-operator": ("@ ops are currently not quantized", RuntimeError), |
| 175 | + "linear_rank1": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 176 | + "linear_rank2_pos": ("name 'int32' is not defined", NameError), |
| 177 | + "linear_rank3_neg": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 178 | + "linear_rank4": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 179 | + "linear_rank5": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 180 | + "linear_bias": ("name 'int32' is not defined", NameError), |
| 181 | +} |
| 182 | + |
| 183 | + |
| 184 | +@parametrize("test_case", test_cases, dialect_xfails) |
| 185 | +def test_dialect_linear(test_case): |
| 186 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 187 | + tester.test_dialect( |
| 188 | + test_case.model.ops_before_transforms, test_case.model.ops_after_transforms |
| 189 | + ) |
| 190 | + |
| 191 | + |
| 192 | +implementation_xfails = { |
| 193 | + "mm": ("torch.mm ops are currently not quantized", RuntimeError), |
| 194 | + "bmm": ("torch.bmm ops are currently not quantized", RuntimeError), |
| 195 | + "addmm": ("torch.addmm ops are currently not quantized", RuntimeError), |
| 196 | + "addmm_scalars": ("torch.addmm ops are currently not quantized", RuntimeError), |
| 197 | + "matmul": ("torch.matmul ops are currently not quantized", RuntimeError), |
| 198 | + "@-operator": ("@ ops are currently not quantized", RuntimeError), |
| 199 | + "linear_rank1": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 200 | + "linear_rank2_pos": ("Output 0 does not match reference output.", AssertionError), |
| 201 | + "linear_rank3_neg": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 202 | + "linear_rank4": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 203 | + "linear_rank5": ("Only rank 2 linear ops are fused currently", RuntimeError), |
| 204 | + "linear_bias": ("Output 0 does not match reference output.", AssertionError), |
| 205 | +} |
| 206 | + |
| 207 | + |
| 208 | +@parametrize("test_case", test_cases, implementation_xfails) |
| 209 | +def test_implementation_linear(test_case): |
| 210 | + tester = CortexMTester(test_case.model, test_case.example_inputs) |
| 211 | + tester.test_implementation() |
0 commit comments