Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
expert_ffn: the layer's expert outputs apart from their rank-order sum
JustVugg1f479abed385
diff --git a/c/expert_ffn.h b/c/expert_ffn.hindex 65415844..ee5b4b2f 100644--- a/c/expert_ffn.h+++ b/c/expert_ffn.h@@ -329,9 +329,13 @@ static inline size_t xf_moe_scratch_bytes(int S, int K, int H, int F) {+ n * sizeof(int) * 4 + 4096;}-static inline void xf_moe_add(float *out, const float *x, int S, int K, int H, int F,- const int *idx, const float *val, const XfExpert *const *experts,- int mode, void *scratch) {+/* The per-(s,k) expert outputs alone: ctb[(s*K+k)*H ..] = expert_k(x[s]) for every+ * pair with an expert, nothing summed. ctb may be the caller's (an engine that adds+ * other outputs in the same rank order, vk_tier.h) or NULL: then it is carved from+ * scratch. Returns it, or NULL when no pair has an expert (nothing written). */+static inline float *xf_moe_compute(float *ctb_out, const float *x, int S, int K, int H, int F,+ const int *idx, const XfExpert *const *experts,+ int mode, void *scratch) {const size_t n = (size_t)S * K;char *p = (char *)scratch;#define XF_TAKE(T, count) ((T *)p); p += (((size_t)(count) * sizeof(T)) + 63) & ~(size_t)63@@ -350,6 +354,7 @@ static inline void xf_moe_add(float *out, const float *x, int S, int K, int H, iint *next = XF_TAKE(int, n); /* chain of pairs sharing an expert */int *cnt = XF_TAKE(int, n);#undef XF_TAKE+ if (ctb_out) ctb = ctb_out;/* activations once per token */if (mode) for (int s = 0; s < S; s++) xsx[s] = xf_act_i8(x + (size_t)s * H, H, xq + (size_t)s * H, xsum + (size_t)s * (H / XF_BLOCK));@@ -363,7 +368,7 @@ static inline void xf_moe_add(float *out, const float *x, int S, int K, int H, iif (j == nu) { uniq[nu] = idx[i]; head[nu] = i; cnt[nu] = 1; nu++; }else { int t = head[j]; while (next[t] >= 0) t = next[t]; next[t] = i; cnt[j]++; }}- if (nu == 0) return;+ if (nu == 0) return NULL;int T = 1;#ifdef _OPENMP@@ -403,6 +408,14 @@ static inline void xf_moe_add(float *out, const float *x, int S, int K, int H, ixf_down_rows(ctb + (size_t)i * H, experts[i], &a, F, r0, r1, mode);}}+ return ctb;+}++static inline void xf_moe_add(float *out, const float *x, int S, int K, int H, int F,+ const int *idx, const float *val, const XfExpert *const *experts,+ int mode, void *scratch) {+ float *ctb = xf_moe_compute(NULL, x, S, K, H, F, idx, experts, mode, scratch);+ if (!ctb) return;/* rank-order sum per token: out[s] += sum_k val[s][k] * ctb[s][k] */#pragma omp parallel for schedule(static)for (int s = 0; s < S; s++) {