Skip to content

Cortex-M: add an opt-in NHWC method I/O contract - #22029

Draft
rascani wants to merge 3 commits into
gh/rascani/14/headfrom
gh/rascani/21/head
Draft

Cortex-M: add an opt-in NHWC method I/O contract#22029
rascani wants to merge 3 commits into
gh/rascani/14/headfrom
gh/rascani/21/head

Conversation

@rascani

@rascani rascani commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Explicit-layout lowering currently emits two cortex_m::transpose nodes per
model, one flipping the method input into NHWC and one flipping the output
back. Legacy lowering emits one, because it carries layout in dim-order
metadata and so needs no output flip. On that count the explicit path is a
regression, and it is the one the RFC asks us to close.

The new use_nhwc_io flag runs AbsorbBoundaryLayoutCopies straight after region
formation, which deletes both transposes and declares the method to take and
return NHWC instead. On Conv2d the transpose count goes 1 (legacy), 2
(explicit), 0 (explicit + use_nhwc_io).

It is a separate flag rather than part of use_explicit_layout because the two
are not equivalent changes. Legacy already hands back a channels-last output —
it just says so in dim order rather than in the shape — so absorbing the output
copy only makes an existing contract visible. Absorbing the input copy is a
real break: legacy takes plain NCHW. Folding that into use_explicit_layout would
mean explicit layout could never become the default without simultaneously
flipping every caller's input layout, so the flags stay separate and the manager
refuses use_nhwc_io on its own.

Because the layout copies are not adjacent to the boundary here — a per-tensor
quantize and dequantize sit in between — this relies on the absorption pass
walking through layout-agnostic quantization nodes.

Two things worth knowing for review. The pass must make the retyped meta["val"]
contiguous: a bare permute keeps the old strides, which serialize as a permuted
dim order, and quantized_conv2d_nhwc then rejects the tensor with 0x12 at
runtime. Nothing in Python catches that, since eager execution reads the logical
shape and never consults the serialized order; only the FVP run found it. And
the planned-memory test baselines against legacy rather than against explicit
layout: absorbing places two fewer tensors but can still total more, because
removing nodes reshuffles the greedy planner's packing. It ties legacy on all
five models measured.

CortexMTester keeps tests in NCHW terms by permuting inputs on the way in and
restating the reference output in the method's layout, so existing helpers and
expectations are unaffected.

Authored with Claude Code.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 9 Unrelated Failures

As of commit 5642344 with merge base 8b93850 (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 21, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

[ghstack-poisoned]
rascani added a commit that referenced this pull request Aug 21, 2026
Explicit-layout lowering currently emits two cortex_m::transpose nodes per
model, one flipping the method input into NHWC and one flipping the output
back. Legacy lowering emits one, because it carries layout in dim-order
metadata and so needs no output flip. On that count the explicit path is a
regression, and it is the one the RFC asks us to close.

The new use_nhwc_io flag runs AbsorbBoundaryLayoutCopies straight after region
formation, which deletes both transposes and declares the method to take and
return NHWC instead. On Conv2d the transpose count goes 1 (legacy), 2
(explicit), 0 (explicit + use_nhwc_io).

It is a separate flag rather than part of use_explicit_layout because the two
are not equivalent changes. Legacy already hands back a channels-last output —
it just says so in dim order rather than in the shape — so absorbing the output
copy only makes an existing contract visible. Absorbing the *input* copy is a
real break: legacy takes plain NCHW. Folding that into use_explicit_layout would
mean explicit layout could never become the default without simultaneously
flipping every caller's input layout, so the flags stay separate and the manager
refuses use_nhwc_io on its own.

Because the layout copies are not adjacent to the boundary here — a per-tensor
quantize and dequantize sit in between — this relies on the absorption pass
walking through layout-agnostic quantization nodes.

Two things worth knowing for review. The pass must make the retyped meta["val"]
contiguous: a bare permute keeps the old strides, which serialize as a permuted
dim order, and quantized_conv2d_nhwc then rejects the tensor with 0x12 at
runtime. Nothing in Python catches that, since eager execution reads the logical
shape and never consults the serialized order; only the FVP run found it. And
the planned-memory test baselines against legacy rather than against explicit
layout: absorbing places two fewer tensors but can still total more, because
removing nodes reshuffles the greedy planner's packing. It ties legacy on all
five models measured.

CortexMTester keeps tests in NCHW terms by permuting inputs on the way in and
restating the reference output in the method's layout, so existing helpers and
expectations are unaffected.

Authored with Claude Code.
ghstack-source-id: e21bd94
ghstack-comment-id: 5374180041
Pull-Request: #22029
[ghstack-poisoned]
rascani added a commit that referenced this pull request Aug 21, 2026
Explicit-layout lowering currently emits two cortex_m::transpose nodes per
model, one flipping the method input into NHWC and one flipping the output
back. Legacy lowering emits one, because it carries layout in dim-order
metadata and so needs no output flip. On that count the explicit path is a
regression, and it is the one the RFC asks us to close.

The new use_nhwc_io flag runs AbsorbBoundaryLayoutCopies straight after region
formation, which deletes both transposes and declares the method to take and
return NHWC instead. On Conv2d the transpose count goes 1 (legacy), 2
(explicit), 0 (explicit + use_nhwc_io).

It is a separate flag rather than part of use_explicit_layout because the two
are not equivalent changes. Legacy already hands back a channels-last output —
it just says so in dim order rather than in the shape — so absorbing the output
copy only makes an existing contract visible. Absorbing the *input* copy is a
real break: legacy takes plain NCHW. Folding that into use_explicit_layout would
mean explicit layout could never become the default without simultaneously
flipping every caller's input layout, so the flags stay separate and the manager
refuses use_nhwc_io on its own.

Because the layout copies are not adjacent to the boundary here — a per-tensor
quantize and dequantize sit in between — this relies on the absorption pass
walking through layout-agnostic quantization nodes.

Two things worth knowing for review. The pass must make the retyped meta["val"]
contiguous: a bare permute keeps the old strides, which serialize as a permuted
dim order, and quantized_conv2d_nhwc then rejects the tensor with 0x12 at
runtime. Nothing in Python catches that, since eager execution reads the logical
shape and never consults the serialized order; only the FVP run found it. And
the planned-memory test baselines against legacy rather than against explicit
layout: absorbing places two fewer tensors but can still total more, because
removing nodes reshuffles the greedy planner's packing. It ties legacy on all
five models measured.

CortexMTester keeps tests in NCHW terms by permuting inputs on the way in and
restating the reference output in the method's layout, so existing helpers and
expectations are unaffected.

Authored with Claude Code.
ghstack-source-id: e21bd94
ghstack-comment-id: 5374180041
Pull-Request: #22029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: arm Issues related to arm backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant