Skip to content

Preserve logical element order when lowering view_copy - #24152

Open
zingo wants to merge 2 commits into
pytorch:mainfrom
zingo:exir-Normalize-input-layouts-before-lowering-view_copy
Open

zingo wants to merge 2 commits into
pytorch:mainfrom
zingo:exir-Normalize-input-layouts-before-lowering-view_copy

Conversation

@zingo

@zingo zingo commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Only replace view_copy with a shared-storage view when its input is
contiguous. Retain mutation safety checks and copy channels-last
inputs in logical order in the portable kernel, keeping the existing
memcpy fast path for contiguous inputs.

Test plan

Cover layout and view-removal configurations, safe contiguous views,
and the Cortex-M patch-token regression.

Authored with assistance from OpenAI Codex.

cc @digantdesai @freddan80 @per @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani

@pytorch-bot

pytorch-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 3 Cancelled Jobs, 6 Unrelated Failures

As of commit ffb9430 with merge base 05092c1 (image):

NEW FAILURE - The following job has failed:

CANCELLED JOBS - The following jobs were cancelled. Please retry:

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 26, 2026
@zingo zingo added partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm release notes: arm Changes to the ARM backend delegate ciflow/trunk help wanted Extra attention is needed labels Oct 26, 2026
@rascani

rascani commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

I’d prefer to enforce this in view lowering instead of adding another correctness requirement behind _skip_dim_order. Whether view_copy can become memory.view depends on the input layout, regardless of which operator representation we use.

Concretely, I suggest:

  1. Make layout compatibility a precondition of ReplaceViewCopyWithViewPass, alongside its mutation checks. Keep view_copy when aliasing would change element order.
  2. Update portable view_copy_out to handle channels-last inputs by copying in logical element order, retaining the existing memcpy fast path for contiguous inputs. Keeping the copy alone is insufficient because the current kernel rejects those layouts.
  3. Remove LegalizeViewCopyPass and its _skip_dim_order gate. The allocation-metadata cleanup can remain independently.

For validation, parameterize the channels-last flatten runtime test over both _skip_dim_order and remove_view_copy, and check exact output values in all four combinations. Also retain a contiguous-input check proving that safe views still avoid copying.

Only replace view_copy with a shared-storage view when its input is
contiguous. Retain mutation safety checks and copy channels-last
inputs in logical order in the portable kernel, keeping the existing
memcpy fast path for contiguous inputs.

Cover layout and view-removal configurations, safe contiguous views,
and the Cortex-M patch-token regression.

Authored with assistance from OpenAI Codex.

Change-Id: Icdc64484edb8085cdb15411f38c00d001232d9a9
Signed-off-by: Zingo Andersen <Zingo.Andersen@arm.com>
@zingo
zingo force-pushed the exir-Normalize-input-layouts-before-lowering-view_copy branch from db3c6d3 to 260036a Compare October 29, 2026 17:00
@zingo zingo changed the title exir: Normalize input layouts before lowering view_copy Preserve logical element order when lowering view_copy Oct 29, 2026
@zingo

zingo commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

@rascani would this update match your idea, the new PR turned out quite differently, I should maybe started a new PR instead of force pushing to the same, sorry about that.

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

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. help wanted Extra attention is needed partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm release notes: arm Changes to the ARM backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants