Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
R4 prototype: lm_head as resident int8 GPU tensor (COLI_LMHEAD_GPU)
kreuzzelg868852a9a636
diff --git a/c/qwen36.c b/c/qwen36.cindex 937166b4..04eedf18 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -2244,7 +2244,8 @@ static float *step(Model *m, const int *ids, int S, int pos_base) {rmsnorm_row(last, x + (int64_t)(S-1)*D, m->final_norm, D, c->eps);float *logit = falloc(c->vocab);double _th = tm_on() ? tm_now() : 0.0;- matmul_d(logit, last, m->lm_head, 1, D, c->vocab);+ if (!qt_lmhead_matmul(logit, last, D, c->vocab))+ matmul_d(logit, last, m->lm_head, 1, D, c->vocab);if (tm_on()) { tm_add(S, 5, tm_now()-_th); if (S==1) g_tm_dec_tokens++; else g_tm_pre_tokens += S; }free(x); free(last);if (lf) fclose(lf);@@ -2854,6 +2855,14 @@ 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);+ /* 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. */+ for (int i = 0; i < g_qdw_n; i++)+ if (g_qdw[i].w == m.lm_head) {+ qt_lmhead_init(g_qdw[i].q, g_qdw[i].sc, g_qdw[i].I, g_qdw[i].O);+ break;+ }/* Warmstart: fill the VRAM budget BEFORE the first token (heat order* when HEAT_FILE exists, natural order otherwise), loading all RAM* slots along the way. */diff --git a/c/qwen36_tier.c b/c/qwen36_tier.cindex c40d1744..810f474f 100644--- a/c/qwen36_tier.c+++ b/c/qwen36_tier.c@@ -146,6 +146,13 @@ static void *uploader(void *arg){}}+/* R4 role split: lm_head as a resident int8 tensor on its own device.+ * The dense-i8 quantization (engine-side) provides q/sc with the same+ * per-row semantics quant_matmul's fmt=1 applies (y[o] = acc * sc[o]),+ * so CPU and GPU compute the same numbers up to accumulation order. */+static struct { ColiCudaTensor *t; int dev, dev_ok, on; } G_lmh;++int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,int expert_is_int4){const char *e=getenv("COLI_CUDA");@@ -176,6 +183,32 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,if(have<G.ndev){ G.ndev=have; }if(G.ndev<1){ fprintf(stderr,"[qtier] no CUDA devices -> CPU path\n"); return 0; }+ /* R4 role split: the lm_head device (COLI_LMHEAD_GPU) is initialized above+ * but removed from the expert PLACEMENT list. Zeroing its budget instead+ * is not enough: home() still hashes experts onto it, and those can never+ * be placed β measured as a hit-rate collapse 89.9% -> 49.5% (only the+ * half of the hot set homed to the remaining device got resident). Its+ * take() would also pace every layer (Quadro: 12.8 ms/token vs 3070 2.4),+ * while lm_head is one latency-tolerant call per token. If it is the ONLY+ * device, experts stay on it β a role split needs two cards. */+ {+ const char *lhx=getenv("COLI_LMHEAD_GPU");+ if(lhx && *lhx){+ int ld=atoi(lhx), w=0, present=0;+ for(int i=0;i<G.ndev;i++){ if(G.dev[i]==ld) present=1; else G.dev[w++]=G.dev[i]; }+ if(present){+ /* remember: the device HAS a CUDA context even after it leaves+ * the placement list β qt_lmhead_init keys on this, not on the+ * compacted list */+ G_lmh.dev=ld; G_lmh.dev_ok=1;+ if(w>0){+ G.ndev=w;+ fprintf(stderr,"[qtier] dev %d reserved for lm_head: excluded from expert placement\n",ld);+ } /* w==0: lm_head device is the only one β experts stay on it */+ } else fprintf(stderr,"[qtier] COLI_LMHEAD_GPU=%d not in COLI_GPUS -> lm_head stays on CPU\n",ld);+ }+ }+/* per-device budget: CUDA_EXPERT_GB, or auto = free minus 1 GB headroom.* Scale counts follow the container: per-row (expert_gs=0) or grouped* (gs64: [O, ceil(I/gs)] per projection). */@@ -241,6 +274,28 @@ int qt_init(int nl, int ne, int D, int Ih, int cap, int topk, int expert_gs,int qt_ready(void){ return G.on; }+int qt_lmhead_init(const int8_t *q, const float *sc, int I, int O){+ if(!G_lmh.dev_ok||!G.on||!q||!sc) return 0;+ int dev=G_lmh.dev;+ if(!coli_cuda_tensor_upload(&G_lmh.t,q,sc,1,I,O,dev)){+ fprintf(stderr,"[lmh] lm_head upload failed -> stays on CPU\n");+ return 0;+ }+ G_lmh.dev=dev; G_lmh.on=1;+ fprintf(stderr,"[lmh] lm_head [%d x %d] int8 resident on CUDA dev %d (%.2f GB)\n",+ O,I,dev,(double)O*I/1073741824.0);+ return 1;+}++int qt_lmhead_matmul(float *y, const float *x, int I, int O){+ if(!G_lmh.on) return 0;+ /* cached-tensor path: upload params are ignored once *t exists */+ if(coli_cuda_matmul(&G_lmh.t,y,x,NULL,NULL,1,1,I,O,G_lmh.dev,0)) return 1;+ fprintf(stderr,"[lmh] GPU matmul failed; falling back to CPU from here on\n");+ G_lmh.on=0;+ return 0;+}+/* Is (layer,eid) currently VRAM-resident? (used to free RAM-side int8 copies) */int qt_is_resident(int layer,int eid){if(!G.on) return 0;diff --git a/c/qwen36_tier.h b/c/qwen36_tier.hindex 0596eaba..b573147a 100644--- a/c/qwen36_tier.h+++ b/c/qwen36_tier.h@@ -29,6 +29,13 @@/* expert_is_int4: 1 = pesi int4 impacchettati (fmt=4), 0 = int8 (fmt=1). Il* chiamante lo determina dalla TAGLIA SU DISCO, non da meta.ebits, che su* qualche container mente (cfr. il rilevamento in qwen36.c). */+/* R4 role split: park the dense-i8 lm_head on its own CUDA device+ * (COLI_LMHEAD_GPU=<dev>). One GEMV per token, at token end β outside the+ * per-layer latency chain β so a slower second card can host it without+ * pacing the expert stream. qt_init places no experts on that device. */+int qt_lmhead_init(const int8_t *q, const float *sc, int I, int O);+int qt_lmhead_matmul(float *y, const float *x, int I, int O);+int qt_init(int n_layers, int n_experts, int hidden, int inter,int cap_experts_per_layer, int topk, int expert_gs,int expert_is_int4);@@ -68,6 +75,8 @@ void qt_stats(void);#else /* !COLI_CUDA: inline stubs, engine stays CPU-only */static inline int qt_init(int a,int b,int c,int d,int e,int f,int g,int h){(void)h;(void)a;(void)b;(void)c;(void)d;(void)e;(void)f;(void)g;return 0;}+static inline int qt_lmhead_init(const int8_t*a,const float*b,int c,int d){(void)a;(void)b;(void)c;(void)d;return 0;}+static inline int qt_lmhead_matmul(float*a,const float*b,int c,int d){(void)a;(void)b;(void)c;(void)d;return 0;}static inline int qt_ready(void){return 0;}static inline int qt_is_resident(int a,int b){(void)a;(void)b;return 0;}static inline void qt_shutdown(void){}