Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
colibri, glm53: the shared chain decision; lost devices counted from the end
JustVugg9a73502f0530
diff --git a/c/glm53_chain.h b/c/glm53_chain.hindex a15135ea..e58a7ebb 100644--- a/c/glm53_chain.h+++ b/c/glm53_chain.h@@ -1,7 +1,8 @@/* glm53_chain.h -- GLM-5.3 Flash's layers as a dense chain on the Vulkan device* (vk_chain.h). Included once by glm53.c in a COLI_VULKAN build, after run_layers, the* CPU forward it stands in for; COLI_VK_CHAIN decides (coli_vk_chain_decide, this- * engine's integrated GPU default off: not measured on a GLM-5.3 checkpoint).+ * engine's integrated GPU default COLI_VK_CHAIN_UNMEASURED: off, not measured on a+ * GLM-5.3 checkpoint).** The residual is hc_mult streams per position (mHC). What runs where, per layer, for* one block of rows (decode: one row):@@ -542,20 +543,12 @@ static void g53c_start(GModel *m) {if (!g_vk_ready) return;const Cfg *c = &m->c;int tier_on = vkt_wanted() && m->streaming && c->n_experts > 0 && c->swiglu_limit > 0.f;- int on = coli_vk_chain_decide(NULL, tier_on, COLI_VK_CHAIN_OFF);- const char *e = getenv("COLI_VK_CHAIN");- char why[160];- if (e && *e) snprintf(why, sizeof why, "COLI_VK_CHAIN=%s", e);- else if (coli_vk_device_integrated())- snprintf(why, sizeof why, "an integrated GPU: not measured on a GLM-5.3 checkpoint; COLI_VK_CHAIN=1 on, 2 prompts only");- else if (coli_vk_device_shares_ram()) snprintf(why, sizeof why, "a CPU device; COLI_VK_CHAIN=1 turns it on");- else snprintf(why, sizeof why, "a discrete GPU%s", tier_on ? ", beside the expert tier" : "");+ int on = coli_vk_chain_decide("glm53", tier_on, COLI_VK_CHAIN_UNMEASURED);const char *no = NULL;if (on && !(g_g53c_inited = vkc_init())) no = "the chain's pipelines did not come up";if (on && !no && !(vkc_mla_ready() && vkc_kda_ready() && vkc_mhc_ready()))no = "the MLA, KDA or mHC shaders are missing";- fprintf(stderr, "[VK] glm53: dense chain %s (%s%s%s)\n",- on && !no ? (on == COLI_VK_CHAIN_PREFILL ? "on for prompts" : "on") : "off", why, no ? "; " : "", no ? no : "");+ if (no) fprintf(stderr, "[VK] glm53: %s: the dense chain stays off\n", no);if (!on || no) return;if (!g53c_setup(m)) return;g_vk_chain = on;diff --git a/c/glm_chain.h b/c/glm_chain.hindex 9d0355a5..4838a27b 100644--- a/c/glm_chain.h+++ b/c/glm_chain.h@@ -1,7 +1,7 @@/* glm_chain.h -- GLM-5.2's layers as a dense chain on the Vulkan device (vk_chain.h).* Included once by colibri.c in a COLI_VULKAN build, after the CPU layer forward it* stands in for; COLI_VK_CHAIN decides (coli_vk_chain_decide, this engine's integrated- * GPU default off: not measured on a GLM checkpoint).+ * GPU default COLI_VK_CHAIN_UNMEASURED: off, not measured on a GLM checkpoint).** What runs where, per layer, for one block of rows (decode: one row):* device, frame A1: the routed MoE output of the layer before joins the residual@@ -415,14 +415,7 @@ static void glmc_report_atexit(void) { glmc_report(g_glmc_model); }static void glmc_start(Model *m) {if (!g_vulkan) return;int tier_on = vkt_wanted() && g_vk_experts != 0 && m->c.n_experts > 0;- int on = coli_vk_chain_decide(NULL, tier_on, COLI_VK_CHAIN_OFF);- const char *e = getenv("COLI_VK_CHAIN");- char why[160];- if (e && *e) snprintf(why, sizeof why, "COLI_VK_CHAIN=%s", e);- else if (coli_vk_device_integrated())- snprintf(why, sizeof why, "an integrated GPU: not measured on a GLM checkpoint; COLI_VK_CHAIN=1 on, 2 prompts only");- else if (coli_vk_device_shares_ram()) snprintf(why, sizeof why, "a CPU device; COLI_VK_CHAIN=1 turns it on");- else snprintf(why, sizeof why, "a discrete GPU%s", tier_on ? ", beside the expert tier" : "");+ int on = coli_vk_chain_decide("colibri", tier_on, COLI_VK_CHAIN_UNMEASURED);const char *no = NULL;#ifdef COLI_CUDAif (on && g_cuda_enabled) no = "the CUDA backend is on and keeps the trunk";@@ -431,8 +424,7 @@ static void glmc_start(Model *m) {if (on && !no && g_pilot) no = "PILOT prefetch reads the residual on the host";if (on && !no && !(g_glmc_inited = vkc_init())) no = "the chain's pipelines did not come up";if (on && !no && !vkc_mla_ready()) no = "the MLA shaders are missing (chain_mla, chain_hgemv, chain_dsa)";- fprintf(stderr, "[VK] colibri: dense chain %s (%s%s%s)\n",- on && !no ? (on == COLI_VK_CHAIN_PREFILL ? "on for prompts" : "on") : "off", why, no ? "; " : "", no ? no : "");+ if (no) fprintf(stderr, "[VK] colibri: %s: the dense chain stays off\n", no);if (!on || no) return;if (!glmc_setup(m)) return;g_vk_chain = on;diff --git a/c/tests/vulkan_engines.sh b/c/tests/vulkan_engines.shindex 197017b7..850c5afd 100755--- a/c/tests/vulkan_engines.sh+++ b/c/tests/vulkan_engines.sh@@ -1346,8 +1346,10 @@ family_qwen_chain_sanitize() {# (colibri: the generated tokens, the teacher-forced predictions and the oracle's# mismatches; glm53: teacher_forcing and greedy), every logits row within 1e-4 of the# largest |logit| (tol 1; DUMP= writes them), and a "[VK] <engine> chain: N forwards"-# line with N > 0. LOST=1: COLI_VK_CHAIN_FAULT is set, and the run must say the device-# was lost (and, for glm53 with REBUILD=1, rebuild the KDA state of some positions).+# line with N > 0. FAULT_BACK=k: the device is lost k frames before the end of the same+# run without a fault (COLI_VK_CHAIN_FAULT counted from that run's frames, so the loss+# lands in the same forward on every device, whatever frames its setup took), and the+# run must say so (and, for glm53 with REBUILD=1, rebuild the KDA state of some positions).mla_toks() { # <engine> <log>if [ "$1" = colibri ]; then grep -aE '^GLM C engine|^PREFILL|^\[ORACLE\] mismatch' "$2" | sed 's/ | [0-9.]* pos\/s//'else grep -aE '^teacher_forcing|^greedy' "$2"; fi@@ -1357,12 +1359,20 @@ mla_gate() {local envs=(); while [ "$1" != "--" ]; do envs+=("$1"); shift; done; shiftrm -f chain.usage cpu.f32 vk.f32env "${envs[@]}" COLI_USAGE=chain.usage USAGE_SAVE=0 DUMP=cpu.f32 ./"$eng" "$@" > cpu.log 2>&1 || true+ if [ -n "${FAULT_BACK:-}" ]; then+ rm -f chain.usage+ env "${envs[@]}" COLI_USAGE=chain.usage USAGE_SAVE=0 COLI_VK_TIER_SYNC=1 COLI_VULKAN=1 COLI_VK_CHAIN=1 \+ ./"$eng" "$@" > vk.log 2>&1 || true+ local frames; frames=$(sed -n "s/^\[VK\] $eng chain: [0-9]* forwards, \([0-9]*\) frames.*/\1/p" vk.log | tail -1)+ [ -n "$frames" ] && [ "$frames" -gt "$FAULT_BACK" ] || { cat vk.log; fail "$tag: no fault-free run to count frames from"; }+ envs+=("COLI_VK_CHAIN_FAULT=$((frames - FAULT_BACK + 1))")+ firm -f chain.usageenv "${envs[@]}" COLI_USAGE=chain.usage USAGE_SAVE=0 DUMP=vk.f32 COLI_VK_TIER_SYNC=1 COLI_VULKAN=1 \COLI_VK_CHAIN=${CHAINMODE:-1} ./"$eng" "$@" > vk.log 2>&1 || truemla_toks "$eng" cpu.log > cpu.tok; mla_toks "$eng" vk.log > vk.tok{ [ -s cpu.tok ] && cmp -s cpu.tok vk.tok; } || { cat cpu.tok vk.tok; tail -20 vk.log; fail "$tag: the chain's tokens differ from the CPU"; }- if [ "${LOST:-0}" = 1 ]; then+ if [ -n "${FAULT_BACK:-}" ]; thengrep -q "$eng chain: the device was lost" vk.log || { cat vk.log; fail "$tag: no loss was handled"; }if [ "${REBUILD:-0}" = 1 ]; then grep -q "rebuilding the state of [1-9]" vk.log || { cat vk.log; fail "$tag: no state was rebuilt"; }; fielse@@ -1438,10 +1448,11 @@ family_glm_chain() {mla_gate colibri "chain colibri tiled GEMM from 2 rows" 1 SNAP=glm_tiny REF=ref_glm.json TF=1 COLI_VK_GEMM_MIN_S=2 -- 64 16 16mla_gate colibri "chain colibri the per-row GEMV" 1 SNAP=glm_tiny REF=ref_glm.json COLI_VK_CHAIN_GEMV=0 -- 64 16 16CHAINMODE=2 mla_gate colibri "chain colibri prompts only, drafts" 1 SNAP=glm_tiny REF=ref_glm.json DRAFT=3 -- 64 16 16- # the device lost: mid-decode, in the prompt, between MTP drafts- LOST=1 mla_gate colibri "chain colibri device lost mid-decode" 1 SNAP=glm_tiny REF=ref_glm.json COLI_VK_CHAIN_FAULT=30 -- 64 16 16- LOST=1 mla_gate colibri "chain colibri device lost in the prompt" 1 SNAP=glm_tiny REF=ref_glm.json COLI_VK_CHAIN_FAULT=3 -- 64 16 16- LOST=1 mla_gate colibri "chain colibri device lost with MTP" 1 SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json COLI_VK_CHAIN_FAULT=40 -- 64 16 16+ # the device lost: mid-decode, in the prompt (the teacher-forced pass is one forward),+ # between MTP drafts+ FAULT_BACK=12 mla_gate colibri "chain colibri device lost mid-decode" 1 SNAP=glm_tiny REF=ref_glm.json -- 64 16 16+ FAULT_BACK=3 mla_gate colibri "chain colibri device lost in the prompt" 1 SNAP=glm_tiny REF=ref_glm.json TF=1 -- 64 16 16+ FAULT_BACK=12 mla_gate colibri "chain colibri device lost with MTP" 1 SNAP=glm_tiny_mtp REF=glm_tiny_mtp/ref_glm.json -- 64 16 16# serve sessions frame for frame: pins, the prompt cache, the prefill read-out, two KV slotsCHAIN_SERVE_DIALECT=colibri $PY tests/vulkan_chain_serve.py ./colibri glm_tiny_serve SERVE_BATCH=1 IDOT=0CHAIN_SERVE_DIALECT=colibri $PY tests/vulkan_chain_serve.py ./colibri glm_tiny_serve SERVE_BATCH=1 IDOT=0 DSA_TOPK=4 DRAFT=3@@ -1463,8 +1474,8 @@ family_glm_chain() {mla_gate glm53 "chain glm53 swiglu_limit 0" 1 GLM53_BITS=32 -- --model glm53_lim0 --ids $ids --greedy 4grep -qa 'tier glm53: swiglu_limit is 0' vk.log || { cat vk.log; fail "chain glm53 swiglu_limit 0: the tier did not decline"; }CHAINMODE=2 mla_gate glm53 "chain glm53 prompts only" 1 GLM53_BITS=32 GLM53_PREFILL_CHUNK=16 -- --model glm53_stream-i4 --ids $ids --greedy 6- LOST=1 REBUILD=1 mla_gate glm53 "chain glm53 device lost, the KDA state rebuilt" 1 GLM53_BITS=32 GLM53_PREFILL_CHUNK=16 COLI_VK_CHAIN_FAULT=20 -- --model glm53_stream-i4 --ids $ids --greedy 6- LOST=1 REBUILD=1 mla_gate glm53 "chain glm53 device lost after an image" 1 GLM53_BITS=32 COLI_VK_CHAIN_FAULT=6 -- --model glm53_mm_tiny --ids 103,117,268,268,268,268,120,121 --patches glm53_mm_tiny/patches.f32 --grid 4x4 --greedy 4+ FAULT_BACK=7 REBUILD=1 mla_gate glm53 "chain glm53 device lost, the KDA state rebuilt" 1 GLM53_BITS=32 GLM53_PREFILL_CHUNK=16 -- --model glm53_stream-i4 --ids $ids --greedy 6+ FAULT_BACK=5 REBUILD=1 mla_gate glm53 "chain glm53 device lost after an image" 1 GLM53_BITS=32 -- --model glm53_mm_tiny --ids 103,117,268,268,268,268,120,121 --patches glm53_mm_tiny/patches.f32 --grid 4x4 --greedy 4CHAIN_SERVE_DIALECT=numeric $PY tests/vulkan_chain_serve.py ./glm53 glm53_serve GLM53_BITS=32CHAIN_SERVE_DIALECT=numeric $PY tests/vulkan_chain_serve.py ./glm53 glm53_serve GLM53_BITS=4 COLI_VK_CHAIN_ROWS=3CHAIN_SERVE_SLOTS=2 CHAIN_SERVE_DIALECT=numeric $PY tests/vulkan_chain_serve.py ./glm53 glm53_serve GLM53_BITS=32 KV_SLOTS=2diff --git a/docs/vulkan.md b/docs/vulkan.mdindex 0c1b60b4..8dda85a9 100644--- a/docs/vulkan.md+++ b/docs/vulkan.md@@ -877,10 +877,15 @@ configuration gives the CPU's tokens, and every logits row is within 2e-6 of thelogit (Lavapipe: 1.9e-6 at worst, colibri's int4-g64 experts on the tier, 6.3e-7 andbelow everywhere else).-**The default** is off on an integrated GPU (`COLI_VK_CHAIN=1` turns it on, `2` for-prompts only) and on a discrete GPU follows the rule above. No GLM checkpoint was run:-the 780M box has none, and both models are hundreds of GB. Speed is not measured; the-tests prove the tokens on the tiny fixtures.+**The default**: both engines pass `COLI_VK_CHAIN_UNMEASURED`, so the chain is off on+an integrated GPU (`COLI_VK_CHAIN=1` turns it on, `2` for prompts only) and on a+discrete GPU follows the rule above. No GLM checkpoint was run: the 780M box has none,+and both models are hundreds of GB. Speed is not measured; the tests prove the tokens on+the tiny fixtures, on Lavapipe and on the 780M:++```+[VK] colibri: dense chain off (an integrated GPU with the expert tier: not measured; COLI_VK_CHAIN=0 off, 1 on, 2 prompts only)+```### Adding an engine to the chain