Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
bench: fmt=8 old-vs-warp kernel bandwidth harness (JSON, fp8-bench target)
monotophica6fb69a553b0
diff --git a/c/Makefile b/c/Makefileindex c6a2e732..abcf2dd2 100644--- a/c/Makefile+++ b/c/Makefile@@ -723,6 +723,15 @@ cuda-bench: backend_cuda.cu backend_cuda.h backend_gpu_compat.h tests/bench_tens"$(GPUCC)" $(GPUFLAGS) backend_cuda.cu tests/bench_tensor_core.cu -o backend_cuda_bench$(EXE) $(ANS_NVCC_LIBS)./backend_cuda_bench$(EXE)+# fmt=8 kernel bench: old vs COLI_CUDA_F8_WARP kernels per decode path, census+# expert shapes, JSON on stdout (kernel time only). The build is a separate+# file target so run_f8_bench.sh (repo root) can keep stdout pure JSON.+fp8_bench$(EXE): backend_cuda.cu backend_cuda.h backend_gpu_compat.h tests/bench_fp8_cuda.cu+ @command -v "$(GPUCC)" >/dev/null 2>&1 || { echo "$(GPUCC_NAME) not found" >&2; exit 1; }+ "$(GPUCC)" $(GPUFLAGS) tests/bench_fp8_cuda.cu -o fp8_bench$(EXE) $(ANS_NVCC_LIBS)+fp8-bench: fp8_bench$(EXE)+ ./fp8_bench$(EXE)+# NOCUDA_*: olmoe.c has no COLI_CUDA code at all, so CUDA=1 would otherwise# hand it a define matching nothing and link a runtime it never calls -- a# build whose compile line, libraries and exit status all claim "CUDA" while@@ -1123,7 +1132,7 @@ bench: iobench$(EXE)@if [ -n "$(ARGS)" ]; then ./iobench$(EXE) $(ARGS); else echo "built iobench$(EXE) β run: ./iobench$(EXE) <file> <MB> <iters> <threads> <direct 0|1>"; fi.PHONY: all colibri glm deepseek-v4 deepseek-v4-clean deepseek-v4-oracle \deepseek-v4-tiny-generate deepseek-v4-tiny-check \- iq3 rans fuzz-rans cuda-test hip-test gpu-compile cuda-bench cuda-dll hip-dll portable \+ iq3 rans fuzz-rans cuda-test hip-test gpu-compile cuda-bench fp8-bench cuda-dll hip-dll portable \test-c test-python test check clean install uninstall bench# Own line: see the TEST_RULES note above on shared .PHONY lines.diff --git a/c/tests/bench_fp8_cuda.cu b/c/tests/bench_fp8_cuda.cunew file mode 100644index 00000000..865cac27--- /dev/null+++ b/c/tests/bench_fp8_cuda.cu@@ -0,0 +1,152 @@+/* fmt=8 kernel bench: the original per-(o,s) kernels vs the COLI_CUDA_F8_WARP+ * rework (shared-LUT decode and, where the toolchain compiles it, the+ * cuda_fp8.h cvt decode), on the census expert shapes grouped as 8 experts x+ * S rows, S in {1,4,8,32}. Kernel time only (events around the launches, no+ * H2D/D2H): the question is achieved weight bandwidth against the device+ * roofline. GB/s is weight GOODPUT β unique weight bytes per launch over+ * time β so a kernel that re-reads weights per row shows the drop instead of+ * hiding it. One JSON object on stdout; progress and errors on stderr.+ *+ * Build: make fp8-bench CUDA=1 (or: nvcc -O2 -std=c++17 -arch=native+ * tests/bench_fp8_cuda.cu -o fp8_bench)+ * The GB10/remote wrapper is run_f8_bench.sh at the repo root.+ */+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <cmath>+#include <cstdint>+#include <cuda_runtime.h>++#include "../backend_cuda.cu"++static float e4m3_ref(uint8_t b){+ int s=b>>7, e=(b>>3)&15, m=b&7;+ if(e==15&&m==7) return NAN;+ float v = e ? ldexpf(1.f+m/8.f, e-7) : ldexpf(m/8.f, -6);+ return s ? -v : v;+}+static uint8_t rnd_e4m3(void){+ uint8_t b=(uint8_t)(rand()&255);+ if((b&0x7F)==0x7F) b&=(uint8_t)~1;+ return b;+}++#define CK(x) do{ if((x)!=cudaSuccess){ \+ fprintf(stderr,"[bench] %s failed: %s\n",#x,cudaGetErrorString(cudaGetLastError())); exit(1);} }while(0)++static const int E=8, D=2048, I=6144; /* census: 8 experts, GLM-5.2 dims */++typedef void (*launch_fn)(GroupDesc*,float*,float*,float*,float*,int);++/* Each variant launches ONE kernel (hidden dual or down) so the two matrix+ * orientations are timed separately. */+static void old_hidden(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)y; grouped_hidden_f8_dual<<<dim3((unsigned)I,(unsigned)S,(unsigned)E),256>>>(g,u,x,d,I,D);+}+static void old_down(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)u;(void)x; grouped_down_f8<<<dim3((unsigned)D,(unsigned)S,(unsigned)E),256>>>(y,g,d,D,I);+}+static void warp_hidden(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)y;(void)S; grouped_hidden_f8w_dual<0><<<dim3((unsigned)((I+7)/8),(unsigned)E),256>>>(g,u,x,d,I,D);+}+static void warp_down(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)u;(void)x;(void)S; grouped_down_f8w<0><<<dim3((unsigned)((D+7)/8),(unsigned)E),256>>>(y,g,d,D,I);+}+#if COLI_F8_HWCVT+static void cvt_hidden(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)y;(void)S; grouped_hidden_f8w_dual<1><<<dim3((unsigned)((I+7)/8),(unsigned)E),256>>>(g,u,x,d,I,D);+}+static void cvt_down(GroupDesc *d,float *g,float *u,float *x,float *y,int S){+ (void)u;(void)x;(void)S; grouped_down_f8w<1><<<dim3((unsigned)((D+7)/8),(unsigned)E),256>>>(y,g,d,D,I);+}+#endif++int main(void){+ srand(5);+ cudaDeviceProp prop; CK(cudaGetDeviceProperties(&prop,0));+ double peak = prop.memoryClockRate>0+ ? 2.0*prop.memoryClockRate*1e3*(prop.memoryBusWidth/8.0)/1e9 : 0.0;+ fprintf(stderr,"[bench] %s sm_%d%d, reported peak %.1f GB/s\n",+ prop.name,prop.major,prop.minor,peak);++ float lut[256]; for(int i=0;i<256;i++) lut[i]=e4m3_ref((uint8_t)i);+ CK(cudaMemcpyToSymbol(c_e4m3,lut,sizeof(lut)));++ /* device weights + scales, 8 experts */+ const int nbD=D/128,nbI=I/128;+ GroupDesc host[E]; void *dg[E],*du[E],*dd[E],*dgs[E],*dus[E],*dds[E];+ {+ size_t hb=(size_t)I*D, db=(size_t)D*I;+ uint8_t *tmp=(uint8_t*)malloc(hb>db?hb:db);+ float *stmp=(float*)malloc((size_t)nbI*nbD*4);+ for(int c=0;c<E;c++){+ CK(cudaMalloc(&dg[c],hb)); CK(cudaMalloc(&du[c],hb)); CK(cudaMalloc(&dd[c],db));+ CK(cudaMalloc(&dgs[c],(size_t)nbI*nbD*4)); CK(cudaMalloc(&dus[c],(size_t)nbI*nbD*4));+ CK(cudaMalloc(&dds[c],(size_t)nbD*nbI*4));+ for(size_t i=0;i<hb;i++) tmp[i]=rnd_e4m3();+ CK(cudaMemcpy(dg[c],tmp,hb,cudaMemcpyHostToDevice));+ CK(cudaMemcpy(du[c],tmp,hb,cudaMemcpyHostToDevice));+ CK(cudaMemcpy(dd[c],tmp,db,cudaMemcpyHostToDevice));+ for(int i=0;i<nbI*nbD;i++) stmp[i]=ldexpf(1.f+rand()/(float)RAND_MAX,-12);+ CK(cudaMemcpy(dgs[c],stmp,(size_t)nbI*nbD*4,cudaMemcpyHostToDevice));+ CK(cudaMemcpy(dus[c],stmp,(size_t)nbI*nbD*4,cudaMemcpyHostToDevice));+ CK(cudaMemcpy(dds[c],stmp,(size_t)nbD*nbI*4,cudaMemcpyHostToDevice));+ }+ free(tmp); free(stmp);+ }+ const int SMAX=32;+ float *x,*gate,*up,*y; GroupDesc *ddesc;+ CK(cudaMalloc(&x,(size_t)E*SMAX*D*4)); CK(cudaMalloc(&y,(size_t)E*SMAX*D*4));+ CK(cudaMalloc(&gate,(size_t)E*SMAX*I*4)); CK(cudaMalloc(&up,(size_t)E*SMAX*I*4));+ CK(cudaMalloc(&ddesc,sizeof(host)));+ {+ float *hx=(float*)malloc((size_t)E*SMAX*D*4);+ for(size_t i=0;i<(size_t)E*SMAX*D;i++) hx[i]=(rand()/(float)RAND_MAX-.5f)*2.f;+ CK(cudaMemcpy(x,hx,(size_t)E*SMAX*D*4,cudaMemcpyHostToDevice)); free(hx);+ }++ struct { const char *path,*kernel; launch_fn fn; double bytes; } cfg[]={+ {"old", "hidden_dual", old_hidden, 2.0*I*D*E},+ {"old", "down", old_down, 1.0*D*I*E},+ {"warp-lut","hidden_dual", warp_hidden, 2.0*I*D*E},+ {"warp-lut","down", warp_down, 1.0*D*I*E},+#if COLI_F8_HWCVT+ {"warp-cvt","hidden_dual", cvt_hidden, 2.0*I*D*E},+ {"warp-cvt","down", cvt_down, 1.0*D*I*E},+#endif+ };+ int svals[4]={1,4,8,32};+ printf("{\"device\":\"%s\",\"cc\":\"sm_%d%d\",\"peak_gbps\":%.1f,"+ "\"experts\":%d,\"D\":%d,\"I\":%d,\"results\":[",+ prop.name,prop.major,prop.minor,peak,E,D,I);+ int first=1;+ for(size_t k=0;k<sizeof(cfg)/sizeof(cfg[0]);k++){+ for(int si=0;si<4;si++){+ int S=svals[si];+ for(int c=0;c<E;c++)+ host[c]={dg[c],du[c],dd[c],(const float*)dgs[c],(const float*)dus[c],+ (const float*)dds[c],8,8,8,S,c*S,0,0,0};+ CK(cudaMemcpy(ddesc,host,sizeof(host),cudaMemcpyHostToDevice));+ for(int w=0;w<3;w++) cfg[k].fn(ddesc,gate,up,x,y,S);+ CK(cudaDeviceSynchronize()); CK(cudaGetLastError());+ cudaEvent_t e0,e1; CK(cudaEventCreate(&e0)); CK(cudaEventCreate(&e1));+ const int iters=20;+ CK(cudaEventRecord(e0));+ for(int it=0;it<iters;it++) cfg[k].fn(ddesc,gate,up,x,y,S);+ CK(cudaEventRecord(e1)); CK(cudaEventSynchronize(e1));+ float ms=0; CK(cudaEventElapsedTime(&ms,e0,e1)); ms/=iters;+ cudaEventDestroy(e0); cudaEventDestroy(e1);+ double gbps=cfg[k].bytes/(ms/1e3)/1e9;+ printf("%s{\"path\":\"%s\",\"kernel\":\"%s\",\"S\":%d,\"ms\":%.4f,"+ "\"gbps\":%.1f,\"pct_peak\":%.1f}",+ first?"":",",cfg[k].path,cfg[k].kernel,S,ms,gbps,+ peak>0?100.0*gbps/peak:-1.0);+ first=0;+ fprintf(stderr,"[bench] %-8s %-11s S=%-2d %.4f ms %.1f GB/s\n",+ cfg[k].path,cfg[k].kernel,S,ms,gbps);+ }+ }+ printf("]}\n");+ return 0;+}