/* Device-router kernel oracle (#431 PR-A). * * Feeds random activations/router weights through pipe_router_logits + * pipe_router_select and checks against a CPU reference that replicates * moe()'s plain routing path verbatim (sigmoid -> bias-augmented top-K by * `choice`, weights from raw `logit`, route-level TOPP truncation, norm_topk, * routed_scale). The dot/expf rounding may differ from libm at ~1e-6 rel, so * a handful of near-tie index flips across trials is tolerated; the weight * math itself must agree to 1e-4 rel on matching selections. * * Build: nvcc -O2 -std=c++17 -arch=native tests/test_router_cuda.cu -o tests/test_router_cuda */ #include #include #include #include #include /* pull in the kernel definitions (same idiom as the CPU tests' #include "../colibri.c") */ #include "../backend_cuda.cu" static void cpu_ref(const float *x,const float *W,const float *bias,int D,int E, int Ksel,float topp,int norm_topk,float rscale, int *idx,float *w,int *keff){ float *logit=(float*)malloc(E*sizeof(float)),*choice=(float*)malloc(E*sizeof(float)); for(int e=0;ebv){bv=choice[e];best=e;} } idx[kk]=best; w[kk]=logit[best]; } int Ke=Ksel; if(topp>0.f && topp<1.f){ for(int a=1;a=0 && w[b]=topp*tot){ Ke=kk+1; break; } } } if(norm_topk){ float sm=0; for(int kk=0;kk>>(x,W,b,D,lg,ch); pipe_router_select<<<1,1>>>(lg,ch,E,K,topp,nt,rs,out); char pack[K*8+4]; if(cudaMemcpy(pack,out,sizeof(pack),cudaMemcpyDeviceToHost)!=cudaSuccess){ printf("FAIL cuda\n"); return 1; } int gidx[K],gkeff; float gw[K]; memcpy(gidx,pack,K*4); memcpy(gw,pack+K*4,K*4); memcpy(&gkeff,pack+K*8,4); int ridx[K],rkeff; float rw[K]; cpu_ref(x,W,b,D,E,K,topp,nt,rs,ridx,rw,&rkeff); int mism=0; for(int k2=0;k21e-4f*(fabsf(ref)+1e-6f)+1e-6f){ bad++; break; } } } printf("router oracle: %d trials, %d near-tie flips, %d weight mismatches\n",TRIALS,flips,bad); if(flips>4||bad){ printf("FAIL\n"); return 1; } printf("OK\n"); return 0; }