Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
cuda: dispatch grouped int4 (fmt=4) in the async expert-group path
kreuzzelg59b303a11376
diff --git a/c/backend_cuda.cu b/c/backend_cuda.cuindex 0ccd1496..356b55d4 100644--- a/c/backend_cuda.cu+++ b/c/backend_cuda.cu@@ -1449,7 +1449,8 @@ extern "C" int coli_cuda_expert_group_issue(ColiCudaTensor *const *gates,if (!gates || !ups || !downs || !rows || !x || count < 1 || count > 64) return 0;ColiCudaTensor *first=gates[0];if (!first) return 0;- int device=first->device,D=first->I,I=first->O,total=0,max_rows=0,all_s4=1,any_e8=0,all_e8=1;+ int device=first->device,D=first->I,I=first->O,total=0,max_rows=0,all_s4=1,any_e8=0,all_e8=1,+ all_q4=1,any_g4=0;GroupDesc host[64];for(int c=0;c<count;c++){ColiCudaTensor *g=gates[c],*u=ups[c],*d=downs[c];@@ -1461,6 +1462,9 @@ extern "C" int coli_cuda_expert_group_issue(ColiCudaTensor *const *gates,all_s4&=g->fmt==2&&u->fmt==2&&d->fmt==2;any_e8|=g->fmt==6||u->fmt==6||d->fmt==6;all_e8&=g->fmt==6&&u->fmt==6&&d->fmt==6;+ all_q4&=(g->fmt==2||g->fmt==4)&&(u->fmt==2||u->fmt==4)&&(d->fmt==2||d->fmt==4)&&+ !(g->gs&1)&&!(u->gs&1)&&!(d->gs&1); /* even gs: a packed byte never straddles groups */+ any_g4|=g->fmt==4||u->fmt==4||d->fmt==4;total+=rows[c]; if(rows[c]>max_rows) max_rows=rows[c];}if(any_e8&&!all_e8) return 0;@@ -1499,6 +1503,19 @@ extern "C" int coli_cuda_expert_group_issue(ColiCudaTensor *const *gates,ctx->gate,ctx->up,(size_t)total*I);}grouped_down_w4<<<og,256,0,ctx->stream>>>(ctx->y,ctx->gate,dev,D,I);+ } else if(all_q4&&any_g4){+ /* grouped int4 (fmt=4) present in the async decode path: per-group+ * scales via the #334 kernels (fmt=2 members ride along as ng=1). The+ * previous fallback ran quant_matmul with gs=0,ng=1, which silently+ * applied one per-row scale to a grouped container -> wrong output. */+ GroupDesc *dev=(GroupDesc*)ctx->group_desc;+ dim3 hg((unsigned)I,(unsigned)max_rows,(unsigned)count);+ dim3 og((unsigned)D,(unsigned)max_rows,(unsigned)count);+ /* silu is fused in the dual kernel's epilogue (like the sync path):+ * an extra silu_mul here would re-apply it against the never-written+ * ctx->up buffer. */+ grouped_hidden_g4_dual<<<hg,256,0,ctx->stream>>>(ctx->gate,ctx->up,ctx->x,dev,I,D);+ grouped_down_g4<<<og,256,0,ctx->stream>>>(ctx->y,ctx->gate,dev,D,I);} else for(int c=0;c<count;c++){int r=rows[c];float *g16=ctx->gate+(size_t)host[c].offset*I,*u16=ctx->up+(size_t)host[c].offset*I;diff --git a/c/tests/test_grouped_g4_cuda.cu b/c/tests/test_grouped_g4_cuda.cuindex b370cdda..9acb82da 100644--- a/c/tests/test_grouped_g4_cuda.cu+++ b/c/tests/test_grouped_g4_cuda.cu@@ -5,10 +5,17 @@* that replicates matmul_i4_grouped's semantics (value = nibble - 8, per-group* partial dot x scale). Covers gs=64, a non-divisible tail group, and a* per-row (gs=0) member riding in the same launch β the fmt=2-compat case.+ * The dual hidden kernel fuses silu into its epilogue, so gate[] holds+ * silu(g)*u and up[] is never written; the oracle checks that contract.** The device buffers get the same XOR 0x88 offset->signed conversion the* upload path applies, so the kernels are exercised exactly as deployed.*+ * Phase 2 goes through the public API instead of raw kernels: upload_g plus+ * coli_cuda_expert_group (sync) and coli_cuda_expert_group_issue/take (the+ * async decode path the VRAM expert tiers ride) against the same reference β+ * the async result must be bit-identical to the sync one.+ ** Build: nvcc -O2 -std=c++17 -arch=native tests/test_grouped_g4_cuda.cu -o tests/test_grouped_g4*/#include <cstdio>@@ -85,13 +92,13 @@ int main(void){if(cudaDeviceSynchronize()!=cudaSuccess){ printf("FAIL cuda\n"); return 1; }for(int c=0;c<COUNT;c++){int cgs=c==2?0:gs;- float rg[512],ru[512],ry[512];+ float rg[512],ru[512],rh[512],ry[512];cpu_gemv_g4(hg[c],hgs[c],D,I,cgs,xs+(size_t)c*D,rg);cpu_gemv_g4(hu[c],hus[c],D,I,cgs,xs+(size_t)c*D,ru);- for(int o=0;o<I;o++){- if(fabsf(gate[(size_t)c*I+o]-rg[o])>1e-3f*(fabsf(rg[o])+1e-3f)||- fabsf(up[(size_t)c*I+o]-ru[o])>1e-3f*(fabsf(ru[o])+1e-3f)) bad++;- }+ /* fused epilogue (a03c79e): gate[] = silu(g)*u, up[] untouched */+ for(int o=0;o<I;o++) rh[o]=(rg[o]/(1.f+expf(-rg[o])))*ru[o];+ for(int o=0;o<I;o++)+ if(fabsf(gate[(size_t)c*I+o]-rh[o])>1e-3f*(fabsf(rh[o])+1e-3f)) bad++;cpu_gemv_g4(hd[c],hds[c],I,D,cgs,(float*)gate+(size_t)c*I,ry);for(int o=0;o<D;o++)if(fabsf(y[(size_t)c*D+o]-ry[o])>1e-3f*(fabsf(ry[o])+1e-3f)) bad++;@@ -104,5 +111,63 @@ int main(void){printf("grouped-g4 oracle: %d trials x %d experts (gs=64 + tail + per-row member), %d mismatches\n",trials,COUNT,bad);if(bad){ printf("FAIL\n"); return 1; }++ /* ---- Phase 2: public API β upload_g + sync group vs async issue/take ----+ * The async path is what MoE decode (and the VRAM expert tiers) actually+ * call; it must dispatch fmt=4 like the sync path and match it bit for bit. */+ {+ int devs[1]={0};+ if(!coli_cuda_init(devs,1)){ printf("FAIL cuda init\n"); return 1; }+ int rows[COUNT]={1,2,1}, total=4, api_bad=0;+ ColiCudaTensor *tg[COUNT]={},*tu[COUNT]={},*td[COUNT]={};+ uint8_t *hg[COUNT],*hu[COUNT],*hd[COUNT]; float *hgs[COUNT],*hus[COUNT],*hds[COUNT];+ for(int c=0;c<COUNT;c++){+ int cgs = c==2 ? 0 : gs; /* expert 2 rides along as fmt=2 */+ int cngD = cgs? ngD:1, cngI = cgs? ngI:1, fmt = cgs?4:2;+ hg[c]=(uint8_t*)malloc((size_t)I*rbD); hu[c]=(uint8_t*)malloc((size_t)I*rbD);+ hd[c]=(uint8_t*)malloc((size_t)D*rbI);+ hgs[c]=(float*)malloc((size_t)I*cngD*4); hus[c]=(float*)malloc((size_t)I*cngD*4);+ hds[c]=(float*)malloc((size_t)D*cngI*4);+ for(size_t i=0;i<(size_t)I*rbD;i++){ hg[c][i]=rand()&255; hu[c][i]=rand()&255; }+ for(size_t i=0;i<(size_t)D*rbI;i++) hd[c][i]=rand()&255;+ for(size_t i=0;i<(size_t)I*cngD;i++){ hgs[c][i]=.01f+.05f*(rand()/(float)RAND_MAX);+ hus[c][i]=.01f+.05f*(rand()/(float)RAND_MAX); }+ for(size_t i=0;i<(size_t)D*cngI;i++) hds[c][i]=.01f+.05f*(rand()/(float)RAND_MAX);+ if(!coli_cuda_tensor_upload_g(&tg[c],hg[c],hgs[c],fmt,D,I,0,cgs)||+ !coli_cuda_tensor_upload_g(&tu[c],hu[c],hus[c],fmt,D,I,0,cgs)||+ !coli_cuda_tensor_upload_g(&td[c],hd[c],hds[c],fmt,I,D,0,cgs)){+ printf("FAIL upload_g\n"); return 1; }+ }+ float *x=(float*)malloc((size_t)total*D*4),*ysync=(float*)malloc((size_t)total*D*4);+ for(size_t i=0;i<(size_t)total*D;i++) x[i]=(rand()/(float)RAND_MAX-.5f)*2.f;+ if(!coli_cuda_expert_group(tg,tu,td,rows,COUNT,ysync,x)){ printf("FAIL sync group\n"); return 1; }+ if(!coli_cuda_expert_group_issue(tg,tu,td,rows,COUNT,x)){ printf("FAIL async issue\n"); return 1; }+ const float *yasync=coli_cuda_expert_group_take(0);+ if(!yasync){ printf("FAIL async take\n"); return 1; }+ int off=0;+ for(int c=0;c<COUNT;c++){+ int cgs=c==2?0:gs;+ for(int s=0;s<rows[c];s++){+ float rg[512],ru[512],rh[512],ry[512];+ const float *xr=x+(size_t)(off+s)*D;+ cpu_gemv_g4(hg[c],hgs[c],D,I,cgs,xr,rg);+ cpu_gemv_g4(hu[c],hus[c],D,I,cgs,xr,ru);+ for(int o=0;o<I;o++) rh[o]=(rg[o]/(1.f+expf(-rg[o])))*ru[o];+ cpu_gemv_g4(hd[c],hds[c],I,D,cgs,rh,ry);+ for(int o=0;o<D;o++){+ size_t z=(size_t)(off+s)*D+o;+ if(fabsf(ysync[z]-ry[o])>1e-3f*(fabsf(ry[o])+1e-3f)) api_bad++;+ if(yasync[z]!=ysync[z]) api_bad++; /* async == sync, bitwise */+ }+ }+ off+=rows[c];+ }+ printf("grouped-g4 API: sync vs oracle + async(issue/take) vs sync, %d mismatches\n",api_bad);+ for(int c=0;c<COUNT;c++){ coli_cuda_tensor_free(tg[c]);coli_cuda_tensor_free(tu[c]);coli_cuda_tensor_free(td[c]);+ free(hg[c]);free(hu[c]);free(hd[c]);free(hgs[c]);free(hus[c]);free(hds[c]); }+ free(x);free(ysync);+ coli_cuda_shutdown();+ if(api_bad){ printf("FAIL\n"); return 1; }+ }printf("OK\n"); return 0;}