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(quant): compute four output rows per pass in matmul_fp8

Captain6be81540d927
diff --git a/c/quant.h b/c/quant.h
index f53ad436..5a552ca3 100644
--- a/c/quant.h
+++ b/c/quant.h
@@ -558,21 +558,44 @@ static inline int64_t fp8_nblk(int n){ return ((int64_t)n + FP8_BLOCK - 1) / FP8
static void matmul_fp8(float *y, const float *x, const uint8_t *q8, const float *bscale,
int S, int I, int O){
int64_t nblkI = fp8_nblk(I);
+ /* Four output rows per pass, each with its own accumulator. Every row's
+ addition sequence is identical to the one-row form - same operands, same
+ column order - so results are bit-identical; only the interleaving of four
+ independent dependency chains differs. The gain is latency hiding plus one
+ load of xs[i] feeding four rows. A tail of fewer than four rows clamps the
+ spare indices onto the last valid row and the stores are guarded, which
+ avoids a separate remainder loop. */
#pragma omp parallel for schedule(static)
- for(int o=0;o<O;o++){
- const uint8_t *w = q8 + (int64_t)o*I;
- int64_t blkO = o / FP8_BLOCK;
- const float *scl = bscale + blkO*nblkI;
+ for(int o=0;o<O;o+=4){
+ int o1=o+1<O ? o+1 : o, o2=o+2<O ? o+2 : o, o3=o+3<O ? o+3 : o;
+ const uint8_t *w0=q8+(int64_t)o*I, *w1=q8+(int64_t)o1*I;
+ const uint8_t *w2=q8+(int64_t)o2*I, *w3=q8+(int64_t)o3*I;
+ const float *scl0=bscale+((int64_t)o /FP8_BLOCK)*nblkI;
+ const float *scl1=bscale+((int64_t)o1/FP8_BLOCK)*nblkI;
+ const float *scl2=bscale+((int64_t)o2/FP8_BLOCK)*nblkI;
+ const float *scl3=bscale+((int64_t)o3/FP8_BLOCK)*nblkI;
for(int s=0;s<S;s++){
const float *xs = x + (int64_t)s*I;
- double a=0;
+ double a0=0,a1=0,a2=0,a3=0;
for(int64_t bi=0; bi*FP8_BLOCK<I; bi++){
int base=(int)(bi*FP8_BLOCK); int blen=FP8_BLOCK; if(base+blen>I) blen=I-base;
- float sc=scl[bi]; float acc=0;
- for(int i=base;i<base+blen;i++) acc += e4m3_decode(w[i])*xs[i];
- a += (double)acc*sc;
+ float acc0=0,acc1=0,acc2=0,acc3=0;
+ for(int i=base;i<base+blen;i++){
+ float xv=xs[i];
+ acc0 += e4m3_decode(w0[i])*xv;
+ acc1 += e4m3_decode(w1[i])*xv;
+ acc2 += e4m3_decode(w2[i])*xv;
+ acc3 += e4m3_decode(w3[i])*xv;
+ }
+ a0 += (double)acc0*scl0[bi];
+ a1 += (double)acc1*scl1[bi];
+ a2 += (double)acc2*scl2[bi];
+ a3 += (double)acc3*scl3[bi];
}
- y[(int64_t)s*O+o]=(float)a;
+ y[(int64_t)s*O+o]=(float)a0;
+ if(o1!=o) y[(int64_t)s*O+o1]=(float)a1;
+ if(o2!=o) y[(int64_t)s*O+o2]=(float)a2;
+ if(o3!=o) y[(int64_t)s*O+o3]=(float)a3;
}
}
}