Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vk_tier: route weights for the device, vkt_wants, and DeepSeek V4's activation
JustVugg1b87832b5626
diff --git a/c/tests/test_vk_tier.c b/c/tests/test_vk_tier.cindex 37d7b6f8..0ec92f4d 100644--- a/c/tests/test_vk_tier.c+++ b/c/tests/test_vk_tier.c@@ -11,7 +11,9 @@* go to the device;* partial a step with more assignments than max_rows: the device takes* max_rows of them, the CPU the rest, the sum is still right;- * books device + CPU = routed, resident <= budget, no failed upload.+ * books device + CPU = routed, resident <= budget, no failed upload;+ * v4 DeepSeek V4's activation (route weight on the device, its bf16 and E4M3+ * roundings): device rows against the engine's CPU arithmetic.** make tests/test_vk_tier VK=1 && VK_ICD_FILENAMES=.../lvp_icd.json ./tests/test_vk_tier */#include <stdio.h>@@ -326,6 +328,116 @@ static void partial(void) {vkt_shutdown(); model_free();}+/* DeepSeek V4's activation (VKT_ACT_SWIGLU_V4) through the tier: MXFP4 ue8m0 experts,+ * x rounded to E4M3 per 128 here as the engine rounds it, the route weight applied on+ * the device (vkt_issue_w), each device row rounded to bf16 and added with no weight+ * of its own. Every row against the engine's CPU arithmetic written out here (bf16+ * gate and up, the clamped SwiGLU, weight, bf16, E4M3 per 128, down, bf16): the two+ * differ only where their summation orders put a value on the other side of one of+ * those roundings, so most rows come back bit-identical and the rest close. */+static float bf16r(float v) {+ uint32_t b; memcpy(&b, &v, 4);+ if ((b & 0x7f800000u) != 0x7f800000u) b += 0x7fffu + ((b >> 16) & 1u);+ b &= 0xffff0000u; memcpy(&v, &b, 4);+ return v;+}+static float e4m3r(float value) {+ int neg = signbit(value) != 0;+ float a = fabsf(value), r;+ if (!a) return value;+ if (a >= 448.0f) r = 448.0f;+ else if (a < 0.015625f) {+ float sc = a * 512.0f; unsigned q = (unsigned)sc; float fr = sc - (float)q;+ if (fr > 0.5f || (fr == 0.5f && (q & 1))) q++;+ r = ldexpf((float)q, -9);+ } else {+ int e; float m = frexpf(a, &e); /* a = m 2^e, m in [0.5, 1) */+ float q = nearbyintf(m * 16.0f); /* 1.mmm: 4 bits, ties to even */+ r = ldexpf(q, e - 4);+ }+ return neg ? -r : r;+}+static void qdq128(float *v, int n) {+ for (int b = 0; b < n; b += 128) {+ int c = n - b < 128 ? n - b : 128;+ float mx = 0;+ for (int i = 0; i < c; i++) mx = fmaxf(mx, fabsf(v[b + i]));+ mx = fmaxf(mx, 1e-4f);+ int ex; float fr = frexpf(mx / 448.0f, &ex);+ float sc = ldexpf(1.0f, fr == 0.5f ? ex - 1 : ex);+ for (int i = 0; i < c; i++) v[b + i] = e4m3r(fmaxf(-448.0f, fminf(448.0f, v[b + i] / sc))) * sc;+ }+}+static void expert_v4(const Ex *e, const float *xq, float w, float limit, float *y) {+ float h[F];+ for (int o = 0; o < F; o++) {+ double a = 0, b = 0;+ for (int i = 0; i < H; i++) { a += (double)e->g.w[(size_t)o * H + i] * xq[i]; b += (double)e->u.w[(size_t)o * H + i] * xq[i]; }+ float g = bf16r((float)a), u = bf16r((float)b);+ if (limit > 0) { g = fminf(g, limit); u = fmaxf(-limit, fminf(u, limit)); }+ float sg = g >= 0 ? 1.0f / (1.0f + expf(-g)) : expf(g) / (1.0f + expf(g));+ h[o] = bf16r(g * sg * u * w);+ }+ qdq128(h, F);+ for (int o = 0; o < H; o++) { double a = 0; for (int i = 0; i < F; i++) a += (double)e->d.w[(size_t)o * F + i] * h[i]; y[o] = bf16r((float)a); }+}+static void v4_act(void) {+ VktFmt f = {VKT_SRC_MXFP4_E8M0, 32};+ model_make(f, f);+ const float limit = 10.0f;+ set_budget(L * E, f, f);+ setenv("COLI_VK_TIER_RATE", "64", 1);+ VktConfig vc = cfg_of(f, f, VKT_ACT_SWIGLU_V4, limit);+ int on = vkt_init(&vc, NULL);+ CHECK(on, "v4: the tier did not start");+ if (!on) { model_free(); return; }+ unsigned long long dev = 0, rows_same = 0, routed = 0; double worst = 0, worst_sum = 0;+ static const int Ss[] = {1, 3, 20, 1, 2, 24};+ for (int t = 0; t < 6; t++)+ for (int l = 0; l < L; l++) {+ int S = Ss[t], idx[24 * K]; float w[24 * K];+ route(S, 0, E, idx, w);+ float *x = malloc(sizeof(float) * S * H), *xq = malloc(sizeof(float) * S * H);+ float *cpu = calloc((size_t)S * K * H, sizeof(float)), *y = malloc(sizeof(float) * H);+ float *out = calloc((size_t)S * H, sizeof(float)), *ref = calloc((size_t)S * H, sizeof(float));+ uint8_t taken[24 * K]; const float *rows[24 * K];+ for (int i = 0; i < S * H; i++) x[i] = frnd() * 4.0f;+ memcpy(xq, x, sizeof(float) * S * H);+ for (int s = 0; s < S; s++) qdq128(xq + (size_t)s * H, H); /* the engine's rounding of x */+ int n = vkt_issue_w(l, xq, S, K, idx, w, taken);+ for (int i = 0; i < S * K; i++) {+ if (taken[i]) continue;+ expert_v4(&ex[l][idx[i]], xq + (size_t)(i / K) * H, w[i], limit, cpu + (size_t)i * H);+ VktExpertSrc src = src_of(&ex[l][idx[i]]); vkt_note(l, idx[i], &src);+ }+ int joined = n ? vkt_join(rows) : 1;+ CHECK(joined, "v4: join failed");+ for (int s = 0; s < S; s++)+ for (int k = 0; k < K; k++) {+ int i = s * K + k;+ expert_v4(&ex[l][idx[i]], xq + (size_t)s * H, w[i], limit, y);+ for (int d = 0; d < H; d++) ref[(size_t)s * H + d] += y[d];+ if (taken[i] && joined) {+ float dr[H];+ for (int d = 0; d < H; d++) dr[d] = bf16r(rows[i][d]);+ double r = rel(dr, y, H); if (r > worst) worst = r;+ rows_same += !memcmp(dr, y, sizeof dr);+ for (int d = 0; d < H; d++) out[(size_t)s * H + d] += dr[d];+ dev++;+ } else for (int d = 0; d < H; d++) out[(size_t)s * H + d] += cpu[(size_t)i * H + d];+ routed++;+ }+ double r = rel(out, ref, S * H); if (r > worst_sum) worst_sum = r;+ free(x); free(xq); free(cpu); free(y); free(out); free(ref);+ }+ printf(" %-34s device %3llu of %3llu assignments, %llu rows bit-identical to the CPU's arithmetic, worst row %.2e, worst sum %.2e\n",+ "MXFP4 ue8m0, DeepSeek V4 roundings", dev, routed, rows_same, worst, worst_sum);+ CHECK(dev > 0, "v4: nothing ran on the device");+ CHECK(rows_same * 2 > dev, "v4: only %llu of %llu device rows equal the CPU's arithmetic", rows_same, dev);+ CHECK(worst < 2e-2 && worst_sum < 2e-2, "v4: device rows off the reference (row %.3g, sum %.3g)", worst, worst_sum);+ vkt_shutdown(); model_free();+}+int main(int argc, char **argv) {char buf[1024];const char *spv = argc > 1 ? argv[1] : coli_vk_shader_path(buf, sizeof buf);@@ -335,6 +447,7 @@ int main(int argc, char **argv) {printf("warm:\n"); warm();printf("adapt:\n"); adapt();printf("partial:\n"); partial();+ printf("DeepSeek V4:\n"); v4_act();ColiVkPoolStats ps; coli_vk_pool_stats(1, &ps);CHECK(ps.live == 0, "%d tier ranges still live after every shutdown", ps.live);printf(fails ? "FAIL (%d)\n" : "PASS\n", fails);diff --git a/c/vk_tier.c b/c/vk_tier.cindex c72fe6e6..f4511ca7 100644--- a/c/vk_tier.c+++ b/c/vk_tier.c@@ -63,6 +63,7 @@ static struct {int *grp; int *touched; /* expert -> group of this step, and the groups' experts */ColiVkExpert **bex; int *brows; int *bfirst; int cb;const float **bx, **by; int *rowsrc; int cbx;+ float *bw; /* the rows' route weights (vkt_issue_w), beside bx */int max_dev_rows;double t_issued;/* the balance: the share of a step's resident experts the device takes, moved by@@ -407,6 +408,18 @@ void vkt_note(int layer, int eid, const VktExpertSrc *src) {pthread_mutex_unlock(&T.mx);}+/* vkt_note's decision without its copy: the same tests, in the same order. */+int vkt_wants(int layer, int eid) {+ if (!T.on || layer < 0 || layer >= T.c.layers || eid < 0 || eid >= T.c.experts) return 0;+ VSlot *v = slot(layer, eid);+ if (v->state != VS_NONE || T.promos >= T.promo_cap) return 0;+ if (T.resident + T.queued >= T.max_resident && (!v->heat || peek_victim(score(v)) < 0)) return 0;+ pthread_mutex_lock(&T.mx);+ int qfull = T.qn >= VKT_QCAP;+ pthread_mutex_unlock(&T.mx);+ return !qfull;+}+/* ---- warm start ------------------------------------------------------------------ */static const uint32_t *const *g_plan_heat;static int g_plan_E;@@ -478,12 +491,13 @@ static int grow_rows(int need) {int nc = grow_to(need, &T.cbx);const float **a = realloc(T.bx, (size_t)nc * sizeof(*a)); if (a) T.bx = a;const float **b = realloc(T.by, (size_t)nc * sizeof(*b)); if (b) T.by = b;- if (!a || !b) return 0;+ float *c = realloc(T.bw, (size_t)nc * sizeof(*c)); if (c) T.bw = c;+ if (!a || !b || !c) return 0;T.cbx = nc;return 1;}-int vkt_issue(int layer, const float *x, int S, int K, const int *idx, uint8_t *taken) {+static int issue(int layer, const float *x, int S, int K, const int *idx, const float *w, uint8_t *taken) {if (S > 0 && K > 0) memset(taken, 0, (size_t)S * K);if (!T.on || T.inflight || layer < 0 || layer >= T.c.layers || S < 1 || K < 1) return 0;if (!coli_vk_xb_ready()) {@@ -566,10 +580,11 @@ int vkt_issue(int layer, const float *x, int S, int K, const int *idx, uint8_t *if (g < 0) continue;int j = T.bfirst[g] + T.brows[g]++;T.bx[j] = x + (size_t)(i / K) * H;+ T.bw[j] = w ? w[i] : 1.0f;T.map[i] = j;}for (int g = 0; g < ng; g++) T.grp[T.touched[g]] = -1;- if (!coli_vk_xb_issue(T.bex, T.brows, ng, T.bx)) {+ if (!coli_vk_xb_issue_w(T.bex, T.brows, ng, T.bx, w ? T.bw : NULL)) {for (int i = 0; i < n; i++) T.map[i] = -1;return 0;}@@ -578,6 +593,12 @@ int vkt_issue(int layer, const float *x, int S, int K, const int *idx, uint8_t *T.t_issued = vkt_now_ms();return total;}+int vkt_issue(int layer, const float *x, int S, int K, const int *idx, uint8_t *taken) {+ return issue(layer, x, S, K, idx, NULL, taken);+}+int vkt_issue_w(int layer, const float *x, int S, int K, const int *idx, const float *w, uint8_t *taken) {+ return issue(layer, x, S, K, idx, w, taken);+}int vkt_join(const float **rows) {if (!T.inflight) return 0;@@ -651,7 +672,8 @@ int vkt_init(const VktConfig *cfg, uint32_t *const *heat) {eng, T.c.gate_up.kind, T.c.down.kind, T.c.gate_up.gs, T.c.down.gs);return 0;}- int act = T.c.act == VKT_ACT_SITU ? COLI_VK_ACT_SITU : COLI_VK_ACT_SWIGLU;+ int act = T.c.act == VKT_ACT_SITU ? COLI_VK_ACT_SITU+ : T.c.act == VKT_ACT_SWIGLU_V4 ? COLI_VK_ACT_SWIGLU_V4 : COLI_VK_ACT_SWIGLU;if (!coli_vk_xb_init(T.c.hidden, T.c.inter, act, T.c.act_limit, T.c.act_a, T.c.act_b)) {fprintf(stderr, "[VK] tier %s: no expert batch on this device (shaders?), the experts stay on the CPU\n", eng);return 0;@@ -792,7 +814,7 @@ void vkt_shutdown(void) {}pthread_mutex_destroy(&T.mx); pthread_cond_destroy(&T.cv); pthread_cond_destroy(&T.cv_room); pthread_cond_destroy(&T.cv_done);free(T.s); free(T.grp); free(T.map); free(T.touched); free(T.bex); free(T.brows); free(T.bfirst);- free(T.bx); free(T.by); free(T.evict); free(T.done);+ free(T.bx); free(T.by); free(T.bw); free(T.evict); free(T.done);memset(&T, 0, sizeof T);}#endif /* COLI_VULKAN */diff --git a/c/vk_tier.h b/c/vk_tier.hindex ccd8d611..61f87e2d 100644--- a/c/vk_tier.h+++ b/c/vk_tier.h@@ -108,7 +108,13 @@* VKT_SRC_BF16, VKT_SRC_F32 -> fmt 11, 10* Activations (VktConfig.act): VKT_ACT_SWIGLU silu(g)*u, act_limit > 0 clamps the* gate from above and up to [-limit, limit] first (GLM-5.3, DeepSeek V4/V4.1);- * VKT_ACT_SITU a*tanh(g/a)*sigmoid(g) * b*tanh(u/b) with act_a, act_b (Kimi K3). */+ * VKT_ACT_SITU a*tanh(g/a)*sigmoid(g) * b*tanh(u/b) with act_a, act_b (Kimi K3);+ * VKT_ACT_SWIGLU_V4 DeepSeek V4's expert with its CPU kernel's roundings: gate and+ * up to bf16, the clamped SwiGLU, times the route weight (vkt_issue_w) and to bf16,+ * then E4M3 and back per 128 inputs before down. The engine hands in x already+ * rounded to E4M3 per 128 (as its kernel rounds it), adds a device row with no+ * weight of its own (the weight is in it) and rounds the row to bf16 first, as+ * its CPU expert rounds its output. */#ifndef COLI_VK_TIER_H#define COLI_VK_TIER_H#include <stdint.h>@@ -125,6 +131,7 @@ typedef struct { VktSrc kind; int gs; } VktFmt; /* gs: group (block) size, 0#define VKT_ACT_SWIGLU 0#define VKT_ACT_SITU 1+#define VKT_ACT_SWIGLU_V4 2typedef struct {const char *engine; /* names the [VK] tier lines */@@ -163,6 +170,14 @@ int vkt_put(int layer, int eid, const VktExpertSrc *src); /* any thread */void vkt_put_done(void);void vkt_note(int layer, int eid, const VktExpertSrc *src);int vkt_issue(int layer, const float *x, int S, int K, const int *idx, uint8_t *taken);+/* vkt_issue with the route weights w[S*K] (beside idx), for VKT_ACT_SWIGLU_V4, which+ * applies them on the device; the other activations ignore them. */+int vkt_issue_w(int layer, const float *x, int S, int K, const int *idx, const float *w, uint8_t *taken);+/* 1 when vkt_note would take this expert now (not on the device or on its way, the+ * promotion rate not spent, room or a colder resident to displace): for an engine+ * whose RAM form must be converted before vkt_note can read it, so it converts only+ * the experts the tier will take. No side effects. */+int vkt_wants(int layer, int eid);int vkt_join(const float **rows);int vkt_resident(int layer, int eid);void vkt_report(const char *scope, unsigned long long ram_hits, unsigned long long disk_loads);@@ -178,6 +193,8 @@ static inline int vkt_put(int l,int e,const VktExpertSrc *s){(void)l;(void)e;(vstatic inline void vkt_put_done(void){}static inline void vkt_note(int l,int e,const VktExpertSrc *s){(void)l;(void)e;(void)s;}static inline int vkt_issue(int l,const float *x,int S,int K,const int *i,uint8_t *t){(void)l;(void)x;(void)S;(void)K;(void)i;(void)t;return 0;}+static inline int vkt_issue_w(int l,const float *x,int S,int K,const int *i,const float *w,uint8_t *t){(void)l;(void)x;(void)S;(void)K;(void)i;(void)w;(void)t;return 0;}+static inline int vkt_wants(int l,int e){(void)l;(void)e;return 0;}static inline int vkt_join(const float **r){(void)r;return 0;}static inline int vkt_resident(int l,int e){(void)l;(void)e;return 0;}static inline void vkt_report(const char *s,unsigned long long r,unsigned long long d){(void)s;(void)r;(void)d;}