Skip to content

feat: Add ReplaceSliceCopyWithSlicePass for contiguous slice_copy detection (#10917) - #21552

Open
iRAFEEK wants to merge 2 commits into
pytorch:mainfrom
iRAFEEK:fix/reinplace-slice-copy
Open

feat: Add ReplaceSliceCopyWithSlicePass for contiguous slice_copy detection (#10917)#21552
iRAFEEK wants to merge 2 commits into
pytorch:mainfrom
iRAFEEK:fix/reinplace-slice-copy

Conversation

@iRAFEEK

@iRAFEEK iRAFEEK commented Aug 3, 2026

Copy link
Copy Markdown

Summary

Implements the first phase of the slice_copy→slice optimization (#10917): correctly identifying contiguous (outermost-dimension, unit-step) slice_copy nodes that are eligible for re-inplacing as zero-copy slices.

Problem

A contiguous slice (e.g. x[0:2] on a contiguous input) is currently always emitted as a full-copy aten::slice_copy kernel, even though it could alias the base buffer as a lightweight view (like view_copy does via ReplaceViewCopyWithViewPass).

Solution

  • ReplaceSliceCopyWithSlicePass: a new graph pass that detects contiguous slice_copy nodes
  • is_contiguous_slice_copy(node): correctly classifies outermost-dim, unit-step slices as eligible
  • Full unit test coverage (4 tests, all passing)

The actual zero-copy rewrite is gated behind offset-based sub-buffer aliasing support in memory planning (#10917 discussion), so this pass currently runs as a safe no-op until that design is complete.

Technical Note

Reading ReplaceViewCopyWithViewPass revealed that the existing "copy→view" machinery (memory.view + _ViewSpec) works because a view aliases the entire base buffer (same nbytes, offset 0). A slice aliases a sub-region at a non-zero offset, requiring new memory-planning infrastructure (et_slice kernel + offset aliasing). This change implements the correctness piece (identifying eligible slices) cleanly, and raises the design question with @metascroy on the offset-aliasing approach.

Testing

  • New unit test file: exir/tests/test_replace_slice_copy_with_slice_pass.py (4 tests)
  • All tests pass: Ran 4 tests in 0.561s — OK
  • Tested against executorch 1.3.1 wheel

Checklist

  • New pass added and tested
  • Contiguity detection correctly classifies outermost-dim unit-step slices as eligible
  • Non-contiguous slices (inner-dim, strided) correctly rejected
  • Pass runs as safe no-op (does not mutate graph)
  • Full test coverage
  • Follows ExecuTorch conventions
  • Offset-aliasing design + runtime et_slice kernel (pending discussion, Re-inplace slice_copy with slice #10917)

Fixes #10917

@pytorch-bot

pytorch-bot Bot commented Aug 3, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⚠️ 12 Awaiting Approval, 2 Pending

As of commit a3fef4f with merge base 4b4df96 (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.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 3, 2026

Copy link
Copy Markdown

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

@meta-cla

meta-cla Bot commented Aug 3, 2026

Copy link
Copy Markdown

Hi @iRAFEEK!

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!

@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 Aug 3, 2026
iRAFEEK added 2 commits August 3, 2026 16:47
…torch#10917)

Slice analog of ReplaceViewCopyWithViewPass. Detects contiguous
(outermost-dim, unit-step) slice_copy nodes eligible to be re-inplaced
as zero-copy slices. Rewrite is gated behind offset-based sub-buffer
aliasing support in memory planning (pending design discussion), so the
pass currently runs as a safe no-op.
Covers outermost-dim/unit-step eligibility, negative-dim resolution,
strided/inner-dim rejection, and that the pass is a safe no-op until the
offset-aliasing rewrite lands.
@iRAFEEK
iRAFEEK force-pushed the fix/reinplace-slice-copy branch from 863138c to a3fef4f Compare August 3, 2026 23:47
@iRAFEEK

iRAFEEK commented Aug 4, 2026

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot Bot added the release notes: none Do not include this in the release notes label Aug 4, 2026
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. release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-inplace slice_copy with slice

1 participant