Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
vulkan: DeepSeek V4's indexer scores past 4096 query floats
JustVugge1fdc52fb9cd
diff --git a/c/shaders/chain_dsv4.comp b/c/shaders/chain_dsv4.compindex f6d6c04a..38af843d 100644--- a/c/shaders/chain_dsv4.comp+++ b/c/shaders/chain_dsv4.comp@@ -1,7 +1,7 @@#version 450// DeepSeek V4.1 Flash's (and DeepSeek V4's) attention on the dense chain (vk_chain.c,// chain_dsv4; vk_chain.h: vkc_dsv4_*). The model is MQA over one KV row per position:-// a sliding window of raw rows plus compressed rows a DSA indexer picks. Nine modes:+// a sliding window of raw rows plus compressed rows a DSA indexer picks. Ten modes://// mode 0, sparse attention with a sink (sparse_attn.h, coli_sparse_attend): one// workgroup per (head h, row s). The row's list is list[l_off + s*l_row + j], j < cnt:@@ -67,6 +67,9 @@// over n values g = bf16(a[a_off + i]), u = bf16(b[b_off + i]); with lim > 0,// g = min(g, lim) and u = clamp(u, -lim, lim); y[y_off + i] = bf16(g * sigmoid(g) * u),// the sigmoid in the CPU's stable form.+// mode 9, mode 3 for queries above 4096 floats (DeepSeek V4's 64 heads of 128): the+// queries read from memory instead of staged, the sums in mode 3's order (the same+// bits). Limit: IH <= 4096.// Offsets and strides in floats (ints for lists and masks).#extension GL_KHR_shader_subgroup_basic : enablelayout(local_size_x = 256) in;@@ -415,6 +418,32 @@ void engram() {for (int i = tid; i < D; i += 256) y5[xb + i] = y5[xb + i] + g * b0[vb + i];}+// ---- mode 9 ------------------------------------------------------------------------+void score_wide() {+ int s = int(gl_WorkGroupID.x), tid = int(gl_LocalInvocationID.x);+ int pos = p.v[2] + s, lens = (pos + 1) / p.v[3];+ int IH = p.v[4], ID = p.v[5], width = p.v[6];+ int qb = p.v[7] + s * p.v[8], wb = p.v[9] + s * p.v[10];+ int ko = p.v[11], kr = p.v[12], mr = p.v[13], sb = p.v[16] + s * p.v[14], mb = p.v[17] + s * mr;+ float wscale = intBitsToFloat(p.v[15]);+ for (int h = tid; h < IH; h += 256) sm[h] = b1[wb + h] * wscale;+ barrier();+ for (int j = tid; j < width; j += 256) {+ float v = NEG_INF;+ if (j < lens && (mr <= 0 || l3[mb + j] != 0)) {+ int kb = ko + j * kr;+ float tot = 0.0;+ for (int h = 0; h < IH; h++) {+ float d = 0.0;+ for (int i = 0; i < ID; i++) d += b0[qb + h * ID + i] * b2[kb + i];+ if (d > 0.0) tot += d * sm[h];+ }+ v = tot;+ }+ y5[sb + j] = v;+ }+}+// ---- mode 7 ------------------------------------------------------------------------float pow2(int k) { return uintBitsToFloat(uint(k + 127) << 23); } // 2^k, -126 <= k <= 127// |x| <= 448: the nearest E4M3 value (ties to even), coli_e4m3fn_encode then _decode@@ -528,4 +557,5 @@ void main() {else if (m == 6) engram();else if (m == 7) roundings();else if (m == 8) swiglu_v4();+ else if (m == 9) score_wide();}diff --git a/c/shaders/chain_dsv4.spv b/c/shaders/chain_dsv4.spvindex b9386e22..e7aba2ec 100644Binary files a/c/shaders/chain_dsv4.spv and b/c/shaders/chain_dsv4.spv differdiff --git a/c/tests/test_vk_chain.c b/c/tests/test_vk_chain.cindex 495730d9..979b53b2 100644--- a/c/tests/test_vk_chain.c+++ b/c/tests/test_vk_chain.c@@ -1166,7 +1166,7 @@ static void test_ds_index(int S, int pos_base, int ratio, int IH, int ID, int toint *keep = calloc((size_t)S * scrow, 4), *lst = malloc((size_t)S * lrow * 4), *lstm = malloc((size_t)S * lrow * 4);for (int s = 0; s < S; s++) {int lens = (pos_base + s + 1) / ratio;- float wt[64];+ float wt[128];for (int h = 0; h < IH; h++) { wt[h] = w[2 + s * wrow + h]; wt[h] *= wscale; }for (int j = 0; j < width; j++) {if (j >= lens) { ref[s * scrow + j] = -INFINITY; continue; }@@ -1547,6 +1547,7 @@ int main(int argc, char **argv) {test_ds_index(1, 30, 1, 4, 32, 4, 2, 2, 0); test_ds_index(5, 40, 2, 4, 32, 4, 2, 2, 0); test_ds_index(3, 0, 1, 2, 16, 8, 2, 3, 0);test_ds_index(2, 900, 1, 3, 64, 100, 4, 8, 0); test_ds_index(4, 300, 4, 16, 64, 16, 1, 3, 1); test_ds_index(3, 1, 2, 2, 16, 4, 2, 1, 0);test_ds_engram(1, 4, 128); test_ds_engram(3, 2, 300);+ test_ds_index(3, 37, 4, 64, 128, 16, 2, 2, 1); test_ds_index(2, 60, 4, 96, 64, 8, 2, 2, 0); /* DeepSeek V4's 64 x 128, past the staging */printf("dsv4 done\n");int in;for (in = 0; in < 2; in++) {diff --git a/c/vk_chain.c b/c/vk_chain.cindex 1c802022..a073eae4 100644--- a/c/vk_chain.c+++ b/c/vk_chain.c@@ -1092,11 +1092,13 @@ int vkc_dsv4_compress(VkcBuf *kv, VkcBuf *sc, VkcBuf *ring, VkcBuf *prm, VkcBufreturn dsv4_rec(PK_DSA, 2, p, sizeof *p, bd, 8, 1, 1);}int vkc_dsv4_score(VkcBuf *iq, VkcBuf *hw, VkcBuf *keys, VkcBuf *mask, VkcBuf *sc, const VkcDsScore *p) {- if (p->S < 1 || p->S > 65535 || p->IH < 1 || p->IH > 64 || p->ID < 1 || p->IH * p->ID > 4096 || p->ratio < 1 ||+ /* queries above 4096 floats: mode 9, the same sums with the queries read from memory */+ int wide = p->IH > 64 || p->IH * p->ID > 4096;+ if (p->S < 1 || p->S > 65535 || p->IH < 1 || p->IH > 4096 || p->ID < 1 || p->ratio < 1 ||p->width < 0 || p->sc_row < p->width || (p->mask_row > 0 && !mask)) return 0;if (p->width == 0) return open_frame() && !K.lost;VkcBind bd[6] = {B(iq, 0), B(hw, 0), B(keys, 0), B(p->mask_row > 0 ? mask : NULL, 0), B(NULL, 0), B(sc, 1)};- return dsv4_rec(PK_DSA, 3, p, sizeof *p, bd, 6, (uint32_t)p->S, 1);+ return dsv4_rec(PK_DSA, wide ? 9 : 3, p, sizeof *p, bd, 6, (uint32_t)p->S, 1);}int vkc_dsv4_cand(VkcBuf *sc, VkcBuf *mask, const VkcDsCand *p) {if (p->S < 1 || p->S > 65535 || p->block < 1 || p->ratio < 1 || p->width < 0 ||diff --git a/c/vk_chain.h b/c/vk_chain.hindex 14d81e18..e645800f 100644--- a/c/vk_chain.h+++ b/c/vk_chain.h@@ -312,7 +312,8 @@ int vkc_mhc(int mode, VkcBuf *x, VkcBuf *m, VkcBuf *hp, VkcBuf *prm, VkcBuf *y,* j < lens = (pos + 1) / ratio (and mask[mask_off + s*mask_row + j] != 0* when mask_row > 0) as sum_h [dot > 0] dot * hw_h * wscale, dot = q_h .* key[j]; every other j < width scores -inf; into sc[sc_off + s*sc_row- * + j]. IH <= 64, IH*ID <= 4096.+ * + j]. IH <= 4096; IH <= 64 and IH*ID <= 4096 stage the queries in+ * shared memory, larger ones read them from memory (the same sums).* vkc_dsv4_cand the candidate blocks: per row, each block's best score, the block of* column lens - 1 pinned, then the best min(topb, blocks) blocks (the* lower on a tie), mask 1 on their columns. At most 4096 blocks.