Compute mamba ratio

From sgl-project/sglang · View interactive version

Compute mamba ratio is an AI agent skill from the GitHub repository sgl-project/sglang. Compute the optimal --mamba-full-memory-ratio (or --max-mamba-cache-size pin) for a hybrid attention + linear-attention (Mamba / GDN / KDA) model's two serving memory pools, from the workload and serving config. Use when a user asks what ratio to set, why concurrency is clamped, or how to size the s

How to use this skill

Skill file

Optimal hybrid dual-pool ratio (`--mamba-full-memory-ratio`)

A hybrid model (attention layers + linear-attention layers — the recurrent-state family: Mamba/SSM, GDN, KDA, etc.) splits serving memory into two independently-budgeted pools, fixed once at startup:

--mamba-full-memory-ratio r splits the post-weight budget: mamba_budget = rest · r/(1+r), i.e. mamba_budget : kv_budget = r. This skill picks the r (or the pin---max-mamba-cache-size alternative) at which neither pool bottlenecks first for the user's workload.

The formula

r*  =  (S + D) · token_equiv · dcp_size / L
token_equiv  =  state_bytes_per_slot / kv_bytes_per_token

r is dimensionless (just the split). To also predict the actual concurrency you need rest (below).

Inputs to collect from the user

  1. L — average context (input + output) in tokens.
  2. The two per-GPU byte constants — one of:
  1. S — from --mamba-radix-cache-strategy, the overlap scheduler, and SGLANG_OPT_MAMBA_SKIP_DECODE_LOCK (table below).
  2. D — --speculative-num-draft-tokens (0 if NOSPEC; also 0 when ReplaySSM spec-verify is enabled — see caveats).
  3. dcp_size — --dcp-size (1 if no DCP). If DCP and spec with a replicated draft KV, apply the draft caveat (below).
  4. KV dtype (bf16 / fp8) and ssm dtype (fp32 / bf16) — they set the two byte constants (see the token_equiv 2×2).
  5. (only to also predict the clamp, not just r) rest = per-GPU memory − weights at the chosen --mem-fraction-static. Read avail mem after Load weight end, or Memory pool end. avail mem + pool sizes, from the boot log.

`S` (state slots per running request), set by `--mamba-radix-cache-strategy`

All strategies keep prefix caching on; they differ in the track buffer that snapshots chunk-boundary state under the overlap scheduler. S = base + ping-pong, where base is 3 (live state + radix retention/COW headroom), and ping-pong = 2 (overlap on, non-lazy) / 1 (lazy, or overlap off) / 0 (no track buffer at all). This mirrors kv_cache_configurator._calculate_mamba_ratio; read it there if a release moves the constants.

SGLANG_OPT_MAMBA_SKIP_DECODE_LOCK=1 takes the base to 2, since the decode-time skip frees one resident slot per running request. no_buffer is the exception and stays at an effective 3: it adds that slot straight back, because its binding limit is the prefill-to-decode peak, which a decode-time change does not shrink.

| strategy | S | S with decode-lock skip | notes |

|---|---|---|---|

| --disable-radix-cache | 1 | 1 | prefix cache off entirely; most concurrency, no reuse |

| no_buffer | 3 | 3 | no track buffer; requires overlap off (+ page_size 1) → lower decode throughput |

| extra_buffer (default), overlap on | 5 | 4 | track buffer reserved per running request |

| extra_buffer, overlap off | 4 | 3 | same buffer, but ping-pong costs 1 slot instead of 2 |

| extra_buffer_lazy (recommended) | 4 | 3 | track buffer allocated lazily at the boundary → 1 fewer slot/req; requires overlap on |

The skip is a real concurrency lever, not a rounding detail: at the default strategy it takes S from 5 to 4, so the same state budget admits 25% more requests and r* drops by the same factor.

Overlap counts as off when --disable-overlap-schedule is passed or --pp-size > 1 (pipeline parallelism turns it off for you). So a PP deployment left on the default strategy sits on the S = 4 row, not S = 5 — using 5 there overstates the state cost and starves KV.

`token_equiv` — compute it from the two byte constants

token_equiv = state_bytes_per_slot / kv_bytes_per_token. Both bytes are per-model, per-GPU — read them from a boot log (or derive from the arch + dtypes) and divide. That's the whole thing; the dtype effects below are just for a quick mental estimate.

The recurrent state has two parts: state_bytes = SSM_bytes + conv_bytes. The SSM part scales with --mamba-ssm-dtype; the conv part is always bf16 (SGLANG_MAMBA_CONV_DTYPE, fixed). How the dtype knobs move token_equiv:

For an actual number, don't apply factors — read state_bytes_per_slot and kv_bytes_per_token for your model+dtype from the boot log and divide.

The GB the boot log prints for both pools is GiB (1024³). Convert both the same way and token_equiv is unaffected either way, since it is a ratio — but get it right before comparing an absolute byte figure against your own log.

Worked example (one measured model, TP8, fp32 ssm + fp8 KV), read off one boot log:

Mamba Cache is allocated. max_mamba_cache_size: 257, conv_state size: 0.46GB, ssm_state size: 13.04GB
KV Cache is allocated. dtype: torch.float8_e4m3fn, #tokens: 1167552, KV size: 15.03 GB

