Skip to content

Commit d8e9394

Browse files
Cortex-M backend: Enable test linting (pytorch#18489)
Signed-off-by: Adrian Lundell <adrian.lundell@arm.com>
1 parent 8422125 commit d8e9394

16 files changed

Lines changed: 72 additions & 66 deletions

.lintrunner.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ exclude_patterns = [
376376
'scripts/check_binary_dependencies.py',
377377
'profiler/test/test_profiler_e2e.py',
378378
'backends/arm/test/ops/*.py',
379-
'backends/cortex_m/test/**/*.py',
380379
]
381380
command = [
382381
'python',
@@ -410,7 +409,6 @@ include_patterns = [
410409
exclude_patterns = [
411410
'third-party/**',
412411
'**/third-party/**',
413-
'backends/cortex_m/test/**/*.py',
414412
]
415413
command = [
416414
'python',

backends/cortex_m/test/misc/test_portable_int8.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from executorch.backends.arm._passes import FoldAndAnnotateQParamsPass
1616
from executorch.backends.arm._passes.arm_pass_utils import get_first_fake_tensor
1717
from executorch.backends.arm.quantizer.arm_quantizer_utils import SharedQspecQuantizer
18-
from executorch.backends.arm.test.common import parametrize
18+
from executorch.backends.arm.test.common import parametrize, xfail_type
1919
from executorch.backends.cortex_m.quantizer.quantizer import CortexMQuantizer
2020
from executorch.backends.cortex_m.test.tester import CortexMTester
2121
from executorch.backends.test.harness.stages import StageType
@@ -660,7 +660,7 @@ def _quantize_and_export(
660660
),
661661
}
662662

663-
xfails = {
663+
xfails: dict[str, xfail_type] = {
664664
"contiguous": "MLETORCH-1863: Contiguos no-op is removed in to-edge, leading to unnecessary Q-DQ-Q-DQ chain.",
665665
"clamp": "MLETORCH-1864: Support non-fused clamp-type activations.",
666666
"clamp_tensor": "MLETORCH-1864: Support non-fused clamp-type activations.",

backends/cortex_m/test/misc/test_quantization.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import torch
88
from executorch.backends.arm._passes.arm_pass_utils import get_first_fake_tensor
9-
from executorch.backends.arm.test.common import parametrize
9+
from executorch.backends.arm.test.common import parametrize, xfail_type
1010
from executorch.backends.cortex_m.test.tester import (
1111
CortexMTester,
1212
McuTestCase,
@@ -141,8 +141,8 @@ def forward(self, x, y):
141141
class SharedQspecInputForkXConstant(torch.nn.Module):
142142
"""Shared qspec cluster with an input fork with left input as global constant."""
143143

144-
ops_before_transforms = {}
145-
ops_after_transforms = {}
144+
ops_before_transforms: dict[str, int] = {}
145+
ops_after_transforms: dict[str, int] = {}
146146
constant = torch.tensor(5.0)
147147

148148
def forward(self, x):
@@ -152,8 +152,8 @@ def forward(self, x):
152152
class SharedQspecInputForkYConstant(torch.nn.Module):
153153
"""Shared qspec cluster with an input fork with left input as local constant."""
154154

155-
ops_before_transforms = {}
156-
ops_after_transforms = {}
155+
ops_before_transforms: dict[str, int] = {}
156+
ops_after_transforms: dict[str, int] = {}
157157

158158
def forward(self, x):
159159
return torch.maximum(x, torch.tensor(5.0))
@@ -259,8 +259,8 @@ def forward(self, x):
259259

260260

261261
class SharedQspecSurroundedQuantizedOpConstant(torch.nn.Module):
262-
ops_before_transforms = {}
263-
ops_after_transforms = {}
262+
ops_before_transforms: dict[str, int] = {}
263+
ops_after_transforms: dict[str, int] = {}
264264

265265
def forward(self, x):
266266
x1 = torch.clone(x)
@@ -270,16 +270,16 @@ def forward(self, x):
270270

271271

272272
class SharedQspecSub(torch.nn.Module):
273-
ops_before_transforms = {}
274-
ops_after_transforms = {}
273+
ops_before_transforms: dict[str, int] = {}
274+
ops_after_transforms: dict[str, int] = {}
275275

276276
def forward(self, x, y):
277277
return torch.clone(x - y)
278278

279279

280280
class SharedQspecCompetingQspecs(torch.nn.Module):
281-
ops_before_transforms = {}
282-
ops_after_transforms = {}
281+
ops_before_transforms: dict[str, int] = {}
282+
ops_after_transforms: dict[str, int] = {}
283283

284284
def __init__(self):
285285
super().__init__()
@@ -299,8 +299,8 @@ def forward(self, x):
299299

300300

301301
class SharedQspecNoQspecs(torch.nn.Module):
302-
ops_before_transforms = {}
303-
ops_after_transforms = {}
302+
ops_before_transforms: dict[str, int] = {}
303+
ops_after_transforms: dict[str, int] = {}
304304

305305
def forward(self, x):
306306
z = torch.clone(x - x)
@@ -358,7 +358,7 @@ def forward(self, x):
358358
),
359359
}
360360

361-
xfails = {
361+
xfails: dict[str, xfail_type] = {
362362
"surrounded_quantized_op_constant": "Numerical error since the add is forced to have non-correct qparams.",
363363
}
364364

backends/cortex_m/test/models/test_mobilenet_v2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from executorch.backends.cortex_m.test.tester import CortexMTester, McuTestCase
1212
from executorch.backends.test.harness.stages import StageType
13-
from torchvision import models
13+
from torchvision import models # type: ignore[import-untyped]
1414

1515

1616
ops_before_transforms: dict[str, int] = {
@@ -56,7 +56,7 @@
5656

5757
@parametrize("test_case", test_cases)
5858
def test_dialect_mv2(test_case):
59-
inputs = test_case.example_inputs()
59+
inputs = test_case.get_example_inputs()
6060
tester = CortexMTester(test_case.model, inputs)
6161
tester.test_dialect(
6262
ops_before_transforms,
@@ -78,7 +78,7 @@ def test_dialect_mv2(test_case):
7878
strict=False,
7979
)
8080
def test_implementation_mv2(test_case):
81-
inputs = test_case.example_inputs()
81+
inputs = test_case.get_example_inputs()
8282
tester = CortexMTester(test_case.model, inputs)
8383
tester.test_implementation(
8484
qtol=10,

backends/cortex_m/test/models/test_mobilenet_v3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from executorch.backends.cortex_m.test.tester import CortexMTester, McuTestCase
1111
from executorch.backends.test.harness.stages import StageType
12-
from torchvision import models
12+
from torchvision import models # type: ignore[import-untyped]
1313

1414

1515
ops_before_transforms: dict[str, int] = {
@@ -65,7 +65,7 @@
6565
strict=False,
6666
)
6767
def test_dialect_mv3(test_case):
68-
inputs = test_case.example_inputs()
68+
inputs = test_case.get_example_inputs()
6969
tester = CortexMTester(test_case.model, inputs)
7070
tester.test_dialect(
7171
ops_before_transforms,
@@ -89,7 +89,7 @@ def test_dialect_mv3(test_case):
8989
strict=False,
9090
)
9191
def test_implementation_mv3(test_case):
92-
inputs = test_case.example_inputs()
92+
inputs = test_case.get_example_inputs()
9393
tester = CortexMTester(test_case.model, inputs)
9494
tester.test_implementation(
9595
qtol=20,

backends/cortex_m/test/models/test_nn_modules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525

2626
import torch
27-
from executorch.backends.arm.test.common import parametrize
27+
from executorch.backends.arm.test.common import parametrize, xfail_type
2828
from executorch.backends.cortex_m.test.tester import (
2929
CortexMTester,
3030
McuTestCase,
@@ -188,7 +188,7 @@ def forward(self, x):
188188
),
189189
}
190190

191-
xfails = {
191+
xfails: dict[str, xfail_type] = {
192192
"conv_add_relu": "Activation fusion does not support relu after add",
193193
}
194194

backends/cortex_m/test/ops/test_add.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
import torch
8-
from executorch.backends.arm.test.common import parametrize
8+
from executorch.backends.arm.test.common import parametrize, xfail_type
99
from executorch.backends.cortex_m.test.tester import (
1010
CortexMTester,
1111
McuTestCase,
@@ -152,10 +152,10 @@ class CortexMAlphaAdd(ModelAlpha):
152152
}
153153

154154

155-
xfails_implementation = {
155+
xfails_implementation: dict[str, xfail_type] = {
156156
"alpha": "Expecting kwargs for aten op IR to be empty - alpha arg not supported.",
157157
}
158-
xfails_dialect = xfails_implementation | {
158+
xfails_dialect: dict[str, xfail_type] = xfails_implementation | {
159159
# Cortex-M quantizer will not quantize additions that require broadcasting
160160
# leading to the add op not being replaced by a cortex-m specific implementation
161161
"broadcast_1": "Broadcasting is not supported in Cortex-M backend",

backends/cortex_m/test/ops/test_conv.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
55

66

77
import torch
8-
from executorch.backends.arm.test.common import parametrize
8+
from executorch.backends.arm.test.common import parametrize, xfail_type
99
from executorch.backends.cortex_m.test.tester import (
1010
CortexMTester,
1111
McuTestCase,
@@ -14,8 +14,8 @@
1414

1515

1616
class CortexMConv1D(torch.nn.Module):
17-
ops_before_transforms = {}
18-
ops_after_transforms = {}
17+
ops_before_transforms: dict[str, int] = {}
18+
ops_after_transforms: dict[str, int] = {}
1919

2020
def __init__(self, *args, **kwargs):
2121
super().__init__()
@@ -72,9 +72,8 @@ def forward(self, x):
7272

7373

7474
class CortexMConv3D(torch.nn.Module):
75-
ops_before_transforms = {}
76-
77-
ops_after_transforms = {}
75+
ops_before_transforms: dict[str, int] = {}
76+
ops_after_transforms: dict[str, int] = {}
7877

7978
def __init__(self, *args, **kwargs):
8079
super().__init__()
@@ -313,7 +312,7 @@ def forward(self, x):
313312
}
314313

315314

316-
xfails_dialect = {
315+
xfails_dialect: dict[str, xfail_type] = {
317316
"conv2d_dilation": "NotImplementedError: 'slow_conv_dilated<>' not implemented for 'Int'",
318317
"conv1d": "Currently not supported.",
319318
"conv2d_nchw": "Currently not supported.",
@@ -330,7 +329,7 @@ def test_dialect_conv2d(test_case):
330329
)
331330

332331

333-
xfails_implementation = {
332+
xfails_implementation: dict[str, xfail_type] = {
334333
"conv1d": "Currently not supported.",
335334
"conv3d": "Currently not supported.",
336335
}

backends/cortex_m/test/ops/test_conv_transpose.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

78

89
import torch
9-
from executorch.backends.arm.test.common import parametrize
10+
from executorch.backends.arm.test.common import parametrize, xfail_type
1011
from executorch.backends.cortex_m.test.tester import (
1112
CortexMTester,
1213
McuTestCase,
@@ -220,7 +221,7 @@ def forward(self, x):
220221
),
221222
}
222223

223-
xfails_dialect = {
224+
xfails_dialect: dict[str, xfail_type] = {
224225
# Grouped convolutions not supported by CMSIS-NN - rejected during quantization
225226
"conv_transpose2d_groups_2": "Grouped transpose conv not supported by CMSIS-NN",
226227
"conv_transpose2d_depthwise": "Depthwise transpose conv not supported by CMSIS-NN",
@@ -248,7 +249,7 @@ def test_dialect_conv_transpose2d(test_case):
248249
# Implementation xfails: empty because unsupported configurations are now
249250
# rejected at AOT time by the quantizer filter, so they fall back to portable
250251
# ops and work correctly. Only xfails_dialect needs to track these.
251-
xfails_implementation = {}
252+
xfails_implementation: dict[str, xfail_type] = {}
252253

253254

254255
@parametrize("test_case", test_cases, xfails=xfails_implementation)

backends/cortex_m/test/ops/test_lstm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -30,7 +30,7 @@ class CortexMLSTM(torch.nn.Module):
3030
"executorch_exir_dialects_edge__ops_aten_cat_default": 1,
3131
}
3232

33-
ops_after_transforms = {}
33+
ops_after_transforms: dict[str, int] = {}
3434

3535
def __init__(self, input_size: int = 4, hidden_size: int = 3) -> None:
3636
super().__init__()
@@ -59,7 +59,7 @@ class CortexMQuantizableLSTM(torch.nn.Module):
5959
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 27,
6060
}
6161

62-
ops_after_transforms = {}
62+
ops_after_transforms: dict[str, int] = {}
6363

6464
def __init__(self, input_size: int = 4, hidden_size: int = 3) -> None:
6565
super().__init__()

0 commit comments

Comments
 (0)