Skip to content

A model's SDPA over repeat_kv copies takes the shared K/V heads (pre-grad enable_gqa) - #527

Merged
student-Jungmin merged 3 commits into
pytorchsim-triton-backendfrom
pr/pts-3-gqa-shared-kv
Nov 10, 2026
Merged

student-Jungmin merged 3 commits into
pytorchsim-triton-backendfrom
pr/pts-3-gqa-shared-kv

Conversation

@student-Jungmin

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #526.

A model's SDPA over repeat_kv's copies now hands the fused kernel the shared K/V heads. transformers calls repeat_kv whenever it gives SDPA a mask, and the dispatcher picks the fused kernel during AOT tracing, so by post-grad no SDPA node is left and the kernel was traced over the H×R copies (Qwen3-8B decode at 1024: 16 MiB of K and V read, not 4). A pre-grad pass (pregrad.py, joining pre_grad_custom_pass after any pass already there, npu nodes only) rewrites sdpa(q, reshape(expand(k)), reshape(expand(v)), mask) to sdpa(q, k, v, mask, enable_gqa=True) while the SDPA node still exists. The post-grad gqa_shared_kv / gqa_shared_kv_fused rewrites, which only rebuilt eager matmul attention, go: every model here runs sdpa (or torchsim::attention, which takes unrepeated heads).

Also: the fused decode kernels take an attention mask, and one_layer hands a layer no mask where the TPU row passes none.

Verification

test_gqa_repeat_kv: the kernel is planned over the 2 shared key heads for 8 query heads (decode and prefill), values match the CPU; it fails on the tree before (planned over 8). A Qwen3 decoder layer compiled with a mask: the post-grad graph has no K/V clone, the kernel gets G: 2 (was 1).

…V layout option is headwise or tokenwise

A HuggingFace layer decoding over a filled KV cache hands SDPA a mask. fused_plan declined every masked L 1 decode, so the call fell back to eager math, which ran repeat_kv in fp32: a copy kernel wrote the eight KV heads out as 32 and the matmuls read them back, about 17 times the KV traffic of the TPU row. _decode_kernel and the tokenwise _paged_decode_kernel now read the mask the way the prefill kernel does, through _bias_as_strides (bool or additive, broadcast or per batch, head and key), with each program's row base taken from its query heads. The Decline is gone. Qwen3-8B layer decode 1024 went from 536,239 to 511,110 cycles on that change alone.

TORCHSIM_KVCACHE_LAYOUT's second value is renamed from paged to tokenwise, after the layout it selects (every KV head of a token side by side, as v6e's ragged paged attention reads it), the pair of headwise. paged now raises.

test_fused_sdpa decodes masked on both kernels: a broadcast mask (bool, additive, padded keys), a per-head one, and a random per-batch, per-head mask at B 2, Hq 8, H 2, S 256, bool and additive, headwise and paged (err about 2.7e-3 against an explicit float32 reference that takes the same mask). The masked decode cases fail on the tree before.
…, and keeps the reference exact

The TPU v6e rows pass no mask: decode is ragged paged attention over the shared KV heads, prefill is causal splash attention. one_layer handed HF layers the mask the model builds, which at decode is all true and at prefill is pure causal, so HF's sdpa path ran repeat_kv (enable_gqa is used only without a mask) and the kernels read 32 heads, and prefill read a mask tile instead of the causal flag. _drop_plain_mask drops a 4-D mask that says only what no mask does (all keys at L 1, causal at L == S, bool or min-valued additive, identical over batch); a sliding window, padding or any other value keeps it.

A softcap layer's CPU reference goes through the model's eager attention, which is causal only through its mask, so with the mask dropped it attended to every key (Gemma2 prefill compared a causal device output with a non-causal reference, max diff 5.4 on a tiny model). The eager reference now builds the causal band, with its window, when the mask is None.

Qwen3-8B decode 1024 reads 8 KV heads with G 4 and no mask tile; prefill takes CAUSAL True. Pins: tests/system/test_one_layer_mask.py and test_one_layer_softcap_mask.py, each failing on the tree before.
…V heads: a pre-grad pass sets enable_gqa

transformers' sdpa attention calls repeat_kv whenever it hands SDPA a mask (enable_gqa only without one), and
the dispatcher picks the fused kernel at AOT time, so by post-grad there is no SDPA node left: the kernel was
traced over the H*R copies (G=1) and post-grad gqa_shared_kv_fused never fired on a model call. pregrad.py
rewrites sdpa(q, reshape(expand(kv[:, :, None])), ...) on the npu to sdpa(q, k, v, ..., enable_gqa=True)
while the SDPA node still exists. It joins pre_grad_custom_pass, which is one slot for every device, after
any pass already there.

Post-grad gqa_shared_kv (bmm) and gqa_shared_kv_fused go: they only rebuilt eager matmul attention, and
every model here runs sdpa (or torchsim::attention, which takes unrepeated heads).

test_gqa_repeat_kv: the last plan (AOT's) is over the 2 shared key heads for 8 query heads, decode and
prefill, values match the CPU. On the tree before: every plan over 8 heads, fails.
@student-Jungmin
student-Jungmin changed the base branch from pr/pts-0-prereq to pytorchsim-triton-backend November 10, 2026 18:06
@student-Jungmin
student-Jungmin merged commit cf5b0a3 into pytorchsim-triton-backend Nov 10, 2026
@YWHyuk
YWHyuk deleted the pr/pts-3-gqa-shared-kv branch November 19, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant