-
Notifications
You must be signed in to change notification settings - Fork 695
Cortex-M backend: Add mul and linear tests #14746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
42923f3
Cortex-M backend: Add mul and linear tests
AdrianLundell c92bc98
Merge branch 'main' into change-1116679
AdrianLundell 8b87f8a
Merge branch 'main' into change-1116679
AdrianLundell 2d25792
Merge branch 'main' of https://github.com/pytorch/executorch into cha…
AdrianLundell 048b0c0
Correct import path
AdrianLundell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
import torch | ||
from executorch.backends.arm.test.common import parametrize | ||
from executorch.backends.cortex_m.test.tester import ( | ||
CortexMTester, | ||
McuTestCase, | ||
ramp_tensor, | ||
) | ||
|
||
|
||
class CortexMMm(torch.nn.Module): | ||
def forward(self, x, y): | ||
return torch.mm(x, y) | ||
|
||
ops_before_transforms = { | ||
"executorch_exir_dialects_edge__ops_aten_mm_default": 1, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, | ||
} | ||
|
||
ops_after_transforms = { | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, | ||
} | ||
|
||
|
||
class CortexMBmm(torch.nn.Module): | ||
def forward(self, x, y): | ||
return torch.bmm(x, y) | ||
|
||
ops_before_transforms = { | ||
"executorch_exir_dialects_edge__ops_aten_bmm_default": 1, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, | ||
} | ||
|
||
ops_after_transforms = { | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, | ||
} | ||
|
||
|
||
class CortexMAddmm(torch.nn.Module): | ||
def forward(self, x, y, z, alpha=None, beta=None): | ||
return torch.addmm(beta, x, alpha, y, z) | ||
|
||
ops_before_transforms = { | ||
"executorch_exir_dialects_edge__ops_aten_addmm_default": 1, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, | ||
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 3, | ||
} | ||
|
||
ops_after_transforms = { | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantized_linear_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_quantize_per_tensor_default": 1, | ||
"executorch_exir_dialects_edge__ops_cortex_m_dequantize_per_tensor_default": 1, | ||
} | ||
|
||
|
||
class CortexMAt(CortexMMm): | ||
def forward(self, x, y): | ||
return x @ y | ||
|
||
|
||
class CortexMMatmul(CortexMMm): | ||
def forward(self, x, y): | ||
return torch.matmul(x, y) | ||
|
||
|
||
class CortexMLinear(CortexMMatmul): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__() | ||
self.linear = torch.nn.Linear(*args, bias=False) | ||
|
||
def forward(self, x): | ||
return self.linear(x) | ||
|
||
|
||
class CortexMLinearBias(CortexMAddmm): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__() | ||
self.linear = torch.nn.Linear(*args, bias=True) | ||
|
||
def forward(self, x): | ||
return self.linear(x) | ||
|
||
|
||
test_cases = { | ||
"mm": McuTestCase( | ||
model=CortexMMm(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
), | ||
), | ||
"bmm": McuTestCase( | ||
model=CortexMBmm(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16, 16)), | ||
ramp_tensor(0, 10, (1, 16, 16)), | ||
), | ||
), | ||
"addmm": McuTestCase( | ||
model=CortexMAddmm(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
2, | ||
4, | ||
), | ||
), | ||
"addmm_scalars": McuTestCase( | ||
model=CortexMAddmm(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
), | ||
), | ||
"@-operator": McuTestCase( | ||
model=CortexMAt(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
), | ||
), | ||
"matmul": McuTestCase( | ||
model=CortexMMatmul(), | ||
example_inputs=( | ||
ramp_tensor(0, 10, (1, 16)), | ||
ramp_tensor(0, 10, (16, 16)), | ||
), | ||
), | ||
"linear_rank1": McuTestCase( | ||
model=CortexMLinear(2, 3), | ||
example_inputs=(ramp_tensor(-1, 1, (2,)),), | ||
), | ||
"linear_rank2_pos": McuTestCase( | ||
model=CortexMLinear(8, 3), | ||
example_inputs=(ramp_tensor(0, 10, (2, 8)),), | ||
), | ||
"linear_rank3_neg": McuTestCase( | ||
model=CortexMLinear(5, 3), | ||
example_inputs=(ramp_tensor(-40, 0, (4, 2, 5)),), | ||
), | ||
"linear_rank4": McuTestCase( | ||
model=CortexMLinear(16, 32), | ||
example_inputs=(ramp_tensor(-100, 100, (2, 1, 2, 16)),), | ||
), | ||
"linear_rank5": McuTestCase( | ||
model=CortexMLinear(4, 3), | ||
example_inputs=(ramp_tensor(-2, 2, (5, 2, 1, 2, 4)),), | ||
), | ||
"linear_bias": McuTestCase( | ||
model=CortexMLinearBias(61, 37), | ||
example_inputs=(ramp_tensor(0, 10, (8, 61)),), | ||
), | ||
} | ||
|
||
dialect_xfails = { | ||
"mm": ("torch.mm ops are currently not quantized", RuntimeError), | ||
"bmm": ("torch.bmm ops are currently not quantized", RuntimeError), | ||
"addmm": ("torch.addmm ops are currently not quantized", RuntimeError), | ||
"addmm_scalars": ("torch.addmm ops are currently not quantized", RuntimeError), | ||
"matmul": ("torch.matmul ops are currently not quantized", RuntimeError), | ||
"@-operator": ("@ ops are currently not quantized", RuntimeError), | ||
"linear_rank1": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank2_pos": ("name 'int32' is not defined", NameError), | ||
"linear_rank3_neg": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank4": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank5": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_bias": ("name 'int32' is not defined", NameError), | ||
} | ||
|
||
|
||
@parametrize("test_case", test_cases, dialect_xfails) | ||
def test_dialect_linear(test_case): | ||
tester = CortexMTester(test_case.model, test_case.example_inputs) | ||
tester.test_dialect( | ||
test_case.model.ops_before_transforms, test_case.model.ops_after_transforms | ||
) | ||
|
||
|
||
implementation_xfails = { | ||
"mm": ("torch.mm ops are currently not quantized", RuntimeError), | ||
"bmm": ("torch.bmm ops are currently not quantized", RuntimeError), | ||
"addmm": ("torch.addmm ops are currently not quantized", RuntimeError), | ||
"addmm_scalars": ("torch.addmm ops are currently not quantized", RuntimeError), | ||
"matmul": ("torch.matmul ops are currently not quantized", RuntimeError), | ||
"@-operator": ("@ ops are currently not quantized", RuntimeError), | ||
"linear_rank1": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank2_pos": ("Output 0 does not match reference output.", AssertionError), | ||
"linear_rank3_neg": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank4": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_rank5": ("Only rank 2 linear ops are fused currently", RuntimeError), | ||
"linear_bias": ("Output 0 does not match reference output.", AssertionError), | ||
} | ||
|
||
|
||
@parametrize("test_case", test_cases, implementation_xfails) | ||
def test_implementation_linear(test_case): | ||
tester = CortexMTester(test_case.model, test_case.example_inputs) | ||
tester.test_implementation() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️