// ======================================================================== // /content/posts/ml-systems/vllm-hybrid-kv-cache.typ // ======================================================================== #import "@tola/source:0.0.0": tola-meta #import "@tola-modules/nasapunk:0.0.0": xref #import "/figures/vllm-hybrid-kv-cache.typ" as fig #tola-meta(( title: "vLLM Hybrid KV Cache", date: "2026-11-04", summary: "Why vLLM's \"GPU KV cache size\" for Qwen3.8-27B is not a count of token slots and changes with max_model_len: GDN state, attention pages and one shared block pool.", tags: ("vLLM", "LLM-Inference"), series: "AI Infra", part: 4, weight: 10, )) At startup vLLM prints how much KV cache it has. For Qwen3.8-27B on two A100s with TP=2 and an FP8 KV cache, the setup of #xref("nvlink-vs-pcie-inference"): ```text Available KV cache memory: 52.89 GiB GPU KV cache size: 3,208,045 tokens, Maximum concurrency for 132,096 tokens per request: 24.29x ``` The 3,208,045 is not the number of tokens the pool can hold. It is also related to `max_model_len`, which should only cap the length of a request: with the same 52.89 GiB, vLLM's formula gives 1,508,693 at `max_model_len=8192`. Qwen3.8-27B mixes full attention with Gated DeltaNet, two kinds of layer that keep different state per sequence, and vLLM puts both in one block pool. The printed number comes from how that pool is shared. = State Qwen3.8-27B has the Qwen3.5 architecture (#xref("qwen3-5")): of its 64 layers, every fourth is full attention, and the other 48 are Gated DeltaNet (GDN), a linear attention. The two kinds keep different state per sequence. #figure( fig.fig-layer-state(), caption: [The layer pattern of Qwen3.8-27B and one repeating unit. Each GDN layer keeps a fixed state per sequence; the full-attention layer keeps a K and V row per token. Sizes are for the whole model, before TP; not to scale.], ) *A full-attention layer keeps K and V for every past token*: 4 KV heads × 256 × 2 = 2048 values. With an FP8 KV cache, the 16 such layers hold 32 KiB per token, so the total grows with the sequence. *A GDN layer keeps a state of fixed size instead*: - a recurrent state of 48 heads × 128 × 128 in FP32 (the config's `mamba_ssm_dtype`): 3 MiB; - a convolution state, the last 3 inputs of its short convolution over 10,240 channels in BF16: 60 KiB. Over the 48 GDN layers that is *147 MiB per sequence*, as much as 4,700 tokens of full-attention KV, *whether the sequence has 100 tokens or 128K*. TP splits the heads of both kinds, so at TP=2 each GPU holds 73 MiB of GDN state per sequence and 16 KiB of KV per token. = Block Pool vLLM keeps both in one pool of pages, so that memory freed by one kind can go to the other. That needs one page size for both, and vLLM gets it by sizing attention blocks to the GDN state. At TP=2 with FP8 KV, per layer and per GPU: #table( columns: 2, table.header([], [Bytes]), [GDN state: 24 × 128 × 128 × 4 B + 30 KiB], [1,603,584], [Attention KV for one token: 2 × 256 × 2 × 1 B], [1,024], [Attention KV for one block of 1568 tokens], [1,605,632], ) 1568 is the smallest multiple of 16 tokens whose KV fills a GDN page. vLLM sets the attention block size to it and pads the GDN page by the difference, 0.13%; both steps show up in its startup log. The same arithmetic gives every block size in the logs: - BF16 KV takes 2 bytes per value, so the block is 784 tokens. - With MTP on, the convolution state keeps 3 more rows for the draft tokens, and the blocks become 1600 (FP8) and 800 (BF16). - At TP=4 the state and the KV both halve, and the block stays at 1568. vLLM then sorts the layers into groups of equal size: one group of the 16 full-attention layers, and the 48 GDN layers in three groups of 16. The log shows `kv cache group sizes (1568, 1568, 1568, 1568)`. A block is one page in each of the 16 layers of a group, 25.7 MB at TP=2, and any group can take any free block: #figure( fig.fig-kv-pool(), caption: [One pool for both kinds of state, TP=2 with FP8 KV. Attention takes a block per 1568 tokens; GDN takes the same number of blocks for any sequence, two per group with prefix caching.], ) A sequence takes *one attention block per 1568 tokens, plus a fixed number of GDN blocks*: - With prefix caching off, one block per GDN group, three in all. - With prefix caching on (`mamba_cache_mode=align`), two blocks per group, six in all. vLLM also keeps GDN states at block boundaries, so that a cached prefix can resume the recurrence. = GPU KV Cache Size *The 3,208,045 is not a count of token slots.* vLLM divides the pool by what one request of `max_model_len` needs, then multiplies back: + 52.89 GiB / 25.7 MB = 2,210 blocks. + A request of 132,096 tokens takes 85 attention blocks and 6 GDN blocks: 91. + 2,210 / 91 = 24.29 such requests, and 24.29 × 132,096 = 3,208,045 tokens. This matches the logged number exactly on every run, including those with prefix caching off, where the GDN part is 3 blocks. = `max_model_len` vLLM computes the number in `get_kv_cache_capacity` and `get_max_concurrency_for_kv_cache_config` (`vllm/v1/core/kv_cache_utils.py`). For this model they reduce to: ```python attn_blocks = cdiv(max_model_len, block_size) # 1 attention group, 1 block per 1568 tokens gdn_blocks = 3 * 2 # 3 GDN groups, 2 blocks each with prefix caching blocks_per_request = attn_blocks + gdn_blocks max_concurrency = num_blocks / blocks_per_request kv_cache_size = int(max_concurrency * max_model_len) ``` With $N$ blocks in the pool, block size $B$, `max_model_len` $L$ and $G$ GDN blocks per request, and ignoring the rounding up: $ "kv_cache_size" approx N / (L \/ B + G) dot L = N B dot L / (L + G B) $ For a model with only full attention, $G = 0$, the two $L$ terms cancel, and the result is $N B$, the token slots in the pool, whatever the setting. Here $G = 6$, so *each request is charged as if it were $G B$ = 9,408 tokens longer*. The attention blocks grow with $L$ but the GDN blocks do not, so a shorter `max_model_len` gives GDN a larger share of each request, and the number drops. The same 2,210 blocks at different settings: #table( columns: 4, table.header([`max_model_len`], [Blocks per request], [Max concurrency], [KV cache size]), [8,192], [6 + 6 = 12], [184.17], [1,508,693], [32,768], [21 + 6 = 27], [81.85], [2,682,121], [132,096], [85 + 6 = 91], [24.29], [3,208,045], [262,144], [168 + 6 = 174], [12.70], [3,329,530], ) Only the 132,096 row is from a log; the others follow from the formula. The pool is the same in every row, and the number approaches 2,210 × 1568 = 3,465,280 as `max_model_len` grows. = Summary - *For a hybrid model, read "GPU KV cache size" as how many max-length requests fit*, times `max_model_len`. It changes with `max_model_len` even when the memory does not. - *The real capacity is the block pool*: here 2,210 blocks, each 1568 tokens of attention KV or one GDN state for 16 layers. - *Every running sequence holds GDN state, however short*: a fixed number of blocks per sequence, on top of its attention blocks. // ======================================================================== // /figures/vllm-hybrid-kv-cache.typ // ======================================================================== // Figures for content/posts/ml-systems/vllm-hybrid-kv-cache.typ, drawn with cetz. // Numbers are from vLLM's startup log for Qwen3.8-27B at TP=2 with FP8 KV. // Same conventions as /utils/figure.typ: 1pt per unit, y grows upward, < ~560pt wide. #import "@tola-modules/nasapunk:0.0.0": cetz #import "@tola-modules/nasapunk:0.0.0": arrow, box-swatch, c, legend, txt // ============================================================================ // One pool for attention KV and GDN state // ============================================================================ #let fig-kv-pool() = cetz.canvas(length: 1pt, { import cetz.draw: * let page(x, fill, stroke, title, l1, l2) = { rect((x, 0), (x + 222, -54), fill: fill, stroke: .8pt + stroke, radius: 3) content((x + 10, -12), txt(title, size: 8.5pt, fill: c.ink, weight: 700), anchor: "west") content((x + 10, -28), txt(l1, size: 7.5pt), anchor: "west") content((x + 10, -42), txt(l2, size: 7.5pt, fill: c.ink, weight: 600), anchor: "west") } page(0, c.orange1, c.orange, "Attention page, one layer", [1568 tokens × 2 KV heads × 256 × (K, V) × 1 B], [1,605,632 B]) content((246, -27), text(size: 14pt, fill: c.muted, "=")) page(270, c.teal1, c.teal, "GDN page, one layer", [24 heads × 128 × 128 × 4 B + 30 KiB conv state], [1,603,584 B, padded by 0.13%]) // The pool: one block = one page in each of a group's 16 layers. let (w, h, y0) = (16, 18, -96) let kinds = ("a",) * 2 + ("g",) * 6 + ("a",) * 7 + ("g",) * 6 + ("f",) * 9 let gdn = ("G1", "G1", "G2", "G2", "G3", "G3") content((0, y0 + 10), txt([Block pool on one GPU. A block is one page in each of a group’s 16 layers: 25.7 MB.], size: 7.5pt, fill: c.ink), anchor: "west") let gi = 0 for (i, k) in kinds.enumerate() { let x = i * w let (fill, stroke) = if k == "a" { (c.orange1, c.orange) } else if k == "g" { (c.teal1, c.teal) } else { (white, c.grey2) } rect((x + .8, y0), (x + w - .8, y0 - h), fill: fill, stroke: .7pt + stroke, radius: 1.5) let label = if k == "a" { "A" } else if k == "g" { gdn.at(calc.rem(gi, 6)) } else { none } if k == "g" { gi += 1 } if label != none { content((x + w / 2, y0 - h / 2), txt(label, size: 6pt, fill: c.ink, weight: 600)) } } // Which blocks belong to which request. let brace(i0, i1, body) = { let (xa, xb, y) = (i0 * w + 1, i1 * w - 1, y0 - h - 6) line((xa, y + 3), (xa, y), (xb, y), (xb, y + 3), stroke: .6pt + c.line) content(((xa + xb) / 2, y - 4), txt(body, size: 7pt), anchor: "north") } brace(0, 8, [3,000 tokens: 2 + 6 blocks]) brace(8, 21, [10,000 tokens: 7 + 6 blocks]) brace(21, 30, [free]) let ly = y0 - h - 40 legend(0, ly, [A: the 16 full-attention layers, one block per 1568 tokens], box-swatch(0, ly, c.orange1, stroke: .7pt + c.orange)) legend(0, ly - 16, [G1–G3: three groups of 16 GDN layers, a fixed number of blocks per sequence], box-swatch(0, ly - 16, c.teal1, stroke: .7pt + c.teal)) }) // ============================================================================ // Layer pattern and per-sequence state of each kind // ============================================================================ /// 64-layer strip (every fourth layer full attention), then one repeating /// unit, layers 4-7, with the state each layer keeps for a sequence. /// Sizes are for the whole model, before TP splits the heads. #let fig-layer-state() = cetz.canvas(length: 1pt, { import cetz.draw: * let is-attn(i) = calc.rem(i, 4) == 3 let tone(attn) = if attn { (c.orange1, c.orange) } else { (c.teal1, c.teal) } // The 64 layers. let (x0, w, h) = (14, 8, 14) content((x0, 14), txt([Qwen3.8-27B: 64 decoder layers, 48 GDN and 16 full attention], size: 7.5pt, fill: c.ink), anchor: "west") for i in range(64) { let (fill, stroke) = tone(is-attn(i)) rect((x0 + i * w + .6, 0), (x0 + (i + 1) * w - .6, -h), fill: fill, stroke: .5pt + stroke, radius: 1) if i == 0 or is-attn(i) { content((x0 + i * w + w / 2, -h - 7), txt(str(i), size: 5.5pt)) } } // Zoom into layers 4-7. let (za, zb) = (x0 + 4 * w, x0 + 8 * w) rect((za - .4, 1.4), (zb + .4, -h - 1.4), stroke: 1pt + c.ink, radius: 1.5) let yd = -62 line((za, -h - 14), (0, yd + 4), stroke: (paint: c.grey2, thickness: .6pt, dash: "dashed")) line((zb, -h - 14), (532, yd + 4), stroke: (paint: c.grey2, thickness: .6pt, dash: "dashed")) // One repeating unit: three GDN layers, then one full-attention layer. let (cw, gap) = (124, 12) for k in range(4) { let i = 4 + k let attn = is-attn(i) let (fill, stroke) = tone(attn) let x = k * (cw + gap) rect((x, yd), (x + cw, yd - 30), fill: fill, stroke: .8pt + stroke, radius: 3) content((x + 8, yd - 10), txt([Layer #i · #if attn [full attention] else [GDN]], size: 8pt, fill: c.ink, weight: 700), anchor: "west") content((x + 8, yd - 22), txt(if attn [softmax attention, GQA] else [linear attention], size: 7pt), anchor: "west") let ys = yd - 42 if not attn { // Recurrent state S: one 128 x 128 matrix per head, 48 heads. for j in range(3) { let o = (2 - j) * 4 rect((x + 8 + o, ys - o), (x + 52 + o, ys - 44 - o), fill: if j == 2 { c.teal2 } else { c.teal1 }, stroke: .6pt + c.teal) } content((x + 30, ys - 22), txt([S], size: 9pt, fill: c.ink, weight: 700)) content((x + 66, ys - 4), txt([state S], size: 7pt, fill: c.ink, weight: 600), anchor: "west") content((x + 66, ys - 16), txt([48 heads], size: 7pt), anchor: "west") content((x + 66, ys - 27), txt([× 128 × 128], size: 7pt), anchor: "west") content((x + 66, ys - 38), txt([FP32: *3 MiB*], size: 7pt), anchor: "west") // Conv state: the last 3 inputs over 10,240 channels. let yc = ys - 60 for r in range(3) { rect((x + 8, yc - r * 3.2), (x + 116, yc - (r + 1) * 3.2), fill: c.teal1, stroke: .4pt + c.teal) } content((x + 8, yc - 18), txt([conv state: 3 × 10,240], size: 7pt, fill: c.ink, weight: 600), anchor: "west") content((x + 8, yc - 29), txt([BF16: *60 KiB*], size: 7pt), anchor: "west") } else { // K and V: one row per past token, the column grows with the sequence. let rows = 7 let rh = 6.5 for (j, name) in ("K", "V").enumerate() { let xc = x + 8 + j * 24 content((xc + 10, ys - 4), txt(name, size: 7.5pt, fill: c.ink, weight: 700)) for r in range(rows) { rect((xc, ys - 10 - r * rh), (xc + 20, ys - 10 - (r + 1) * rh), fill: c.orange1, stroke: .4pt + c.orange) } rect((xc, ys - 10 - rows * rh), (xc + 20, ys - 10 - rows * rh - 26), stroke: (paint: c.grey2, thickness: .6pt, dash: "dashed")) } arrow((x + 58, ys - 10), (x + 58, ys - 10 - rows * rh - 24), color: c.orange) content((x + 66, ys - 4), txt([per token], size: 7pt, fill: c.ink, weight: 600), anchor: "west") content((x + 66, ys - 16), txt([4 KV heads], size: 7pt), anchor: "west") content((x + 66, ys - 27), txt([× 256 × 2], size: 7pt), anchor: "west") content((x + 66, ys - 38), txt([FP8: *2 KiB*], size: 7pt), anchor: "west") content((x + 66, ys - 64), txt([grows with], size: 7pt, fill: c.orange), anchor: "west") content((x + 66, ys - 75), txt([the sequence], size: 7pt, fill: c.orange), anchor: "west") } } // Totals over the 16 repeating units. let yb = yd - 156 let brace(xa, xb, body) = { line((xa, yb + 4), (xa, yb), (xb, yb), (xb, yb + 4), stroke: .6pt + c.line) content(((xa + xb) / 2, yb - 5), txt(align(center, body), size: 7.5pt, fill: c.ink), anchor: "north") } brace(0, 3 * cw + 2 * gap, [× 16 units = 48 GDN layers \ *147 MiB per sequence*, for 100 tokens or 128K]) brace(3 * (cw + gap), 4 * cw + 3 * gap, [× 16 layers \ *32 KiB per token*]) })