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: SIMD MLA-absorb score + value-mix reductions (#442)

woolcoxmd469c54ade3c
diff --git a/c/Makefile b/c/Makefile
index 06fe176e..77a69255 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -371,6 +371,11 @@ tests/bench_dsa_select$(EXE): tests/bench_dsa_select.c colibri.c st.h uring.h js
tests/bench_idot$(EXE): tests/bench_idot.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+# bench_mla_simd: microbenchmark (scalar vs AVX2/NEON MLA-absorb reductions, #442),
+# NOT a test gate. Build on demand: make tests/bench_mla_simd
+tests/bench_mla_simd$(EXE): tests/bench_mla_simd.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
tests/test_uring$(EXE): tests/test_uring.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
diff --git a/c/colibri.c b/c/colibri.c
index 8878ae3a..be865ca5 100644
--- a/c/colibri.c
+++ b/c/colibri.c
@@ -2456,7 +2456,26 @@ static void attention_rows(Model *m, Layer *l, int layer, float *x, int S, int p
for(int jj=0;jj<nt;jj++){ int t = tlist ? tlist[jj] : st0+jj;
const float *Lt=coli_kv_row(ks->Lc[layer],t,kvl);
const float *kr=coli_kv_row(ks->Rc[layer],t,c->qk_rope);
- float a=0; for(int i=0;i<kvl;i++) a+=qabs[i]*Lt[i];
+ /* MLA-absorb score: dot(qabs, Lt) + dot(qr, kr). #442: the qabs·Lt
+ * reduction is the hot f32 dot at this site (kvl=512 on GLM-5.2,
+ * runs nt times per (s,h), grows with context). SIMD-ify under
+ * AVX2 (8-lane fmadd + hsum256, same shape as matmul_q in quant.h)
+ * and NEON, with a scalar tail for the remainder. Reassociation
+ * is accepted here — softmax downstream softens the rounding flip. */
+ float a=0; int i=0;
+#if defined(__AVX2__)
+ __m256 acc=_mm256_setzero_ps();
+ for(;i+8<=kvl;i+=8)
+ acc=_mm256_fmadd_ps(_mm256_loadu_ps(qabs+i), _mm256_loadu_ps(Lt+i), acc);
+ a=hsum256(acc);
+#elif defined(__ARM_NEON)
+ float32x4_t ac0=vdupq_n_f32(0), ac1=vdupq_n_f32(0);
+ for(;i+8<=kvl;i+=8){
+ ac0=vfmaq_f32(ac0, vld1q_f32(qabs+i), vld1q_f32(Lt+i));
+ ac1=vfmaq_f32(ac1, vld1q_f32(qabs+i+4), vld1q_f32(Lt+i+4)); }
+ a=vaddvq_f32(vaddq_f32(ac0,ac1));
+#endif
+ for(;i<kvl;i++) a+=qabs[i]*Lt[i];
for(int d=0;d<c->qk_rope;d++) a+=qr[d]*kr[d];
sc[jj]=a*c->attn_scale;
}
@@ -2464,7 +2483,25 @@ static void attention_rows(Model *m, Layer *l, int layer, float *x, int S, int p
float clat[512]; memset(clat,0,kvl*sizeof(float));
for(int jj=0;jj<nt;jj++){ int t = tlist ? tlist[jj] : st0+jj;
const float *Lt=coli_kv_row(ks->Lc[layer],t,kvl);
- float a=sc[jj]; for(int i=0;i<kvl;i++) clat[i]+=a*Lt[i]; }
+ /* MLA-absorb value mix: clat += sc[jj] * Lt (AXPY over kvl).
+ * #442: SIMD-ified — each lane writes back independently so there
+ * is no reassociation here (strictly bit-identical to scalar). */
+ float a=sc[jj]; int i=0;
+#if defined(__AVX2__)
+ __m256 va=_mm256_set1_ps(a);
+ for(;i+8<=kvl;i+=8){
+ __m256 cl=_mm256_loadu_ps(clat+i), lt=_mm256_loadu_ps(Lt+i);
+ _mm256_storeu_ps(clat+i, _mm256_fmadd_ps(va, lt, cl));
+ }
+#elif defined(__ARM_NEON)
+ float32x4_t va=vdupq_n_f32(a);
+ for(;i+8<=kvl;i+=8){
+ vst1q_f32(clat+i, vfmaq_f32(vld1q_f32(clat+i), va, vld1q_f32(Lt+i)));
+ vst1q_f32(clat+i+4, vfmaq_f32(vld1q_f32(clat+i+4), va, vld1q_f32(Lt+i+4)));
+ }
+#endif
+ for(;i<kvl;i++) clat[i]+=a*Lt[i];
+ }
qt_matvec_rows(&l->kv_b, rbase+r0v, vh, clat, ctx+((int64_t)s*H+h)*vh);
}
}
diff --git a/c/tests/bench_mla_simd.c b/c/tests/bench_mla_simd.c
new file mode 100644
index 00000000..6a7d1774
--- /dev/null
+++ b/c/tests/bench_mla_simd.c
@@ -0,0 +1,126 @@
+/* Microbenchmark: scalar vs SIMD MLA-absorb reductions (the #442 change).
+ *
+ * NOT a unit test — correctness is gated by the glm_tiny TF oracle (SNAP=./glm_tiny
+ * TF=1, expected 30/32 unchanged). This measures the headline claim: SIMD-ifying
+ * the f32 score dot (qabs·Lt, kvl=512 on GLM-5.2) and the value-mix AXPY lifts
+ * the per-(s,h) attention reduction off the scalar floor, the same shape of win
+ * the int8 matmul_q kernel (quant.h:91) and the AVX-VNNI idot kernels (#264)
+ * already took.
+ *
+ * Re-implements the OLD scalar reductions inline and calls the REAL attention
+ * path via the include-colibri.c pattern — but since the reductions live inline
+ * in the MLA attention block (not a callable), this bench re-implements both
+ * the OLD (scalar) and NEW (SIMD) reductions verbatim from the colibri.c source
+ * and times them back-to-back at GLM-5.2 dimensions (kvl=512, realistic nt).
+ *
+ * Run: make tests/bench_mla_simd && ./tests/bench_mla_simd (not a gate)
+ */
+#define main coli_glm_main_unused
+#include "../colibri.c"
+#undef main
+#include <stdint.h>
+#include <string.h>
+
+static uint32_t rs=0x2545F491u;
+static uint32_t xr(void){ rs^=rs<<13; rs^=rs>>17; rs^=rs<<5; return rs; }
+
+/* ---- OLD: verbatim scalar reductions (pre-#442 colibri.c:2459, 2467) ---- */
+static void score_scalar(const float *qabs, const float *Lt, int kvl, float *out){
+ float a=0;
+ for(int i=0;i<kvl;i++) a+=qabs[i]*Lt[i];
+ *out=a;
+}
+static void vmix_scalar(float *clat, const float *Lt, int kvl, float a){
+ for(int i=0;i<kvl;i++) clat[i]+=a*Lt[i];
+}
+
+/* ---- NEW: verbatim SIMD reductions (post-#442 colibri.c, AVX2 path) ---- */
+#if defined(__AVX2__)
+static void score_simd(const float *qabs, const float *Lt, int kvl, float *out){
+ float a=0; int i=0;
+ __m256 acc=_mm256_setzero_ps();
+ for(;i+8<=kvl;i+=8)
+ acc=_mm256_fmadd_ps(_mm256_loadu_ps(qabs+i), _mm256_loadu_ps(Lt+i), acc);
+ a=hsum256(acc);
+ for(;i<kvl;i++) a+=qabs[i]*Lt[i];
+ *out=a;
+}
+static void vmix_simd(float *clat, const float *Lt, int kvl, float a){
+ int i=0;
+ __m256 va=_mm256_set1_ps(a);
+ for(;i+8<=kvl;i+=8){
+ __m256 cl=_mm256_loadu_ps(clat+i), lt=_mm256_loadu_ps(Lt+i);
+ _mm256_storeu_ps(clat+i, _mm256_fmadd_ps(va, lt, cl));
+ }
+ for(;i<kvl;i++) clat[i]+=a*Lt[i];
+}
+#else
+/* non-AVX2 build: SIMD path == scalar, bench will show parity */
+#define score_simd score_scalar
+#define vmix_simd vmix_scalar
+#endif
+
+/* now_s() comes from colibri.c (included above) */
+
+int main(void){
+ /* GLM-5.2 dims: kvl=512, and nt (context tokens per head) at a realistic
+ * mid-length decode. The score loop runs nt times per (s,h); we time the
+ * full inner nt-loop to mirror the real attention block. */
+ int kvl=512, nt=256, H=128; /* H heads — the outer parallel dim */
+ float *qabs=malloc(kvl*sizeof(float));
+ float *Lt=malloc((size_t)nt*kvl*sizeof(float));
+ float *clat_old=malloc(kvl*sizeof(float));
+ float *clat_new=malloc(kvl*sizeof(float));
+ float *sc_old=malloc(nt*sizeof(float));
+ float *sc_new=malloc(nt*sizeof(float));
+
+ /* deterministic but non-trivial inputs — bounded floats in [-1,1] (avoids
+ * NaN/Inf from arbitrary bit patterns, which would make the correctness
+ * diff meaningless while still timing the same fp arithmetic). */
+ for(int i=0;i<kvl;i++){ qabs[i]=((float)(xr()&0xFFFF)/65535.0f)*2.0f-1.0f; }
+ for(size_t i=0;i<(size_t)nt*kvl;i++){ Lt[i]=((float)(xr()&0xFFFF)/65535.0f)*2.0f-1.0f; }
+
+ /* warmup + correctness sanity: SIMD and scalar agree within float tolerance */
+ memset(clat_old,0,kvl*sizeof(float));
+ memset(clat_new,0,kvl*sizeof(float));
+ for(int jj=0;jj<5;jj++){
+ score_scalar(qabs, Lt+(size_t)jj*kvl, kvl, &sc_old[jj]);
+ score_simd (qabs, Lt+(size_t)jj*kvl, kvl, &sc_new[jj]);
+ vmix_scalar(clat_old, Lt+(size_t)jj*kvl, kvl, 0.5f);
+ vmix_simd (clat_new, Lt+(size_t)jj*kvl, kvl, 0.5f);
+ }
+ float max_err=0; for(int i=0;i<kvl;i++){ float e=fabsf(clat_old[i]-clat_new[i]); if(e>max_err)max_err=e; }
+ printf("correctness: score max abs diff over 5 = %.3e, vmix max abs diff = %.3e\n",
+ fabsf(sc_old[0]-sc_new[0]), max_err);
+ printf("(score reassociates -> small diff expected; vmix is lane-wise -> ~0 diff)\n\n");
+
+ /* bench: H * (nt score calls + nt vmix calls), as in one attention forward */
+ int reps=200;
+ double t0=now_s();
+ for(int r=0;r<reps;r++){
+ for(int h=0;h<H;h++){
+ for(int jj=0;jj<nt;jj++) score_scalar(qabs, Lt+(size_t)jj*kvl, kvl, &sc_old[jj]);
+ for(int jj=0;jj<nt;jj++) vmix_scalar(clat_old, Lt+(size_t)jj*kvl, kvl, sc_old[jj]);
+ }
+ }
+ double t_old=now_s()-t0;
+
+ t0=now_s();
+ for(int r=0;r<reps;r++){
+ for(int h=0;h<H;h++){
+ for(int jj=0;jj<nt;jj++) score_simd(qabs, Lt+(size_t)jj*kvl, kvl, &sc_new[jj]);
+ for(int jj=0;jj<nt;jj++) vmix_simd(clat_new, Lt+(size_t)jj*kvl, kvl, sc_new[jj]);
+ }
+ }
+ double t_new=now_s()-t0;
+
+ double per_old = t_old/reps/H; /* per head */
+ double per_new = t_new/reps/H;
+ printf("MLA-absorb reductions (kvl=%d, nt=%d, H=%d, %d reps):\n", kvl,nt,H,reps);
+ printf(" scalar: %.3f us / head\n", per_old*1e6);
+ printf(" SIMD : %.3f us / head\n", per_new*1e6);
+ printf(" speedup: %.2fx (%.1f%% faster)\n", per_old/per_new, 100.0*(per_old-per_new)/per_old);
+
+ free(qabs); free(Lt); free(clat_old); free(clat_new); free(sc_old); free(sc_new);
+ return 0;
+}