Skip to content

cuda: batch the rollback conv-state snapshot copies into one launch (+2.6% DFlash2 decode, exact) - #379

Merged
bri-prism merged 1 commit into
prismfrom
cuda/batched-cpy
Oct 24, 2026
Merged

bri-prism merged 1 commit into
prismfrom
cuda/batched-cpy

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Overview

With speculative decoding, build_conv_state writes one conv-state snapshot per rollback slot: K = n_rs_seq + 1 CPY nodes of a [3, 8192] window per GDN layer per step. Each one is a launch-bound copy of about 2.8 µs. On the 9B with DFlash2 at depth 15, nsys counts about 255k of these copies, 7.5% of GPU time.

The CUDA backend now runs such a group of copies as one kernel, with up to 32 pointer pairs. A group qualifies when:

  • the copies are consecutive F32 → F32 CPY nodes with only view nodes between them;
  • they share shape and strides, so only the data pointers differ;
  • no destination overlaps a later copy's source or destination.

GGML_CUDA_BATCHED_CPY=0 restores one launch per copy.

The batch performs the same copies, so the output is bit-identical. Graph-level changes would be harder: the snapshot windows overlap and have descending slot order, so they can't be expressed as a single ggml view.

Validation

All runs are on an H200 with CUDA 12.8 and a Release sm_90 build, at prism 73bfe6cc plus this patch.

Correctness

  • New test CPY_SNAPSHOTS: 10/10 with and without the fusion. It covers 8192 and 37 channels, n_tokens < K (clamped, repeated windows) and runs longer than 32.
  • CPY: 252/252.

End to end: Ternary-Bonsai-2-9B PQ2_0-MTP-Q8_0 on llama-server, 4 prompts × 128 greedy tokens, 2 rounds, same binary with the env switch on and off. Generated tokens are identical in every mode.

mode decode change
DFlash2, depth 15 +2.5% to +2.7%
MTP, depth 3 +0.2% to +0.6%
no speculation ±0.1% (no snapshots)

The gain grows with n_rs_seq and with launch overhead, so GPUs where launches cost relatively more should gain more. I only measured H200.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code found the hotspot with nsys, wrote the fusion and test, and ran the benchmarks; reviewed by me.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo

The recurrent rollback writes one conv-state snapshot per slot
(build_conv_state: K = n_rs_seq + 1 CPY nodes of a [3, 8192] window per GDN
layer per step), each a launch-bound ~2.8 us copy. A run of F32 -> F32 CPY
nodes that share shape and strides (only the data pointers differ), with only
view nodes in between and no destination overlapping a later source or
destination, now runs as one kernel with up to 32 pointer pairs.
GGML_CUDA_BATCHED_CPY=0 restores one launch per copy.

Bit-exact (a copy). New test CPY_SNAPSHOTS covers the pattern, including
n_tokens < K (clamped, repeated windows) and runs longer than 32.

H200, Ternary-Bonsai-2-9B PQ2_0-MTP, llama-server, identical tokens:
DFlash2 depth 15 +2.5..+2.7%, MTP depth 3 +0.6%, no speculation +-0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PRQc3TQpn72qBxrUn5vKqo
@bri-prism
bri-prism merged commit 66ae2fd into prism Oct 24, 2026
8 checks passed
bri-prism added a commit that referenced this pull request Oct 24, 2026
… mat-vecs (GGML_CUDA_GDN_FUSION=1), +5-10% decode on H200 (stacked on #379) (#383)

Opt-in (GGML_CUDA_GDN_FUSION=1) exact fusion of the GDN decode conv chain into one kernel: conv-state get_rows, concat, state write-back, ssm_conv + silu, and the q/k L2 norm. The ssm_alpha/ssm_beta single-column mat-vecs run as one paired mmvf launch. Bit-exact: greedy tokens and top-k logprobs are identical with it on and off, including MTP.

On H200: decode +5-10% (2B to 27B) and 9B +7.2%, with 90-240 fewer kernels per token. Default off. The fusion matcher's design follows the launch-bubble analysis behind Hazy Research's megakernel work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant