Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(coli): Windows GPU probe missed Kimi CUDA_DLL and HIP hosts
kevin93276d616c5a8a05
diff --git a/c/coli b/c/coliindex ba4358e2..20318db9 100755--- a/c/coli+++ b/c/coli@@ -610,6 +610,14 @@ def env_for_engine(a, arch, plan=None):gain=100.0*profile["gain"]print(f" {C.dim}[TUNE] applied measured profile · +{gain:.1f}% "f"calibration throughput{C.r}",file=sys.stderr)+ # Kimi's CUDA path is gated on K3_CUDA, not COLI_CUDA. --gpu / --vram /+ # auto-tier write COLI_CUDA=1 after proving the build; without this copy+ # the flag was accepted and the experts still stayed on the CPU.+ if arch == "kimi":+ if env.get("COLI_CUDA") == "0":+ env["K3_CUDA"] = "0"+ elif env.get("COLI_CUDA") == "1" and "K3_CUDA" not in explicit_env:+ env["K3_CUDA"] = "1"return envdef dsv4_cuda_available(model=None):@@ -676,15 +684,20 @@ def cuda_binary(engine=None):for line in linked.stdout.splitlines())except (OSError,subprocess.SubprocessError): return Falseif sys.platform == "win32":- # Windows CUDA_DLL=1 builds never link libcudart directly: glm.exe loads- # coli_cuda.dll at runtime via LoadLibrary (backend_loader.c), so there's no- # import-table entry for ldd/dumpbin to see. Detect the COLI_CUDA build via a- # marker string baked into glm.c's #ifdef COLI_CUDA block instead, and require- # coli_cuda.dll to actually sit next to glm.exe (else CUDA init fails at startup).+ # Windows CUDA_DLL/HIP_DLL hosts never link the GPU runtime directly:+ # the engine LoadLibrary's its backend (backend_loader.c). The host+ # compiles exactly one basename -- coli_hip.dll or coli_cuda.dll --+ # and that is the file that must sit next to it. The GLM/Qwen banner+ # is not a GPU-build marker: Kimi K3 CUDA_DLL builds link the same+ # loader without printing it, and a HIP host that only had coli_hip.dll+ # used to be refused as CPU-only.try:- with open(engine,"rb") as f: built=b"[CUDA] mode: routed experts" in f.read()+ with open(engine,"rb") as f: image=f.read()except OSError: return False- return built and os.path.exists(os.path.join(os.path.dirname(engine),"coli_cuda.dll"))+ from doctor import windows_backend_dll+ expected=windows_backend_dll(image)+ if not expected: return False+ return os.path.exists(os.path.join(os.path.dirname(engine),expected))return Falsedef resource_request(a, env):diff --git a/c/doctor.py b/c/doctor.pyindex b8fc8783..9b7045bb 100644--- a/c/doctor.py+++ b/c/doctor.py@@ -457,6 +457,26 @@ def deep_container_report(model, mirror_dir=None):}+def windows_backend_dll(image):+ """Which GPU backend DLL a Windows host compiled in, or None if CPU-only.++ backend_loader.c bakes exactly one basename: coli_hip.dll under COLI_HIP_DLL+ and coli_cuda.dll otherwise. That string is the build marker. The GLM/Qwen+ banner "[CUDA] mode: routed experts" is only printed by those two engines;+ a Kimi K3 CUDA_DLL host links the same loader and prints [K3-CUDA] instead.+ DeepSeek V4 has its own pair and is not this function's job.+ """+ if not image or b"[DSV4 CUDA]" in image:+ return None+ if b"coli_hip.dll" in image:+ return "coli_hip.dll"+ if b"coli_cuda.dll" in image:+ return "coli_cuda.dll"+ if b"[CUDA] mode: routed experts" in image or b"[K3-CUDA]" in image:+ return "coli_cuda.dll"+ return None++def cuda_linkage(engine_path):"""Return CUDA linkage state without loading the executable or CUDA runtime."""engine = Path(engine_path)@@ -484,17 +504,11 @@ def cuda_linkage(engine_path):if sys.platform == "win32":# Windows DLL-split builds never link the GPU runtime directly: the host# LoadLibrary's its backend at runtime (backend_loader.c), so there's no- # import-table entry for ldd/dumpbin to see. Detect the GPU build via a- # marker string baked into the engine's #ifdef COLI_CUDA block, then- # require the backend artifact to sit next to the executable.- #- # WHICH artifact is not a guess. backend_loader.c compiles exactly one- # basename into the host -- COLI_BACKEND_DLL is "coli_hip.dll" under- # COLI_HIP_DLL and "coli_cuda.dll" otherwise -- so the binary states- # what it will load and we check for that. Asking for coli_cuda.dll- # unconditionally failed a working HIP host (a hard error, not a- # warning), and accepting either name would have passed a HIP host that- # only had a stray CUDA backend beside it.+ # import-table entry for ldd/dumpbin to see. Detect the GPU build from+ # the backend basename compiled into the host, then require that file+ # next to the executable. Asking for coli_cuda.dll unconditionally+ # failed a working HIP host (a hard error, not a warning), and requiring+ # the GLM routed-experts banner missed every Kimi K3 CUDA_DLL build.try:image = engine.read_bytes()except OSError:@@ -506,10 +520,7 @@ def cuda_linkage(engine_path):present = any((engine.parent / name).is_file()for name in ("coli_cuda_dsv4_dg.dll", "coli_cuda_dsv4.dll"))return {"linked": present, "missing": not present}- if b"[CUDA] mode: routed experts" not in image:- return {"linked": False, "missing": False}- expected = next((name for name in ("coli_hip.dll", "coli_cuda.dll")- if name.encode() in image), None)+ expected = windows_backend_dll(image)if expected is None:return {"linked": False, "missing": False}dll_present = (engine.parent / expected).is_file()diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex a05f6336..549d1eb8 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -533,6 +533,13 @@ static int g_k3_mmap=0; /* prepared U8/F32 tensors stay file-bastatic uint64_t g_k3_mmap_bytes=0, g_k3_mmap_views=0;#ifdef COLI_CUDAstatic int g_k3_cuda=0; /* K3_CUDA=1: MXFP4 routed experts on CUDA at decode */+/* K3_CUDA is the explicit override. COLI_CUDA is what `coli --gpu` writes. */+static int k3_cuda_requested(void){+ const char *e=getenv("K3_CUDA");+ if(e && *e) return atoi(e);+ e=getenv("COLI_CUDA");+ return e ? atoi(e) : 0;+}#endifstatic int g_k3_direct=-1; /* K3_DIRECT: O_DIRECT expert reads */static int g_k3_idot=1; /* K3_IDOT: int8-activation expert matmuls */@@ -840,7 +847,7 @@ static void model_init_range(Model *m, const char *snap, int layer_begin,{ const char *ev=getenv("K3_VK"); vk_requested=!ev||atoi(ev); }#endif#ifdef COLI_CUDA- cuda_requested=getenv("K3_CUDA")&&atoi(getenv("K3_CUDA"));+ cuda_requested=k3_cuda_requested();#endifif(!k3_mmap_backend_allowed(1,vk_requested,cuda_requested)){fprintf(stderr,"K3_MMAP=1 is CPU-only; set K3_VK=0 and K3_CUDA=0 -- refusing before model load\n");@@ -849,7 +856,7 @@ static void model_init_range(Model *m, const char *snap, int layer_begin,fprintf(stderr,"[K3-MMAP] prepared tensor mapping enabled (CPU-only, no conversion fallback)\n");}#ifdef COLI_CUDA- g_k3_cuda = getenv("K3_CUDA") ? atoi(getenv("K3_CUDA")) : 0;+ g_k3_cuda = k3_cuda_requested();if(g_k3_cuda){int dev0 = 0;if(!coli_cuda_init(&dev0, 1)){diff --git a/c/tests/test_cuda_binary_engine.py b/c/tests/test_cuda_binary_engine.pyindex 6130cf44..5aa84114 100644--- a/c/tests/test_cuda_binary_engine.py+++ b/c/tests/test_cuda_binary_engine.py@@ -1,12 +1,15 @@"""#1533: cuda_binary() must inspect the engine that will run, not always the-GLM binary. On Windows a CUDA_DLL build is recognised by a marker string in-the executable plus coli_cuda.dll next to it; a qwen36 build that carries-both must pass even when colibri.exe is CPU-only or absent."""+GLM binary. On Windows a CUDA_DLL/HIP_DLL host is recognised by the backend+basename compiled into the loader, plus that file next to the engine. The+GLM/Qwen banner is not required: Kimi K3 CUDA_DLL builds link the same+loader without printing it, and a HIP host loads coli_hip.dll."""import importlib.machineryimport importlib.util+import jsonimport osimport sysimport tempfile+import typesimport unittestfrom pathlib import Pathfrom unittest import mock@@ -48,9 +51,66 @@ class CudaBinaryEngineTest(unittest.TestCase):with mock.patch.object(self.m.sys, "platform", "win32"):self.assertFalse(self.m.cuda_binary(str(qwen)), "no DLL next to the engine")+ def test_windows_kimi_cuda_dll_without_glm_banner(self):+ """Kimi K3 CUDA_DLL=1 links backend_loader.o and prints [K3-CUDA], not+ the GLM routed-experts banner. --gpu used to refuse that install."""+ with tempfile.TemporaryDirectory() as d:+ eng = Path(d) / "kimi_k3.exe"+ eng.write_bytes(b"MZ [K3-CUDA] MXFP4 routed experts coli_cuda.dll")+ (Path(d) / "coli_cuda.dll").write_bytes(b"MZ")+ with mock.patch.object(self.m.sys, "platform", "win32"):+ self.assertTrue(self.m.cuda_binary(str(eng)))+ (Path(d) / "coli_cuda.dll").unlink()+ with mock.patch.object(self.m.sys, "platform", "win32"):+ self.assertFalse(self.m.cuda_binary(str(eng)))++ def test_windows_hip_host_accepts_coli_hip_dll(self):+ """backend_loader.c under HIP_DLL=1 loads coli_hip.dll. Requiring+ coli_cuda.dll refused a working HIP host that doctor already accepted."""+ with tempfile.TemporaryDirectory() as d:+ eng = Path(d) / "colibri.exe"+ eng.write_bytes(b"MZ [CUDA] mode: routed experts coli_hip.dll")+ (Path(d) / "coli_hip.dll").write_bytes(b"MZ")+ with mock.patch.object(self.m.sys, "platform", "win32"):+ self.assertTrue(self.m.cuda_binary(str(eng)))+ (Path(d) / "coli_cuda.dll").write_bytes(b"MZ")+ (Path(d) / "coli_hip.dll").unlink()+ with mock.patch.object(self.m.sys, "platform", "win32"):+ self.assertFalse(self.m.cuda_binary(str(eng)),+ "a stray CUDA DLL must not satisfy a HIP host")+def test_missing_engine_is_false(self):self.assertFalse(self.m.cuda_binary("/no/such/engine"))+ def test_kimi_gpu_flag_sets_k3_cuda(self):+ """--gpu writes COLI_CUDA=1. Kimi reads K3_CUDA, so the flag used to+ pass the build check and still leave experts on the CPU."""+ with tempfile.TemporaryDirectory() as d:+ model = Path(d)+ (model / "config.json").write_text(+ json.dumps({"model_type": "kimi_linear"}), encoding="utf-8")+ a = types.SimpleNamespace(+ model=str(model), gpu="0", vram=None, ram=0, ctx=None,+ ngen=None, temp=None, cap=None, auto_tier=False)+ with mock.patch.dict(os.environ, {}, clear=True), \+ mock.patch.object(self.m, "cuda_binary", return_value=True):+ env = self.m.env_for_engine(a, "kimi")+ self.assertEqual(env["COLI_CUDA"], "1")+ self.assertEqual(env["K3_CUDA"], "1")++ def test_kimi_gpu_none_clears_k3_cuda(self):+ with tempfile.TemporaryDirectory() as d:+ model = Path(d)+ (model / "config.json").write_text(+ json.dumps({"model_type": "kimi_linear"}), encoding="utf-8")+ a = types.SimpleNamespace(+ model=str(model), gpu="none", vram=None, ram=0, ctx=None,+ ngen=None, temp=None, cap=None, auto_tier=False)+ with mock.patch.dict(os.environ, {"K3_CUDA": "1"}, clear=True):+ env = self.m.env_for_engine(a, "kimi")+ self.assertEqual(env["COLI_CUDA"], "0")+ self.assertEqual(env["K3_CUDA"], "0")+if __name__ == "__main__":unittest.main()diff --git a/c/tests/test_doctor.py b/c/tests/test_doctor.pyindex 419347c4..3e2d5029 100644--- a/c/tests/test_doctor.py+++ b/c/tests/test_doctor.py@@ -242,6 +242,22 @@ class DoctorTest(unittest.TestCase):self.assertEqual(self._win_linkage(engine),{"linked": False, "missing": False})+ def test_windows_kimi_cuda_host_without_glm_banner(self):+ """Kimi K3 CUDA_DLL=1 never prints [CUDA] mode: routed experts.+ The loader still compiles coli_cuda.dll into the host, and that is+ the artifact doctor must require -- the same pair cuda_binary uses."""+ engine = self.root / "kimi_k3.exe"+ engine.write_bytes(b"MZ [K3-CUDA] MXFP4 routed experts coli_cuda.dll")+ (self.root / "coli_cuda.dll").write_bytes(b"")+ self.assertEqual(self._win_linkage(engine),+ {"linked": True, "missing": False})++ def test_windows_kimi_cuda_host_missing_dll_is_missing(self):+ engine = self.root / "kimi_k3.exe"+ engine.write_bytes(b"MZ [K3-CUDA] MXFP4 routed experts coli_cuda.dll")+ self.assertEqual(self._win_linkage(engine),+ {"linked": False, "missing": True})+def test_hip_host_with_its_backend_reports_gpu_available(self):# End-to-end: the same host that previously reported a hard error# ("GPU runtime library is missing") now passes, with #903's