Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(deepseek-v41): the attention block's scratch is bounded, and CI crosses it
JustVugg8d1a7ab836c5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 437fba85..5a779f41 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -596,6 +596,20 @@ jobs:SNAP=dsv41_tiny ./deepseek_v41 "$cap" dsv41_tiny/ref.jsonecho "::endgroup::"done+ - name: "A prompt longer than one block, so the block boundary is crossed"+ run: |+ cd c+ # Attention projects a block of positions at a time and the MoE routes one,+ # both capped at 32. The default fixture's prompt is eight tokens and never+ # reaches that cap, so a forty-token one is built as well: it crosses the+ # boundary in both, and has to stay token-exact anyway.+ python3 tools/make_dsv41_tiny.py --out dsv41_long --emit-ref dsv41_long/ref.json \+ --prompt-len 40 --max-new 6+ for cap in 2 8; do+ echo "::group::long prompt, cap=$cap"+ SNAP=dsv41_long ./deepseek_v41 "$cap" dsv41_long/ref.json+ echo "::endgroup::"+ done- name: The published index cache is the vendor's, not the tidy onerun: |cd cdiff --git a/c/deepseek_v41.c b/c/deepseek_v41.cindex c499572e..21513bca 100644--- a/c/deepseek_v41.c+++ b/c/deepseek_v41.c@@ -1671,8 +1671,12 @@ static void attention_run(Model *m, int layer, const float *x, int n, int start_}const float *kv_read = ratio ? kv_all : window_kv;- /* every position's heads, so the output projection can run as one block too */- float *heads = xmalloc((size_t)n * nh * hd * sizeof(float), "attention output");+ /* A block of positions' heads, so the output projection can run as one block+ * too. Bounded rather than the whole chunk: at 64 heads of 512 this is 128 KB a+ * position, and a long prompt would otherwise ask for a quarter of a gigabyte+ * of it. */+ int hblock = n < MV_ROWS_MAX ? n : MV_ROWS_MAX;+ float *heads = xmalloc((size_t)hblock * nh * hd * sizeof(float), "attention output");float *head_score = xmalloc((size_t)nh * total_idx * sizeof(float), "attention scores");float attn_scale = 1.0f / sqrtf((float)hd);for (int t = 0; t < n; t++) {@@ -1693,7 +1697,7 @@ static void attention_run(Model *m, int layer, const float *x, int n, int start_for (int k = count; k < window_width; k++) idx[(size_t)t * total_idx + k] = -1;}const int *row = idx + (size_t)t * total_idx;- float *row_heads = heads + (size_t)t * nh * hd;+ float *row_heads = heads + (size_t)(t % hblock) * nh * hd;#pragma omp parallel for schedule(static)for (int h = 0; h < nh; h++)sparse_attend(row_heads + (size_t)h * hd, q + (size_t)t * nh * hd + (size_t)h * hd,@@ -1701,10 +1705,13 @@ static void attention_run(Model *m, int layer, const float *x, int n, int start_head_score + (size_t)h * total_idx);for (int h = 0; h < nh; h++)rope_apply(row_heads + (size_t)h * hd + hd - rd, rope, start_pos + t, rd, 1);+ /* The projection reads the heads and writes `out`; it touches no cached+ * state, so it waits until the block is full and then runs once for it. */+ if (t % hblock == hblock - 1 || t == n - 1) {+ int first = t - (t % hblock);+ attn_project_out_rows(m, l, heads, t - first + 1, out + (size_t)first * dim);+ }}- /* The projection reads the heads and writes `out`; it touches no cached state,- * so it can wait until every position has its heads and then run once. */- attn_project_out_rows(m, l, heads, n, out);m->t_attn += now_s() - started;free(head_score); free(kv_raw);free(heads); free(kv_all); free(idx); free(scratch); free(kv); free(q); free(qr);