Skip to content

cuda: the graph's decode attention sums its live splits alone - #60

Merged
yhirose merged 2 commits into
masterfrom
cuda-dpos-live-splits
Nov 7, 2026
Merged

yhirose merged 2 commits into
masterfrom
cuda-dpos-live-splits

Conversation

@yhirose

@yhirose yhirose commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Follow-up 2 of #51. The graph form of the decode attention launches S_max splits, sized at the cache's capacity, so one graph serves every position. Past the live count each split still staged and wrote a neutral partial, and tl_attn_combine read all S_max of them (Gemma 4 E2B's 512-wide layers: 1.3 MB written and read a layer at any depth, with the default 4096 capacity).

  • attn_dec_core: an empty range (a dpos split past the live count) writes its heads' m alone, at attn_m_empty, and returns before any barrier. attn_m_empty names the softmax's starting m, which the core and the combine share.
  • tl_attn_combine: the max still reads every split's m (16 loads in flight, as before) and counts up to the last split whose m is not empty; the sums stop there. Live splits come first, so this is the live count, read from the partials themselves: no d_pos, no split rule, no second combine kernel. The host path's splits are never empty, so it sums all S as before.

The sums run over the same splits in the same order as the host path's, so the output stays bit-identical to it.

Two forms tried first and dropped:

  • a combine that derives the live count from d_pos (attn_dpos_chunk again): the load and the rule's divisions sit before its sums, +0.3 to +0.6 us a call where every split is live;
  • skipping an empty split inside the sums loop: the branch on each loaded m serializes the loads, +1 us.

GPU time a call (graph replay over 8 caches, capacity 4096, f16 cache, median of 5 alternating pairs, RTX 3090)

shape (D, q/kv heads, window) cached before after
512, 8/1 (Gemma 4 E2B global) 128 16.07 us 10.59 us
512, 8/1 512 17.70 12.71
512, 8/1 2048 19.97 19.01
256, 8/1, 512 (E2B local) 128 6.47 5.60
256, 8/1, 512 512 6.63 6.66
512, 16/1 (Gemma 4 12B global) 128 14.51 10.95
256, 16/8, 1024 (12B local) 128 9.06 7.69
256, 16/8, 1024 2048 17.19 16.87
512, 32/4 (31B global) 2048 38.58 38.57
64, 14/2 (Qwen2.5 0.5B) 128 8.43 6.01
64, 14/2 512 8.48 6.79
64, 14/2 2048 11.10 11.24
64, 32/8 (Llama 3.2 1B) 128 7.24 5.62
64, 32/8 512 8.72 8.42
64, 32/8 2048 15.54 15.83

Where every split is live (2048 cached at 4096 capacity) the change is -0.4 to +0.3 us; the Llama 1B shape keeps +0.27 us (its 5 pairs +0.24 to +0.39).

End to end (bench_qwen_llama, tok/s, alternating rounds)

  • Qwen2.5 0.5B F16, tg256 at depth 0, 8 rounds: before 491-504 (median 497), after 501-518 (median 510); every pair faster, +1.2% to +4.5%.
  • Llama 3.2 1B Q4_K_M, tg256 at depth 2048, 8 rounds: before 523-530, after 521-527; pairs -0.8% to +0.3%, median -0.3%.
  • tg128 at depth 0 and 2048, 4 rounds, Gemma 4 E2B, Gemma 4 12B, Qwen2.5 0.5B, Llama 3.2 1B: all within this box's noise (about 5% a run).

Tests

  • ctest passes (22 s; attn64 0.6-0.7 s, as before). attn64's host-vs-dpos sweep is bit-equal at every checkpoint for all 6 shapes, and cuda_ukernel still finds no local memory.
  • Mutants: an empty split that writes no m fails the sweep in all 6 shapes (the windowed passes after the unwindowed one leave its later splits' partials behind). A combine that sums all S_max still passes, as it should: an empty split weighs 0, so that form only reads more.
  • The dpos outputs hash the same before and after at all 15 shapes above, and equal the host path's.
  • check_qwen gives the same results as master on 17 files: ALL OK on all but qwen2.5-0.5b-instruct-bf16q.gguf, whose "final norm" check fails on master too (bf16-rounded weights against the F16 oracle).

Metal has no graph form of this attention, so nothing changes there.

The graph form launches S_max splits, sized at the cache's capacity, so
that one graph serves every position. Past the live count each split
still staged and wrote a neutral partial, and tl_attn_combine read all
S_max of them: at 4096 cached tokens of capacity, Gemma 4 E2B's 512-wide
layers wrote and read 1.3 MB a layer at any depth, and a short cache paid
for the capacity's splits.

- attn_dec_core: an empty range (a dpos split past the live count) writes
  its heads' m alone, at attn_m_empty, and returns before any barrier.
  attn_m_empty names the softmax's starting m, which the core and the
  combine share.
- tl_attn_combine: the max still reads every split's m (16 loads in
  flight, as before), and counts up to the last split whose m is not
  empty; the sums stop there. Live splits come first, so that is the live
  count, read from the partials themselves: no d_pos, no rule, no second
  combine kernel. The host path's splits are never empty, so it sums all S.

The sums run over the same splits in the same order as the host path's,
so the output stays bit-identical to it.

Two forms tried first and dropped: a combine that derives the live count
from d_pos (the load and the rule's divisions sit before its sums, +0.3
to +0.6 us a call where every split is live), and one that skips an
empty split inside the sums loop (the branch on each loaded m serializes
the loads, +1 us).
Cleanups from /simplify on the live-splits change.

- The empty split's rule (its m alone, at attn_m_empty; empty splits come
  after the rest, so the combine's sums stop after the last that is not)
  is part of the partials' layout, and is now said once, on
  attn_partials. The kernels and launcher that relied on it in their own
  words point there.
- attn_dec_core's warp merge starts from attn_m_empty, as its states do,
  rather than a second spelling of -1e30.
- attn64's comment says what the empty m guards: the combine never reads
  the partials the windowed passes leave behind.

The PTX is the same, instruction for instruction.
@yhirose
yhirose merged commit 7b74b8d into master Nov 7, 2026
9 checks passed
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