Skip to content

perf(glm5_next cuda): DFlash2's block attention reads only its sliding window; its context in a ring of that window - #134

Closed
MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/glm-dflash2-window
Closed

MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/glm-dflash2-window

Conversation

@MiaAI-Lab

@MiaAI-Lab MiaAI-Lab commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: every layer of GLM-5.3-Flash's DFlash2 drafter is sliding_attention with a 2,048-row window, but _dattn_kernel read every context row from position 0 and masked all but the window, and the drafter kept capacity rows of keys and values. The block attention now starts at the window's first tile, and the context lives in a ring of window + block + a tile (2,176 rows). Same draft bits; at 196k context the block attention goes from 16.2 ms to 0.20 ms, and the drafter's context memory no longer grows with the window.

The problem

  • _dattn_kernel looped for start in range(0, klen, BK): at context end s it loaded every key and value from 0 to s + block, then masked everything older than s - window. A block pass cost grew linearly with the context although only ~2,048 rows can contribute.
  • Drafter allocated capacity + block rows of K/V for each of its 5 layers: 10 KiB a token on each rank, 10 GiB at a 1,048,576-token window. The startup estimate (draft_geometry, shared with the 27B's drafter) counted two copies of it, 20 GiB.
  • The estimate also counted the draft model's weights as its checkpoint at 4 bytes a value (4.36 GiB a rank). The engine actually holds 4-bit copies (0.63 GiB a rank).

The change

  • Window loop (dflash2._dattn_kernel): the loop starts at the 64-row tile holding s - window. Every skipped tile is masked for every query, and a fully masked tile leaves the running max, sum and accumulator bit for bit unchanged (max(-1e30, -inf) = -1e30, alpha = exp(0) = 1, p = 0). The tiles that still run are the same tiles at the same offsets, so the output has the same bits. SKIP=False keeps the old loop as the reference the tests compare against.
  • Ring (Drafter(ring=True), TF_GLM_DRAFT_RING, default on): keys and values go in a ring of geometry.draft_ring_rows(window, block) = 2,176 rows, addressed modulo the ring (RING in the kernel, Drafter._slots for writes). The drafts are unchanged. When the window already exceeds capacity + block, the flat buffer is kept. TF_GLM_DRAFT_RING=0 keeps the flat buffer, and it joins the two ranks' settings check.
  • Kept prompts (decode): a flat buffer still holds a kept prompt's drafter rows after the reply, but a ring overwrites them. So take_snapshot copies the window (Snapshot.drafter_rows, 20 MiB a rank at DFlash2's sizes), restore puts it back and refuses a ring restore without one, save_rows drops it along with drafter_end, and snapshot_bytes counts it within TF_GLM_CACHE_GIB.
  • Estimate (geometry.dflash2_geometry, geometry.dflash2_weights, capacity.admit(draft_weights=)): one context copy, the one the engine allocates. With the ring it is a constant 21.25 MiB; flat, it is 10,240 B a slot, where it used to be 20,480. The weights are counted as held: 4-bit copies (k and v twice, as in qkv and kv), BF16 norms and selector projection, and the selector's float32 codebooks. The 27B's draft_geometry is untouched.
  • docs/recipes/glm-5.3-flash.md: one paragraph.

Measurements

One DGX Spark (GB10), one rank's dimensions of GLM-5.3-Flash-DFlash2 (16 query heads, 4 KV heads, head_dim 128, block 8). These are kernel-only timings of the block attention, not end-to-end decode:

Context Before (5 layers) After (5 layers)
196k 16.2 ms 0.20 ms
524k 43 ms 0.20 ms

A re-run of the same kernel on this branch, on the same Spark while another model server was resident (one layer, fastest of 7): old loop 0.51 / 6.40 / 17.8 ms at 16k / 196k / 524k, window loop 0.037 ms and ring 0.039 ms at all three. The old loop's absolute times vary with what else holds the memory bus. The new loop's time doesn't depend on the context.

Memory, per rank, at a 1,048,576-token window: the drafter's context is 21 MiB (was 10 GiB allocated and 20 GiB in the estimate). The drafter's weights are estimated at 0.63 GiB (was 4.36 GiB).

Tests

  • tests/cuda/test_glm_draft_ring.py (new, small, no engine): 8 passed on the Spark.
    • The window loop and the ring give bitwise the output of the loop from tile 0, for windows 2,047 and 48, causal and not, and context ends up to 12,345 rows, with stale rows in the ring.
    • The synthetic drafter of test_glm_engine gives the same candidates, logits and selector rows with the ring as with the flat buffer, bit for bit, over 160 rounds. The contexts run from 3.5k to 9k rows, and a kept state is taken, the ring overwritten by 3,000 rows and the state restored. This runs both eager and with CUDA graphs.
  • tests/test_glm_draft_ring.py (new, CPU): the ring size, both estimates (checked against GLM-5.3-Flash-DFlash2's shapes), and the kept-state plumbing. The plumbing check: after a reply has wrapped the ring three times, a restored state reads the same window as a flat drafter and a cold start, a saved state drops the window, and a ring restore without a window is refused.
  • CPU, in the tensorfold CUDA image: test_glm_draft_ring, test_glm_cuda_kept, test_cuda_geometry, test_cuda_capacity, test_cuda_admission, test_cuda_failed_admission, test_cuda_parallel_admission, test_flash_capacity_dispatch, test_glm_drafter_choice, test_glm5_memory, test_glm5_next_family and test_glm_tool_calls: 188 passed, 2 skipped. The whole tests/ directory was not run to completion here: in this image two MLX-only modules fail to collect, and the run was stopped for time.
  • tests/cuda/test_glm_engine.py: 14 passed, 5 errors. The passing tests include DFlash2 replies equal to serial, the drafter choice, and resumed prompts equal to fresh prefills; the ring is on by default there, since 2,176 < 2,560 + 8. The 5 errors are the engine_n (no MTP head, DFlash2 only) and engine_long (past the dense limit) fixtures. Admission refused them ("startup memory budget cannot fit requested context 2600") because another model server was holding that Spark's memory. They didn't run; they did not fail on the change.
  • Not run: two-Spark serving with the real GLM-5.3-Flash and DFlash2 checkpoints on this branch.

…g window; its context in a ring of that window

Every DFlash2 layer is sliding_attention with sliding_window 2048: a context key at distance > 2047 from the
query is masked. _dattn_kernel nevertheless looped over every context tile from position 0, and the drafter kept
`capacity` rows of keys and values, so a block pass cost grew with the context and the drafter's buffer (and its
estimate, which counted two copies) with the window.

- _dattn_kernel starts at the 64-row tile holding s - window. The skipped tiles are masked for every query and
  leave the running max, sum and accumulator bit for bit as they were (max(-1e30, -inf) = -1e30, alpha = 1,
  p = 0); the tiles run are the same tiles at the same offsets, so the output is the same bits.
- Drafter(ring=True), TF_GLM_DRAFT_RING (default 1): keys and values in a ring of draft_ring_rows(2047, 8) =
  2,176 rows (window + block + a tile, whole tiles) addressed modulo the ring; the same drafts.
  TF_GLM_DRAFT_RING=0 keeps the flat buffer; both ranks compare it with their other settings.
- decode: take_snapshot copies a ring drafter's window (drafter_rows, 20 MiB a rank at GLM-5.3-Flash-DFlash2's
  sizes) since the reply overwrites it in the ring; restore puts it back; save_rows drops it with drafter_end;
  snapshot_bytes counts it against TF_GLM_CACHE_GIB.
- geometry.dflash2_geometry: the one context copy the engine allocates, a constant 21.25 MiB ring (was 20,480 B
  a slot: 20 GiB estimated at a 1,048,576 window). geometry.dflash2_weights / admit(draft_weights=): the drafter
  as held (4-bit copies, BF16 norms and selector projection, float32 codebooks): 0.63 GiB a rank instead of
  4.36 GiB (the checkpoint at 4 bytes a value).

One DGX Spark (GB10), kernel only, all 5 layers at one rank's dims: block attention 16.2 -> 0.20 ms at 196k
context, 43 -> 0.20 ms at 524k.

Tests: tests/cuda/test_glm_draft_ring.py (the window loop and the ring equal the loop from tile 0 bitwise up to
12,345 rows, stale ring rows included; the synthetic drafter's candidates, logits and selector rows bitwise
equal flat vs ring over 3.5k-9k contexts, with a kept state restored after the ring wrapped, eager and CUDA
graphs); tests/test_glm_draft_ring.py (ring size, estimates, kept-state plumbing on CPU).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014wCj5zx4NuKdUNWUeJi3X1
ashhart added a commit that referenced this pull request Sep 30, 2026
…keeps DFlash2's ring window (#134)

- The lane and prompt matmuls build for Ada (sm_89); sm_90 and newer compile the same kernels as before
- CUDA serves on 8.9, NVFP4 and FP8 checkpoints keep a 9.0 floor, and a GPU below its floor is refused at startup
- The 27B's startup estimate counts what it loads, and its verify scratch the rows a round takes
- The 27B's prompt chunk comes from the GPU's memory: the most rows, up to 4,096, whose arrays fit a 32nd of it
- Clones of a 27B state share one attention list, so a grow frees the buffers it replaces
- On one GPU the 27B's kept prompt states give way, oldest first, so admission counts one live window
- GLM-5.3 on CUDA: a prompt kept one token early keeps DFlash2's ring window, so resends resume with it on (#134)
- The Nemotron resume test expects prompts kept one token early (#98)
- The vision clone test expects the shared attention list and the carried byte budget
@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks for this. DFlash2's block attention on GLM now reads only its sliding window, with the context kept in a ring. At 52k tokens that DFlash2 decode went from 37.1 tok/s to 40.1, and the startup estimate from 96.91 GiB to 93.11 GiB a rank. Every reply matched, a resend and the next turn included, and a dip on a 7k prompt did not reproduce. This is in 0.6.0, out tonight.

@ashhart ashhart closed this Sep 30, 2026
ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Oct 2, 2026
…ROCm)

It came in with ashhart#134 after this PR's ROCm skips; it calls qmm.matmul, which raises on HIP by design. On the Strix it
was the only failing GPU-side test file (2 failed), on 0.6.0 and on 0.6.1 alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Oct 2, 2026
…ROCm)

It came in with ashhart#134 after this PR's ROCm skips; it calls qmm.matmul, which raises on HIP by design. On the Strix it
was the only failing GPU-side test file (2 failed), on 0.6.0 and on 0.6.1 alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Oct 3, 2026
…ROCm)

It came in with ashhart#134 after this PR's ROCm skips; it calls qmm.matmul, which raises on HIP by design. On the Strix it
was the only failing GPU-side test file (2 failed), on 0.6.0 and on 0.6.1 alike.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…keeps DFlash2's ring window (ashhart#134)

- The lane and prompt matmuls build for Ada (sm_89); sm_90 and newer compile the same kernels as before
- CUDA serves on 8.9, NVFP4 and FP8 checkpoints keep a 9.0 floor, and a GPU below its floor is refused at startup
- The 27B's startup estimate counts what it loads, and its verify scratch the rows a round takes
- The 27B's prompt chunk comes from the GPU's memory: the most rows, up to 4,096, whose arrays fit a 32nd of it
- Clones of a 27B state share one attention list, so a grow frees the buffers it replaces
- On one GPU the 27B's kept prompt states give way, oldest first, so admission counts one live window
- GLM-5.3 on CUDA: a prompt kept one token early keeps DFlash2's ring window, so resends resume with it on (ashhart#134)
- The Nemotron resume test expects prompts kept one token early (#98)
- The vision clone test expects the shared attention list and the carried byte budget
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.

2 participants