Skip to content

[ExecuTorch][WebGPU] SDPA: skip QK contraction for fully-masked causal tiles#20492

Open
JulianCloudNTH wants to merge 1 commit into
gh/JulianCloudNTH/62/basefrom
gh/JulianCloudNTH/62/head
Open

[ExecuTorch][WebGPU] SDPA: skip QK contraction for fully-masked causal tiles#20492
JulianCloudNTH wants to merge 1 commit into
gh/JulianCloudNTH/62/basefrom
gh/JulianCloudNTH/62/head

Conversation

@JulianCloudNTH

@JulianCloudNTH JulianCloudNTH commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Skip the QK contraction for fully-masked causal tiles — at S=128 prefill ~48% of the (query, key) tiles are entirely above the diagonal and contribute nothing; this elides their dot products (prefill-only; bit-identical output).

Problem: For causal prefill, ~half the (query S-tile, key context-tile) pairs are entirely above the diagonal, yet the kernel still computes their full d4 dot product before masking the result to NEG_INF.

Solution: Skip the contraction for fully-masked tiles; the existing per-element mask still writes the sentinel:

  • Before: every (s0, c0) tile runs the full d4 dot-product loop, then store_qk masks above-diagonal elements to NEG_INF.
  • After: a fully-masked tile (c0 > s0 + TM-1 + input_pos) breaks the d4 loop immediately (acc stays 0); store_qk masks every element to NEG_INF exactly as before.

Implementation:

  • Add skip_tile = c0 > s0 + (TM - 1) + params.input_pos, folded into the d4 loop break condition.
  • Store loop unchanged — runs unconditionally, so no scratch entry is left stale.
  • Mirrors Vulkan sdpa_compute_attn_weights_tiled.glsl (tile_in_mask_region).

Constraints:

  • No KV-cache, host, dispatch, or uniform change (all tiles still launch; the skip is in-shader).
  • Prefill-only: decode S=1 never triggers it (c0 <= input_pos < input_pos + TM - 1).
  • NEG_INF stays the WGSL-safe -1.0e30 (WGSL forbids a literal -inf); does not copy Vulkan's -1.0/0.0.

Co-authored with Claude Code.
@exported-using-ghexport

Differential Revision: D109517773

Differential Revision: D109517773

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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:

❌ 3 New Failures, 3 Unrelated Failures

As of commit 40acc76 with merge base 0e65ba6 (image):

NEW FAILURES - The following jobs have 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.

@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 JulianCloudNTH requested a review from psiddh June 24, 2026 21:46
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