Cortex-M backend: Refactor ConvertToCortexMPass#20070
Conversation
Change to use the general AtenToDialectPass structure. Removes the 1-1 ensure check as it was to restrictive, cortex-m backend needs to both insert scratch nodes and for BMM an additional transpose at the time of dialect replacement. Bonus small fix: Moves yolo import into test to avoid download at test collection time Signed-off-by: Adrian Lundell <adrian.lundell@arm.com> Change-Id: Ic62964522704d31f75c653bc265bcce02ce375c6
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20070
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Cancelled JobAs of commit c338d86 with merge base e56be3e ( CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
Pull request overview
Refactors the Cortex-M backend’s ATen→dialect lowering to use the shared AtenToDialectPass substitution/decorator structure (replacing ConvertToCortexMPass), and relaxes the prior 1:1 “call_function count preserved” postcondition to allow scratch/aux node insertion (needed for CMSIS-NN scratch buffers and BMM transpose handling). Also adjusts the YOLO test to avoid triggering weights download during test collection.
Changes:
- Remove the
requires()/ensures()“call_function count preserved” check fromAtenToDialectPassand delete the corresponding test. - Introduce
AtenToCortexMPassand wire it into the Cortex-M pass manager in place ofConvertToCortexMPass. - Move YOLO model instantiation/weights loading into the test body to avoid collection-time side effects.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| backends/transforms/test/test_aten_to_dialect_pass.py | Removes the test that asserted the old 1:1 call_function count invariant. |
| backends/transforms/aten_to_dialect_pass.py | Removes the 1:1 substitution invariant enforcement (requires/ensures). |
| backends/cortex_m/test/models/test_yolo11.py | Defers YOLO weights/model creation to test runtime to avoid collection-time downloads. |
| backends/cortex_m/passes/cortex_m_pass_manager.py | Switches the pass pipeline to use AtenToCortexMPass. |
| backends/cortex_m/passes/aten_to_cortex_m_pass.py | New pass implementing Cortex-M dialect substitutions + scratch alloc sizing logic. |
| backends/cortex_m/passes/init.py | Updates public pass imports to expose AtenToCortexMPass. |
Comments suppressed due to low confidence (1)
backends/cortex_m/test/models/test_yolo11.py:28
McuTestCase.modelis typed astorch.nn.Module, but this test passesNonewith a type-ignore. This can hide real issues and could break if future test helpers start usingtest_case.model. Use a lightweight placeholder module instead so the dataclass contract stays valid.
"yolo11n": McuTestCase(
model=None, # type: ignore[arg-type]
example_inputs=lambda: (
torch.randn(1, 3, 640, 640).to(memory_format=torch.channels_last),
),
),
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| raise RuntimeError( | ||
| f"Expected scratch alloc node as final argument(s) for {node.target}, got {scratch_arg}." | ||
| ) | ||
|
|
||
| scratch_arg.args = (((scratch_buffer_size,), torch.uint8),) |
rascani
left a comment
There was a problem hiding this comment.
Can you also update backends/cortex_m/passes/BUCK?
| from .activation_fusion_pass import ActivationFusionPass | ||
| from .aten_to_cortex_m_pass import AtenToCortexMPass | ||
| from .clamp_hardswish_pass import ClampHardswishPass | ||
| from .convert_to_cortex_m_pass import ConvertToCortexMPass |
There was a problem hiding this comment.
Shall we delete convert_to_cortex_m_pass.py?
|
|
||
| class AtenToDialectPass(ExportPass): | ||
| """ | ||
| General pass to convert ops 1-1 from ATen to a specific dialect. |
There was a problem hiding this comment.
Can you update the docstring here since its no longer 1:1?
Change to use the general AtenToDialectPass structure, quantized_op_fusion_pass
to be changed similarly in an upcoming PR.
Removes the 1-1 ensure check as it was to restrictive, cortex-m backend needs to both insert scratch nodes and for BMM an additional transpose at the time of dialect replacement.
Bonus small fix: Moves yolo import into test to avoid download at test collection time
cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani