okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/tests/route_parity.cpp - moe_route (softmax + top-10 + the expert plan) against a CPU replica of 2// native_router.cu's rule, for 1..8 tokens; logits with deliberate ties. Experts and experts per token: the shape this build is compiled for. 3#include "mtl_rt.h" 4#include "ref_quant.h" 5#include "engine/geo.h" 6 7#include <algorithm> 8#include <cfloat> 9#include <cmath> 10#include <cstdio> 11#include <cstring> 12#include <set> 13#include <vector> 14 15namespace { 16constexpr int TK = strata_metal::geo::TOPK ,NE = strata_metal::geo::NEXP ;// the shape this build is compiled for (engine/geo.h); the kernel's logits row stride is 512 17constexpr int P_NG = 0 ,P_MAXE = 128 ,P_GEXP = 16 ,P_GSTART = P_GEXP + P_MAXE ,P_ETOK = P_GSTART + P_MAXE + 4 , 18P_EW = P_ETOK + P_MAXE ,P_TOKENT = P_EW + P_MAXE ,P_INTS = P_TOKENT + P_MAXE ; 19struct MoeArgs { 20uint64_t gate_off ,up_off ,down_off ,gu_stride ,d_stride ; 21uint32_t gu_row ,d_row ,n_embd ,n_ff ,n_tok ,topk ,n_expert ,gx_gu ,gx_dn ,pad ; 22}; 23}// namespace 24 25int main () { 26 mtl::Context cx ; 27const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"moe.metal" }, strata_metal::geo::prelude ()); 28 mtl::Pipeline p = cx .pipeline (lib ,"moe_route" ); 29 mtl::Buffer lg = cx .alloc (4 * 8 * 512 ),ids = cx .alloc (4 * 8 * TK ),w = cx .alloc (4 * 8 * TK ),plan = cx .alloc (4 * P_INTS ); 30 ref::Rng r (9 ); 31int fails = 0 ; 32double worst_w = 0 ; 33for (int trial = 0 ;trial < 200 ;++ trial ) { 34const int nt = 1 + trial %8 ; 35for (int i = 0 ;i < nt * 512 ;++ i ) { 36float v = r .sym ()* 4.0f ; 37if (trial %3 == 0 )v = std::round (v * 4.0f ) /4.0f ;// many exact ties 38lg .as < float > ()[i ]= v ; 39 } 40MoeArgs a {}; 41a .n_tok = nt ; 42a .topk = TK ; 43a .n_expert = NE ; 44a .gx_gu = 1 ; 45a .gx_dn = 1 ; 46 mtl::Dispatch d ; 47d .p = & p ; 48d .args = {mtl::Arg ::B (lg ), mtl::Arg ::B (ids ), mtl::Arg ::B (w ), mtl::Arg ::B (plan ), mtl::Arg ::V (a )}; 49d .tg [0 ]= 512 ; 50cx .run ({d }); 51for (int t = 0 ;t < nt ;++ t ) { 52const float * l = lg .as < float > ()+ t * 512 ; 53float mx = - INFINITY ; 54for (int e = 0 ;e < NE ;++ e )mx = std::max (mx ,l [e ]); 55 std::vector < double > pr (NE ); 56double s = 0 ; 57for (int e = 0 ;e < NE ;++ e )s += (pr [e ]= std::exp ((double )l [e ]- mx )); 58for (int e = 0 ;e < NE ;++ e )pr [e ] /=s ; 59// top 10: largest probability, ties -> lower index (probabilities of equal logits are equal) 60 std::vector < int > order (NE ); 61for (int e = 0 ;e < NE ;++ e )order [e ]= e ; 62 std::stable_sort (order .begin (),order .end (), [& ](int x ,int y ) {return l [x ]> l [y ]; }); 63double ssum = 0 ; 64for (int k = 0 ;k < TK ;++ k )ssum += pr [order [k ]]; 65for (int k = 0 ;k < TK ;++ k ) { 66if (ids .as < int > ()[t * TK + k ]!= order [k ]) {++ fails ;break ; } 67const double ref = pr [order [k ]] / std::max (ssum ,6.103515625e-5 ); 68worst_w = std::max (worst_w , std::fabs (w .as < float > ()[t * TK + k ]- ref ) /ref ); 69 } 70 } 71// the plan 72const int * P = plan .as < int > (); 73 std::set < int > distinct (ids .as < int > (),ids .as < int > ()+ nt * TK ); 74bool ok = P [P_NG ]== (int )distinct .size (); 75int gi = 0 ; 76for (int e :distinct ) { 77if (P [P_GEXP + gi ]!= e )ok = false; 78int j = P [P_GSTART + gi ]; 79for (int i = 0 ;i < nt * TK ;++ i ) 80if (ids .as < int > ()[i ]== e ) { 81if (P [P_ETOK + j ]!= i /TK || P [P_TOKENT + i ]!= j )ok = false; 82++ j ; 83 } 84if (j != P [P_GSTART + gi + 1 ])ok = false; 85++ gi ; 86 } 87fails += !ok ; 88 } 89 std::printf ("route parity, 200 windows of 1..8 tokens (a third with exact ties): ids/plan mismatches %d, worst weight " 90"rel err %.2e %s\n" ,fails ,worst_w ,fails == 0 && worst_w < 2e-6 ?"PASS" :"FAIL" ); 91return fails ?1 :0 ; 92}