state_bytes_per_slot = (0.46 + 13.04) GiB / 257 ≈ 56.4 MB, kv_bytes_per_token = 15.03 GiB / 1167552 ≈ 13.8 KB → token_equiv ≈ **4080**.

Compute

def optimal_ratio(L, state_bytes_per_slot, kv_bytes_per_token, S, D=0, dcp_size=1):
    token_equiv = state_bytes_per_slot / kv_bytes_per_token
    r = (S + D) * token_equiv * dcp_size / L
    return r  # value of --mamba-full-memory-ratio (>1 is legal)

def predict_clamp(rest_bytes, r, state_bytes_per_slot, S, D=0):
    mamba_budget = rest_bytes * r / (1 + r)
    slots = mamba_budget / state_bytes_per_slot
    # spec: each running req reserves (S+D) worth; non-spec just S
    return int(slots // S)  # NOSPEC; with spec the budget joint-solves for (S+D)·per_req per req

Then state the result three ways: the r value, the predicted clamp (if rest given), and which pool binds (min(mamba_clamp, KV_cap), where KV_cap = kv_tokens · dcp_size / L).

Procedure

  1. Free lever first: if the boot log shows large idle in Memory pool end. avail mem (e.g. 30–40 GB at mem-frac 0.85), raise --mem-fraction-static (→0.92) before touching the split — it grows rest for both pools at no cost. Validate graph-capture headroom once.
  2. Collect the inputs. Prefer a real boot log for the two byte constants.
  3. Compute r*. If r* would drive the state pool below one request's worth (mamba_budget < S · per_req, happens at very long L), switch to pinning --max-mamba-cache-size = target_concurrency · S and let the rest go to KV — a sub-0.15 r is fragile.
  4. Report r, predicted clamp, binding pool, and any dtype accuracy gate that applies.

Multi-turn and agentic traffic: size for residency, not only concurrency

The formula balances the pools for the requests that are running. For multi-turn chat and agents, also budget the KV prefixes and recurrent checkpoints you want to retain between turns. A prefix needs a matching device state or a usable host backup; enough KV alone does not guarantee reuse.

Validate with your session count, concurrency, and time between turns, including a case with many more sessions than concurrent requests. Track prefix reuse and both pools' occupancy; do not infer checkpoint headroom from active-request usage alone.

Measured (GLM-5.3-Flash NVFP4, TP2 B300, AgentX trace replay, 48 sessions, pools fixed at a 67 GB budget, device only, --mamba-max-states-per-path 1; the S=3 rows add SGLANG_OPT_MAMBA_SKIP_DECODE_LOCK=1 to extra_buffer_lazy):

| state slots / KV tokens (S) | effective r | hit rate | output tok/min/GPU |

|---|---|---|---|

| 429 / 5.89M (S=5, default 0.9) | 0.90 | 0.06 | 7.6K |

| 300 / 7.48M (S=5) | 0.48 | 0.22 | 9.2K |

| 240 / 8.22M (S=5) | 0.36 | 0.85 | 29.8K |

| 192 / 8.81M (S=4, lazy) | 0.27 | 0.89 | 36.4K |

| 144 / 9.41M (S=3) | 0.18 | 0.88 | 35.1K |

| 120 / 9.70M (S=3, running capped at 40) | 0.15 | 0.84 | 29.7K |

Total sessions and peak concurrency were both 48 (p50 concurrency 27); reported state-pool usage never passed 72% even at 144 slots. This trace does not establish the state budget for workloads with sessions far above peak concurrency. GLM-5.3-Flash now defaults to r = 0.25 with extra_buffer_lazy (model override), and its high-throughput recipes pin 0.9 on GPUs where 0.25 would cap running requests below the benchmarked concurrency (e.g. GB300 TP4 FP8: 219 running requests at 0.25).

The direction is model- and deployment-specific: the Kimi-K3.1 cookbook's agentic PD decode recipe runs at 1.2, above the default. Measure; do not carry a ratio across models.

Worked examples (TP8, B300, validated against measured clamps)

Constants (from boot logs): state_bytes_per_slot ≈ 56.4 MB (fp32 ssm), kv_bytes_per_token ≈ 13.8 KB (fp8) → token_equiv ≈ 4080. Cache strategy extra_buffer_lazy → S = 4. Workload L = 9216 (8192 in + 1024 out). NOSPEC → D = 0.

predict_clamp on the same model at the default extra_buffer, overlap on and the decode-lock skip off (S = 5), three boot logs:

| config | max_mamba_cache_size | mmcs // S | measured max_running_requests |

|---|---|---|---|

| TP8, no DCP | 257 | 51 | 51 |

| DCP8, r = 5.97 | 451 | 90 | 90 |

| DCP8, r = 9 | 474 | 94 | 94 |

Reference r for this example model (token_equiv ≈ 4080, i.e. fp32 ssm + fp8 KV; S=5 default; multiply by (S+D)/S for spec, by dcp_size for DCP) — recompute with your own token_equiv for a different model:

| L | 2K | 4K | 8K | 32K | 64K | 128K |

|---|---|---|---|---|---|---|

| r | 10.0 | 5.0 | 2.5 | 0.62 | 0.31 | 0.16 |

Caveats

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.