Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
qwen36: measure the trunk placement at startup, withdraw it when the GPU loses
JustVugg0e84b37ca965
diff --git a/c/Makefile b/c/Makefileindex 96433f08..913205ea 100644--- a/c/Makefile+++ b/c/Makefile@@ -1859,6 +1859,10 @@ tests/test_qwen36_tier_fp8$(EXE): tests/test_qwen36_tier_fp8.c tests/qwen36_faketests/test_qwen36_tier_dense$(EXE): tests/test_qwen36_tier_dense.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)+# the automatic trunk placement can be withdrawn after the startup probe (fake backend).+tests/test_qwen36_tier_withdraw$(EXE): tests/test_qwen36_tier_withdraw.c tests/qwen36_fake_cuda.h qwen36_tier.c qwen36_tier.h backend_cuda.h+ $(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)+tests/test_qwen36_tier_int8_engine$(EXE): tests/test_qwen36_tier_int8_engine.c tests/qwen36_fake_cuda.h qwen36.c expert_ffn.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)diff --git a/c/qwen36.c b/c/qwen36.cindex 656cb346..94ced9e8 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -2486,6 +2486,57 @@ static int trunk_place_dense(Model *m, double *vram_bytes){return placed;}+/* Measured, not assumed. The placer prices a trunk component by the bytes it+ * saves on the CPU's memory bus, which presumes the GPU answers a GEMV faster+ * than the CPU does. Four Tesla M10 (sm_50) said otherwise: every placed+ * component ran slower there, lm_head 68.8 ms against 41.7 on the CPU, and+ * decode fell from 3.56 to 2.68 tok/s (#1652). So before any trunk upload,+ * one DeltaNet input projection (the most numerous placed matrix) is timed+ * both ways on the device that would host it, ten GEMVs each, best of three+ * rounds after a warm-up, and the trunk goes to VRAM only if the GPU wins.+ * Only the automatic placement is questioned: a hand-written COLI_PLACE+ * stands. COLI_TRUNK_PROBE=0 skips the probe and trusts the placer. The+ * probe's copy stays resident (one projection, ~25 MB on the 35B). */+static int trunk_probe_gpu_wins(Model *m){+ const char *e = getenv("COLI_TRUNK_PROBE");+ if (e && *e == '0') return 1;+ if (!qt_place_is_auto()) return 1;+ Cfg *c = &m->c;+ int qi = -1, dev = QT_PLACE_CPU;+ for (int i = 0; i < c->n_layers && qi < 0; i++) {+ if (c->is_attn[i]) continue;+ for (int j = 0; j < g_qdw_n; j++)+ if (g_qdw[j].w == m->L[i].dn_qkv) { qi = j; dev = qt_place_of("dnproj", i); break; }+ }+ if (qi < 0) return 1; /* dense-i8 off: nothing will be placed */+ if (dev == QT_PLACE_CPU) dev = qt_place_of("lmhead", 0);+ if (dev == QT_PLACE_CPU) return 1; /* nothing placed: nothing to measure */+ int I = g_qdw[qi].I, O = g_qdw[qi].O;+ int h = qt_dense_init(g_qdw[qi].q, g_qdw[qi].sc, I, O, dev);+ if (h < 0) return 1; /* cannot measure: the placer's word stands */+ float *x = malloc((size_t)I * sizeof(float)), *y = malloc((size_t)O * sizeof(float));+ if (!x || !y) { free(x); free(y); return 1; }+ for (int i = 0; i < I; i++) x[i] = sinf(0.37f * (float)i);+ double gpu = 1e30, cpu = 1e30;+ for (int r = 0; r < 3; r++) {+ for (int k = 0; k < 3; k++) if (!qt_dense_matmul(h, y, x, I, O)) { free(x); free(y); return 1; }+ double t0 = now_s();+ for (int k = 0; k < 10; k++) if (!qt_dense_matmul(h, y, x, I, O)) { free(x); free(y); return 1; }+ double tg = (now_s() - t0) / 10;+ for (int k = 0; k < 3; k++) matmul_q(y, x, g_qdw[qi].q, g_qdw[qi].sc, I, O);+ t0 = now_s();+ for (int k = 0; k < 10; k++) matmul_q(y, x, g_qdw[qi].q, g_qdw[qi].sc, I, O);+ double tc = (now_s() - t0) / 10;+ if (tg < gpu) gpu = tg;+ if (tc < cpu) cpu = tc;+ }+ free(x); free(y);+ int wins = gpu < cpu;+ fprintf(stderr, "[place] probe: one [%d x %d] int8 GEMV takes %.3f ms on CUDA dev %d, %.3f ms on the CPU -> trunk %s\n",+ O, I, gpu * 1e3, dev, cpu * 1e3, wins ? "to VRAM" : "stays on the CPU");+ return wins;+}+static void layers_forward_range(Model *m, float *x, int S, int pos_base,int layer_begin, int layer_end,int allow_prefetch, FILE *lf) {@@ -3594,6 +3645,8 @@ int main(int argc, char **argv) {m.c.expert_gs, expert_is_int4)) {fprintf(stderr, "[gpu] MoE experts -> CUDA VRAM tier\n");atexit(qt_shutdown);+ /* The placer predicted; measure before uploading a byte of trunk. */+ if (!trunk_probe_gpu_wins(&m)) qt_trunk_withdraw("measured slower than the CPU");/* R4 role split: park the dense-i8 lm_head on COLI_LMHEAD_GPU. The* qdw entry keyed by m.lm_head holds the int8 rows + per-row scales* the CPU path uses; the GPU applies the identical semantics. */diff --git a/c/qwen36_tier.c b/c/qwen36_tier.cindex 7322dae9..2b2e24e5 100644--- a/c/qwen36_tier.c+++ b/c/qwen36_tier.c@@ -408,6 +408,29 @@ static double auto_displaced_value(int di, size_t room, int k, size_t exp_bytes,return value;}+/* Whether the trunk placement is the automatic one (COLI_PLACE unset or+ * "auto"): the only decision the engine's startup probe may overturn. A+ * hand-written list, or "off", is the user's word and stands. */+int qt_place_is_auto(void){ return G_auto_on && auto_mode(); }++/* Undo the automatic trunk placement: every offer back to the CPU, lm_head and+ * the DeltaNet projections included, and the bytes it had taken back into each+ * device's expert budget. The engine calls this BEFORE any trunk upload, when+ * its startup probe measured the GPU GEMV slower than the CPU's: on four Tesla+ * M10 every placed component lost, lm_head 68.8 ms against 41.7 on the CPU,+ * decode 2.68 against 3.56 tok/s (#1652). Nothing to undo when the placement+ * was not automatic. */+void qt_trunk_withdraw(const char *why){+ if(!qt_place_is_auto()) return;+ size_t back = 0;+ for(int o = 0; o < G_offer_n; o++) G_offer[o].dev = QT_PLACE_CPU;+ G_auto_lmh = QT_PLACE_CPU; G_lmh.dev_ok = 0;+ for(int l = 0; l < QT_DN_MAX_LAYERS; l++) G_auto_dnp[l] = QT_PLACE_CPU;+ for(int i = 0; i < G.ndev; i++){ back += G_trunk_bytes[i]; G.budget[i] += G_trunk_bytes[i]; G_trunk_bytes[i] = 0; }+ fprintf(stderr,"[place] trunk stays on the CPU (%s): %.2f GB of VRAM back to the experts\n",+ why ? why : "withdrawn", back/1073741824.0);+}+static void auto_place(int nl, int ne, int topk, const size_t *capacity, const uint32_t *heat0){size_t room[QT_MAX_DEV];for(int i = 0; i < G.ndev; i++){ room[i] = capacity[i]; G_trunk_bytes[i] = 0; }diff --git a/c/qwen36_tier.h b/c/qwen36_tier.hindex 5caef9f6..228288d6 100644--- a/c/qwen36_tier.h+++ b/c/qwen36_tier.h@@ -65,6 +65,12 @@ int qt_place_of(const char *component, int layer);* bytes come out of that device's expert budget. Sizes only; the tensors* follow through qt_lmhead_init / qt_dnproj_init as before. */void qt_trunk_offer(const char *component, int layer, size_t bytes);+/* The automatic placement is a prediction; the engine measures it at startup+ * (one GEMV both ways, qwen36.c trunk_probe_gpu_wins) and withdraws the whole+ * trunk when the GPU loses, giving the bytes back to the expert budget. Only+ * the automatic placement can be withdrawn; a COLI_PLACE list stands. */+int qt_place_is_auto(void);+void qt_trunk_withdraw(const char *why);/* DeltaNet input projections, qkv ++ z fused into one resident tensor per* layer: one GEMV instead of two, and the engine's qkv/z buffers are laid out@@ -135,6 +141,8 @@ static inline int qt_lmhead_matmul(float*a,const float*b,int c,int d){(void)a;(#define QT_PLACE_CPU (-1)static inline int qt_place_of(const char*a,int b){(void)a;(void)b;return QT_PLACE_CPU;}static inline void qt_trunk_offer(const char*a,int b,size_t c){(void)a;(void)b;(void)c;}+static inline int qt_place_is_auto(void){return 0;}+static inline void qt_trunk_withdraw(const char*a){(void)a;}static inline int qt_dnproj_init(int a,const int8_t*b,const float*c,int d,int e,int f){(void)a;(void)b;(void)c;(void)d;(void)e;(void)f;return 0;}static inline int qt_dnproj_matmul(int a,float*b,const float*c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return 0;}static inline int qt_dense_init(const int8_t*a,const float*b,int c,int d,int e){(void)a;(void)b;(void)c;(void)d;(void)e;return -1;}diff --git a/c/tests/test_qwen36_tier_withdraw.c b/c/tests/test_qwen36_tier_withdraw.cnew file mode 100644index 00000000..54dd092b--- /dev/null+++ b/c/tests/test_qwen36_tier_withdraw.c@@ -0,0 +1,67 @@+/* The automatic trunk placement is a prediction the engine measures at startup+ * (#1652: on four Tesla M10 every placed component ran slower than the CPU).+ * When the probe says the GPU loses, qt_trunk_withdraw() must put every offer+ * back on the CPU, lm_head included, and return the bytes the trunk had taken+ * to the expert budget, so the warmstart that follows fills that VRAM with+ * experts instead. A hand-written COLI_PLACE is the user's word: not auto, not+ * withdrawn. Fake CUDA backend, no GPU, no toolkit. */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "../compat.h" /* setenv: MinGW has none */+#include "qwen36_fake_cuda.h"++#include "../qwen36_tier.c"++static int fails;+static void check(int ok, const char *what) { if (!ok) { printf(" FAIL: %s\n", what); fails++; } }++enum { NL = 2, NE = 8, D = 64, IH = 32, TOPK = 2 };++int main(void) {+ setenv("COLI_CUDA", "1", 1); setenv("COLI_GPUS", "0", 1);+ setenv("QT_NO_WARMSTART", "1", 1); setenv("HEAT_FILE", "", 1);+ setenv("COLI_PLACE", "", 1); /* "" == unset == auto */+ fake_ndev = 1; fake_uploads = 0;++ /* room for 8 experts plus a little; each offer is 1 expert worth of bytes */+ size_t exp_bytes = 3 * dev_alloc_footprint((size_t)D * IH / 2) + 3 * dev_alloc_footprint((2 * IH + D) / 3 * sizeof(float));+ size_t cap = 8 * exp_bytes + exp_bytes / 2;+ char gb[64]; snprintf(gb, sizeof gb, "%.15f", (double)cap / 1073741824.0);+ setenv("CUDA_EXPERT_GB", gb, 1);++ qt_trunk_offer("lmhead", 0, exp_bytes);+ qt_trunk_offer("dnproj", 0, exp_bytes); qt_trunk_offer("dnout", 0, exp_bytes);+ qt_trunk_offer("attnproj", 1, exp_bytes); qt_trunk_offer("shexp", 1, exp_bytes);+ check(qt_init(NL, NE, D, IH, NE, TOPK, 0, 1), "tier starts (int4 mode, cap == n_experts)");+ check(qt_place_is_auto(), "COLI_PLACE unset: the placement is automatic");+ check(qt_place_of("lmhead", 0) == 0 && qt_place_of("dnproj", 0) == 0 && qt_place_of("dnout", 0) == 0 &&+ qt_place_of("attnproj", 1) == 0 && qt_place_of("shexp", 1) == 0, "all five offers placed on the one device");+ check(G_lmh.dev_ok, "lm_head has a device before the withdrawal");+ size_t before = G.budget[0];+ check(before + 5 * exp_bytes <= cap && before + 5 * exp_bytes + exp_bytes > cap,+ "the expert budget is the allowance minus the five placed offers");++ qt_trunk_withdraw("test");+ check(qt_place_of("lmhead", 0) == QT_PLACE_CPU && qt_place_of("dnproj", 0) == QT_PLACE_CPU &&+ qt_place_of("dnout", 0) == QT_PLACE_CPU && qt_place_of("attnproj", 1) == QT_PLACE_CPU &&+ qt_place_of("shexp", 1) == QT_PLACE_CPU, "after the withdrawal every offer answers CPU");+ check(!G_lmh.dev_ok, "lm_head has no device any more: qt_lmhead_init will refuse");+ check(G.budget[0] == before + 5 * exp_bytes, "the trunk's bytes are back in the expert budget");+ check(G_trunk_bytes[0] == 0, "no trunk bytes are charged to the device");+ check(qt_lmhead_init((const int8_t *)"x", (const float *)"x", 1, 1) == 0, "a later lm_head upload is refused");+ check(fake_uploads == 0, "nothing was uploaded: the withdrawal came before any trunk upload");++ /* a hand-written list is not automatic: nothing to withdraw, nothing changes */+ setenv("COLI_PLACE", "lmhead=0", 1);+ check(!qt_place_is_auto(), "an explicit COLI_PLACE list is not automatic");+ size_t held = G.budget[0];+ qt_trunk_withdraw("test again");+ check(G.budget[0] == held, "withdrawing an explicit placement is a no-op");++ qt_shutdown();+ if (fails) { printf("test_qwen36_tier_withdraw: %d failure(s)\n", fails); return 1; }+ printf("OK test_qwen36_tier_withdraw: the automatic trunk placement can be withdrawn, budget restored\n");+ return 0;+}diff --git a/docs/qwen36-cuda-tier.md b/docs/qwen36-cuda-tier.mdindex 1bc894c9..edf04a5e 100644--- a/docs/qwen36-cuda-tier.md+++ b/docs/qwen36-cuda-tier.md@@ -81,8 +81,21 @@ on a CPU without AVX2; measured on a 16-core CPU, of the 37.5 ms a decodedtoken spends in the DeltaNet stack 23.4 are the input projections, 8.3 theout_proj and norm, 3.3 the convolution and 2.4 the recurrence -- the matmuls,not the recurrence, are what the trunk costs. They are served from VRAM on-decode (one GEMV each); a prompt batch keeps the batched CPU matmul. The-pricing rule:+decode (one GEMV each); a prompt batch keeps the batched CPU matmul.++**Measured, not assumed.** The pricing rule below presumes the GPU answers a+GEMV faster than the CPU does. Four Tesla M10 (sm_50, no tensor cores, four+GPUs on one PCIe board) said otherwise in #1652: with every expert+VRAM-resident, placing the trunk made every component slower (lm_head 68.8 ms+against 41.7 on the CPU, the 30 DeltaNet projections 106 against 66) and+decode fell from 3.56 to 2.68 tok/s. So the engine measures before it uploads+a byte of trunk: one DeltaNet input projection is timed both ways on the+device that would host it (ten GEMVs, best of three rounds, after a warm-up),+one `[place] probe:` line reports both times, and if the GPU loses the whole+automatic placement is withdrawn (`[place] trunk stays on the CPU`) and its+bytes go back to the expert budget before the warmstart. Only the automatic+placement is questioned: a hand-written `COLI_PLACE` stands, and+`COLI_TRUNK_PROBE=0` skips the probe. The pricing rule:- a dense component is read every token: 1.0 per byte;- a routed expert is read with the probability a token routes to it -- its heat