Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Why Splitting Prefill for Prefix Caching Costs 30ms on 4B Models (And When It Pays Off)

Analysis of prefix caching in hybrid attention-recurrent models shows splitting prefill to improve cache hit rates adds 30ms scheduling overhead per request on 4B models, making it a net loss, while larger models benefit; FrontierReplay avoids splitting by reconstructing recurrent state exactly with 5.8-36.9x speedup over full recomputation.

Granularity Mismatch: Recurrent State Is Updated In-Place

Full-attention models store a KV cache per token (16 tokens per page), allowing prefix reuse at any boundary with zero waste. Hybrid models replace half the layers with recurrent layers (Gated DeltaNet, Mamba-2) that fold history into a fixed-size tensor updated in-place per token. To cache this state, vLLM stores it only at coarse block boundaries determined by state size: Qwen3.5-9B uses 528-token blocks, Falcon-H1-7B 1568 tokens, and Qwen3.5-4B on L40 with Triton backend falls to 576 tokens — nine times coarser than the 64-token blocks of full-attention Qwen3-4B.

Granularity mismatch
Granularity mismatch

Figure 1: Full attention (top) can resume at any 16-token page; hybrid model (bottom) only has state on coarse grid g₁. Tokens between grid and reuse point t must be recomputed because state is missing.

Paper measurements show reusable tokens falling outside the grid: 2.1%-7.8% for Qwen3.5-9B, 5.9%-19.8% for Falcon-H1-7B. In multi-turn dialogue, this waste compounds every turn.

Table 1: Hit rate / median recomputed prompt tokens per turn (thinking enabled, first request excluded). Data from vLLM issue #67674.

WildChat: Full-attention Qwen3-4B 71.4% / 333; Hybrid Qwen3.5-4B 56.1% / 532; Hybrid + unit 16 72.3% / 293

SWE-smith: Full-attention Qwen3-4B 93.9% / 410; Hybrid Qwen3.5-4B 92.0% / 709; Hybrid + unit 16 93.9% / 418

SWE-rebench: Full-attention Qwen3-4B 96.2% / 462; Hybrid Qwen3.5-4B 94.8% / 774; Hybrid + unit 16 96.2% / 460

Agent workloads show high hit rates but 70% more recomputed tokens because new tokens fall exactly outside the grid.

First Cost: The 30ms Is Not GPU Time

To recover off-grid tokens, vLLM splits prefill: run forward to target position t, save state, then continue. This adds scheduling steps — 1.05x to 1.86x more — independent of tokens saved, only dependent on number of cuts.

Split and replay cost
Split and replay cost

Figure 2: Left: scheduling step inflation 1.05-1.86x. Right: state-only replay vs full recompute GPU time ratio 5.8-36.9x (larger models benefit more).

On L40, a 560-token prompt with unit=16 adds 45.2ms total latency; GPU busy only 15.1ms, the remaining 30.1ms spent waiting for CPU to launch 658 extra kernels. On 9B, overhead is 42.2ms (GPU 26.9ms). Root cause: scheduler splits prefill unless backend can save state itself. Qwen's GDN layer only declares this capability in FlashInfer prefill backend; L40 runs Triton/FLA which lacks it, so every cut forces a real scheduling step. Same code on H100 follows a different path (source suggests no extra step), but author did not test.

Resulting per-request overhead: 4B ~30ms (saves 176-288 tokens, not worth it), 9B ~38ms (still not worth it), 27B ~60ms but saves 97-241ms per subsequent request — the equation flips.

Second Cost: H100 GPU Idle 33%

Noman654's profiling (no prefix caching, disabled because it hurt his no-shared-prefix benchmark by 19%) shows Python thread spends 43% of time launching kernels sequentially, only 17% waiting for GPU. Stable hybrid step median 770 tokens, p90 1300 tokens. 46% of wall time spent on steps between 512-2048 tokens, but vLLM default CUDA graph captures only up to 512 tokens. Excess falls back to eager mode: CPU launches one kernel, GPU executes one kernel.

