- 
                Notifications
    
You must be signed in to change notification settings  - Fork 712
 
Cortex_m backend: Add cortex_m tester + test_add #14510
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # 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. | ||
| 
     | 
||
| 
     | 
||
| from dataclasses import dataclass | ||
| from typing import Any | ||
| 
     | 
||
| import torch | ||
| 
     | 
||
| from executorch.backends.arm.quantizer.arm_quantizer import ( | ||
| get_symmetric_quantization_config, | ||
| TOSAQuantizer, | ||
| ) | ||
| from executorch.backends.arm.test.common import get_u55_compile_spec | ||
| from executorch.backends.arm.test.tester.arm_tester import Serialize | ||
| from executorch.backends.cortex_m.passes.quantized_op_fusion_pass import ( | ||
| QuantizedOpFusionPass, | ||
| ) | ||
| 
     | 
||
| from executorch.backends.cortex_m.passes.replace_quant_nodes_pass import ( | ||
| ReplaceQuantNodesPass, | ||
| ) | ||
| from executorch.backends.test.harness import Tester as TesterBase | ||
| from executorch.backends.test.harness.stages import ( | ||
| Export, | ||
| Quantize, | ||
| RunPasses, | ||
| StageType, | ||
| ToEdgeTransformAndLower, | ||
| ToExecutorch, | ||
| ) | ||
| from executorch.backends.xnnpack._passes import XNNPACKPassManager | ||
| 
     | 
||
| 
     | 
||
| class CortexMQuantize(Quantize): | ||
| def __init__(self): | ||
| compile_spec = get_u55_compile_spec() | ||
| quantizer = TOSAQuantizer(compile_spec) | ||
| config = get_symmetric_quantization_config() | ||
| 
     | 
||
| super().__init__(quantizer, config) | ||
| 
     | 
||
| 
     | 
||
| class CortexMRunPasses(RunPasses): | ||
| def __init__(self): | ||
| super().__init__( | ||
| XNNPACKPassManager, pass_list=[QuantizedOpFusionPass, ReplaceQuantNodesPass] | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| class CortexMSerialize(Serialize): | ||
| def __init__(self): | ||
| compile_spec = get_u55_compile_spec() | ||
| super().__init__(compile_spec, 1024) | ||
| 
     | 
||
| 
     | 
||
| cortex_m_stage_classes = { | ||
| StageType.EXPORT: Export, | ||
| StageType.QUANTIZE: CortexMQuantize, | ||
| StageType.RUN_PASSES: CortexMRunPasses, | ||
| StageType.SERIALIZE: Serialize, | ||
| StageType.TO_EDGE_TRANSFORM_AND_LOWER: ToEdgeTransformAndLower, | ||
| StageType.TO_EXECUTORCH: ToExecutorch, | ||
| StageType.SERIALIZE: CortexMSerialize, | ||
| } | ||
| 
     | 
||
| 
     | 
||
| class CortexMTester(TesterBase): | ||
| def __init__(self, module, example_inputs): | ||
| super().__init__(module, example_inputs, cortex_m_stage_classes) | ||
| 
     | 
||
| def test_dialect(self, ops_before_transforms, ops_after_transforms, qtol=0): | ||
| """ | ||
| Test the python dialect op implementation. | ||
| """ | ||
| self.quantize() | ||
| self.export() | ||
| self.to_edge_transform_and_lower() | ||
| self.check_count(ops_before_transforms) | ||
| self.run_passes() | ||
| self.check_count(ops_after_transforms) | ||
| self.run_method_and_compare_outputs(inputs=self.example_inputs, qtol=qtol) | ||
| 
     | 
||
| def test_implementation(self, qtol=0): | ||
| """ | ||
| Test the optimized op implementation in simulation | ||
| """ | ||
| self.quantize() | ||
| self.export() | ||
| self.to_edge_transform_and_lower() | ||
| self.run_passes() | ||
| self.to_executorch() | ||
| self.serialize() | ||
| self.run_method_and_compare_outputs(inputs=self.example_inputs, qtol=qtol) | ||
| 
     | 
||
| 
     | 
||
| @dataclass | ||
| class McuTestCase: | ||
| model: torch.nn.Module | ||
| example_inputs: tuple[Any] | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # 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. | 
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,175 @@ | ||||||
| # 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.cortex_m_tester import CortexMTester, McuTestCase | ||||||
| from executorch.backends.test.suite.operators.test_add import Model, ModelAlpha | ||||||
| 
     | 
||||||
| 
     | 
||||||
| class SelfAdd(torch.nn.Module): | ||||||
                
       | 
||||||
| class SelfAdd(torch.nn.Module): | |
| class SelfCortexMAdd(torch.nn.Module): | 
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.
I added CortexM as a prefix to all models here instead, I prefer to keep to one same naming scheme and not put the target description part of the name (CortexM) in the middle of the model description part (SelfAdd).
Uh oh!
There was an error while loading. Please reload this page.