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

vulkan: the chain's attention takes a sliding window, a ring and a sink

JustVugge33da31e771e
diff --git a/c/shaders/chain_attn.comp b/c/shaders/chain_attn.comp
index 655f083b..155a820f 100644
--- a/c/shaders/chain_attn.comp
+++ b/c/shaders/chain_attn.comp
@@ -13,6 +13,20 @@
// normalizes first); the two agree to rounding.
// Cache layout as the engines keep it on the host: K[(kvh*cap + t)*hd + d].
// Limits: hd <= 256 (two dims per thread).
+//
+// The trailing push constants (all zero for the callers that predate them, who get
+// exactly the arithmetic above):
+// win a sliding window: row s sees the positions max(0, pos - win + 1)..pos,
+// pos = pos_base + s (MiMo's SWA layers); 0 = every position from 0
+// ring the cache is a ring of `ring` rows: position t sits in row t % ring;
+// 0 = position t in row t
+// vd V's head dim when it differs from K's (the output's per-head stride too);
+// vk_chain.c passes hd for 0. vd <= 256
+// kv_pm 1: position-major rows, K[(row*KVH + kvh)*hd + d], V[(row*KVH + kvh)*vd + d]
+// (MiMo's host layout); 0: the head-major layout above
+// sink, sink_off 1: one more logit per head, snk[sink_off + h], joins the softmax
+// denominator and nothing else (MiMo's attention sink): the running max and
+// sum start from it instead of from nothing
layout(local_size_x = 128) in;
layout(std430, binding = 0) readonly buffer QB { float q[]; };
@@ -21,6 +35,7 @@ layout(std430, binding = 2) readonly buffer VB { float vc[]; };
layout(std430, binding = 3) buffer OB { float o[]; };
layout(std430, binding = 4) readonly buffer GB { float gt[]; };
layout(std430, binding = 5) readonly buffer LB { int sel[]; };
+layout(std430, binding = 6) readonly buffer SB { float snk[]; };
layout(push_constant) uniform P {
int S, H, KVH, hd, pos_base, cap;
@@ -30,6 +45,7 @@ layout(push_constant) uniform P {
int sel_off, sel_row;
float scale;
int k_off, v_off; // where this layer's cache starts in kc / vc
+ int win, ring, vd, kv_pm, sink, sink_off;
} p;
shared float qs[256];
@@ -43,7 +59,8 @@ void main() {
int kvh = h / (p.H / p.KVH);
int qb = p.q_off + s * p.q_row + h * p.q_seg;
for (int d = tid; d < p.hd; d += 128) qs[d] = q[qb + d];
- int n = p.pos_base + s + 1;
+ int first = p.win > 0 ? max(0, p.pos_base + s - p.win + 1) : 0;
+ int n = p.pos_base + s + 1 - first;
bool list = false;
int lb = 0;
if (p.sel_row > 0) {
@@ -52,15 +69,16 @@ void main() {
if (c >= 0) { n = c; list = true; }
}
barrier();
- float m = -3.0e38, l = 0.0, acc0 = 0.0, acc1 = 0.0;
+ float m = p.sink != 0 ? snk[p.sink_off + h] : -3.0e38, l = p.sink != 0 ? 1.0 : 0.0, acc0 = 0.0, acc1 = 0.0;
int kbase = kvh * p.cap;
for (int t0 = 0; t0 < n; t0 += 128) {
int i = t0 + tid;
float sv = -3.0e38;
if (i < n) {
- int t = list ? sel[lb + 1 + i] : i;
- pt[tid] = t;
- int kb = p.k_off + (kbase + t) * p.hd;
+ int t = list ? sel[lb + 1 + i] : first + i;
+ int r = p.ring > 0 ? t % p.ring : t;
+ pt[tid] = r;
+ int kb = p.k_off + (p.kv_pm != 0 ? r * p.KVH + kvh : kbase + r) * p.hd;
float a = 0.0;
for (int d = 0; d < p.hd; d++) a += qs[d] * kc[kb + d];
sv = a * p.scale;
@@ -87,20 +105,20 @@ void main() {
int cnt = min(128, n - t0);
int d0 = tid, d1 = tid + 128;
for (int j = 0; j < cnt; j++) {
- int vb = p.v_off + (kbase + pt[j]) * p.hd;
+ int vb = p.v_off + (p.kv_pm != 0 ? pt[j] * p.KVH + kvh : kbase + pt[j]) * p.vd;
float w = pe[j];
- if (d0 < p.hd) acc0 += w * vc[vb + d0];
- if (d1 < p.hd) acc1 += w * vc[vb + d1];
+ if (d0 < p.vd) acc0 += w * vc[vb + d0];
+ if (d1 < p.vd) acc1 += w * vc[vb + d1];
}
m = mn;
barrier();
}
float inv = l > 0.0 ? 1.0 / l : 0.0;
- int ob = p.o_off + s * p.o_row + h * p.hd;
+ int ob = p.o_off + s * p.o_row + h * p.vd;
int gb = p.g_off + s * p.g_row + h * p.g_seg;
for (int k = 0; k < 2; k++) {
int d = tid + 128 * k;
- if (d >= p.hd) break;
+ if (d >= p.vd) break;
float v = (k == 0 ? acc0 : acc1) * inv;
if (p.has_gate != 0) v *= 1.0 / (1.0 + exp(-gt[gb + d]));
o[ob + d] = v;
diff --git a/c/shaders/chain_attn.spv b/c/shaders/chain_attn.spv
index 43fffc79..463deb6c 100644
Binary files a/c/shaders/chain_attn.spv and b/c/shaders/chain_attn.spv differ
diff --git a/c/shaders/chain_ew.comp b/c/shaders/chain_ew.comp
index decabdf5..4a743790 100644
--- a/c/shaders/chain_ew.comp
+++ b/c/shaders/chain_ew.comp
@@ -14,6 +14,7 @@
// 4 HC_MIX y[r*D+d] = sum_k sigmoid(a[r*W+k*D+d]) * b[r*W+k*D+d] / fc, k < C, W = C*D
// 5 HC_INJ y[i] = 2 * sigmoid(a[i] / fc)
// 6 HC_APPLY y[r*W+k*D+d] += a[r*C+k] * b[r*D+d] (i over S*C*D)
+// 7 SCALE y[i] = a[i] * fc (MiMo's value scale)
// Offsets in floats: y_off, a_off, b_off, c_off, e_off.
layout(local_size_x = 256) in;
@@ -64,5 +65,7 @@ void main() {
int W = p.C * p.D;
int r = i / W, rem = i - r * W, k = rem / p.D, d = rem - k * p.D;
y[p.y_off + i] += a[p.a_off + r * p.C + k] * b[p.b_off + r * p.D + d];
+ } else if (p.op == 7) {
+ y[p.y_off + i] = a[p.a_off + i] * p.fc;
}
}
diff --git a/c/shaders/chain_ew.spv b/c/shaders/chain_ew.spv
index c261bc08..d8d729e8 100644
Binary files a/c/shaders/chain_ew.spv and b/c/shaders/chain_ew.spv differ
diff --git a/c/tests/test_vk_chain.c b/c/tests/test_vk_chain.c
index cf036906..7b74e066 100644
--- a/c/tests/test_vk_chain.c
+++ b/c/tests/test_vk_chain.c
@@ -11,10 +11,12 @@
* weight slice per stream, in place
* rope rotate-half from a host table, heads at a stride
* attn causal GQA with the output gate over a cache at an offset, prefill rows
- * after earlier ones, and a selection list
+ * after earlier ones, and a selection list; MiMo's form (vkc_attn_w): a
+ * sliding window, a ring of rows, position-major rows, V's own head dim and
+ * a sink logit per head
* dnconv both orders of the sum, the ring carried across calls, the snapshot row
* dnrec KD 8 and 128, silu and sigmoid gates, the state carried, the snapshot
- * ew every element-wise op
+ * ew every element-wise op (SCALE: MiMo's value scale)
* qsa block keys, and the selection with ties broken as the CPU sorts
* ple the gate and the dilated convolution with its ring and snapshot
* frames vkc_write, vkc_read, a frame left in flight and one ordered after it
@@ -202,6 +204,51 @@ static void test_attn(int S, int pos_base, int hd, int use_list) {
free(q); free(kc); free(vc); free(sel); free(ref);
}
+/* MiMo's attention (vkc_attn_w): row s at pos = pos_base + s sees the positions
+ * max(0, pos - win + 1)..pos (win 0: from 0); position t sits in row t % ring of the
+ * cache (ring 0: row t), rows position-major (kv_pm) or head-major; V has its own head
+ * dim vd; with a sink, one more logit per head joins the softmax denominator. */
+static void test_attn_win(int S, int pos_base, int H, int KVH, int hd, int vd, int win, int ring, int sink, int kv_pm) {
+ int rows = ring ? ring : pos_base + S + 3, koff = 32, voff = 16, qrow = H * hd + 40;
+ size_t kn = (size_t)koff + (size_t)rows * KVH * hd, vn = (size_t)voff + (size_t)rows * KVH * vd;
+ float *q = fvec((size_t)S * qrow, 1.f), *kc = fvec(kn, 1.f), *vc = fvec(vn, 1.f), *snk = fvec(H + 5, 2.f);
+ float scale = 1.f / sqrtf((float)hd);
+ float *ref = malloc((size_t)S * H * vd * sizeof *ref);
+ for (int s = 0; s < S; s++) for (int h = 0; h < H; h++) {
+ int kvh = h / (H / KVH), pos = pos_base + s, first = win ? (pos - win + 1 > 0 ? pos - win + 1 : 0) : 0, n = pos - first + 1;
+ double *sc = malloc(n * sizeof *sc), mx = sink ? snk[5 + h] : -1e300, sum = 0;
+ for (int j = 0; j < n; j++) {
+ int r = ring ? (first + j) % ring : first + j;
+ size_t kb = koff + (kv_pm ? ((size_t)r * KVH + kvh) : ((size_t)kvh * rows + r)) * hd;
+ double a = 0; for (int d = 0; d < hd; d++) a += (double)q[(size_t)s * qrow + h * hd + d] * kc[kb + d];
+ sc[j] = a * scale; if (sc[j] > mx) mx = sc[j];
+ }
+ if (sink) sum = exp(snk[5 + h] - mx);
+ for (int j = 0; j < n; j++) { sc[j] = exp(sc[j] - mx); sum += sc[j]; }
+ for (int d = 0; d < vd; d++) {
+ double a = 0;
+ for (int j = 0; j < n; j++) {
+ int r = ring ? (first + j) % ring : first + j;
+ size_t vb = voff + (kv_pm ? ((size_t)r * KVH + kvh) : ((size_t)kvh * rows + r)) * vd;
+ a += sc[j] / sum * vc[vb + d];
+ }
+ ref[((size_t)s * H + h) * vd + d] = (float)a;
+ }
+ free(sc);
+ }
+ VkcBuf *qb = up(q, (size_t)S * qrow), *kb = up(kc, kn), *vb = up(vc, vn), *sb = up(snk, H + 5);
+ VkcBuf *ob = vkc_buf((size_t)S * H * vd * 4, VKC_DOWN);
+ VkcAttnW p = {{S, H, KVH, hd, pos_base, rows, 0, qrow, hd, 0, 0, 0, 0, 0, H * vd, 0, 0, scale, koff, voff},
+ win, ring, vd, kv_pm, sink, 5};
+ vkc_begin(); int ok = vkc_attn_w(qb, kb, vb, ob, NULL, NULL, sink ? sb : NULL, &p); vkc_submit(1);
+ float *o = vkc_ptr(ob);
+ double e = relerr(o, ref, (size_t)S * H * vd, 1e-3);
+ CHECK(ok && e < 2e-5, "attn window S %d pos %d hd %d vd %d win %d ring %d sink %d pm %d: err %.2e",
+ S, pos_base, hd, vd, win, ring, sink, kv_pm, e);
+ vkc_free(qb); vkc_free(kb); vkc_free(vb); vkc_free(sb); vkc_free(ob);
+ free(q); free(kc); free(vc); free(snk); free(ref);
+}
+
/* ---- DeltaNet: convolution and recurrence ------------------------------------------ */
static void conv_ref(int S, int CD, int CK, const float *in, const float *w, float *ring, float *out, int order, int snap_row, float *snap) {
for (int s = 0; s < S; s++) for (int c = 0; c < CD; c++) {
@@ -303,7 +350,8 @@ static void test_ew(void) {
struct { int op, flags, n; const char *name; } ops[] = {
{VKC_EW_ADD, 0, R * D, "add"}, {VKC_EW_COMBINE, 1 | 2 | 4, R * D, "combine"}, {VKC_EW_COMBINE, 2 | 8, R * D, "combine-noresid"},
{VKC_EW_COMBINE, 1, R * D, "combine-routed"}, {VKC_EW_SWIGLU, 0, R * D, "swiglu"}, {VKC_EW_HC_LOW, 0, R * D, "hc-low"},
- {VKC_EW_HC_MIX, 0, R * D, "hc-mix"}, {VKC_EW_HC_INJ, 0, R * C, "hc-inj"}, {VKC_EW_HC_APPLY, 0, R * W, "hc-apply"}};
+ {VKC_EW_HC_MIX, 0, R * D, "hc-mix"}, {VKC_EW_HC_INJ, 0, R * C, "hc-inj"}, {VKC_EW_HC_APPLY, 0, R * W, "hc-apply"},
+ {VKC_EW_SCALE, 0, R * D, "scale"}};
for (size_t k = 0; k < sizeof ops / sizeof *ops; k++) {
int n = ops[k].n, op = ops[k].op, f = ops[k].flags;
for (int i = 0; i < n; i++) {
@@ -319,6 +367,7 @@ static void test_ew(void) {
case VKC_EW_HC_INJ: ref[i] = 2.f * sigm(a[i] / C); break;
case VKC_EW_HC_APPLY: { int rr = i / W, rem = i % W, s = rem / D, dd = rem % D;
ref[i] = c[i] + a[rr * C + s] * b[rr * D + dd]; break; }
+ case VKC_EW_SCALE: ref[i] = a[i] * (float)C; break;
}
}
if (op == VKC_EW_HC_APPLY) { vkc_begin(); vkc_write(yb, 0, c, nw * 4); vkc_submit(1); }
@@ -513,6 +562,15 @@ int main(int argc, char **argv) {
printf("norm done\n");
test_rope();
test_attn(1, 0, 16, 0); test_attn(1, 140, 32, 0); test_attn(5, 200, 64, 0); test_attn(6, 9, 256, 1); test_attn(130, 3, 24, 0);
+ /* MiMo: a prompt's rows through a window (from position 0, and past it), a decode row
+ * over a ring as large as the window, prefill rows over a larger ring, full attention
+ * with V's own head dim, a window wider than a tile, the head-major layout with a
+ * window, the release's head dims (192 / 128) over a ring of 128 */
+ test_attn_win(5, 0, 4, 2, 48, 32, 8, 0, 1, 1); test_attn_win(5, 20, 4, 2, 48, 32, 8, 0, 1, 1);
+ test_attn_win(1, 30, 4, 4, 48, 32, 8, 8, 1, 1); test_attn_win(1, 3, 4, 4, 48, 32, 8, 8, 1, 1);
+ test_attn_win(3, 40, 4, 2, 48, 32, 8, 10, 0, 1); test_attn_win(4, 6, 4, 2, 64, 48, 0, 0, 1, 1);
+ test_attn_win(2, 300, 4, 2, 16, 16, 150, 0, 1, 1); test_attn_win(130, 3, 4, 2, 24, 40, 16, 0, 0, 0);
+ test_attn_win(1, 500, 8, 4, 192, 128, 128, 128, 1, 1);
printf("attn done\n");
test_dnconv(0); test_dnconv(1);
test_dnrec(8, 8, 8, 4, 0); test_dnrec(4, 4, 4, 2, 1); test_dnrec(128, 128, 4, 2, 0); test_dnrec(32, 100, 6, 3, 1);
diff --git a/c/vk_chain.c b/c/vk_chain.c
index f4b81f04..bcdec516 100644
--- a/c/vk_chain.c
+++ b/c/vk_chain.c
@@ -690,10 +690,19 @@ int vkc_rope(VkcBuf *x, VkcBuf *cs, const VkcRope *p) {
return record(K.pipe[P_ROPE], bd, 2, p, sizeof *p, gx, gy, 1);
}
int vkc_attn(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *gate, VkcBuf *sel, const VkcAttn *p) {
+ VkcAttnW w;
+ memset(&w, 0, sizeof w);
+ w.a = *p;
+ return vkc_attn_w(q, kc, vc, o, gate, sel, NULL, &w);
+}
+int vkc_attn_w(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *gate, VkcBuf *sel, VkcBuf *snk,
+ const VkcAttnW *p) {
K.kind = PK_ATTN;
- if (p->hd > 256 || p->H % p->KVH) return 0;
- VkcBind bd[6] = {B(q, 0), B(kc, 0), B(vc, 0), B(o, 1), B(gate, 0), B(sel, 0)};
- return record(K.pipe[P_ATTN], bd, 6, p, sizeof *p, (uint32_t)p->H, (uint32_t)p->S, 1);
+ VkcAttnW w = *p; /* the shader's push constants: VkcAttn's fields, then these */
+ if (w.vd <= 0) w.vd = w.a.hd;
+ if (w.a.hd > 256 || w.vd > 256 || w.a.H % w.a.KVH || w.win < 0 || w.ring < 0 || (w.sink && !snk)) return 0;
+ VkcBind bd[7] = {B(q, 0), B(kc, 0), B(vc, 0), B(o, 1), B(gate, 0), B(sel, 0), B(snk, 0)};
+ return record(K.pipe[P_ATTN], bd, 7, &w, sizeof w, (uint32_t)w.a.H, (uint32_t)w.a.S, 1);
}
int vkc_dnconv(VkcBuf *in, VkcBuf *w, VkcBuf *ring, VkcBuf *out, VkcBuf *snap, const VkcDnConv *p) {
K.kind = PK_DNCONV;
diff --git a/c/vk_chain.h b/c/vk_chain.h
index 5c177295..664d2264 100644
--- a/c/vk_chain.h
+++ b/c/vk_chain.h
@@ -92,6 +92,15 @@ int vkc_rope(VkcBuf *x, VkcBuf *cs, const VkcRope *p);
typedef struct { int S, H, KVH, hd, pos_base, cap, q_off, q_row, q_seg, g_off, g_row, g_seg, has_gate,
o_off, o_row, sel_off, sel_row; float scale; int k_off, v_off; } VkcAttn;
int vkc_attn(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *gate, VkcBuf *sel, const VkcAttn *p);
+/* The same core with what MiMo adds (vkc_attn is this with every field below zero):
+ * win a sliding window of that many positions (row s sees max(0, pos - win + 1)..pos);
+ * ring the cache a ring of that many rows (position t in row t % ring); vd V's head dim
+ * (0 = hd; also the output's per-head stride), at most 256; kv_pm position-major rows
+ * (K[(row*KVH + kvh)*hd + d], V[(row*KVH + kvh)*vd + d]) instead of head-major; sink a
+ * sink logit per head at snk[sink_off + h], which joins the softmax denominator only. */
+typedef struct { VkcAttn a; int win, ring, vd, kv_pm, sink, sink_off; } VkcAttnW;
+int vkc_attn_w(VkcBuf *q, VkcBuf *kc, VkcBuf *vc, VkcBuf *o, VkcBuf *gate, VkcBuf *sel, VkcBuf *snk,
+ const VkcAttnW *p);
/* chain_dnconv.comp */
typedef struct { int S, CD, CK, in_off, in_row, out_off, out_row, snap_row, order, w_off, ring_off, snap_off; } VkcDnConv;
int vkc_dnconv(VkcBuf *in, VkcBuf *w, VkcBuf *ring, VkcBuf *out, VkcBuf *snap, const VkcDnConv *p);
@@ -107,6 +116,7 @@ int vkc_dnrec(int KD, VkcBuf *cv, VkcBuf *ab, VkcBuf *z, VkcBuf *st, VkcBuf *pr
#define VKC_EW_HC_MIX 4
#define VKC_EW_HC_INJ 5
#define VKC_EW_HC_APPLY 6
+#define VKC_EW_SCALE 7
typedef struct { int op, n, D, C, flags, e_row, y_off, a_off, b_off, c_off, e_off; float fc; } VkcEw;
int vkc_ew(VkcBuf *y, VkcBuf *a, VkcBuf *b, VkcBuf *c, VkcBuf *e, const VkcEw *p);
/* chain_qsa.comp (mode 0: nb block keys from b0; mode 1: S rows' selections) */