Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vulkan: one rule for where the dense matrices go; on a shared-RAM device with the tier, the CPU
JustVuggbe77bf351f5b
diff --git a/c/backend_vulkan.c b/c/backend_vulkan.cindex a645eef2..b4cd83e0 100644--- a/c/backend_vulkan.c+++ b/c/backend_vulkan.c@@ -1194,17 +1194,47 @@ const char *coli_vk_shader_path(char *buf, size_t n) {return "shaders/qmatmul.spv";}-int coli_vk_init_env(const char *engine) {+/* The dense matrices' place (coli_vk_dense_decide): the last decision, and the one+ * last printed (-1 none). */+static int g_dense_on = 1, g_dense_said = -1;+static int dense_choice(int tier_on, int def, char *why, size_t n) {+ const char *e = getenv("COLI_VK_DENSE");+ if (e && *e) { snprintf(why, n, "COLI_VK_DENSE=%s", e); return atoi(e) != 0; }+ if (def && tier_on && coli_vk_device_shares_ram()) {+ snprintf(why, n, "%s shares the CPU's RAM and the expert tier is on; COLI_VK_DENSE=1 puts them on the device",+ coli_vk_device_integrated() ? "an integrated GPU" : "a CPU device");+ return 0;+ }+ snprintf(why, n, "default");+ return def != 0;+}+int coli_vk_dense_decide(const char *engine, int tier_on, int def) {+ char why[192];+ g_dense_on = dense_choice(tier_on, def, why, sizeof why);+ if (engine && g_dense_on != g_dense_said) {+ fprintf(stderr, "[VK] %s: dense matrices on the %s (%s)\n", engine, g_dense_on ? "device" : "CPU", why);+ g_dense_said = g_dense_on;+ }+ return g_dense_on;+}+int coli_vk_dense(void) { return g_dense_on; }++int coli_vk_init_env_tier(const char *engine, int tier_on) {const char *on = getenv("COLI_VULKAN");if (!on || !atoi(on)) return 0;char buf[1024];const char *spv = coli_vk_shader_path(buf, sizeof buf);int ok = coli_vk_init(spv) && coli_vk_available();- if (ok) fprintf(stderr, "[VK] %s: device ready\n", engine);- else fprintf(stderr, "[VK] %s: no usable Vulkan device (shaders %s), running on the CPU\n",- engine, spv);+ if (ok) {+ char why[192];+ g_dense_on = g_dense_said = dense_choice(tier_on, 1, why, sizeof why);+ fprintf(stderr, "[VK] %s: device ready, dense matrices on the %s (%s)\n", engine,+ g_dense_on ? "device" : "CPU", why);+ } else fprintf(stderr, "[VK] %s: no usable Vulkan device (shaders %s), running on the CPU\n",+ engine, spv);return ok;}+int coli_vk_init_env(const char *engine) { return coli_vk_init_env_tier(engine, 0); }/* Fused first half of the expert MLP: hidden = silu(gate(x)) * up(x), computed in ONE* dispatch that reads x once for both projections. gate/up are resident (uploaded ondiff --git a/c/backend_vulkan.h b/c/backend_vulkan.hindex 15bd4d78..d7f0f315 100644--- a/c/backend_vulkan.h+++ b/c/backend_vulkan.h@@ -207,9 +207,23 @@ size_t coli_vk_device_local_bytes(void);const char *coli_vk_device_name(void);/* For engines: COLI_VULKAN=1 opens the device with the shaders found by- * coli_vk_shader_path() and prints one line naming the engine; 0 (and one line) when- * Vulkan is not asked for or no device is usable, so the engine stays on the CPU. */+ * coli_vk_shader_path() and prints one line naming the engine and where its dense+ * matrices go (coli_vk_dense_decide below); 0 (and one line) when Vulkan is not asked+ * for or no device is usable, so the engine stays on the CPU. tier_on: the engine is+ * about to start the routed-expert tier (vk_tier.c); coli_vk_init_env(engine) is+ * coli_vk_init_env_tier(engine, 0). */int coli_vk_init_env(const char *engine);+int coli_vk_init_env_tier(const char *engine, int tier_on);+/* Where an engine's dense (resident, non-expert) matrices run, the one rule every+ * engine follows. COLI_VK_DENSE set and non-empty: 0 keeps them on the CPU, any other+ * number puts them on the device. Unset: the engine's default `def`, except that with+ * the routed-expert tier on (tier_on) a device that shares the CPU's RAM (an+ * integrated GPU, or a CPU device such as Lavapipe) keeps them on the CPU: there the+ * dense matmuls, one synchronous call each, cost more than the tier gains (measured+ * on a Radeon 780M, docs/vulkan.md). The decision is kept for coli_vk_dense(); with+ * an engine name it is printed as a [VK] line when it changes (NULL: silent). */+int coli_vk_dense_decide(const char *engine, int tier_on, int def);+int coli_vk_dense(void); /* the last decision; 1 before any *//* COLI_VK_SHADERS (the .spv or its directory), else shaders/ next to the binary, else* shaders/ in the working directory. buf holds the result when it is not a literal. */const char *coli_vk_shader_path(char *buf, size_t n);diff --git a/c/colibri.c b/c/colibri.cindex 20b9048d..dfd3bed8 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -12324,7 +12324,7 @@ int main(int argc, char **argv){/* 320 = sweep optimum on a 16 GB card (256-384 measured flat, 320 best median;* ~6 GB tier + ~8 GB dense leaves headroom for the long-context KV mirror). */g_vk_budget = getenv("COLI_VK_EXPERTS") ? atoi(getenv("COLI_VK_EXPERTS")) : 320;- g_vk_dense = getenv("COLI_VK_DENSE") ? atoi(getenv("COLI_VK_DENSE")) : 0;+ g_vk_dense = coli_vk_dense_decide(NULL, g_vk_budget > 0, 0); /* COLI_VK_DENSE; this engine's default is off */g_vk_attn = getenv("COLI_VK_ATTN") ? atoi(getenv("COLI_VK_ATTN")) : 0;fprintf(stderr,"[VK] expert tier active: routed quantized experts on the GPU (budget %d)%s%s\n",g_vk_budget, g_vk_dense ? " + dense projections + shared expert" : "",diff --git a/c/deepseek_v4.c b/c/deepseek_v4.cindex dc9fba30..7d1780df 100644--- a/c/deepseek_v4.c+++ b/c/deepseek_v4.c@@ -12105,7 +12105,7 @@ static void v4_vk_open(const ColiV4Engine *engine) {g_v4_vk_thread = pthread_self();g_v4_vk_engine = engine;g_v4_vk_ready = coli_vk_init_env("deepseek_v4");- if (g_v4_vk_ready) coli_v4_vk_matmul = v4_vk_matmul_impl;+ if (g_v4_vk_ready && coli_vk_dense()) coli_v4_vk_matmul = v4_vk_matmul_impl; /* COLI_VK_DENSE=0: no hook */#else(void)engine;#endifdiff --git a/c/deepseek_v41.c b/c/deepseek_v41.cindex 9e634021..ae09542d 100644--- a/c/deepseek_v41.c+++ b/c/deepseek_v41.c@@ -425,7 +425,7 @@ static VkEntry *vk_entry(const void *data, int fmt, int O, int I) {* device wants them packed, so they are packed here. */static int vk_mul(int fmt, const void *data, const uint8_t *tiles, int O, int I,float *y, int ystride, const float *x, int xstride, int rows) {- if (!g_vk_ready || rows < 1 || !pthread_equal(pthread_self(), g_vk_thread)) return 0;+ if (!g_vk_ready || !coli_vk_dense() || rows < 1 || !pthread_equal(pthread_self(), g_vk_thread)) return 0;#ifdef _OPENMPif (omp_in_parallel()) return 0;#endifdiff --git a/c/inkling.c b/c/inkling.cindex f5fcb55f..4a3eb8af 100644--- a/c/inkling.c+++ b/c/inkling.c@@ -996,7 +996,7 @@ static void ink_vk_mark(Wt *w, int views, int *by, int *cpu) {}static void ink_vk_init_model(Model *m, int layer_begin, int layer_end) {if (!g_vk_ready) g_vk_ready = coli_vk_init_env("inkling");- if (!g_vk_ready) return;+ if (!g_vk_ready || !coli_vk_dense()) return; /* COLI_VK_DENSE=0: nothing marked, the CPU computes */int by[13] = {0}, cpu = 0;ink_vk_mark(&m->lm_head, 1, by, &cpu);for (int i = layer_begin; i < layer_end; i++) {diff --git a/c/mimo.c b/c/mimo.cindex c837b4bc..e69c5fe8 100644--- a/c/mimo.c+++ b/c/mimo.c@@ -353,7 +353,7 @@ static int dw_upload(DW *d) {}static int dw_matmul_vk(float *y, const float *x, int S, const DW *d) {- if (!g_vk_ready || d->vk_off || !vk_main_thread()) return 0;+ if (!g_vk_ready || !coli_vk_dense() || d->vk_off || !vk_main_thread()) return 0;DW *dev = (DW *)d; /* the device copy is a cache inside a read-only matrix */if (!dw_upload(dev)) return 0;int gs; const float *sc;@@ -1792,7 +1792,7 @@ int main(int argc, char **argv) {#ifdef COLI_VULKAN/* after the weights, so a missing device costs one line and nothing else */g_vk_ready = coli_vk_init_env("mimo");- if (g_vk_ready) {+ if (g_vk_ready && coli_vk_dense()) { /* COLI_VK_DENSE=0: the trunk stays on the CPU */int up = vk_dense_upload(m);size_t used = 0, count = 0;coli_vk_mem_info(&used, &count);diff --git a/c/olmoe.c b/c/olmoe.cindex 7881a0ba..cb3d719e 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -321,7 +321,7 @@ static void matmul_res(float *y, const float *x, const float *W, void **vk, int#ifdef _OPENMPserial = !omp_in_parallel();#endif- if (g_vk_ready && serial && *vk != (void *)&g_vk_refused) {+ if (g_vk_ready && serial && *vk != (void *)&g_vk_refused && coli_vk_dense()) {if (coli_vk_matmul((ColiVkTensor **)vk, y, x, W, NULL, 10, S, I, O, 0)) return;if (!*vk) *vk = &g_vk_refused;}@@ -718,7 +718,7 @@ static void model_init(Model *m, const char *snap, int cap, int bits) {* the CPU). The host copies stay: they are the fallback, so on a GPU that* shares RAM with the CPU the dense set is held twice. */if (!g_vk_ready) g_vk_ready = coli_vk_init_env("olmoe");- if (g_vk_ready)+ if (g_vk_ready && coli_vk_dense())fprintf(stderr, "[VK] olmoe: %d resident f32 matrices (attention q/k/v/o, router, lm_head) ""go to the GPU on first use; routed experts and the embedding lookup stay on the CPU\n",5 * m->c.n_layers + 1);diff --git a/c/qwen36.c b/c/qwen36.cindex 3a7324b2..2d438929 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -4692,8 +4692,8 @@ int main(int argc, char **argv) {#ifdef COLI_VULKAN/* After the weights: the dense matrices upload at their first matmul_d.* No device (or COLI_VULKAN unset) leaves g_vk_ready 0, the CPU path. */- g_vk_ready = coli_vk_init_env("qwen36");- { const char *vd = getenv("COLI_VK_DENSE"); g_vk_dense = !(vd && vd[0] == '0'); }+ g_vk_ready = coli_vk_init_env_tier("qwen36", vkt_wanted() && m.c.n_experts > 0 && !qq_active());+ g_vk_dense = coli_vk_dense(); /* COLI_VK_DENSE; unset, off on a device sharing the CPU's RAM with the tier on */#endifif (ref_image && ref_image->t == J_OBJ) {jval *gh = json_get(ref_image, "grid_h"), *gw = json_get(ref_image, "grid_w");@@ -4878,6 +4878,7 @@ int main(int argc, char **argv) {#ifdef COLI_VULKANvk_tier_start(&m, snap, cap, expert_is_int4, expert_mixed); /* COLI_VULKAN=1: hot routed experts on the device */+ if (g_vk_ready && !vkt_ready() && !g_vk_dense) g_vk_dense = coli_vk_dense_decide("qwen36", 0, 1); /* no tier after all */#endif/* coli serve mode: speak the gateway wire protocol instead of argvdiff --git a/c/qwen38.c b/c/qwen38.cindex bba4e429..8613f13d 100644--- a/c/qwen38.c+++ b/c/qwen38.c@@ -2042,8 +2042,8 @@ int main(int argc, char **argv) {#ifdef COLI_VULKAN/* After the trunk is int8: those rows upload at their first matmul. No* device (or COLI_VULKAN unset) leaves g_vk_ready 0, the CPU path. */- g_vk_ready=coli_vk_init_env("qwen38");- { const char *vd=getenv("COLI_VK_DENSE"); g_vk_dense=!(vd&&vd[0]=='0'); }+ g_vk_ready=coli_vk_init_env_tier("qwen38",vkt_wanted()&&m.c.experts>0&&!qt_ready());+ g_vk_dense=coli_vk_dense(); /* COLI_VK_DENSE; unset, off on a device sharing the CPU's RAM with the tier on */#endifif(is_ref)ref_logits=read_reference_logits(ref_root,m.c.vocab);g_capture_last_logit=ref_logits!=NULL||getenv("DUMP")!=NULL;@@ -2055,6 +2055,7 @@ int main(int argc, char **argv) {q38_telemetry_init(snap, &m);#ifdef COLI_VULKANq38_vk_tier_start(&m, cap); /* COLI_VULKAN=1: hot routed experts on the device (vk_tier.c) */+ if(g_vk_ready&&!vkt_ready()&&!g_vk_dense)g_vk_dense=coli_vk_dense_decide("qwen38",0,1); /* no tier after all */#endiffprintf(stderr, "resident weights loaded in %.1fs | RSS after load: %.2f GB\n", m.dense_load_s, rss_gb());diff --git a/c/qwenimage.c b/c/qwenimage.cindex 7c37475e..e74287e6 100644--- a/c/qwenimage.c+++ b/c/qwenimage.c@@ -173,7 +173,7 @@ static float *vec_load(shards *S, const char *name, int n){* quantize its activations. 0 when the product stays on the CPU. */static int qi_vk_linear(float *y, const float *x, int M, const Lin *l){#ifdef COLI_VULKAN- if (!g_vk_ready || !l->gpu || l->vk_off || l->m.ld || M < 1) return 0;+ if (!g_vk_ready || !coli_vk_dense() || !l->gpu || l->vk_off || l->m.ld || M < 1) return 0;#ifdef _OPENMPif (omp_in_parallel()) return 0;#endifdiff --git a/c/vk_tier.c b/c/vk_tier.cindex 2a920c65..c186d56b 100644--- a/c/vk_tier.c+++ b/c/vk_tier.c@@ -625,10 +625,14 @@ static double env_gb(const char *name, double def) {return e && *e ? atof(e) : def;}+int vkt_wanted(void) {+ const char *on = getenv("COLI_VK_TIER");+ return !(on && *on == '0');+}+int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {if (T.on || !cfg) return 0;- const char *on = getenv("COLI_VK_TIER");- if (on && *on == '0') return 0;+ if (!vkt_wanted()) return 0;if (!coli_vk_available()) return 0;const char *eng = cfg->engine ? cfg->engine : "engine";memset(&T, 0, sizeof T);diff --git a/c/vk_tier.h b/c/vk_tier.hindex dc2acff2..ccd8d611 100644--- a/c/vk_tier.h+++ b/c/vk_tier.h@@ -21,6 +21,16 @@* VK=1 (the inline stubs below), nothing here runs and the engine is unchanged.** ---- integrating an engine (the phase-2 guide) -------------------------------+ * 0. Open the device saying whether the tier will be tried, so the dense matrices+ * get their default place (on a device that shares the CPU's RAM they stay on+ * the CPU while the tier is on; COLI_VK_DENSE decides when set):+ *+ * ready = coli_vk_init_env_tier("glm53", vkt_wanted() && E > 0 && !cuda_tier);+ * dense = coli_vk_dense(); // the engine's dense hook checks this+ * ... // step 1+ * if (ready && !vkt_ready() && !dense)+ * dense = coli_vk_dense_decide("glm53", 0, 1); // no tier after all+ ** 1. Describe the experts once, after the model and the dense trunk are loaded and* after the engine's history is read (rt_load), before the first token:*@@ -141,6 +151,9 @@ typedef struct {} VktExpertSrc;#ifdef COLI_VULKAN+/* 1 unless COLI_VK_TIER=0: whether an engine with routed experts will try the tier+ * (for coli_vk_init_env_tier, before the engine knows whether vkt_init succeeds). */+int vkt_wanted(void);/* 1 = the tier is on. heat: [layers][experts] routing counts, or NULL. */int vkt_init(const VktConfig *cfg, uint32_t *const *heat);int vkt_ready(void);@@ -156,6 +169,7 @@ void vkt_report(const char *scope, unsigned long long ram_hits, unsigned long lo/* Sizing helpers for engines: bytes one expert takes on the device in a source format. */size_t vkt_expert_bytes(int hidden, int inter, VktFmt gate_up, VktFmt down);#else+static inline int vkt_wanted(void){return 0;}static inline int vkt_init(const VktConfig *c, uint32_t *const *h){(void)c;(void)h;return 0;}static inline int vkt_ready(void){return 0;}static inline void vkt_shutdown(void){}