/* Phase 2e: MoE (router + top-12 experts + identity zero-experts) in C. * Uses the dumped top-k indices/weights + reads expert fp8 weights from the model shards via mmap. * To keep 2e ISOLATED (validate MoE math, not routing rediscovery), we consume the dumped * moe_topk_idx.i32 / moe_topk_w.f32 AND recompute the router to double-check it matches. * Real experts: SwiGLU on fp8 weights. Zero experts (idx>=512): identity. * Build: gcc -O2 -D_GNU_SOURCE -o phase2e_moe phase2e_moe.c -lm */ #include #include #include #include #include #include #include #include #include #define T 5 #define HID 6144 #define FFN 2048 #define NR 512 #define NTOT 768 #define K 12 static float* Lf(const char* n, long cnt) { char p[512]; snprintf(p, sizeof p, "ref/%s.f32", n); FILE* f = fopen(p, "rb"); if (!f) { perror(p); exit(1); } float* b = malloc(cnt * 4); if (fread(b,4,cnt,f)!=(size_t)cnt){fprintf(stderr,"short %s\n",n);exit(1);} fclose(f); return b; } static int32_t* Li(const char* n, long cnt) { char p[512]; snprintf(p, sizeof p, "ref/%s.i32", n); FILE* f = fopen(p, "rb"); if (!f) { perror(p); exit(1); } int32_t* b = malloc(cnt*4); if (fread(b,4,cnt,f)!=(size_t)cnt){fprintf(stderr,"short %s\n",n);exit(1);} fclose(f); return b; } /* --- safetensors index: map tensor name -> (shard path, dtype, offset, shape) --- */ static float e4m3(uint8_t b){int s=(b>>7)&1,e=(b>>3)&0xF,m=b&7;float v; if(e==0xF&&m==7)return NAN; if(e==0)v=ldexpf(m/8.0f,-6);else v=ldexpf(1.0f+m/8.0f,e-7);return s?-v:v;} /* read an fp8 expert weight [OUT,IN] into float via python-dumped dequant is easier, but to prove native path we read from shard. For 2e isolation we instead load python-dequanted expert files IF present; else fall back. Simpler+decisive: dump the 12 experts for token-set from python. -> here we read pre-dequanted expert files named exp__{gate,up,down}.f32 */ static float* expw(int idx, const char* which, long cnt) { char p[512]; snprintf(p, sizeof p, "ref/exp_%d_%s.f32", idx, which); FILE* f = fopen(p, "rb"); if (!f) return NULL; float* b = malloc(cnt*4); if (fread(b,4,cnt,f)!=(size_t)cnt){fclose(f);free(b);return NULL;} fclose(f); return b; } static void matvec(const float* W,const float* x,float* o,int M,int Kd){ for(int i=0;i=NR){ /* identity */ for(int i=0;idmax)dmax=dd;} printf("moe_out[0,:6]: ");for(int i=0;i<6;i++)printf("%.4f ",out[i]);printf("\n"); printf("MoE max abs diff vs Python: %.3e\n",dmax); printf("VERDICT: %s\n",dmax<5e-2?"PASS — MoE matches Python":"FAIL"); return dmax<5e-2?0:1; }