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.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Qwen serve: refuse to start when less than 1 GiB of VRAM is left after load, and name the context that fits
YanissAmze985bab90fbe
diff --git a/tools/qwen/serve.py b/tools/qwen/serve.pyindex 0e6da1a..563944c 100644--- a/tools/qwen/serve.py+++ b/tools/qwen/serve.py@@ -2169,6 +2169,19 @@ 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+++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:+ """None when the memory left after load covers the first real request, else the start-up error with the context that fits.+ 3090, Qwen3.8-27B K4V4: sessions 3 at -c 262144 left 0.47 GiB and every cold prefill died with CUDA out of memory while /health said ok."""+ if free_mib >= min_free_mib:+ return None+ fits = max(0, int(ctx - (min_free_mib - free_mib) / kv_mib_per_token) // 256 * 256) if kv_mib_per_token > 0 else 0+ return (f"only {free_mib:.0f} MiB of VRAM left after load (ctx={ctx}, sessions={sessions}; {min_free_mib:.0f} MiB needed for the first prefill): "+ 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 load_all(args: argparse.Namespace):"""Everything before the port opens: template, defaults, engine, slot store, warm-up."""model_dir = str(Path(args.model).expanduser())@@ -2194,6 +2207,13 @@ def load_all(args: argparse.Namespace):startup_health.stage("warm-up")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+ 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+ 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)if os.environ.get("EXL3_SWAP_IN", "1") != "0":from exllamav3.util.memory import swap_in_processprint(f"qserve: process pages swapped out during the load, read back before READY: {swap_in_process()}", flush=True)diff --git a/tools/qwen/test_serve.py b/tools/qwen/test_serve.pyindex c9093d0..a195ac9 100644--- a/tools/qwen/test_serve.py+++ b/tools/qwen/test_serve.py@@ -1253,5 +1253,21 @@ class PrefixDiskConcurrencyTests(unittest.TestCase):self.assertEqual(calls, [("save", "ids-a"), ("save", "ids-b")])+class VramHeadroomTests(unittest.TestCase):+ def test_enough_is_none(self):+ self.assertIsNone(serve.vram_headroom_problem(2200, 262144, 0.0182, 1))++ def test_three_sessions_262144_names_a_context_that_fits(self):+ msg = serve.vram_headroom_problem(468, 262144, 0.0182, 3) # measured on the 24 GB 3090: Qwen3.8-27B K4V4, sessions 3+ self.assertIn("-c ", msg)+ fits = int(msg.split("-c ")[1].split()[0])+ self.assertEqual(fits % 256, 0)+ self.assertLessEqual(fits, 262144 - (1024 - 468) / 0.0182)+ self.assertGreater(fits, 200000)++ def test_override(self):+ self.assertIsNone(serve.vram_headroom_problem(468, 262144, 0.0182, 3, min_free_mib=256))++if __name__ == "__main__":unittest.main()