Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
qwen36: read the mixed expert layout (int4 gate/up, int8 down) on the CPU path
kreuzzelg87d4139198f9
diff --git a/c/qwen36.c b/c/qwen36.cindex d2b66137..70517240 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -660,6 +660,11 @@ typedef struct {/* Gated DeltaNet (linear_attention) dims, read from qwen36_meta.json. */int dn_vheads, dn_kheads, dn_kdim, dn_vdim, dn_convk, dn_conv_dim;int expert_gs; /* expert scale group size along input dim; 0 = per-row */+ /* Mixed expert layout (convert_qwen36.py --down-bits): gate/up stay int4+ * (ebits, expert_gs), down_proj is int8 with its own group size. One slab+ * per expert, [gate int4 packed | up int4 packed | down int8], 2*inter*hidden+ * bytes -- told apart from int4 (1.5x) and int8 (3x) by size, like today. */+ int expert_down_bits, expert_down_gs;} Cfg;/* ---------- per-layer dense weights ---------- */@@ -978,6 +983,8 @@ static void matmul_q_batch(float *y, const float *x, const int8_t *q,static int g_expert_gs = 0; /* set from qwen36_meta.json (expert_gs) at load */#include "gsgemv.h"+static int g_expert_mixed = 0; /* mixed layout on disk (int4 gate/up, int8 down) */+static int g_expert_down_gs = 0; /* down_proj's group size in the mixed layout (0 = per row) *//* 1 = expert container packs int4 (tier fmt=4); 0 = int8 per-row (tier fmt=1).* Same signal main's nbytes probe and tier_warmstart receive; the decode path* needs it to offer int8 experts (#1391): on an int8 container e->g4 is NULL. */@@ -1039,6 +1046,13 @@ static void matmul_qe(float *y, const float *x, const int8_t *q, const float *scif (g_expert_gs) matmul_q_gs(y, x, q, scale, I, O, g_expert_gs);else matmul_q(y, x, q, scale, I, O);}+/* down_proj: in the mixed layout it carries its own scale layout (int8, per+ * row or expert_down_gs), everywhere else it is matmul_qe. */+static void matmul_qd(float *y, const float *x, const int8_t *q, const float *scale, int I, int O) {+ if (!g_expert_mixed) { matmul_qe(y, x, q, scale, I, O); return; }+ if (g_expert_down_gs) matmul_q_gs(y, x, q, scale, I, O, g_expert_down_gs);+ else matmul_q(y, x, q, scale, I, O);+}/* ---- Dense int8: per-row quantized copies of the large f32 matrices.* matmul_d dispatches via pointer lookup to matmul_q; COLI_DENSE_I8=0 falls@@ -1374,6 +1388,7 @@ static void load_meta(Cfg *c, const char *snap) {G("q_head_dim", q_head_dim); G("k_head_dim", k_head_dim); G("v_head_dim", v_head_dim);G("o_in", o_in); G("rope_dim", rope_dim); G("qk_rope_head_dim", rope_dim);G("expert_gs", expert_gs);+ G("expert_down_bits", expert_down_bits); G("expert_down_gs", expert_down_gs);G("num_experts", n_experts); G("topk", topk);G("moe_inter", inter); G("shared_inter", shared_inter);G("n_group", n_group); G("topk_group", topk_group);@@ -1582,7 +1597,8 @@ static void model_init(Model *m, const char *snap, int cap, int bits) {/* scale counts per expert matrix: per-row (gs=0) or grouped along input dim */static int64_t scale_count_gu(const Cfg *c){ return c->expert_gs ? (int64_t)c->inter * ((c->hidden + c->expert_gs - 1) / c->expert_gs) : c->inter; }-static int64_t scale_count_d (const Cfg *c){ return c->expert_gs ? (int64_t)c->hidden * ((c->inter + c->expert_gs - 1) / c->expert_gs) : c->hidden; }+static int down_gs_of(const Cfg *c){ return c->expert_down_bits ? c->expert_down_gs : c->expert_gs; }+static int64_t scale_count_d (const Cfg *c){ int gs = down_gs_of(c); return gs ? (int64_t)c->hidden * ((c->inter + gs - 1) / gs) : c->hidden; }static void slot_ensure_allocated(Model *m, Slot *s) {if (s->g || s->pw) return;@@ -1680,9 +1696,10 @@ static void load_expert_merged(Model *m, int layer, int eid, Slot *s) {int64_t want_w = ng + ng + nd;int64_t want_s = 2*scale_count_gu(cc) + scale_count_d(cc);st_tensor *tw = st_find(&m->S, nm), *ts = st_find(&m->S, qsnm);- if (!tw || (tw->nbytes != want_w && tw->nbytes != want_w / 2)) {- fprintf(stderr, "%s: expert weight is %lld bytes β expected %lld (int8) or %lld (int4)\n",- nm, (long long)(tw ? tw->nbytes : -1), (long long)want_w, (long long)(want_w / 2)); exit(1); }+ int64_t want_mixed = ng + nd; /* gate|up packed int4 (ng bytes) + down int8 (nd bytes) */+ if (!tw || (tw->nbytes != want_w && tw->nbytes != want_w / 2 && tw->nbytes != want_mixed)) {+ fprintf(stderr, "%s: expert weight is %lld bytes β expected %lld (int8), %lld (int4) or %lld (int4 gate/up + int8 down)\n",+ nm, (long long)(tw ? tw->nbytes : -1), (long long)want_w, (long long)(want_w / 2), (long long)want_mixed); exit(1); }if (!ts || ts->numel != want_s) {fprintf(stderr, "%s: scale array is %lld elems β expected %lld (refusing)\n",qsnm, (long long)(ts ? ts->numel : -1), (long long)want_s); exit(1); }@@ -1692,6 +1709,23 @@ static void load_expert_merged(Model *m, int layer, int eid, Slot *s) {rest of the MoE path (matmul_q) is unchanged. Nibble convention (must matchc/tools/convert_qwen36.py pack_int4): LOW nibble = element 2k, HIGH nibble = 2k+1;each nibble is signed 4-bit (sign-extend if bit3 set). */+ if (tw->nbytes == want_mixed) {+ /* mixed layout: unpack gate|up (2*ng int4 elements in ng bytes) into the+ * slot's g|u block, copy down's int8 rows behind them. No packed copy is+ * kept: the tier does not take this layout yet (main refuses it). */+ static int noted_m = 0;+ if (!noted_m) { fprintf(stderr, "[qwen36] mixed expert layout detected (int4 gate/up, int8 down) β unpacking gate/up to int8 in slot\n"); noted_m = 1; }+ uint8_t *raw = (uint8_t *)malloc((size_t)want_mixed);+ if (!raw) { fprintf(stderr, "OOM reading mixed expert %s\n", nm); exit(1); }+ st_read_raw(&m->S, nm, raw, 1);+ unpack_int4_to_int8(s->g, raw, ng + ng); /* 2*ng elements from ng bytes */+ memcpy(s->d, raw + ng, (size_t)nd);+ free(raw);+ s->is_int4 = 0;+ free(s->g4); free(s->u4); free(s->d4); s->g4 = s->u4 = s->d4 = NULL;+ st_read_f32(&m->S, qsnm, s->gs, 0);+ return;+ }if (tw->nbytes == want_w / 2) {static int noted = 0;if (!noted) { fprintf(stderr, "[qwen36] int4 packed weights detected β %s\n", s->pw ? "kept int4, repacked planar for expert_ffn.h" : "unpacking to int8 in slot"); noted = 1; }@@ -2362,7 +2396,7 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {matmul_qe(g, xs, e->g, e->gs, D, I);matmul_qe(u, xs, e->u, e->us, D, I);for (int i = 0; i < I; i++) { float gv = g[i]; g[i] = (gv / (1.f + expf(-gv))) * u[i]; }- matmul_qe(hh, g, e->d, e->ds, I, D);+ matmul_qd(hh, g, e->d, e->ds, I, D);float w = val[kk]; float *os = out + (int64_t)s*D;for (int d = 0; d < D; d++) os[d] += w * hh[d];}@@ -2401,7 +2435,7 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {matmul_qe(g, xs, e->g, e->gs, D, I);matmul_qe(u, xs, e->u, e->us, D, I);for (int i = 0; i < I; i++) { float gv = g[i]; g[i] = (gv / (1.f + expf(-gv))) * u[i]; }- matmul_qe(hh, g, e->d, e->ds, I, D);+ matmul_qd(hh, g, e->d, e->ds, I, D);float w = val[kk];float *os = out + (int64_t)s*D;for (int d = 0; d < D; d++) os[d] += w * hh[d];@@ -3809,7 +3843,7 @@ int main(int argc, char **argv) {* riservare qualunque budget: e' int4 impacchettato che va in VRAM come* fmt=4, int8 come fmt=1. Sbagliare qui era #1331 -- budget riservato,* planned=1, e zero promozioni per tutta la vita del processo. */- int expert_is_int4 = 1;+ int expert_is_int4 = 1, expert_mixed = 0;{char probe[256];snprintf(probe, sizeof(probe),@@ -3817,12 +3851,18 @@ int main(int argc, char **argv) {st_tensor *pt = st_find(&m.S, probe);int64_t want = 2*(int64_t)m.c.inter*m.c.hidden + (int64_t)m.c.hidden*m.c.inter;if (pt && pt->nbytes == want) expert_is_int4 = 0; /* int8: un byte per elemento */+ /* mixed (convert_qwen36.py --down-bits): int4 gate/up + int8 down = 2/3 of int8 */+ if (pt && pt->nbytes == want * 2 / 3) { expert_is_int4 = 0; expert_mixed = 1; }}/* Una riga, sempre: e' l'unico modo di verificare il probe dall'esterno* (CI sul container tiny int8, #1331) senza una scheda. */fprintf(stderr, "[qwen36] expert format on disk: %s\n",- expert_is_int4 ? "int4 packed (tier fmt=4)" : "int8 (tier fmt=1)");+ expert_mixed ? "int4 gate/up + int8 down (mixed; CPU path, no VRAM tier yet)"+ : expert_is_int4 ? "int4 packed (tier fmt=4)" : "int8 (tier fmt=1)");g_expert_is_int4 = expert_is_int4;+ g_expert_mixed = expert_mixed; g_expert_down_gs = expert_mixed ? m.c.expert_down_gs : 0;+ if (expert_mixed && m.c.expert_down_bits == 0)+ fprintf(stderr, "[qwen36] mixed layout on disk but qwen36_meta.json has no expert_down_bits -- down scales assumed per row\n");/* Offer the dense trunk to the placer before the tier decides its budget:* sizes only, from the same dense-i8 entries the uploads below will use.* No entry (dense-i8 off) means nothing to offer, and the CPU path stands. */@@ -3841,7 +3881,10 @@ int main(int argc, char **argv) {}trunk_offer_dense(&m); /* dnout, attnproj, shexp: the rest of the per-token dense work */}- if (qt_init(m.c.n_layers, m.c.n_experts, m.c.hidden, m.c.inter, cap, m.c.topk,+ if (expert_mixed && getenv("COLI_CUDA") && getenv("COLI_CUDA")[0] == '1')+ fprintf(stderr, "[qwen36] COLI_CUDA=1 ignored: the VRAM expert tier does not take the mixed layout yet (one format per expert)\n");+ if (!expert_mixed &&+ qt_init(m.c.n_layers, m.c.n_experts, m.c.hidden, m.c.inter, cap, m.c.topk,m.c.expert_gs, expert_is_int4)) {fprintf(stderr, "[gpu] MoE experts -> CUDA VRAM tier\n");atexit(qt_shutdown);