#!/usr/bin/env python3 """How many requests fit in a hybrid model's KV memory: fixed pools vs one shared arena. Fixed pools follow SGLang v0.5.23's sizing rules: - sliding-window models: the window pool holds swa_full_tokens_ratio (default 0.8) times as many tokens as the full-attention pool (pool_configurator.py L801, L881); - recurrent-state models: state gets ratio / (1 + ratio) of the memory, mamba_full_memory_ratio defaulting to 0.9 (kv_cache_configurator.py L3354-L3355), and with the radix cache on (the default) each running request is priced at 3 state slots, so running requests are capped at slots // 3 (MAMBA_CACHE_SIZE_MAX_RUNNING_REQUESTS_RATIO, L227; cap in resolve_max_num_reqs, L3134-L3139). The arena gives every request what it uses, from one budget, with the same 3 slots per request. Simplifications: no page rounding, no scheduler headroom; a window request holds window + one 64-token page, no page rounding, no scheduler headroom. Real engines admit fewer requests than these bounds; the ratios between columns are the point. python3 memory_split.py # the article's tables, for a 40 GiB budget python3 memory_split.py --gib 120 # another budget """ import argparse GIB = 1 << 30 # Bytes per unit, BF16 KV cache, from each model's config.json. QWEN35_4B = dict( name="Qwen3.5-4B", full_per_token=8 * 2 * 4 * 256 * 2, # 8 full layers, K+V, 4 KV heads, dim 256, 2 B state_per_request=24 * (32 * 128 * 128 * 4 # 24 linear layers: FP32 state, 32 heads x 128 x 128 + (2 * 16 * 128 + 32 * 128) * 3 * 2), # + BF16 conv state, 8,192 channels x 3 ) GPT_OSS_20B = dict( name="gpt-oss-20b", full_per_token=12 * 2 * 8 * 64 * 2, # 12 full layers, 8 KV heads, dim 64 window_per_token=12 * 2 * 8 * 64 * 2, # 12 sliding-window layers, same shape window=128, ) PAGE = 64 SLOTS_PER_REQ = 3 # radix cache on; 1 with --disable-radix-cache def fixed_state(m, budget, length, ratio=0.9): state_bytes = budget * ratio / (1 + ratio) slots = int(state_bytes // m["state_per_request"]) kv_tokens = (budget - state_bytes) / m["full_per_token"] fit = min(slots // SLOTS_PER_REQ, int(kv_tokens // length)) idle = budget - fit * (SLOTS_PER_REQ * m["state_per_request"] + length * m["full_per_token"]) return fit, slots, int(kv_tokens // length), idle def fixed_window(m, budget, length, ratio=0.8): per_full_token = m["full_per_token"] + ratio * m["window_per_token"] full_tokens = budget / per_full_token window_tokens = ratio * full_tokens held = min(length, m["window"] + PAGE) fit = min(int(full_tokens // length), int(window_tokens // held)) idle = budget - fit * (length * m["full_per_token"] + held * m["window_per_token"]) return fit, int(full_tokens // length), int(window_tokens // held), idle def arena(m, budget, length): per_req = length * m["full_per_token"] per_req += SLOTS_PER_REQ * m.get("state_per_request", 0) if "window" in m: per_req += min(length, m["window"] + PAGE) * m["window_per_token"] return int(budget // per_req) def main(): ap = argparse.ArgumentParser() ap.add_argument("--gib", type=float, default=40) budget = ap.parse_args().gib * GIB print(f"KV memory budget: {budget / GIB:.0f} GiB, BF16 KV") m = QWEN35_4B print(f"\n{m['name']}: {m['full_per_token']:,} B per token (8 full layers), " f"{m['state_per_request'] / (1 << 20):.1f} MiB of state per request (24 linear layers)") print(f" default split: {0.9 / 1.9:.1%} of the budget to state slots") for label, length in [("16K in + 4K out", 20480), ("1K in + 256 out", 1280)]: fit, slots, kv_fit, idle = fixed_state(m, budget, length) a = arena(m, budget, length) print(f" {label:16s} fixed pools: {fit:4d} requests (state slots {slots} = {slots // SLOTS_PER_REQ} requests, KV for {kv_fit}), " f"{idle / GIB:5.1f} GiB idle ({idle / budget:.0%}) | arena: {a:4d} requests ({a / fit:.2f}x)") m = GPT_OSS_20B print(f"\n{m['name']}: {m['full_per_token']:,} B per token in the full layers and as much per window token " f"(window {m['window']})") print(f" default split: {0.8 * m['window_per_token'] / (m['full_per_token'] + 0.8 * m['window_per_token']):.1%} " f"of the budget to the window pool") for label, length in [("512 in + 1,536 out", 2048), ("32K in + 1K out", 33792)]: fit, full_fit, win_fit, idle = fixed_window(m, budget, length) a = arena(m, budget, length) print(f" {label:18s} fixed pools: {fit:4d} requests (full pool for {full_fit}, window pool for {win_fit}), " f"{idle / GIB:5.1f} GiB idle ({idle / budget:.0%}) | arena: {a:4d} requests ({a / fit:.2f}x)") if __name__ == "__main__": main()