setup: parallel counts pinned RAM and per-cell VRAM at long contexts

With KV streaming, each slot pins its whole K/V in RAM (12.38 GiB at
1M int8) and keeps about 1.8 KB of VRAM per context cell beyond 32K.
parallel_slot_gb counted 0.59 GB per slot whatever the context, so
setup recommended 2 slots for IQ3_S at 1M on a 32 GB card. Measured on
an RTX 5090 with 125.6 GiB of RAM: 4 slots ran every request 3-4x
slower, 8 slots stopped the engine at start.

The recommendation now also keeps the expert arena plus every
session's pinned K/V within 75% of the RAM (Windows refused to pin
past 99 of 125.6 GiB), takes the K/V bytes per cell from KV_CELL_BYTES
(k8v4 816), and the note says how much RAM each request would pin when
RAM is what rules it out.

Doc: docs/BATCHING.md, what a slot costs at long contexts
This commit is contained in:
Guillaume PUTIER committed 2026-11-15 18:50:05 +01:00
1 parent 13eedf5524
commit c9a879750f
4 files changed
+87 -17

No files matched your search

+8 -2
View File
@@ -144,7 +144,9 @@ where the experts do not all fit, 16 slots cost the single-user speed (see docs
Every slot's session takes VRAM that the expert cache would otherwise hold: 0.56 GiB at a 32K context with 8-bit
KV, more with a longer context unless the KV cache streams (`--kv-resident`: then only the attention's 32K window
stays in VRAM, and each slot's whole KV cache takes pinned RAM - 1.6 GB at 128K). On a card whose experts mostly run
stays in VRAM, and each slot's whole KV cache takes pinned RAM - 1.6 GB at 128K, 12.4 GiB at 1M). Even with
streaming, a session keeps about 1.8 KB of VRAM per context cell beyond 32K (indexer rows, RoPE tables): measured on
an RTX 5090 with `--kv-resident 32768`, 0.95 GiB per slot at 262K and 2.27 GiB at 1M. On a card whose experts mostly run
on the CPU, a batch also reads about as many distinct experts as the requests one by one (different conversations
route to different experts), so the gain is in **latency** (nobody waits for a whole answer), and a request alone
runs slower (the smaller expert cache): 11-24 % on a 12 GB card, see the measurements below.
@@ -154,7 +156,11 @@ less ~5 GB, every card of a layer split counted) must still hold at least half o
slots, and the slots may take at most a fifth of it, up to 4 slots. With Q2_0 at 32K that is 3 slots on a 24 GB
card, 4 from 32 GB or on a split such as 2 x 16 GB; IQ3_S needs 32 GB or a split. Everywhere else (any 12 or 16 GB
card alone) it stays at one at a time and setup says: "parallel N reduces waiting for several users but costs
about 10-25% speed per request on this card". `--parallel N` is honoured as asked either way.
about 10-25% speed per request on this card". With KV streaming the slots must also fit in RAM: the expert arena,
the server's own session and every slot's pinned K/V must stay within 75 % of the RAM (Windows refused to pin more
at 99 of 125.6 GiB), or setup stays at one at a time and says how much RAM each request would pin. Measured on 125.6 GiB with IQ3_S at 1M: 8 slots stopped the
engine (pinning refused at 99 GiB), 4 slots started with 8 GiB free and half the arena unpinned, and every request
ran 3-4x slower, a request alone included. `--parallel N` is honoured as asked either way.
## How the server uses the slots
+50 -12
View File
@@ -8193,47 +8193,85 @@ PARALLEL_COST_NOTE = ("parallel N reduces waiting for several users but costs ab
"card")
PARALLEL_CELL_BYTES = 1850 # VRAM per context cell beyond 32K a session keeps in any KV mode (QSA indexer rows,
# RoPE tables): 0.95 GiB at 262K, 2.27 GiB at 1M per slot with --kv-resident 32768
PARALLEL_PIN_SHARE = 0.75 # share of the RAM the expert arena plus every session's pinned K/V may take: measured on
# a 125.6 GiB PC, pinning was refused at 99 GiB (79%)
def _parallel_kv_tok(kv: str) -> int:
# 12 layers: the measured pin (12.38 GiB at 1M int8), not the 13 of kv_streaming_ram_gb
return 12 * KV_CELL_BYTES.get(kv, 1056)
def parallel_slot_gb(ctx: int, kv: str, streaming: bool) -> float:
"""#465: the VRAM one batch slot's session takes: its KV cache (12 QSA layers; with KV streaming only the 32K
positions the attention reads stay in VRAM) and the DeltaNet state (~0.17 GB). Measured: 0.56 GiB at 32K int8."""
kv_tok = 12 * (576 if kv == "q4_0" else 1056)
return (min(ctx, 32768) if streaming else ctx) * kv_tok / 1e9 + 0.17
positions the attention reads stay in VRAM), the DeltaNet state (~0.17 GB) and the per-cell rows every session
sizes on the whole context. Measured: 0.56 GiB at 32K int8; with KV streaming, 0.95 GiB at 262K and 2.27 GiB at
1M (RTX 5090, 15/11/2026)."""
kv_cells = min(ctx, 32768) if streaming else ctx
return (kv_cells * _parallel_kv_tok(kv) + max(0, ctx - 32768) * PARALLEL_CELL_BYTES) / 1e9 + 0.17
def parallel_recommend(vram_gbs, arena_gb: float, ctx: int, kv: str, streaming: bool) -> int:
def parallel_pinned_gb(ctx: int, kv: str) -> float:
"""The RAM one session pins with KV streaming: the host copy of its whole K/V (12.38 GiB at 1M int8, measured).
The server's own session and every slot each pin one. Callers apply it only when streaming."""
return ctx * _parallel_kv_tok(kv) / 1e9
def parallel_ram_fits(n: int, ram_gb: float | None, arena_gb: float, ctx: int, kv: str, streaming: bool) -> bool:
"""Whether the expert arena plus the pinned K/V of the server's session and n slots stay within PARALLEL_PIN_SHARE
of the RAM (ram_gb is in GiB, the arena and the pins in GB).
Measured on 125.6 GiB with the 50.3 GB IQ3_S arena at 1M: 8 slots stopped the engine (pinning refused at 99 GiB),
4 slots started with 8 GiB free and half the arena unpinned, every request 3-4x slower. ram_gb None: not known."""
if ram_gb is None or not streaming:
return True
pinned_gib = (arena_gb + (n + 1) * parallel_pinned_gb(ctx, kv)) * 1e9 / 2**30
return pinned_gib <= PARALLEL_PIN_SHARE * ram_gb
def parallel_recommend(vram_gbs, arena_gb: float, ctx: int, kv: str, streaming: bool,
ram_gb: float | None = None) -> int:
"""#465: how many requests at once ("parallel") to recommend: 0 = none (one at a time). Only where the experts
mostly fit in VRAM - the expert cache (each card's VRAM less ~5 GB, every card of a layer split) still holds
PARALLEL_HELD of the model's experts beside the slots' sessions, which take at most PARALLEL_SHARE of it, up to 4.
Where the experts mostly run on the CPU a batch reads about as many experts as the requests one by one and every
slot's VRAM is expert cache lost: measured on a 12 GB RTX 5070 (Q2_0, 32K), a request alone 11-24% slower with 2-4
slots, 4 requests together 63 tok/s against 71 one after the other (docs/BATCHING.md)."""
slots, 4 requests together 63 tok/s against 71 one after the other (docs/BATCHING.md). With KV streaming each
slot also pins its whole K/V in RAM, so the count must fit there too (parallel_ram_fits)."""
if isinstance(vram_gbs, (int, float)):
vram_gbs = [vram_gbs]
cache_gb = sum(max(0.0, v - 5) for v in vram_gbs)
slot = parallel_slot_gb(ctx, kv, streaming)
best = 0
for n in (2, 3, 4):
if n * slot <= PARALLEL_SHARE * cache_gb and (cache_gb - n * slot) >= PARALLEL_HELD * arena_gb:
if n * slot <= PARALLEL_SHARE * cache_gb and (cache_gb - n * slot) >= PARALLEL_HELD * arena_gb \
and parallel_ram_fits(n, ram_gb, arena_gb, ctx, kv, streaming):
best = n
return best
def parallel_note(asked: int | None, vram_gbs, arena_gb: float, ctx: int, kv: str, streaming: bool) -> list[str]:
def parallel_note(asked: int | None, vram_gbs, arena_gb: float, ctx: int, kv: str, streaming: bool,
ram_gb: float | None = None) -> list[str]:
"""#465: what setup says about "parallel": the recommendation (or, where it would cost speed, why it is left at
one), or how the asked count compares with it (kept as asked: recommend, never force)."""
rec = parallel_recommend(vram_gbs, arena_gb, ctx, kv, streaming)
rec = parallel_recommend(vram_gbs, arena_gb, ctx, kv, streaming, ram_gb)
slot = parallel_slot_gb(ctx, kv, streaming)
pinned = parallel_pinned_gb(ctx, kv) if streaming else 0.0
ram_bound = rec == 0 and parallel_recommend(vram_gbs, arena_gb, ctx, kv, streaming) >= 2
why = (f"each request at once pins ~{pinned:.1f} GB of RAM for its K/V at this context, more than this PC can "
"spare beside the experts" if ram_bound else PARALLEL_COST_NOTE)
if asked is None or asked <= 1:
if not rec:
return [f"Several requests at once: left at one at a time - {PARALLEL_COST_NOTE} (docs/BATCHING.md)."]
return [f"Several requests at once: left at one at a time - {why} (docs/BATCHING.md)."]
return [f"Several requests at once (opt-in): --parallel {rec} decodes up to {rec} together instead of one "
f"after the other (each takes ~{slot:.1f} GB of VRAM from the expert cache; docs/BATCHING.md)."]
lines = [f"parallel requests: {asked} at once (each takes ~{slot:.1f} GB of VRAM from the expert cache, "
f"{asked * slot:.1f} GB in all)"]
f"{asked * slot:.1f} GB in all" + (f"; each also pins ~{pinned:.1f} GB of RAM" if pinned else "") + ")"]
if asked > PARALLEL_MAX:
lines.append(f"the engine runs at most {PARALLEL_MAX} at once; it will use {PARALLEL_MAX}")
if not rec:
lines.append(f"recommended for this card: one at a time - {PARALLEL_COST_NOTE}; kept as you chose")
lines.append(f"recommended for this card: one at a time - {why}; kept as you chose")
elif asked > rec:
lines.append(f"recommended for this card: {rec} - more slots leave fewer experts in VRAM, which can make every "
"request slower; kept as you chose")
@@ -11483,7 +11521,7 @@ def main() -> int:
say(f" Image generation: POST http://127.0.0.1:{port}/v1/images/generations (OpenAI's images API)")
if a.parallel is None: # #465: the opt-in, said once (nothing changes)
for line in parallel_note(None, [g.get("vram_gb", 0.0) for g in chosen], MODELS[model]["arena_gb"], ctx, kv,
"--kv-resident" in cfg["args"]):
"--kv-resident" in cfg["args"], ram):
say(" " + line)
if tuned is False: # #447: a failed tuning is repeated here, not only above
say(" Tuning: FAILED (the reason is above): the default settings stay - "
+1 -1
View File
@@ -542,7 +542,7 @@ def config_for(inp: SetupInputs, lib) -> SetupConfig:
if a.parallel >= 2:
cfg["parallel"] = a.parallel
for i, line in enumerate(parallel_note(a.parallel, [g.get("vram_gb", 0.0) for g in chosen],
MODELS[model]["arena_gb"], ctx, kv, streaming)):
MODELS[model]["arena_gb"], ctx, kv, streaming, ram)):
(ok if i == 0 else warn)(line)
else:
cfg["parallel"] = 1
+28 -2
View File
@@ -12,10 +12,24 @@ class Parallel(unittest.TestCase):
def test_slot_size(self):
# measured: a slot's session at 32K, int8 KV: 0.56 GiB = 0.60 GB
self.assertAlmostEqual(S.parallel_slot_gb(32768, "int8", False), 0.59, delta=0.03)
# KV streaming keeps only the attention's 32K positions in VRAM, whatever the context
self.assertEqual(S.parallel_slot_gb(262144, "int8", True), S.parallel_slot_gb(32768, "int8", False))
# KV streaming keeps only the attention's 32K positions of K/V in VRAM, but the session still sizes its
# per-cell rows on the whole context: measured 0.95 GiB (1.02 GB) at 262K and 2.27 GiB (2.44 GB) at 1M
self.assertAlmostEqual(S.parallel_slot_gb(262144, "int8", True), 1.02, delta=0.05)
self.assertAlmostEqual(S.parallel_slot_gb(1048576, "int8", True), 2.44, delta=0.05)
self.assertLess(S.parallel_slot_gb(262144, "int8", True), S.parallel_slot_gb(262144, "int8", False))
self.assertLess(S.parallel_slot_gb(32768, "q4_0", False), S.parallel_slot_gb(32768, "int8", False))
def test_pinned_ram(self):
# measured: 12.38 GiB (13.29 GB) of pinned RAM per session at 1M int8 with KV streaming; none without it
self.assertAlmostEqual(S.parallel_pinned_gb(1048576, "int8"), 13.29, delta=0.05)
self.assertAlmostEqual(S.parallel_pinned_gb(1048576, "k8v4"), 10.27, delta=0.05)
self.assertLess(S.parallel_pinned_gb(1048576, "q4_0"), S.parallel_pinned_gb(1048576, "k8v4"))
iq3s = S.MODELS["IQ3_S"]["arena_gb"]
# the measured PC (125.6 GiB): 4 slots at 1M left 8 GiB free and half the arena unpinned
self.assertFalse(S.parallel_ram_fits(4, 125.6, iq3s, 1048576, "int8", True))
self.assertTrue(S.parallel_ram_fits(4, 125.6, iq3s, 262144, "int8", True))
self.assertTrue(S.parallel_ram_fits(4, None, iq3s, 1048576, "int8", True)) # RAM unknown: not checked
def test_recommendation_only_where_the_experts_mostly_fit(self):
q2, iq3s = S.MODELS["Q2_0"]["arena_gb"], S.MODELS["IQ3_S"]["arena_gb"]
# most experts on the CPU: slots cost speed per request (measured on the 12 GB 5070) - none recommended
@@ -29,6 +43,18 @@ class Parallel(unittest.TestCase):
self.assertEqual(S.parallel_recommend([16, 16], q2, 32768, "int8", False), 4)
self.assertEqual(S.parallel_recommend(32, q2, 262144, "int8", False), 0) # a 262K KV per slot: no
self.assertEqual(S.parallel_recommend(32, q2, 262144, "int8", True), 4) # ... unless it streams
# RTX 5090, IQ3_S, 1M streamed, 125.6 GiB: measured 3-4x slower with 4 slots, no start with 8 - none
self.assertEqual(S.parallel_recommend(32, iq3s, 1048576, "int8", True, 125.6), 0)
# a 48 GB card that holds the slots in VRAM, but 64 GB of RAM cannot pin a second 1M K/V: none
self.assertEqual(S.parallel_recommend(48, q2, 1048576, "int8", True), 3)
self.assertEqual(S.parallel_recommend(48, q2, 1048576, "int8", True, 64), 0)
note = S.parallel_note(None, 48, q2, 1048576, "int8", True, 64)
self.assertIn("pins ~13.3 GB of RAM", note[0])
self.assertIn("pins ~13.3 GB of RAM", S.parallel_note(2, 48, q2, 1048576, "int8", True, 64)[0])
# VRAM is the limit and RAM is fine: the note does not blame the pinned RAM
note = S.parallel_note(None, 12, q2, 32768, "int8", False, 128)
self.assertNotIn("pins", note[0])
self.assertIn("10-25% speed", note[0])
def test_notes_recommend_never_force(self):
q2 = S.MODELS["Q2_0"]["arena_gb"]