Skip to content

[ExecuTorch][WebGPU] Coalesce SDPA AV V-cache reads along contiguous head-dim#20459

Open
JulianCloudNTH wants to merge 3 commits into
gh/JulianCloudNTH/54/basefrom
gh/JulianCloudNTH/54/head
Open

[ExecuTorch][WebGPU] Coalesce SDPA AV V-cache reads along contiguous head-dim#20459
JulianCloudNTH wants to merge 3 commits into
gh/JulianCloudNTH/54/basefrom
gh/JulianCloudNTH/54/head

Conversation

@JulianCloudNTH

@JulianCloudNTH JulianCloudNTH commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

~19% faster SDPA attention-output (AV) stage — 393→317 µs on llama3 prefill (Chrome Canary / M4 Pro).

Problem: V-cache reads load 4 strided context rows × 1 head-dim lane, missing coalescing.

Solution: Flip access pattern to read 4 contiguous head-dim lanes per context row:

  • Before: load_v_vec4(d, kvh, c4) → 4 strided rows, dot() along D
  • After: load_v_d4(c, kvh, d0) → 4 contiguous D-lanes (16-byte texel), scalar broadcast

Implementation:

  • Reindex load_v helper to read contiguous head-dim
  • Replace dot(A, V) with acc += A[c] * V_vec4(d0:d0+3)
  • Mirrors Vulkan load_v_cache_d4 coalescing pattern

Constraints:

  • No KV-cache layout change (still [C, Hkv, D])
  • Output numerically identical (FP-reassociated, max abs diff 1.43e-6 vs torch)
    @exported-using-ghexport

Differential Revision: D109339276

Differential Revision: D109339276

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 2 Active SEVs

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

❌ 1 New Failure, 2 Unrelated Failures

As of commit 25551a5 with merge base e03f777 (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.

@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 Jun 23, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

JulianCloudNTH added a commit that referenced this pull request Jun 23, 2026
…head-dim

Pull Request resolved: #20459

**~19% faster SDPA attention-output (AV) stage** — 393→317 µs on llama3 prefill (Chrome Canary / M4 Pro).

**Problem**: V-cache reads load 4 strided context rows × 1 head-dim lane, missing coalescing.

**Solution**: Flip access pattern to read 4 contiguous head-dim lanes per context row:
- **Before**: `load_v_vec4(d, kvh, c4)` → 4 strided rows, `dot()` along D
- **After**: `load_v_d4(c, kvh, d0)` → 4 contiguous D-lanes (16-byte texel), scalar broadcast

**Implementation**:
- Reindex `load_v` helper to read contiguous head-dim
- Replace `dot(A, V)` with `acc += A[c] * V_vec4(d0:d0+3)`
- Mirrors Vulkan `load_v_cache_d4` coalescing pattern

**Constraints**:
- No KV-cache layout change (still `[C, Hkv, D]`)
- Output numerically identical (FP-reassociated, max abs diff 1.43e-6 vs torch)
ghstack-source-id: 395771238
@exported-using-ghexport

Differential Revision: [D109339276](https://our.internmc.facebook.com/intern/diff/D109339276/)
[ghstack-poisoned]

@SS-JIA SS-JIA left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

[ghstack-poisoned]
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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants