Skip to content

perf(cuda): a q8_0 recurrent-state cache takes the fused snapshot write - #13

Merged
marcospaulo merged 2 commits into
mainfrom
feat/gdn-q8-cache-fusion
Sep 23, 2026
Merged

marcospaulo merged 2 commits into
mainfrom
feat/gdn-q8-cache-fusion

Conversation

@marcospaulo

@marcospaulo marcospaulo commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

With -cts q8_0, every Gated DeltaNet layer used to write its new state as f32 into the GDN output. A separate cpy_f32_q8_0 then quantized it into the cache: 48 launches per step at ~14 µs each. The kernel now quantizes as it stores:

  • One q8_0 block is one warp-wide slice of a state column, so the block scale is a warp max.
  • The formula is cpy's quantize_f32_q8_0_block, so the cache holds the same bytes the unfused pair wrote.
  • The fusion takes a q8_0 destination only where that holds: a 32-lane warp, S_v % 32 == 0, the scalar gate (not KDA) and the recurrent kernel.
  • The chunked prefill writes f32 and keeps its cpy.

Measured (RTX 5070 Ti, Ternary Bonsai 2 27B PQ2_0, -cts q8_0)

  • GPU time per step, one state written (nsys, legacy / fused / legacy):
    • decode, ubatch 1: 14.195 / 13.403 / 14.108 ms (−5.3 %)
    • ubatch 3: 17.006 / 16.570 / 16.873 ms
  • Served with the MTP draft head: a verify writes K = 3 snapshots per layer, so the old path ran three cpys per layer. Six greedy prompts in one conversation, legacy / fused A-B-A-B:
    • throughput: 104.8 / 118.1 / 110.1 / 120.2 tok/s (+9-10 %)
    • the generated text is byte-identical in every leg, and the draft acceptance counts are equal.
  • Logits: identical against the cpy path at ubatch 1 and 3. Mean KLD is 0.000000, the 99 % KLD is at the protocol's noise floor, and same top p is 100 %.
  • test-backend-ops on sm_120, rebased build: GATED_DELTA_NET_CACHE_FUSION 21/21, GATED_DELTA_NET 63/63, CONCAT 216/216, SCALE 6/6 and SSM_CONV 45/45, with and without the off switch.
    • The 13 new q8_0 cases cover the served layer: 16 k-heads, 48 v-heads, raw gates and K = 3, at 1-3 tokens over 1/2/4 sequences. They also cover K = 1, activated gates, and head widths 32/64/128.
    • All 13 fail when the block scale is mutated to amax/126.
    • Two q8_0 shapes the fusion refuses (a 16-wide head, and the 512-token chunked ubatch) keep the cpy and pass.

Off switch

  • GGML_CUDA_GDN_Q8_CACHE_LEGACY=1 keeps a q8_0 cache on the cpy.

TORAD.md gains the row after b4c2d66.

marcospaulo and others added 2 commits September 23, 2026 00:22
With -cts q8_0 every Gated DeltaNet layer wrote its new state as f32 into the gdn output and a
separate cpy then quantized it into the cache: 48 cpy_f32_q8_0 launches per step, ~13.9 us each
on an RTX 5070 Ti (the kernel gives one thread a 32-value block, so it reads 3 MB of state
uncoalesced). The f32 cache already skipped that cpy (the kernel writes the snapshots into the
cache view); a q8_0 cache now does too.

gdn_store_state quantizes as it stores: one q8_0 block is one warp-wide slice of a state column
(QK8_0 == warp_size, and every column starts on a block since S_v % 32 == 0), so the block scale
is a warp max, and the formula is cpy's quantize_f32_q8_0_block: the cache holds the bytes the
unfused gdn -> cpy pair would write. ggml_cuda_try_gdn_cache_fusion takes a q8_0 destination only
where that holds: a 32-lane warp, S_v a multiple of 32, the scalar gate (not KDA), and the
recurrent kernel; the chunked prefill pipeline writes f32 and keeps its cpy.
GGML_CUDA_GDN_Q8_CACHE_LEGACY=1 keeps a q8_0 cache on the cpy.

RTX 5070 Ti, Ternary Bonsai 2 27B, -cts q8_0:
- GPU time per step (nsys, legacy / fused / legacy): decode (ubatch 1) 14.195 / 13.403 / 14.108 ms,
  an MTP verify's width (ubatch 3) 17.006 / 16.570 / 16.873 ms. Per layer the cpy took ~14 us
  and the kernel's in-register quantize adds 1.4 us (ubatch 1) and 2.2 us (ubatch 3)
- served with the MTP draft head (K = 3 snapshots per verify: three cpys per layer before this),
  one conversation, six greedy prompts, legacy / fused A-B-A-B: 104.8 / 118.1 / 110.1 / 120.2
  tok/s (+9-10 %), the generated text byte-identical in every leg and the draft acceptance counts
  equal
- logits identical: KL against the cpy path at ubatch 1 and 3, Mean KLD 0.000000, 99 % KLD at
  the protocol's noise floor, same top p 100 %
- test-backend-ops GATED_DELTA_NET_CACHE_FUSION: 13 new q8_0 cases (the served layer, 16 k-heads,
  48 v-heads, raw gates, K = 3, at 1-3 tokens over 1/2/4 sequences; K = 1; activated gates; head
  widths 32/64/128) pass against the CPU cpy, and all 13 fail with the block scale mutated to
  amax/126; two q8_0 shapes the fusion refuses (a 16-wide head, the 512-token chunked ubatch)
  keep the cpy and pass, with and without the off switch

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01UjmSA3sZea1AekeAiJ4zer
@marcospaulo
marcospaulo merged commit d43bd7e into main Sep 23, 2026
5 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