Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(qwen38): fit before the chain's pipelines, coli plan's numbers in the family
JustVugg42f9d86112a6
diff --git a/c/qwen38.c b/c/qwen38.cindex 9ff6ac3c..438167bb 100644--- a/c/qwen38.c+++ b/c/qwen38.c@@ -2142,8 +2142,8 @@ int main(int argc, char **argv) {* on an integrated GPU (docs/vulkan.md, "The dense chain") */if(g_vk_ready&&!qt_ready())g_vk_chain=coli_vk_chain_decide("qwen38",vkt_wanted()&&m.c.experts>0,COLI_VK_CHAIN_OFF);+ q38c_start(&m); /* the layers that fit the device, before anything of the chain goes up (a partial chain placed now; qwen38_chain.h) */if(g_vk_chain&&!vkc_init())g_vk_chain=0;- q38c_start(&m); /* the layers that fit the device (a partial chain placed now; qwen38_chain.h) */q38_dho_start(&m); /* COLI_VK_DENSE_HOST: the trunk on the device only, before the tier sizes its budget */#endifif(is_ref)ref_logits=read_reference_logits(ref_root,m.c.vocab);diff --git a/c/qwen38_chain.h b/c/qwen38_chain.hindex e3eb1a74..188c2b9e 100644--- a/c/qwen38_chain.h+++ b/c/qwen38_chain.h@@ -488,10 +488,11 @@ static Q38Chain *q38c_setup(Model *m) {}static int q38c_layers(Model *m) { Q38Chain *ch = (Q38Chain *)m->vkchain; return ch && ch->ok ? ch->nl : 0; }-/* At startup, with the chain on (before any upload, the dense-host pass and the expert- * tier): N, and a partial chain placed now. N = 0: the chain off, its device memory given- * back, nothing more going up (every byte left to the tier). A model the chain declines- * keeps today's path (the decline at its first forward). */+/* At startup, with the chain on (before anything of it is on the device, the dense-host+ * pass and the expert tier): N, the chain's pipelines (vkc_init), and a partial chain+ * placed now. N = 0: the chain off, nothing of it on the device, nothing more going up+ * (every byte left to the tier). A model the chain declines keeps today's path (the+ * caller's vkc_init, the decline at its first forward). */static void q38c_start(Model *m) {if (!g_vk_chain || qt_ready()) return;Cfg *c = &m->c; int L = c->layers;@@ -503,10 +504,12 @@ static void q38c_start(Model *m) {vkc_fit("qwen38", L, lb, mb, q38c_fit_fixed(m, rows), q38c_fit_tail(m), &g_q38c_fit);free(lb); free(mb);g_q38c_fitted = 1;- if (!vkc_fit_partial(&g_q38c_fit)) return; /* every layer and the head: set up at the first forward, as before */- Q38Chain *ch = g_q38c_fit.n ? q38c_setup(m) : NULL;- g_q38_vk_noup = 1;- if (!ch) { vkc_shutdown(); g_vk_chain = 0; }+ int partial = vkc_fit_partial(&g_q38c_fit);+ if (!g_q38c_fit.n) { g_vk_chain = 0; g_q38_vk_noup = 1; return; } /* the chain off: its pipelines never come up */+ if (!vkc_init()) { g_vk_chain = 0; g_q38c_fitted = 0; return; } /* the per-matrix path, as before */+ if (!partial) return; /* every layer and the head: set up at the first forward, as before */+ if (!q38c_setup(m)) { vkc_shutdown(); g_vk_chain = 0; }+ g_q38_vk_noup = 1; /* after the setup's own uploads */}/* The dense-host pass's bound (q38_dho_start): the N layers' matrices of a partial chain,* the head's and the MTP head's only with the tail on the device; with N = 0 the densediff --git a/c/tests/vulkan_partial_qwen38.sh b/c/tests/vulkan_partial_qwen38.shindex d6a7032e..9bf0b32c 100644--- a/c/tests/vulkan_partial_qwen38.sh+++ b/c/tests/vulkan_partial_qwen38.sh@@ -18,7 +18,9 @@# pins, the KV split, prompts only, and the device lost mid-decode and in a verify;# - COLI_VK_DENSE_HOST=0 with N < L: only the N layers' host copies dropped, none read# back on a healthy run, and after a lost device only theirs;-# - nothing forced on a device where everything fits: N = L with the head, as before.+# - nothing forced on a device where everything fits: N = L with the head, as before, and+# the same logits as with COLI_VK_CHAIN_LAYERS=4; coli plan's N, per-layer, fixed and+# tail bytes the engine's (under the caps above).# The CPU's tokens everywhere, logits within 1e-4 of the largest (bf16 trunk).# ptl_q38_res <log>: the matrices the engine holds on the device at exit (the report line)@@ -74,18 +76,48 @@ ptl_q38_run() {}# ptl_q38_cap <tag> <k> <env...> -- <argv...>: COLI_VK_DEVICE_CAP_MB from a probe's fit# line under which exactly k layers fit; the run's fit gives k by the rule, the final N is-# k, and the CPU's tokens. The probe runs under a 256 MiB cap: up to there the pools take-# 64 KiB blocks, so the probe's fixed bytes (the pools' granularity among them) are the-# run's; a larger probe cap takes larger blocks, and on these layers of tens of KiB that+# k, the CPU's tokens (logits within 1e-4 of the largest unless PTL_Q38_TOL=0), and coli+# plan's prediction the same as the engine's. The probe runs under a 256 MiB cap: up to+# there the pools take 64 KiB blocks, so what the probe held at the fit is what the run+# holds; a larger probe cap takes larger blocks, and on these layers of tens of KiB that# difference alone decides N.ptl_q38_cap() {local tag=$1 k=$2; shift 2local envs=(); while [ "$1" != "--" ]; do envs+=("$1"); shift; done; shiftlocal cap; cap=$(PTL_PROBE_CAP_MB=256 ptl_calc cap qwen38 probe.log "$k") || { grep -a '^\[VK\] qwen38 chain' probe.log; fail "$tag: no cap"; }- ptl_q38_gate "$tag" "$k" 1 "${envs[@]}" COLI_VK_DEVICE_CAP_MB="$cap" -- "$@"+ ptl_q38_gate "$tag" "$k" "${PTL_Q38_TOL:-1}" "${envs[@]}" COLI_VK_DEVICE_CAP_MB="$cap" -- "$@"local p; p=$(ptl_calc predict qwen38 vk.log)[ "$p" = "$k" ] || { grep -a '^\[VK\] qwen38 chain' vk.log; fail "$tag: the fit line gives $p layers, not $k"; }echo " $tag: COLI_VK_DEVICE_CAP_MB=$cap, the fit's rule gives $p"+ local snap; snap=$(printf '%s\n' "${envs[@]}" | sed -n 's/^SNAP=//p' | tail -1)+ ptl_q38_plan "$tag" "$snap" vk.log "${envs[@]}" COLI_VK_DEVICE_CAP_MB="$cap"+}+# ptl_q38_plan <tag> <fixture> <log> <env...>: coli plan's prediction (resource_plan.py,+# vk_chain_fit with qwen38's layout) for the same device and settings: the engine's free+# bytes, per-layer bytes, fixed bytes, tail and N, from the checkpoint's header and config+ptl_q38_plan() {+ local tag=$1 fx=$2 log=$3; shift 3+ $PY - "$fx" "$log" "$@" <<'PY' || fail "$tag: coli plan predicts otherwise"+import re, sys+sys.path.insert(0, ".")+import resource_plan as rp+fx, log = sys.argv[1], sys.argv[2]+env = dict(a.split("=", 1) for a in sys.argv[3:] if "=" in a)+env.update(COLI_VULKAN="1", COLI_VK_CHAIN="1")+lines = open(log, errors="replace").read().splitlines()+f = [l for l in lines if l.startswith("[VK] qwen38 chain fit: ")][-1]+num = lambda key: int(re.search(key + r" (\d+) B", f)[1])+layers = [int(x) for x in re.search(r"layers((?: \d+)*) B", f)[1].split()]+n = int(re.findall(r"\[VK\] qwen38 chain: (\d+) of \d+ layers on the device", "\n".join(lines))[0])+info = rp.analyze_model(fx)+fit = rp.vk_chain_fit(info, "qwen38", env, {"type": "cpu"})+tail = rp._VK_CHAIN_LAYOUT["qwen38"](info, env, {"type": "cpu"}).tail+ok = (fit["layers"], fit["fixed"], fit["free"], tail, fit["n"]) == (layers, num("fixed"), num("free"), num("tail"), n)+print(f" coli plan: N = {fit['n']} of {fit['L']} ({'as the engine' if ok else 'the engine: %d' % n}), "+ f"layers {'= the engine' if fit['layers'] == layers else fit['layers']}, fixed {fit['fixed']} B, "+ f"tail {tail} B, free {fit['free']} B")+sys.exit(0 if ok else 1)+PY}# ptl_q38_fault <tag> <k> <probe log> <env...> -- <argv...>: COLI_VK_STAGED_FAULT aimed# inside layer k's setup (the probe's marks); the run keeps the k layers before it.@@ -145,6 +177,10 @@ ptl_family_qwen38() {for k in 1 3 4; doptl_q38_cap "partial qwen38 a device holding $k layers" $k $R SNAP=qwen38_tiny -- 4 8 $Tdone+ # the int8 trunk and the MTP head's tail, in the engine's fit and in coli plan's+ ptl_q38_run probe.log $R COLI_VK_DEVICE_CAP_MB=256 Q38_TRUNK_MIN_KB=0 Q38_MTP=1 SNAP=qwen38_tiny_mtp -- 2 8 $M+ PTL_Q38_TOL=0 ptl_q38_cap "partial qwen38 int8 trunk, MTP, a device holding 2 layers" 2 $R Q38_TRUNK_MIN_KB=0 Q38_MTP=1 \+ SNAP=qwen38_tiny_mtp -- 2 8 $M# 3. an upload refused inside layer k's setup: on the full chain's first forward (k = 0# turns the chain off), at startup for a partial chain, and in the dense-host pass@@ -213,9 +249,14 @@ ptl_family_qwen38() {CHAIN_SERVE_EXPECT="$E" COLI_VK_DENSE_HOST=0 $PY tests/vulkan_chain_serve.py ./qwen38 qwen38_tiny_mtp Q38_MTP=1 $P# 6. nothing forced on a device where everything fits: every layer and the head, as before- ptl_q38_gate "partial qwen38 everything fits" 4 1 SNAP=qwen38_tiny -- 4 8 $T+ # (the tier's balance off in these two: their logits must be the same bytes)+ ptl_q38_gate "partial qwen38 everything fits" 4 1 COLI_VK_TIER_BALANCE=0 SNAP=qwen38_tiny -- 4 8 $Tgrep -qa '^\[VK\] qwen38 chain: 4 of 4 layers on the device ([^)]*), 0 on the CPU (free' vk.log ||{ grep -a '^\[VK\] qwen38 chain' vk.log; fail "partial qwen38 everything fits: not the full chain with its head"; }+ cp vk.f32 fits.f32+ ptl_q38_gate "partial qwen38 every layer forced" 4 1 COLI_VK_TIER_BALANCE=0 COLI_VK_CHAIN_LAYERS=4 SNAP=qwen38_tiny -- 4 8 $T+ cmp -s fits.f32 vk.f32 || fail "partial qwen38: the logits with COLI_VK_CHAIN_LAYERS=4 differ from the fit's full chain"+ rm -f fits.f32dho_gate qwen38 "partial qwen38 everything fits, dense-only" 1 SNAP=qwen38_tiny -- 4 8 $T[ "$(dho_dropped vk.log)" = 65 ] || { grep -a '^\[VK\] qwen38' vk.log; fail "partial qwen38 everything fits: $(dho_dropped vk.log) dropped, not 65"; }rm -f chain.usage chain-serve.usage probe.logdiff --git a/docs/vulkan.md b/docs/vulkan.mdindex bcb57c58..dc11950c 100644--- a/docs/vulkan.md+++ b/docs/vulkan.md@@ -888,8 +888,10 @@ streams come back to the host once per chunk, and the CPU runs layers N.. over arows, the final mixer and lm_head. The device's layers keep their DeltaNet, conv, K/V,index-key and pooled-key state on the device. A verify copies their state into the chain'sslots. The CPU's layers keep theirs on the host, and the CPU's copies roll them back. The PLE-ring and its n-gram history go with whichever side holds the PLE layer. The MTP head always-runs on the CPU and reads the final streams from whichever side ran the last layer. A lost+ring and its n-gram history go with whichever side holds the PLE layer. The MTP head runs+outside the chain and reads the final streams from whichever side ran the last layer: with a+partial chain its matrices stay on the CPU, with the full chain and the per-matrix path on+they answer from the device one at a time, as before. A lostdevice rebuilds only the device's layers from the prefix record. The vision tower's rows andthe n-gram table stay on the host as before. With `COLI_VK_DENSE_HOST=0` only the N layers'host copies go (with the full chain fitted, each layer once all of it is on the device). The@@ -2362,6 +2364,7 @@ predict a layer more than the engine places.| deepseek_v4 | the hc_mult streams; DSpark's taps of the last three layers come from whichever side ran them | yes || qwen36 (Qwen3.6, Qwen3-Coder, Qwen3.8-27B, Clef) | the residual rows alone; a prompt-lookup verify rolls each side back with its own copies (the device's DeltaNet slots, the CPU's snapshots) ([qwen36](#the-dense-chain-vk_chainc)) | yes || olmoe | the residual rows alone; `PILOT` keeps prefetching the next layers from the chain's rows ([olmoe](#olmoe-and-inkling)) | yes |+| qwen38 | the four hyper-connection streams of every row; the MTP head reads the final streams from whichever side ran the last layer, and the PLE ring and n-gram history stay with the PLE layer's side | yes |**`COLI_VK_DEVICE_CAP_MB=n`** (tests) makes the device hold at most n MiB of device-localmemory (a fraction is taken): every allocation of the backend and the chain (tensors, the