Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Qwen serve: the start-up VRAM guard needs 1.5 GiB free (was 1 GiB, counted torch's cache) and prints what it measured

YanissAmz4b3c9143738f
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.py
index 02abf1a..c5e11a9 100644
--- a/tools/qwen/serve.py
+++ b/tools/qwen/serve.py
@@ -2176,7 +2176,7 @@ def create_app(engine: Any, model_id: str, template: str, defaults: dict[str, An
# ---------------------------------------------------------------------------- main
-MIN_FREE_MIB = 1024 # VRAM that must stay free after load: a 2048-row prefill chunk and the multi-row graph pools come on top of the resident state
+MIN_FREE_MIB = 1536 # VRAM that must stay free after load: a 2048-row prefill chunk and the multi-row graph pools come on top of the resident state (measured 3090 Qwen3.8-27B: the first prefills raise the use by 1.3-1.4 GiB; 1024 let -c 262144 sessions 3, 1369 MiB free, start and then die with CUDA out of memory on the third distinct prompt)
def vram_headroom_problem(free_mib: float, ctx: int, kv_mib_per_token: float, sessions: int, min_free_mib: float = MIN_FREE_MIB) -> str | None:
@@ -2189,6 +2189,12 @@ def vram_headroom_problem(free_mib: float, ctx: int, kv_mib_per_token: float, se
f"every long prompt would end in CUDA out of memory. Start with -c {fits} or fewer --sessions (each session holds its recurrent slots, about 1.1 GiB)")
+def free_vram_mib(torch: Any) -> float:
+ """VRAM the device has left once torch's cache is returned (the number the first prefill really competes for)."""
+ torch.cuda.empty_cache()
+ return torch.cuda.mem_get_info()[0] / 2**20
+
+
def load_all(args: argparse.Namespace):
"""Everything before the port opens: template, defaults, engine, slot store, warm-up."""
model_dir = str(Path(args.model).expanduser())
@@ -2215,9 +2221,9 @@ def load_all(args: argparse.Namespace):
with Heartbeat("warm-up (first decode steps)"):
engine.warmup()
if engine.torch.cuda.is_available() and not engine.torch.version.hip and os.environ.get("EXL3_MIN_FREE_MIB", "") != "0":
- free, _ = engine.torch.cuda.mem_get_info()
- free_mib = (free + engine.torch.cuda.memory_reserved() - engine.torch.cuda.memory_allocated()) / 2**20 # torch's cached blocks are reusable
+ free_mib = free_vram_mib(engine.torch)
kv = sum(l.storage_size() for c in (engine.cache, engine.draft_cache) if c is not None for l in c.layers.values()) / 2**20 / args.ctx
+ print(f"qserve: VRAM left after load: {free_mib:.0f} MiB (ctx={args.ctx}, sessions={args.sessions})", flush=True)
bad = vram_headroom_problem(free_mib, args.ctx, kv, args.sessions, float(os.environ.get("EXL3_MIN_FREE_MIB") or MIN_FREE_MIB))
if bad:
raise RuntimeError(bad)
diff --git a/tools/qwen/test_serve.py b/tools/qwen/test_serve.py
index a195ac9..ec8e50a 100644
--- a/tools/qwen/test_serve.py
+++ b/tools/qwen/test_serve.py
@@ -1268,6 +1268,21 @@ class VramHeadroomTests(unittest.TestCase):
def test_override(self):
self.assertIsNone(serve.vram_headroom_problem(468, 262144, 0.0182, 3, min_free_mib=256))
+ def test_free_vram_is_the_device_figure_after_the_cache_is_returned(self):
+ calls = []
+ cuda = types.SimpleNamespace(empty_cache=lambda: calls.append("empty"), mem_get_info=lambda: (1369 * 2**20, 24124 * 2**20))
+ self.assertEqual(serve.free_vram_mib(types.SimpleNamespace(cuda=cuda)), 1369)
+ self.assertEqual(calls, ["empty"])
+
+ def test_1369_mib_left_at_262144_sessions_3_is_refused(self):
+ # measured on the 24 GB 3090: this start was accepted (limit 1024), then the third distinct 50K prompt died with CUDA OOM
+ msg = serve.vram_headroom_problem(1369, 262144, 0.0182, 3)
+ self.assertIn("-c ", msg)
+ self.assertLess(int(msg.split("-c ")[1].split()[0]), 262144)
+
+ def test_196608_sessions_3_is_accepted(self):
+ self.assertIsNone(serve.vram_headroom_problem(2600, 196608, 0.0182, 3)) # measured: ~2.6 GiB left, 50 calls clean in realA
+
if __name__ == "__main__":
unittest.main()