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

perf(deepseek-v41): attention reads its matrices once per block, not once per token

JustVugg7355ab07a209
diff --git a/c/Makefile b/c/Makefile
index a76c5e8d..c384421d 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1361,6 +1361,9 @@ tests/test_e8_kernel$(EXE): tests/test_e8_kernel.c quant.h
tests/test_e4m3_vector$(EXE): tests/test_e4m3_vector.c quant.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+tests/test_sparse_attn$(EXE): tests/test_sparse_attn.c sparse_attn.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
tests/test_rans$(EXE): tests/test_rans.c rans.h compat.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
diff --git a/c/deepseek_v41.c b/c/deepseek_v41.c
index bce4be39..48fe73d9 100644
--- a/c/deepseek_v41.c
+++ b/c/deepseek_v41.c
@@ -57,6 +57,7 @@
#include "json.h"
#include "st.h"
#include "quant.h"
+#include "sparse_attn.h"
#include <pthread.h> /* ehit_mark publishes the lazy HITS table under a lock */
#if defined(__AVX2__)
#include <immintrin.h>
@@ -358,6 +359,70 @@ static void mv8(float *y, const W8 *w, const float *x) {
}
}
+/* mv8 and mvb for a block of positions at once.
+ *
+ * The matrix is the thing that does not fit in cache -- wq_b is 42 MB on the
+ * released checkpoint, and so is wo_b -- while a block of positions does. Driving
+ * the block through one pass of the matrix instead of one pass per position turns
+ * prefill's twenty-six trips over 42 MB into a single trip, which is memory
+ * traffic the FMA units were waiting on rather than arithmetic.
+ *
+ * Values are bit-identical to the one-at-a-time kernels, not merely close: each
+ * output still folds the same tiles in the same order, and the decoded weight
+ * being held across the block instead of re-decoded per position does not change
+ * what is multiplied. The block is capped so the positions stay in L2 while the
+ * matrix streams past. */
+#define MV_ROWS_MAX 32
+
+static int mv_block_rows(int I) {
+ int block = (int)((192 * 1024) / ((size_t)I * sizeof(float) + 1));
+ if (block > MV_ROWS_MAX) block = MV_ROWS_MAX;
+ return block < 1 ? 1 : block;
+}
+
+static void mv8_rows(float *y, int ystride, const W8 *w, const float *x, int xstride, int rows) {
+ int I = w->I, tiles_i = (I + FP8_TILE - 1) / FP8_TILE;
+ int block = mv_block_rows(I);
+ for (int r0 = 0; r0 < rows; r0 += block) {
+ int nr = rows - r0 < block ? rows - r0 : block;
+ #pragma omp parallel for schedule(static)
+ for (int o = 0; o < w->O; o++) {
+ const uint8_t *row = w->q + (size_t)o * I;
+ const uint8_t *scale = w->s + (size_t)(o / FP8_TILE) * tiles_i;
+ float sum[MV_ROWS_MAX];
+ for (int r = 0; r < nr; r++) sum[r] = 0.0f;
+ for (int base = 0; base < I; base += FP8_TILE) {
+ int width = I - base < FP8_TILE ? I - base : FP8_TILE;
+ float tile = ue8m0(scale[base / FP8_TILE]);
+#if defined(__AVX2__)
+ if (width == FP8_TILE) {
+ __m256 w0 = e4m3_decode8(row + base);
+ __m256 w1 = e4m3_decode8(row + base + 8);
+ __m256 w2 = e4m3_decode8(row + base + 16);
+ __m256 w3 = e4m3_decode8(row + base + 24);
+ for (int r = 0; r < nr; r++) {
+ const float *xr = x + (size_t)(r0 + r) * xstride + base;
+ __m256 acc = _mm256_mul_ps(w0, _mm256_loadu_ps(xr));
+ acc = _mm256_fmadd_ps(w1, _mm256_loadu_ps(xr + 8), acc);
+ acc = _mm256_fmadd_ps(w2, _mm256_loadu_ps(xr + 16), acc);
+ acc = _mm256_fmadd_ps(w3, _mm256_loadu_ps(xr + 24), acc);
+ sum[r] += hsum256(acc) * tile;
+ }
+ continue;
+ }
+#endif
+ for (int r = 0; r < nr; r++) {
+ const float *xr = x + (size_t)(r0 + r) * xstride + base;
+ float part = 0.0f;
+ for (int i = 0; i < width; i++) part += e4m3_decode(row[base + i]) * xr[i];
+ sum[r] += part * tile;
+ }
+ }
+ for (int r = 0; r < nr; r++) y[(size_t)(r0 + r) * ystride + o] = sum[r];
+ }
+ }
+}
+
static void mvb(float *y, const WB *w, const float *x) {
int I = w->I;
#pragma omp parallel for schedule(static)
@@ -1008,9 +1073,12 @@ static void cache_init(Model *m, LCache *cache, int ecap) {
}
}
+static void attn_project_check(const Cfg *c);
+
static void model_load(Model *m, const char *snap, int ecap, int engram_cache_rows) {
Cfg *c = &m->c;
cfg_load(c, snap);
+ attn_project_check(c);
st_init(&m->S, snap);
engram_load_sidecar(&m->engram, snap);
@@ -1178,29 +1246,13 @@ static void window_idxs(const Cfg *c, const Layer *l, int n, int start_pos, int
/* kernel.py sparse_attn for one query: `idx` lists the rows of `kv` this query reads,
* -1 meaning nothing. The sink joins the denominator only, so a query with no readable
* row comes out zero instead of NaN. */
+/* The kernel itself lives in sparse_attn.h, where a test can reach it. `score` is
+ * the caller's scratch: this runs once per head per position per layer, and a
+ * malloc in that place was costing more than some of the arithmetic. */
static void sparse_attend(float *out, const float *q, const float *kv, const int *idx,
- int count, int nh, int hd, float sink_h, float scale) {
- float best = -1e30f;
- float *score = xmalloc((size_t)count * sizeof(float), "attention scores");
- for (int j = 0; j < count; j++) {
- if (idx[j] < 0) { score[j] = -INFINITY; continue; }
- const float *k = kv + (size_t)idx[j] * hd;
- float dot = 0.0f;
- for (int i = 0; i < hd; i++) dot += q[i] * k[i];
- score[j] = dot * scale;
- if (score[j] > best) best = score[j];
- }
- float denom = expf(sink_h - best);
- for (int i = 0; i < hd; i++) out[i] = 0.0f;
- for (int j = 0; j < count; j++) {
- if (!(score[j] > -INFINITY)) continue;
- float weight = expf(score[j] - best);
- denom += weight;
- const float *k = kv + (size_t)idx[j] * hd;
- for (int i = 0; i < hd; i++) out[i] += weight * k[i];
- }
- for (int i = 0; i < hd; i++) out[i] /= denom;
- free(score);
+ int count, int nh, int hd, float sink_h, float scale,
+ float *score) {
+ coli_sparse_attend(out, q, kv, idx, count, hd, sink_h, scale, score);
(void)nh;
}
@@ -1434,43 +1486,41 @@ static void indexer_run(Model *m, int layer, const float *x, const float *qr, in
/* wo_a is block diagonal over o_groups: each group projects only its own heads, and
* only then does wo_b mix the groups. Shared with the DSpark stages, whose attention
* differs in what it reads but not in how it comes out. */
-static void attn_project_out(Model *m, Layer *l, const float *heads, float *out) {
+static void attn_project_out_rows(Model *m, Layer *l, const float *heads, int rows, float *out) {
Cfg *c = &m->c;
int per_group = c->n_heads * c->head_dim / c->o_groups;
- float *grouped = xmalloc((size_t)c->o_groups * c->o_lora * sizeof(float), "grouped");
+ int width = c->o_groups * c->o_lora;
+ int head_stride = c->n_heads * c->head_dim;
+ float *grouped = xmalloc((size_t)rows * width * sizeof(float), "grouped");
for (int g = 0; g < c->o_groups; g++) {
- const uint8_t *rows = l->wo_a.q + (size_t)g * c->o_lora * per_group;
- const uint8_t *scales = l->wo_a.s;
- int tiles_i = (per_group + FP8_TILE - 1) / FP8_TILE;
- for (int r = 0; r < c->o_lora; r++) {
- int o = g * c->o_lora + r;
- const uint8_t *w = rows + (size_t)r * per_group;
- const uint8_t *scale_row = scales + (size_t)(o / FP8_TILE) * tiles_i;
- const float *xg = heads + (size_t)g * per_group;
- float sum = 0.0f;
- for (int base = 0; base < per_group; base += FP8_TILE) {
- int width = per_group - base < FP8_TILE ? per_group - base : FP8_TILE;
- float tile = ue8m0(scale_row[base / FP8_TILE]);
- float part = 0.0f;
-#if defined(__AVX2__)
- if (width == FP8_TILE) {
- __m256 acc = _mm256_setzero_ps();
- for (int i = 0; i < FP8_TILE; i += 8)
- acc = _mm256_fmadd_ps(e4m3_decode8(w + base + i),
- _mm256_loadu_ps(xg + base + i), acc);
- part = hsum256(acc);
- } else
-#endif
- for (int i = 0; i < width; i++) part += e4m3_decode(w[base + i]) * xg[base + i];
- sum += part * tile;
- }
- grouped[o] = sum;
- }
- }
- mv8(out, &l->wo_b, grouped);
+ /* A W8 view of group g's block. The scale array is indexed by the GLOBAL
+ * output row, so the view is only the same matrix when a group's rows start
+ * on a tile boundary -- which o_lora being a multiple of 32 is what makes
+ * true, and what the fallback below covers when it is not. */
+ W8 block;
+ block.q = l->wo_a.q + (size_t)g * c->o_lora * per_group;
+ block.s = l->wo_a.s + (size_t)(g * c->o_lora / FP8_TILE)
+ * ((per_group + FP8_TILE - 1) / FP8_TILE);
+ block.O = c->o_lora;
+ block.I = per_group;
+ mv8_rows(grouped + (size_t)g * c->o_lora, width, &block,
+ heads + (size_t)g * per_group, head_stride, rows);
+ }
+ mv8_rows(out, c->dim, &l->wo_b, grouped, width, rows);
free(grouped);
}
+/* wo_a's groups have to start on a scale tile for the block view above to name the
+ * same weights. Checked once at load rather than asserted in the hot path. */
+static void attn_project_check(const Cfg *c) {
+ if (c->o_lora % FP8_TILE != 0) {
+ fprintf(stderr, "[v41] o_lora_rank %d is not a multiple of the %d-wide scale tile: "
+ "the grouped output projection cannot be read as blocks\n",
+ c->o_lora, FP8_TILE);
+ exit(1);
+ }
+}
+
/* model.py Attention.forward. `x` is the normalized block input [n, dim]. */
static void attention_run(Model *m, int layer, const float *x, int n, int start_pos, float *out) {
Cfg *c = &m->c;
@@ -1483,16 +1533,22 @@ static void attention_run(Model *m, int layer, const float *x, int n, int start_
float *qr = xmalloc((size_t)n * c->q_lora * sizeof(float), "q latent");
float *q = xmalloc((size_t)n * nh * hd * sizeof(float), "queries");
float *kv = xmalloc((size_t)n * hd * sizeof(float), "kv");
- float *scratch = xmalloc((size_t)(nh * hd > dim ? nh * hd : dim) * sizeof(float), "scratch");
+ float *scratch = xmalloc((size_t)n * c->q_lora * sizeof(float), "q projection");
+ float *kv_raw = xmalloc((size_t)n * hd * sizeof(float), "kv projection");
+ /* One pass of each matrix for the whole block of positions, rather than one
+ * pass per position: wq_b alone is 42 MB, and during prefill this is the
+ * difference between reading it once and reading it once per prompt token. */
+ mv8_rows(scratch, c->q_lora, &l->wq_a, x, dim, n);
+ for (int t = 0; t < n; t++)
+ rms_into(qr + (size_t)t * c->q_lora, scratch + (size_t)t * c->q_lora,
+ l->q_norm.w, c->q_lora, c->norm_eps);
+ mv8_rows(q, nh * hd, &l->wq_b, qr, c->q_lora, n);
+ mv8_rows(kv_raw, hd, &l->wkv, x, dim, n);
for (int t = 0; t < n; t++) {
- mv8(scratch, &l->wq_a, x + (size_t)t * dim);
- rms_into(qr + (size_t)t * c->q_lora, scratch, l->q_norm.w, c->q_lora, c->norm_eps);
- mv8(q + (size_t)t * nh * hd, &l->wq_b, qr + (size_t)t * c->q_lora);
for (int h = 0; h < nh; h++)
rope_apply(q + (size_t)t * nh * hd + (size_t)h * hd + hd - rd, rope, start_pos + t, rd, 0);
- mv8(scratch, &l->wkv, x + (size_t)t * dim);
- rms_into(kv + (size_t)t * hd, scratch, l->kv_norm.w, hd, c->norm_eps);
+ rms_into(kv + (size_t)t * hd, kv_raw + (size_t)t * hd, l->kv_norm.w, hd, c->norm_eps);
rope_apply(kv + (size_t)t * hd + hd - rd, rope, start_pos + t, rd, 0);
}
@@ -1615,7 +1671,9 @@ static void attention_run(Model *m, int layer, const float *x, int n, int start_
}
const float *kv_read = ratio ? kv_all : window_kv;
- float *heads = xmalloc((size_t)nh * hd * sizeof(float), "attention output");
+ /* 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");
+ 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++) {
if (start_pos > 0) {
@@ -1635,15 +1693,20 @@ 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;
#pragma omp parallel for schedule(static)
for (int h = 0; h < nh; h++)
- sparse_attend(heads + (size_t)h * hd, q + (size_t)t * nh * hd + (size_t)h * hd,
- kv_read, row, total_idx, nh, hd, l->attn_sink.w[h], attn_scale);
+ sparse_attend(row_heads + (size_t)h * hd, q + (size_t)t * nh * hd + (size_t)h * hd,
+ kv_read, row, total_idx, nh, hd, l->attn_sink.w[h], attn_scale,
+ head_score + (size_t)h * total_idx);
for (int h = 0; h < nh; h++)
- rope_apply(heads + (size_t)h * hd + hd - rd, rope, start_pos + t, rd, 1);
- attn_project_out(m, l, heads, out + (size_t)t * dim);
+ 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 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);
}
@@ -2171,30 +2234,34 @@ static void spec_attention(Model *m, int stage, const float *x, int block, int s
int dim = c->dim, hd = c->head_dim, nh = c->n_heads, rd = c->rope_dim, win = c->window;
const float *rope = m->rope_window; /* a stage never compresses */
double started = now_s();
- float *scratch = xmalloc((size_t)(nh * hd > dim ? nh * hd : dim) * sizeof(float), "spec scratch");
+ int scratch_rows = main_rows > block ? main_rows : block;
+ float *scratch = xmalloc((size_t)scratch_rows * (c->q_lora > hd ? c->q_lora : hd)
+ * sizeof(float), "spec scratch");
+ mv8_rows(scratch, hd, &l->wkv, main_x, dim, main_rows);
for (int t = 0; t < main_rows; t++) {
int pos = start_pos + t;
float *slot = l->window + (size_t)(pos % win) * hd;
- mv8(scratch, &l->wkv, main_x + (size_t)t * dim);
- rms_into(slot, scratch, l->kv_norm.w, hd, c->norm_eps);
+ rms_into(slot, scratch + (size_t)t * hd, l->kv_norm.w, hd, c->norm_eps);
rope_apply(slot + hd - rd, rope, pos, rd, 0);
l->window_pos[pos % win] = pos;
}
if (!out) { free(scratch); m->t_attn += now_s() - started; return; }
- float *qr = xmalloc((size_t)c->q_lora * sizeof(float), "spec q latent");
+ float *qr = xmalloc((size_t)block * c->q_lora * sizeof(float), "spec q latent");
float *q = xmalloc((size_t)block * nh * hd * sizeof(float), "spec queries");
float *kv = xmalloc((size_t)block * hd * sizeof(float), "spec kv");
+ mv8_rows(scratch, c->q_lora, &l->wq_a, x, dim, block);
+ for (int i = 0; i < block; i++)
+ rms_into(qr + (size_t)i * c->q_lora, scratch + (size_t)i * c->q_lora,
+ l->q_norm.w, c->q_lora, c->norm_eps);
+ mv8_rows(q, nh * hd, &l->wq_b, qr, c->q_lora, block);
+ mv8_rows(scratch, hd, &l->wkv, x, dim, block);
for (int i = 0; i < block; i++) {
int pos = start_pos + main_rows + i;
- mv8(scratch, &l->wq_a, x + (size_t)i * dim);
- rms_into(qr, scratch, l->q_norm.w, c->q_lora, c->norm_eps);
- mv8(q + (size_t)i * nh * hd, &l->wq_b, qr);
for (int h = 0; h < nh; h++)
rope_apply(q + (size_t)i * nh * hd + (size_t)h * hd + hd - rd, rope, pos, rd, 0);
- mv8(scratch, &l->wkv, x + (size_t)i * dim);
- rms_into(kv + (size_t)i * hd, scratch, l->kv_norm.w, hd, c->norm_eps);
+ rms_into(kv + (size_t)i * hd, scratch + (size_t)i * hd, l->kv_norm.w, hd, c->norm_eps);
rope_apply(kv + (size_t)i * hd + hd - rd, rope, pos, rd, 0);
}
@@ -2208,19 +2275,23 @@ static void spec_attention(Model *m, int stage, const float *x, int block, int s
memcpy(kv_all, l->window, (size_t)win * hd * sizeof(float));
memcpy(kv_all + (size_t)win * hd, kv, (size_t)block * hd * sizeof(float));
- float *heads = xmalloc((size_t)nh * hd * sizeof(float), "spec heads");
+ float *heads = xmalloc((size_t)block * nh * hd * sizeof(float), "spec heads");
+ float *head_score = xmalloc((size_t)nh * total * sizeof(float), "spec scores");
float scale = 1.0f / sqrtf((float)hd);
for (int i = 0; i < block; i++) {
int pos = start_pos + main_rows + i;
+ float *row_heads = heads + (size_t)i * nh * hd;
#pragma omp parallel for schedule(static)
for (int h = 0; h < nh; h++)
- sparse_attend(heads + (size_t)h * hd, q + (size_t)i * nh * hd + (size_t)h * hd,
- kv_all, idx, total, nh, hd, l->attn_sink.w[h], scale);
+ sparse_attend(row_heads + (size_t)h * hd, q + (size_t)i * nh * hd + (size_t)h * hd,
+ kv_all, idx, total, nh, hd, l->attn_sink.w[h], scale,
+ head_score + (size_t)h * total);
for (int h = 0; h < nh; h++)
- rope_apply(heads + (size_t)h * hd + hd - rd, rope, pos, rd, 1);
- attn_project_out(m, l, heads, out + (size_t)i * dim);
+ rope_apply(row_heads + (size_t)h * hd + hd - rd, rope, pos, rd, 1);
}
+ attn_project_out_rows(m, l, heads, block, out);
m->t_attn += now_s() - started;
+ free(head_score);
free(heads); free(kv_all); free(idx); free(kv); free(q); free(qr); free(scratch);
}
diff --git a/c/sparse_attn.h b/c/sparse_attn.h
new file mode 100644
index 00000000..427c2a5e
--- /dev/null
+++ b/c/sparse_attn.h
@@ -0,0 +1,122 @@
+/* sparse_attn.h -- the kernel DeepSeek V4.1 Flash reads attention with.
+ *
+ * One query head against a LIST of positions rather than a prefix: the window's
+ * ring slots plus whatever the DSA indexer kept, concatenated. A slot the query
+ * cannot reach is -1 and scores -INFINITY, which is why the list is passed
+ * instead of a length. The model is MQA, so every head reads the same kv rows;
+ * `kv` is one row per position, `hd` wide.
+ *
+ * Two passes over those rows, both of them FMA loops: scores, then the weighted
+ * sum. Four accumulators in the dot because a single dependent FMA chain runs at
+ * the latency of the unit and not its throughput, and the rows are 512 wide on
+ * the released checkpoint -- long enough for the difference to be the whole cost.
+ *
+ * Numerics: the vector path folds the dot into four partial sums where the
+ * scalar path keeps one, and its multiply-adds are fused where the scalar ones
+ * round twice. So the two agree to fp32 accumulation and not bit for bit -- the
+ * same relationship mv8 already has with its own scalar fallback. Nothing else
+ * differs: the exponentials, the sink and the final division are the same
+ * operations in the same order, and the division stays a division so that it
+ * rounds the way the scalar one does. tests/test_sparse_attn.c holds the two
+ * against each other across widths, including the ones with a tail.
+ *
+ * Header-only, all static, no dependency beyond immintrin -- the quant.h and
+ * fused_simd.h convention.
+ */
+#ifndef COLI_SPARSE_ATTN_H
+#define COLI_SPARSE_ATTN_H
+
+#include <math.h>
+#include <stddef.h>
+
+#if defined(__AVX2__)
+#include <immintrin.h>
+
+/* named apart from quant.h's hsum256: this header stands alone */
+static inline float coli_sa_hsum(__m256 v) {
+ __m128 low = _mm256_castps256_ps128(v);
+ __m128 high = _mm256_extractf128_ps(v, 1);
+ low = _mm_add_ps(low, high);
+ low = _mm_add_ps(low, _mm_movehl_ps(low, low));
+ low = _mm_add_ss(low, _mm_shuffle_ps(low, low, 0x55));
+ return _mm_cvtss_f32(low);
+}
+#endif
+
+/* The reading of the kernel, one multiply at a time. Kept because it is what the
+ * vector path is checked against, and because a machine without AVX2 runs it. */
+static inline void coli_sparse_attend_scalar(float *out, const float *q, const float *kv,
+ const int *idx, int count, int hd,
+ float sink, float scale, float *score) {
+ float best = -1e30f;
+ for (int j = 0; j < count; j++) {
+ if (idx[j] < 0) { score[j] = -INFINITY; continue; }
+ const float *k = kv + (size_t)idx[j] * hd;
+ float dot = 0.0f;
+ for (int i = 0; i < hd; i++) dot += q[i] * k[i];
+ score[j] = dot * scale;
+ if (score[j] > best) best = score[j];
+ }
+ float denom = expf(sink - best);
+ for (int i = 0; i < hd; i++) out[i] = 0.0f;
+ for (int j = 0; j < count; j++) {
+ if (!(score[j] > -INFINITY)) continue;
+ float weight = expf(score[j] - best);
+ denom += weight;
+ const float *k = kv + (size_t)idx[j] * hd;
+ for (int i = 0; i < hd; i++) out[i] += weight * k[i];
+ }
+ for (int i = 0; i < hd; i++) out[i] /= denom;
+}
+
+static inline void coli_sparse_attend(float *out, const float *q, const float *kv,
+ const int *idx, int count, int hd,
+ float sink, float scale, float *score) {
+#if !defined(__AVX2__)
+ coli_sparse_attend_scalar(out, q, kv, idx, count, hd, sink, scale, score);
+#else
+ float best = -1e30f;
+ for (int j = 0; j < count; j++) {
+ if (idx[j] < 0) { score[j] = -INFINITY; continue; }
+ const float *k = kv + (size_t)idx[j] * hd;
+ __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps();
+ __m256 a2 = _mm256_setzero_ps(), a3 = _mm256_setzero_ps();
+ int i = 0;
+ for (; i + 32 <= hd; i += 32) {
+ a0 = _mm256_fmadd_ps(_mm256_loadu_ps(q + i), _mm256_loadu_ps(k + i), a0);
+ a1 = _mm256_fmadd_ps(_mm256_loadu_ps(q + i + 8), _mm256_loadu_ps(k + i + 8), a1);
+ a2 = _mm256_fmadd_ps(_mm256_loadu_ps(q + i + 16), _mm256_loadu_ps(k + i + 16), a2);
+ a3 = _mm256_fmadd_ps(_mm256_loadu_ps(q + i + 24), _mm256_loadu_ps(k + i + 24), a3);
+ }
+ for (; i + 8 <= hd; i += 8)
+ a0 = _mm256_fmadd_ps(_mm256_loadu_ps(q + i), _mm256_loadu_ps(k + i), a0);
+ float dot = coli_sa_hsum(_mm256_add_ps(_mm256_add_ps(a0, a1), _mm256_add_ps(a2, a3)));
+ for (; i < hd; i++) dot += q[i] * k[i];
+ score[j] = dot * scale;
+ if (score[j] > best) best = score[j];
+ }
+ float denom = expf(sink - best);
+ int i = 0;
+ for (; i + 8 <= hd; i += 8) _mm256_storeu_ps(out + i, _mm256_setzero_ps());
+ for (; i < hd; i++) out[i] = 0.0f;
+ for (int j = 0; j < count; j++) {
+ if (!(score[j] > -INFINITY)) continue;
+ float weight = expf(score[j] - best);
+ denom += weight;
+ const float *k = kv + (size_t)idx[j] * hd;
+ __m256 w = _mm256_set1_ps(weight);
+ i = 0;
+ for (; i + 8 <= hd; i += 8)
+ _mm256_storeu_ps(out + i, _mm256_fmadd_ps(w, _mm256_loadu_ps(k + i),
+ _mm256_loadu_ps(out + i)));
+ for (; i < hd; i++) out[i] += weight * k[i];
+ }
+ __m256 d = _mm256_set1_ps(denom);
+ i = 0;
+ for (; i + 8 <= hd; i += 8)
+ _mm256_storeu_ps(out + i, _mm256_div_ps(_mm256_loadu_ps(out + i), d));
+ for (; i < hd; i++) out[i] /= denom;
+#endif
+}
+
+#endif /* COLI_SPARSE_ATTN_H */
diff --git a/c/tests/test_sparse_attn.c b/c/tests/test_sparse_attn.c
new file mode 100644
index 00000000..c6ceb5fb
--- /dev/null
+++ b/c/tests/test_sparse_attn.c
@@ -0,0 +1,105 @@
+/* The vector attention kernel against the scalar reading of it.
+ *
+ * They are not bit-identical and are not meant to be: the vector path folds the
+ * dot into four partial sums and fuses its multiply-adds. What is checked is that
+ * the difference stays at fp32 accumulation noise across every shape the engine
+ * can hand the kernel -- widths with and without a tail, lists that are entirely
+ * unreachable, a single position, and a sink large enough to hold the softmax
+ * down on its own.
+ */
+#include "../sparse_attn.h"
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+static uint64_t seed = 0x9E3779B97F4A7C15ull;
+static float next_float(void) {
+ seed ^= seed << 13; seed ^= seed >> 7; seed ^= seed << 17;
+ return (float)((double)(seed >> 11) / 9007199254740992.0) * 2.0f - 1.0f;
+}
+
+/* Relative to the magnitude of the row the kernel sums, not to 1: the outputs are
+ * a convex combination of kv rows, so that is the scale the error lives on. */
+static int agrees(const float *a, const float *b, int n, float tolerance, const char *what) {
+ float worst = 0.0f;
+ for (int i = 0; i < n; i++) {
+ float delta = fabsf(a[i] - b[i]);
+ if (delta > worst) worst = delta;
+ }
+ if (!(worst <= tolerance)) {
+ fprintf(stderr, "%s: worst |delta| %.3e exceeds %.3e\n", what, worst, tolerance);
+ return 0;
+ }
+ return 1;
+}
+
+static int one_case(int hd, int count, int rows, int holes, float sink, const char *what) {
+ float *q = malloc((size_t)hd * sizeof(float));
+ float *kv = malloc((size_t)rows * hd * sizeof(float));
+ int *idx = malloc((size_t)count * sizeof(int));
+ float *score_a = malloc((size_t)count * sizeof(float));
+ float *score_b = malloc((size_t)count * sizeof(float));
+ float *out_a = malloc((size_t)hd * sizeof(float));
+ float *out_b = malloc((size_t)hd * sizeof(float));
+ if (!q || !kv || !idx || !score_a || !score_b || !out_a || !out_b) {
+ fprintf(stderr, "out of memory\n"); exit(1); }
+
+ for (int i = 0; i < hd; i++) q[i] = next_float();
+ for (int i = 0; i < rows * hd; i++) kv[i] = next_float();
+ for (int j = 0; j < count; j++)
+ idx[j] = (holes && (j % holes) == 0) ? -1 : (int)(seed % (uint64_t)rows);
+ /* the scale the engine uses */
+ float scale = 1.0f / sqrtf((float)hd);
+
+ coli_sparse_attend_scalar(out_a, q, kv, idx, count, hd, sink, scale, score_a);
+ coli_sparse_attend(out_b, q, kv, idx, count, hd, sink, scale, score_b);
+
+ int ok = agrees(out_a, out_b, hd, 1e-5f, what);
+ free(q); free(kv); free(idx); free(score_a); free(score_b); free(out_a); free(out_b);
+ return ok;
+}
+
+/* A list with nothing reachable in it: every score is -INFINITY, the sink is the
+ * whole denominator, and the output must be exactly zero -- not a NaN, which is
+ * what a denominator of zero would give. */
+static int all_holes(void) {
+ enum { HD = 64, COUNT = 12 };
+ float q[HD], kv[HD], out[HD], score[COUNT];
+ int idx[COUNT];
+ for (int i = 0; i < HD; i++) { q[i] = next_float(); kv[i] = next_float(); }
+ for (int j = 0; j < COUNT; j++) idx[j] = -1;
+ coli_sparse_attend(out, q, kv, idx, COUNT, HD, 0.0f, 1.0f, score);
+ for (int i = 0; i < HD; i++)
+ if (out[i] != 0.0f) {
+ fprintf(stderr, "unreachable list: out[%d] is %g, expected 0\n", i, out[i]);
+ return 0;
+ }
+ return 1;
+}
+
+int main(void) {
+ struct { int hd, count, rows, holes; float sink; const char *what; } cases[] = {
+ { 512, 640, 700, 0, 0.0f, "released shape (hd 512, window + index_topk)" },
+ { 512, 640, 700, 5, 0.0f, "released shape with unreachable slots" },
+ { 64, 128, 130, 0, 0.0f, "fixture shape (hd 64)" },
+ { 32, 33, 40, 0, 0.0f, "hd 32, one vector block, no tail" },
+ { 40, 17, 20, 3, 0.0f, "hd 40, an 8-wide block past the 32-wide loop" },
+ { 33, 9, 12, 0, 0.0f, "hd 33, scalar tail of one" },
+ { 7, 5, 6, 2, 0.0f, "hd 7, entirely scalar tail" },
+ { 1, 3, 4, 0, 0.0f, "hd 1" },
+ { 512, 1, 2, 0, 0.0f, "a single reachable position" },
+ { 64, 48, 50, 0, 30.0f, "a sink that dominates the softmax" },
+ { 64, 48, 50, 0, -30.0f, "a sink that does not reach it" },
+ };
+ int failures = 0;
+ for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
+ if (!one_case(cases[i].hd, cases[i].count, cases[i].rows, cases[i].holes,
+ cases[i].sink, cases[i].what)) failures++;
+ if (!all_holes()) failures++;
+ if (failures) { fprintf(stderr, "sparse attention: %d case(s) failed\n", failures); return 1; }
+ printf("sparse attention: vector and scalar agree on %zu shapes, and an "
+ "unreachable list gives zero\n", sizeof(cases) / sizeof(cases[0]) + 1);
+ return 0;
+}