Skip to content

step==1 contiguous fast path in portable compute_slice#19606

Open
pssrawat wants to merge 1 commit into
pytorch:mainfrom
pssrawat:export-D105241644
Open

step==1 contiguous fast path in portable compute_slice#19606
pssrawat wants to merge 1 commit into
pytorch:mainfrom
pssrawat:export-D105241644

Conversation

@pssrawat
Copy link
Copy Markdown
Contributor

Summary:
When step == 1 (the common case: tensor.narrow, x[a:b], KV cache reads, etc.),
the per-row slice is a single contiguous block of length*length_per_step bytes.
Replace the inner loop of length separate memcpy(length_per_step) calls with a
single bulk memcpy.

For length=1 slices: equivalent (1 memcpy either way).
For length>1: ~2-10x speedup of the slice itself (fewer function calls, better
cache prefetch, SIMD-friendly bulk copy).

Llama4 speech encoder hot-path: mask.narrow (12x/chunk), freqs_cos/sin.narrow
(2x/chunk), KV reads (~5x/layer/chunk). 62 slice_copy/chunk * 72 chunks =
~4500 slices per audio prefill.

Differential Revision: D105241644

Summary:
When step == 1 (the common case: tensor.narrow, x[a:b], KV cache reads, etc.),
the per-row slice is a single contiguous block of length*length_per_step bytes.
Replace the inner loop of length separate memcpy(length_per_step) calls with a
single bulk memcpy.

For length=1 slices: equivalent (1 memcpy either way).
For length>1: ~2-10x speedup of the slice itself (fewer function calls, better
cache prefetch, SIMD-friendly bulk copy).

Llama4 speech encoder hot-path: mask.narrow (12x/chunk), freqs_cos/sin.narrow
(2x/chunk), KV reads (~5x/layer/chunk). 62 slice_copy/chunk * 72 chunks =
~4500 slices per audio prefill.

Differential Revision: D105241644
@pssrawat pssrawat requested a review from manuelcandales as a code owner May 14, 2026 22:31
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 14, 2026

🔗 Helpful Links

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

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:

✅ No Failures

As of commit 81e9bb9 with merge base 4c474af (image):
💚 Looks good so far! There are no failures yet. 💚

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 May 14, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 14, 2026

@pssrawat has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105241644.

@pssrawat pssrawat added the release notes: none Do not include this in the release notes label May 15, 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. fb-exported meta-exported release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant