Skip to content

[ExecuTorch][WebGPU] et_vk.sdpa: shape-route QK to a per-entry kernel for channel attention (15-30x faster)#20871

Open
JCNTH wants to merge 1 commit into
gh/JCNTH/46/basefrom
gh/JCNTH/46/head
Open

[ExecuTorch][WebGPU] et_vk.sdpa: shape-route QK to a per-entry kernel for channel attention (15-30x faster)#20871
JCNTH wants to merge 1 commit into
gh/JCNTH/46/basefrom
gh/JCNTH/46/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

Problem: the fused et_vk.sdpa QK kernel runs one thread per (b,h,s) row with vec4 loads — ideal for standard attention, but on channel attention (DaViT/Florence, where S_q = head_dim ~= 32) num_rows = B*H*S_q is tiny, so only a handful of workgroups run serially over a huge S_kv*D, starving the GPU (the (2,1,1)@103ms dispatch).

Solution: add a per-entry QK kernel (one thread per (b,h,s,c) attention entry, 2D-folded) and host-route to it when num_rows is below an occupancy floor (4096); standard attention keeps the per-row + vec4 path unchanged.

Before: et_vk_sdpa_qk (per-row, vec4) — the only QK kernel; channel-attn shapes are occupancy-starved.
After: router picks et_vk_sdpa_qk_entry (per-entry, scalar, 2D-folded) for small num_rows, else the unchanged per-row kernel.

Implementation:

  • New et_vk_sdpa_qk_entry.wgsl (+ generated header) — same bindings and Params as the per-row kernel, so it is a drop-in under layout:"auto"; writes a layout-identical attn[B,H,S_q,S_kv] (attn[idx]), so softmax/AV are unchanged and either branch is numerically correct — the floor is a pure perf knob.
  • EtVkSdpa.cpp selects the shader and a 2D dispatch (compute_2d_workgroup_count, mirroring the softmax grid) when routed, else the existing 1D per-row dispatch; the grid + dispatch-limit check is computed up front (throw before any buffer alloc -> no leak).
  • Mirrors the codebase's host shape-router precedents (LinearFp32.cpp K%4 vec4 selection, Sdpa.cpp variant selection).

Constraints: per-entry drops vec4, so it only wins when the per-row path is occupancy-starved (small num_rows); the 4096 floor is Canary-tuned.

Co-authored-with: Claude Code.

Differential Revision: D110994975

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit 269817d with merge base aceeb40 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was 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.

@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.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants