step==1 contiguous fast path in portable compute_slice#19606
Open
pssrawat wants to merge 1 commit into
Open
Conversation
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
🔗 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 SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ No FailuresAs of commit 81e9bb9 with merge base 4c474af ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
|
@pssrawat has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105241644. |
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.
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