Cortex-M: add an opt-in NHWC method I/O contract - #22029
Draft
rascani wants to merge 3 commits into
Draft
Conversation
Contributor
Author
🔗 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 FailuresAs of commit 5642344 with merge base 8b93850 ( 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. |
This was referenced Aug 21, 2026
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
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.