Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(inkling): measure RAM on Windows so auto-cap is not 16 experts

kevin9327c703d2651c36
diff --git a/c/inkling.c b/c/inkling.c
index b58b6bb5..c7ce91ff 100644
--- a/c/inkling.c
+++ b/c/inkling.c
@@ -1122,24 +1122,18 @@ static void model_init(Model *m, const char *snap, int cap, int bits) {
}
static double mem_avail_bytes(void) {
-#if defined(__linux__)
- FILE *f = fopen("/proc/meminfo", "r");
- if (!f) return 0;
- char ln[256]; double kb = 0;
- while (fgets(ln, sizeof(ln), f)) if (sscanf(ln, "MemAvailable: %lf", &kb) == 1) break;
- fclose(f);
- return kb * 1024.0;
-#elif defined(__APPLE__)
- /* free + inactive + purgeable ~ Linux MemAvailable. Without this the auto
- * cap fell back to 16 experts/layer on a 128 GB Mac. */
- vm_size_t page = 0; host_page_size(mach_host_self(), &page);
- vm_statistics64_data_t vs; mach_msg_type_number_t n = HOST_VM_INFO64_COUNT;
- if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&vs, &n) != KERN_SUCCESS)
- return 0;
- return (double)(vs.free_count + vs.inactive_count + vs.purgeable_count) * page;
-#else
+ /* Shared probe: Linux MemAvailable, macOS free+inactive+purgeable,
+ * Windows ullAvailPhys/commit. The #else here used to return 0, so
+ * Windows auto-cap was always 16 experts/layer and never warned. */
+ double gb = compat_mem_available_gb();
+ if (gb > 0.0) return gb * 1e9;
+ static int noted = 0;
+ if (!noted) {
+ noted = 1;
+ fprintf(stderr, "[inkling] could not measure available RAM on this platform; "
+ "auto cache falls back to 16 experts/layer. Pass --cap to set it.\n");
+ }
return 0;
-#endif
}
/* ---------- routed-expert slots: serial bookkeeping, parallel fills ---------- */
@@ -2499,6 +2493,25 @@ static void serve_hwinfo(Model *m) {
if (sscanf(ln, "MemTotal: %lf", &v) == 1) rt = v/1e6;
if (sscanf(ln, "MemAvailable: %lf", &v) == 1) ra = v/1e6;
} fclose(mi); }
+ if (rt <= 0.0 || ra <= 0.0) {
+ double t2 = 0, a2 = 0;
+ compat_meminfo_gb(&t2, &a2);
+ if (rt <= 0.0) rt = t2;
+ if (ra <= 0.0) ra = a2;
+ }
+#ifdef _WIN32
+ if (cores <= 0) {
+ SYSTEM_INFO si;
+ GetSystemInfo(&si);
+ cores = (int)si.dwNumberOfProcessors;
+ }
+#endif
+#ifdef __APPLE__
+ if (!cpu[0]) {
+ size_t sl = sizeof(cpu);
+ if (sysctlbyname("machdep.cpu.brand_string", cpu, &sl, NULL, 0)) cpu[0] = 0;
+ }
+#endif
int ngpu = 0; double vram = 0;
const char *gpu = "";
#ifdef COLI_CUDA
diff --git a/c/tests/test_inkling_cache_index.c b/c/tests/test_inkling_cache_index.c
index 797f6af4..72d5571e 100644
--- a/c/tests/test_inkling_cache_index.c
+++ b/c/tests/test_inkling_cache_index.c
@@ -82,6 +82,14 @@ int main(void){
free_cache(&m);
check_lookup_scaling(44);
check_lookup_scaling(219);
+ {
+ double avail = mem_avail_bytes();
+ double probe = compat_mem_available_gb();
+ CHECK(avail > 0.0, "mem_avail_bytes returned 0; auto cap would be 16 experts/layer");
+ double gb = avail / 1e9;
+ double gap = gb > probe ? gb - probe : probe - gb;
+ CHECK(gap < 0.25, "mem_avail_bytes diverged from the shared probe (%.3f vs %.3f GB)", gb, probe);
+ }
if(failures){ fprintf(stderr,"inkling cache index: %d failure(s)\n",failures); return 1; }
puts("inkling cache index: ok");
return 0;
diff --git a/c/tests/test_inkling_mem_probe.py b/c/tests/test_inkling_mem_probe.py
new file mode 100644
index 00000000..c25f6b6e
--- /dev/null
+++ b/c/tests/test_inkling_mem_probe.py
@@ -0,0 +1,79 @@
+"""Inkling auto-sized its expert cache from mem_avail_bytes().
+
+On Windows that function took the #else return 0 branch, so every
+`coli serve --model <inkling>` with no --cap used 16 experts/layer
+regardless of installed RAM, and /health reported ram_total_gb 0.0.
+"""
+import ctypes
+import re
+import sys
+import unittest
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent
+ENGINE_C = HERE.parent / "inkling.c"
+
+
+def _c_function(source, signature):
+ match = re.search(re.escape(signature) + r"\s*\{", source)
+ if not match:
+ raise ValueError("no definition for " + signature)
+ start = match.start()
+ brace = match.end() - 1
+ depth = 0
+ for index, char in enumerate(source[brace:], start=brace):
+ if char == "{":
+ depth += 1
+ elif char == "}":
+ depth -= 1
+ if depth == 0:
+ return source[start:index + 1]
+ raise ValueError("unbalanced braces in " + signature)
+
+
+def _windows_avail_phys():
+ class MEMORYSTATUSEX(ctypes.Structure):
+ _fields_ = [
+ ("dwLength", ctypes.c_ulong),
+ ("dwMemoryLoad", ctypes.c_ulong),
+ ("ullTotalPhys", ctypes.c_ulonglong),
+ ("ullAvailPhys", ctypes.c_ulonglong),
+ ("ullTotalPageFile", ctypes.c_ulonglong),
+ ("ullAvailPageFile", ctypes.c_ulonglong),
+ ("ullTotalVirtual", ctypes.c_ulonglong),
+ ("ullAvailVirtual", ctypes.c_ulonglong),
+ ("ullAvailExtendedVirtual", ctypes.c_ulonglong),
+ ]
+ stat = MEMORYSTATUSEX()
+ stat.dwLength = ctypes.sizeof(stat)
+ ok = ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat))
+ return stat.ullAvailPhys if ok else 0
+
+
+class InklingMemProbeTest(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.source = ENGINE_C.read_text(encoding="utf-8")
+ cls.avail = _c_function(cls.source, "static double mem_avail_bytes(void)")
+ cls.hwinfo = _c_function(cls.source, "static void serve_hwinfo(Model *m)")
+
+ def test_mem_avail_bytes_uses_the_shared_probe(self):
+ self.assertIn("compat_mem_available_gb", self.avail)
+ self.assertNotRegex(
+ self.avail, r"#else\s*\n\s*return 0;",
+ "Windows still takes a return-0 branch instead of measuring RAM")
+
+ def test_serve_hwinfo_fills_ram_when_proc_is_missing(self):
+ self.assertIn("compat_meminfo_gb", self.hwinfo)
+
+ def test_windows_host_has_ram_the_old_probe_would_ignore(self):
+ if sys.platform != "win32":
+ return
+ free = _windows_avail_phys()
+ self.assertGreater(free, 1_000_000_000,
+ "this host reports no reclaimable RAM")
+ if re.search(r"#else\s*\n\s*return 0;", self.avail):
+ self.fail(
+ "mem_avail_bytes() returns 0 on Windows; this host has "
+ f"{free / 1e9:.1f} GB free, so auto cap would stay at "
+ "16 experts/layer")