Table 2: Single H100, 1024 requests, 256 concurrency, 512-token prompt, output 128-1024 uniform.

Step 0: Vanilla vLLM — 210.1s (1.00x)

Step 1: GDN state to bf16 — 171.3s (1.23x)

Step 2: Disable prefix cache — 144.6s (1.45x)

Step 3: Graph limit 512→2048 — 128.8s (1.63x)

Step 4: Custom replay decode kernel — 122.5s (1.72x)

Step 3 alone reduces service time 142-145s to 128-129s (11%), GPU utilization 67%→87%. Step 4 custom kernel gives 1.4x isolated speedup but only 3% end-to-end. Noman654 notes prefix cache is worth 49% on shared-prefix traffic; his benchmark disabled it intentionally. The key insight: hybrid model step lengths are irregular, many steps fall outside CUDA graph coverage, forcing CPU kernel launch overhead. The extra cut for prefix caching creates exactly such a short, hard-to-capture tail step. L40 GPU busy 1/3 of extra 30ms; H100 GPU idle 33% — same number.

Third Approach: FrontierReplay — No Split, Exact State Reconstruction

FrontierReplay decouples state production from forward splitting. Forward runs [s, n) in one go. During forward, input hooks copy each recurrent layer's inputs for [s, t) into per-request log pages, and record chunk boundaries and decode steps where state was rounded to storage precision. This log contains everything needed to rebuild state.

A state-only kernel then replays the recurrent update:

S(c+h) = γ(1:h)·S(c) + Σ(j=1..h) γ(j+1:h)·vⱼ·kⱼᵀ

γ is cumulative decay, h window length. Formula uses only v, k, and decay — no attention, MLP, or output projection. Three optimizations keep cost low: (1) all recurrent layer pointers in a single device table, one replay launches few kernels not per-layer; (2) forward logs rounding positions, replay splits and rounds identically, achieving bit-exact match with uninterrupted forward; (3) entirely on GPU, no host sync. Fixed cost 1.0-3.8ms per replay, per-token 3.0-9.8μs vs full recompute 22.5-241μs — leverage 5.8-36.9x, growing with model size (4B 5.8x, 34B 36.9x).

System structure
System structure

Figure 3: Five positions in a request. Host maintains 16-token page prefix index for checkpoint placement/retention/recycling. GPU recovers state from deepest checkpoint, runs unsplit forward, uses replay in same step to write t checkpoint into free slot; GPU event triggers publish. Dead state slots lent to KV pool via CUDA virtual memory pages.

Why Only These Positions?

Observation: real workloads rarely reuse at arbitrary positions. Multi-turn dialogue and agent loops reuse at turn boundaries (end of previous turn, fixed template ends). Tree search reuses at known branch points. Document QA reuses at document ends. Random mid-prompt reuse is rare.

Reuse boundaries
Reuse boundaries

Figure 4: Top: fraction of reusable tokens missed by vLLM state grid. Bottom: composition of reuse boundaries — turn boundaries dominate, and they are predictable in advance.

Thus checkpoints only materialized at four predictable boundaries: stable template end, completed turn end (state already in hand at decode end, copy at zero cost), branch nodes, chunk ends (every 16-token boundary, state free). Replay window capped at 1024 tokens; median in-flight window 96, p90 768, 99.5% fit within 32GiB budget. Overflow falls back to exact split (correctness preserved, cut returns). Checkpoints are large: Qwen3.5-9B 49.1MiB, Falcon-H1-7B 66.9MiB, 32GiB holds only 105-208. Tiered retention: running requests highest, ≥2 restores = proven, branch nodes = proven, unverified = speculative, evicted prefix KV = dead. Dead slots scanned every 16 requests, lent to KV pool via CUDA virtual memory pages.

Numbers

