Skip to content

cuda window attention: a decode row merges and computes partials only over chunks it has keys in - #3123

Open
ashhart wants to merge 6 commits into
mainfrom
lead/window-rows-live-chunks
Open

ashhart wants to merge 6 commits into
mainfrom
lead/window-rows-live-chunks

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

CUDA decode attention (window_attention, shared by Qwen3.5/3.8, Llama, MiniMax-M3, Spark 2.5, Step5 and LFM): window_rows merged every chunk's partial for every row, 128 chunks at the default 32K context, though a row at position p only has keys in the chunks up to p/CK. Two commits:

  1. The merge stops at the last chunk a row has keys in. An empty chunk's partial is -inf with zero weight, so the bytes are the same.
  2. The partials kernel strides over a row's live chunks, with grid z fixed by the row count, so captured graphs stay valid and every live chunk's partial is computed by the unchanged body.

Tests: test-window-rows-host 10/10; test-runtime-host 329 after part 1 and 330 after part 2; compile-cuda-tests passes; CUDA source and fragment checks clean.

Device check pending (GB10): tf-cuda-test kernel-contract window_rows window_pages (the 4 new equality cases byte-equal, controls red), tf-cuda-test window-rows, kernel-contract-limits with register records updated if the kernels moved, and a Qwen3.8-27B decode A/B with long contexts and equal token hashes.

@ashhart ashhart changed the title cuda window_rows: a decode row merges and computes partials only over chunks it has keys in cuda window attention: a decode row merges and computes partials only over chunks it has keys in Nov 13, 2026
@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner Author

GB10 results on this head: tf-cuda-test window-rows passes, every output bit-equal to float64's value or within its range. kernel-contract window_rows window_pages has two problems, and both need fixing before this lands:

  1. The four new "bounded merge equals every chunk's" cases (tf_wrows_merge_512 at 16/1 heads and tf_wrows_merge_256 at 16/4 heads, contexts 32768 and 131072) are refused as "not a served shape", so they never compare. They need served shapes.
  2. Register counts rose: tf_wrows_part_256 104 to 128, tf_wrows_part_512 125 to 128, the paged twins 108 to 127 and 122 to 128, tf_wrows_merge_512 62 to 64. We'll see whether the stride change can keep the old counts. If it can't, the records move, along with a decode A/B at long contexts to show speed holds.

We're on both.

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/window-rows-live-chunks branch 2 times, most recently from 24a9c26 to 7683b1c Compare November 13, 2026 20:20
ashhart added a commit that referenced this pull request Nov 13, 2026
…ols (the live-chunk count, as the kernels they mirror)
ashhart added a commit that referenced this pull request Nov 18, 2026
…ols (the live-chunk count, as the kernels they mirror)
@ashhart
ashhart force-pushed the lead/window-rows-live-chunks branch from ad68ae2 to 3936d28 Compare November 18, 2026 19:43
ashhart added a commit that referenced this pull request Nov 21, 2026
…ols (the live-chunk count, as the kernels they mirror)
@ashhart
ashhart force-pushed the lead/window-rows-live-chunks branch from 3936d28 to fa0fb87 Compare November 21, 2026 11:57
ashhart added a commit that referenced this pull request Nov 22, 2026
…ols (the live-chunk count, as the kernels they mirror)
@ashhart
ashhart force-pushed the lead/window-rows-live-chunks branch from fa0fb87 to 280baa9 Compare November 22, 2026 01:32
… keys in

With no window, chunk c holds keys [c * 256, c * 256 + 256), so every chunk past a row's own position is empty and
its partial is m = -inf, l = 0. The merge now loops to min(chunks, pos / 256 + 1) instead of every chunk: 4 chunks
instead of 128 for a row at position 1000 of a 32K context. An empty chunk changes neither of the merge's loops (the
max ignores -inf and the sum skips it), so every output byte is unchanged; a windowed row keeps every chunk. The one
merge template serves the 256 and 512 kernels, the paged kernels and the contract's controls.

The planted faults move to window_rows_fault.cuh beside liveChunks, with ALL (every chunk, as before) for the new
tf_ctl_wrows_merge_all_256 and _512 controls. Four contract registrations (head dims 512 and 256, no window, contexts
32768 and 131072, rows at positions 0, 255, 256, 1000 and 32767) run the bounded merge and the all-chunk merge on the
same partials and require byte equality.
…, grid z fixed by the row count

part and part_staged ran one block a chunk, grid z = the context's chunks (128 at 32K, 512 at 128K), most of them
empty for a row early in its context. Each block now loops c = blockIdx.z; c < live; c += gridDim.z over the row's
live chunks (with no window, those up to its position; a window keeps every chunk) and calls the unchanged body,
part_one or part_staged_one, which now takes c and the chunk count instead of reading them from the grid. Grid z is
partBlocks(rows, key heads x head pairs, chunks): about 1,024 blocks a launch, from the row count and the model's
head shape alone, so a captured decode graph stays valid as positions move. Each written chunk's partial has the
bytes the one-block-a-chunk launch wrote; chunks past a row's live ones are no longer written, and the bounded merge
never reads them.

The kernels take the chunk count after scale (before the page arguments). tf_ctl_wrows_part_all_256 and _512 write
every chunk as before, and the equality registrations now compare the bounded partials and merge against the
all-chunk partials and merge, byte for byte.
…B10 register records

The four equality registrations served exactly five rows (one a test
position) and refused any other count, but gate 4 (gates.sums) runs a
served shape's first four rows at most, so it was refused ("refused a
served shape"). They now take up to five rows, their positions' first
ones, and the positions are ordered so gate 4's four keep the longest
row (32767) and both chunk edges (255, 256); gates 1 to 3 still run all
five.

The partials' stride loop raised the decode kernels' registers on
sm_120/121: the work that does not depend on the chunk (the row's
position, slot and window, its query and base pointers) is computed
once before the loop and kept live across every chunk and barrier,
where the one-chunk body computed and dropped it. The records take the
GB10's counts: tf_wrows_part_256 104 -> 128, tf_wrows_part_512 125 ->
128, tf_wrows_merge_512 62 -> 64, tf_wrows_part_256_pg 108 -> 127,
tf_wrows_part_512_pg 122 -> 128. No occupancy changes: the 256-dim
partials stay at two blocks an SM (46,208 B of shared memory each, and
2 x 256 x 128 registers fit 65,536), the 512-dim at one (82 KiB), the
merge at four.
…ols (the live-chunk count, as the kernels they mirror)
@ashhart
ashhart force-pushed the lead/window-rows-live-chunks branch from 280baa9 to aa456cc Compare November 26, 2026 15:21

This branch has not been deployed

No deployments
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