GPU Kernel Engineering Study · Triton · Mamba-2 SSD · RTX 4090 · CUDA Graphs

Sub-Millisecond Overhead for Mamba-2 Packed NaN Isolation

How moving an untaken cold branch out of a Triton kernel into a 128-CTA persistent grid-stride fixup kernel cut packed Mamba-2 chunk-scan overhead from 1.44× down to 1.01× (+2.8–4.0 µs) with zero host sync.

Brian Woods · November 2026 · sglang#51342 · state-spaces/mamba#1083

1.01×Isolated kernel vs. main on 16K & 16×512 (down from 1.40–1.44×)
+2.8–4.0 µsFixed kernel overhead on finite batches under CUDA-graph replay
0.0%–1.9%Full mamba_chunk_scan_combined pipeline overhead vs. main
0 syncs100% CUDA-graph-safe via 1-int device flag + 128-CTA persistent fixup

1. The Bug: IEEE-754 Cross-Sequence Contamination

In continuous-batching / varlen Mamba-2 inference (seq_idx != None), multiple requests are packed into the same 256-token chunk. The Triton _chunk_scan_fwd_kernel zeros cross-sequence entries of cb[m, k] so request A should not read request B's tokens.

Under IEEE-754 and Tensor Core MMA, however, 0.0 * NaN = NaN and 0.0 * ±Inf = NaN. If request B produces a single non-finite value in x, dt, or carried prev_states, tl.dot(cb, x) leaks NaN into every co-batched request sharing that chunk.

2. The Performance Trap: Register Pressure From an Untaken Branch

Exact per-(row, channel) isolation requires computing both dot_raw = tl.dot(cb, x) and dot_safe = tl.dot(cb, x_safe) plus an ownership mask own_has_nonfinite. Even when gated behind if chunk_has_boundary and has_nonfinite: so the branch never executes on finite inputs, the Triton/PTX compiler still allocates registers for all three simultaneous (BLOCK_M, BLOCK_N) FP32 tiles across the entire kernel.

That static register-file tax lowered SM occupancy on every call, leaving the kernel 1.40–1.44× slower on 16K and 16×512 packed workloads even when 100% of inputs were finite.

3. The Fix: Two-Kernel Split + 128-CTA Persistent Grid-Stride Fixup

To eliminate the register tax without breaking CUDA graphs (which forbid host-side .any().item() checks), we split the fast and slow paths into two separately compiled kernels:

# Kernel 1 (after k-loop, outside register-critical inner loop):
if HAS_SEQ_IDX and not HAS_INITSTATES:
    if tl.load(seq_idx_ptr) != tl.load(seq_idx_ptr + (chunk_size_limit - 1) * stride_seq_idx_seqlen):
        if tl.max(((acc - acc) != 0.0).to(tl.int32)) > 0:
            tl.store(dirty_flag_ptr, 1)

# Kernel 2 (launched with grid=(128,), 100% CUDA-graph-safe):
if tl.load(dirty_flag_ptr) == 0:
    return  # 1 cached L2 load, ~2-3 us total

4. RTX 4090 Benchmarks (CUDA-Graph Replay, Median)

Isolated _chunk_scan_fwd Kernel (SGLang, bf16, 48×64, dstate=128)

ShapemainSingle-kernel (fb366f55)Full-grid fixup128-CTA persistent fixup
16k_single_seq_idx0.4443 ms (1.00×)0.6213 ms (1.40×)0.4567 ms (1.03×)0.4483 ms (1.01×)
16x512_packed0.2132 ms (1.00×)0.3069 ms (1.44×)0.2203 ms (1.03×)0.2160 ms (1.01×)
48x16_packed0.0182 ms (1.00×)0.0258 ms (1.42×)0.0211 ms (1.16×)0.0208 ms (1.14×)

Full mamba_chunk_scan_combined Pipeline

Repo & WorkloadmainPersistent fixupOverhead
state-spaces/mamba · single_16k_seq_idx (16,384 tok)1.2274 ms1.2272 ms0.0% (1.000×)
state-spaces/mamba · dense_16k_packed (128×128)1.2317 ms1.2470 ms+1.2% (+15.3 µs)
state-spaces/mamba · short_packed_64seq (5,202 tok)0.3531 ms0.3613 ms+2.3% (+8.2 µs)
sglang · dense_16k_packed (128×128)1.0617 ms1.0800 ms+1.7% (+18.3 µs)
sglang · short_packed_64seq (5,202 tok)0.2920 ms0.2975 ms+1.9% (+5.5 µs)