Falcon-H1-7B on SWE-agent: goodput 4.26x vanilla vLLM, median TTFT 1.1s → 125ms. Vanilla recomputes up to 1567 tokens per agent call, overloads, p99 latency unbounded. Qwen3.5-9B: ShareGPT 1.27x, LongBench 1.23x, 80% SLO capacity 1.15-1.73x.

Scale results
Scale results

Figure 5: Five models, four workloads. Top: goodput relative to vLLM. Bottom: median TTFT speedup. 34B tree search 3.67x goodput, 7.69x TTFT; 4B tree search 0.99x (bar on 1.0 line).

Ablation: largest single gain is split-free itself. Falcon SWE-agent: removing split alone raises goodput from 0.37 to 0.67 calls/s (vanilla 0.19). Adding frontier tiering, branch checkpoints, dead slot recycling reaches 0.74. Fixed-1024 (cut every 1024 tokens) loses to vanilla in all 12 configs.

Exactness: 303 LongBench items + needle-in-haystack + RULER variable-length tracking. Greedy output vs vLLM 100.2%-100.3%, 92% tokens identical. Approximate SuffixReplay drops 13-18% at 24K/32K, only 33% tokens match.

Exactness
Exactness

Figure 6: Qwen3.5-9B RULER variable tracking accuracy relative to vLLM. Blue: FrontierReplay stays within ±2% of 100% up to 32K. Dashed: approximate methods fall to 81.7% and 83.3% after 24K.

Where It Loses

34B LongBench λ=0.5: TTFT 225→135ms but goodput 0.76x, hit rate 0.83 vs 0.90. Cause: static tensor-parallel sub-pools (22GiB KV + 82 state slots per GPU) vs vLLM unified 32GiB pool. KV pressure + sparse replay reuse blocks contiguous recycling, 22 blocking retries fail.

4B tree search λ=12: 0.99x goodput (445 vs 444 calls). Eliminates all 18 TPOT violations but adds 7ms median TTFT. Small model replay fixed cost doesn't amortize (leverage 5.8x).

In-kernel state export: On Qwen3.5-9B (fine chunk 64 GDN) beats replay 1.5-2.5%; on Falcon-H1-7B (coarse chunk 256 Mamba-2) boundary not on tile forces half-tile scan, adds 18.4-26.6ms per replay.

Caveats: Paper runs on A800, vLLM 0.30.0, static memory pool acknowledged limitation. hybrid-prefix-lab only tested L40 + Triton backend; H100 extra step not tested (source says shouldn't happen). Noman654's wrapper silently dropped a flag; 8-GPU "graph limit effective" run actually ran baseline, +13.6% is noise; single-client same config varies 6-13%; default graph limit two identical GSM8K runs only 701/1319 tokens match exactly. Author did not reproduce any numbers; cross-condition references noted.

So Don't Rush to Tune unit

Three sources converge: focus is not finer cuts but cheaper reconstruction. Finer cuts hit a wall: hit rate matches full attention, but each request pays extra forward, hundreds of extra kernels, 30-60ms fixed overhead. For contexts under 4K, reuse savings don't cover cost.

hybrid-prefix-lab recommends on post-#65186 mainline: --prefix-match-unit 64, not documented 16. Unit=16 checkpoints rarely land on 64-grid, still adds extra step (~30ms/request). Unit=64: agent loads no longer regress, hit rate ~same as 16, dialogue loads add 8-9ms (design cost).

Reserve 1.5-2x peak state memory for hybrid models under cache pressure. Otherwise tail state evicted first, killing attention block hits too.

Noman654's transferable lesson: raise CUDA graph limit, but account three costs: startup 23s→7min, +1.2GiB graph memory per GPU, -2.5% KV capacity; on KV-tight loads net effect -1%. His custom replay kernel didn't hold advantage at node scale (30.4k vs 30.7k tok/s). Production advice: only adopt steps 1-3 of ladder.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

vLLMCUDA graphhybrid modelsLLM servingGated DeltaNetprefix cachingMamba-2FrontierReplay

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion