Skip to content

Cortex-M: Don't replace channels-first conv with channels-last kernel - #23943

Open
rahimuddin140 wants to merge 1 commit into
pytorch:mainfrom
rahimuddin140:fix/cortex-m-channels-first
Open

rahimuddin140 wants to merge 1 commit into
pytorch:mainfrom
rahimuddin140:fix/cortex-m-channels-first

Conversation

@rahimuddin140

@rahimuddin140 rahimuddin140 commented Oct 17, 2026 •

Copy link
Copy Markdown

Fixes #23812. _get_convolution_replacement replaced any batch-1 depthwise (or regular) quantized conv with cortex_m::quantized_depthwise_conv2d / quantized_conv2d without verifying the input layout, but those kernels require channels-last input and output (checked at runtime). A channels-first input — e.g. from x.contiguous() or mean(dim=2) — produced a graph that exported fine (the fake unconditionally reports a channels-last output) and then failed at runtime with 'input and output must be channels_last'. Now the pass checks the input's dim order with the existing is_channels_last() util and leaves non-channels-last convs un-replaced so they fall back to portable ops. The explicit-NHWC pipeline is unaffected (uses the _nhwc kernels). Test plan: new dialect tests covering a channels-first depthwise conv (via .contiguous() and via mean-pooling) asserting no replacement, and a channels-last control asserting replacement still happens.

cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani @nil-is-all @psiddh @AdrianLundell

@pytorch-bot

pytorch-bot Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23943

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 14 Awaiting Approval

As of commit 72455f8 with merge base 596a5a8 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Oct 17, 2026

Copy link
Copy Markdown

Hi @rahimuddin140!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 17, 2026
@Erik-Lundell Erik-Lundell changed the title fix(cortex-m): don't replace channels-first conv with channels-last kernel Cortex-M: Don't replace channels-first conv with channels-last kernel Oct 19, 2026

@Erik-Lundell Erik-Lundell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Could you include the tests I attached (preferably the ones that don't require adding more kernels to the test runner). The fix additionally targets transposed and normal convolution, so please add tests for those as well.

I also dug more into the issue. It is actually a bit hard to recover here than it looks, since the FoldAndAnnotateQParamsPass has folded away quantization nodes. This means that the existing fp kernel will break if it is left as is. That pass is not specific to the Cortex-M backend, so it is tricky to modify.

I think a proper solution actually needs to insert a layout op. Otherwise, raising early with a good error message could be the easy way out. What do you think?

/Erik

@nil-is-all nil-is-all added module: microcontrollers For embedded MCUs like Cortex-M, or RTOS like Zephyr, does not track NPU backend like Arm Ethos. module: arm Issues related to arm backend labels Oct 21, 2026
@nil-is-all nil-is-all added the need-user-input The issue needs more information from the reporter before moving forward label Oct 21, 2026
@nil-is-all

Copy link
Copy Markdown
Contributor

Thanks for the fix! Could you include the tests I attached (preferably the ones that don't require adding more kernels to the test runner). The fix additionally targets transposed and normal convolution, so please add tests for those as well.

I also dug more into the issue. It is actually a bit hard to recover here than it looks, since the FoldAndAnnotateQParamsPass has folded away quantization nodes. This means that the existing fp kernel will break if it is left as is. That pass is not specific to the Cortex-M backend, so it is tricky to modify.

I think a proper solution actually needs to insert a layout op. Otherwise, raising early with a good error message could be the easy way out. What do you think?

/Erik

@rahimuddin140 gentle ping to address these, thanks

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community: contribution PRs coming from community (excluding hardware partners) module: arm Issues related to arm backend module: microcontrollers For embedded MCUs like Cortex-M, or RTOS like Zephyr, does not track NPU backend like Arm Ethos. need-user-input The issue needs more information from the reporter before moving forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cortex-M backend replaces channels-first dw conv with channels-last dw conv without handling the changed layout, fails at runtime

3 participants