Skip to content

Channels last: absorb boundary layout copies into the method contract - #22034

Open
rascani wants to merge 1 commit into
gh/rascani/8/headfrom
gh/rascani/22/head
Open

Channels last: absorb boundary layout copies into the method contract#22034
rascani wants to merge 1 commit into
gh/rascani/8/headfrom
gh/rascani/22/head

Conversation

@rascani

@rascani rascani commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

A layout region formed by ToContiguousChannelsLastPass is bracketed by
channels_last.permute_copy. Interior copies cancel against each other, but
the ones sitting on a method input or output have nothing to cancel against
and survive. On the 36-case matrix in
backends/transforms/test/test_to_contiguous_channels_last_pass.py they cost
+18 permutes against a no-layout-pass baseline of 138.

This pass deletes those copies and declares the corresponding input or output
channels-last instead, which moves the transpose to the caller — free whenever
the caller already holds the data in that layout. That takes the in-graph count
to 137, one below the baseline.

Read that number carefully, because most of it is a transfer rather than a
saving. Absorption removes 19 permutes from the graph and creates 17 obligations
on the caller; only the difference is genuinely eliminated, and it comes from
fan-out, where one placeholder feeding several branches needs several copies but
only one contract entry. For a caller that already holds NHWC the other 17 are
free, and for a delegate the boundary is internal so they disappear entirely,
but neither is true by construction. The test pins both totals, since an
in-graph count on its own could be driven to zero by handing the caller
unlimited work.

Two things about the design are worth stating because the obvious alternatives
are worse. First, ordering: permuting the boundary up front and hoping the
copies cancel during formation only reaches 151, and regresses more cases than
it fixes, because graphs with no layout anchors get copies inserted that nothing
can ever cancel. Absorbing after formation is what pays. Second, fan-out: a
residual block feeds one placeholder to several branches, each with its own
copy, and skipping those leaves most of the win on the table — handling them is
the difference between 141 and 137.

The copy need not be adjacent to the boundary. Quantized graphs interpose a
per-tensor quantize or dequantize, which reorders nothing, so the search walks
through those and relabels them on the way. Per-channel quantization is
excluded: its axis is dimension-dependent.

Because this changes what callers must pass and what they get back, the pass
reports the applied permutations rather than leaving them to be inferred. No
in-tree pipeline runs it yet.

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/22034

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

❌ 1 New Failure, 6 Unrelated Failures

As of commit 6a5005d 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.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant