Skip to content

Run frontier MoE models on hardware you already own β€” pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

Merge branch 'JustVugg:main' into feat/degrade-zero-miss-policy

Kritika Garg14b85bd29132
diff --cc c/colibri.c
index 31817b7b,1e3748ce..6f493dd0
--- a/c/colibri.c
+++ b/c/colibri.c
@@@ -4256,80 -4881,15 +4887,87 @@@ static void moe(Model *m, Layer *l, in
nu=nu2;
free(wsum); free(is_hit); free(keep);
}
+ /* ---- DEGRADE_ZERO: zero-fill miss slots below the gate-weight threshold --------
+ * When a prefetch deadline is missed, blocking on a demand-load stalls the compute
+ * thread. For experts whose per-position gate weight is below DEGRADE_TAU the
+ * contribution is small enough that zeroing the slot costs less in output quality
+ * than the I/O stall costs in latency (issue #865: tau=0.03 zeroes 21.8% of slots
+ * for +2.9% perplexity). tau is compared per-position (post-norm_topk,
+ * pre-routed_scale) β€” each position independently, matching the measured numbers.
+ * No renorm: the approximation IS the dropped mass; renorm would hide it and bias
+ * the output upward.
+ * Opt-in only (DEGRADE_ZERO=1); decode-only (S<=4) for the same reason as
+ * EXPERT_BUDGET: during prefill every dropped expert corrupts the KV cache. */
+ if(g_degrade_zero && S<=4){
+ /* residency scan: hits are always kept regardless of weight */
+ unsigned char *dg_keep=xzalloc((size_t)nu,"moe dg_keep");
+ for(int j=0;j<nu;j++){
+ int eid=uniq[j], resident=0;
+ ESlot *P=m->pin[layer];
+ for(int z=0;z<m->npin[layer];z++) if(P[z].eid==eid){ resident=1; break; }
+ if(!resident){ ESlot *Sl=m->ecache[layer]; int nn=m->ecn[layer];
+ for(int z=0;z<nn;z++) if(Sl[z].eid==eid){ resident=1; break; } }
+ if(resident) dg_keep[j]=1;
+ }
+ /* per-position tau gate: a miss expert is kept if ANY position routes to it
+ * with weight >= tau. Each position's weight is tested independently β€” this
+ * is the gate the +2.9% ppl measurement was taken under. */
+ for(int s=0;s<S;s++) for(int kk=0;kk<keff[s];kk++){
+ float wv=ws[(int64_t)s*K+kk];
+ if(wv>=g_degrade_tau){
+ int e=idxs[(int64_t)s*K+kk];
+ for(int j=0;j<nu;j++) if(uniq[j]==e){ dg_keep[j]=1; break; }
+ }
+ }
+ /* rescue: no position may end up with zero routed experts.
+ * If all of a position's experts were misses below tau, reinstate the
+ * highest-gate-weight one β€” same guard as EXPERT_BUDGET (#292). */
+ memset(seen,0,(size_t)E);
+ for(int j=0;j<nu;j++) if(dg_keep[j]) seen[uniq[j]]=1;
+ for(int s=0;s<S;s++){
+ int alive=0;
+ for(int kk=0;kk<keff[s] && !alive;kk++) if(seen[idxs[(int64_t)s*K+kk]]) alive=1;
+ if(alive || keff[s]<=0) continue;
+ int be=-1; float bw=-1e30f;
+ for(int kk=0;kk<keff[s];kk++){
+ float wv=ws[(int64_t)s*K+kk];
+ if(wv>bw){ bw=wv; be=idxs[(int64_t)s*K+kk]; }
+ }
+ if(be<0) be=idxs[(int64_t)s*K];
+ seen[be]=1;
+ for(int j=0;j<nu;j++) if(uniq[j]==be && !dg_keep[j]){ dg_keep[j]=1; break; }
+ }
+ /* apply: compact routing lists, no renorm β€” survivors keep original weights */
+ int dg_dropped=0;
+ for(int j=0;j<nu;j++) if(!dg_keep[j]) dg_dropped++;
+ if(dg_dropped){
+ g_degrade_dropped+=dg_dropped;
+ memset(seen,0,(size_t)E);
+ for(int j=0;j<nu;j++) if(dg_keep[j]) seen[uniq[j]]=1;
+ for(int s=0;s<S;s++){
+ int w=0;
+ for(int kk=0;kk<keff[s];kk++){
+ int e=idxs[(int64_t)s*K+kk]; float wv=ws[(int64_t)s*K+kk];
+ if(seen[e]){ idxs[(int64_t)s*K+w]=e; ws[(int64_t)s*K+w]=wv; w++; }
+ }
+ if(w<keff[s]) keff[s]=w;
+ }
+ /* compact uniq[] to kept experts only */
+ int nu2=0;
+ for(int j=0;j<nu;j++) if(dg_keep[j]) uniq[nu2++]=uniq[j];
+ nu=nu2;
+ }
+ free(dg_keep);
+ }
/* ---- FASE C/D: risolvi (pin/cache/disco) e calcola, a blocchi di 64 unici ---- */
float *xg=falloc((int64_t)S*D), *gg=falloc((int64_t)S*I), *uu=falloc((int64_t)S*I), *hh=falloc((int64_t)S*D);
+ /* quantizzazione sollevata (g_pq): materializzata al PRIMO expert che prenderebbe
+ * l'IDOT β€” mai per fmt 0/3/4/5/6/8, mai sui percorsi GPU, costo zero se inutile.
+ * EN: hoisted activation quantization, materialized on the FIRST IDOT-eligible
+ * expert; zero cost when no expert would take the IDOT branch. */
+ int8_t *xq_all=NULL, *xqg=NULL; float *sx_all=NULL, *sxg=NULL; int pq_ready=0;
+ int32_t *xsum_all=NULL, *xsumg=NULL; /* K1: termine -8*sum(x) per il dot planare */
+ g_pq.x=NULL; /* mai fidarsi di un contesto di un moe() precedente / never trust a stale context */
float *xe=NULL; /* fmt=6: x under the rotation Q^T, built once per call β€” all routed
* experts of the layer share it (the placement rule in quant.h) */
/* Materialise xe on first use. Every site that feeds a routed expert's gate/up