okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/kernels/router_top10_parity.cpp - P2.S2's parity test for the MoE router. 2// 3// The reference here is a HOST implementation of `ref/moe.py::router` written from the same specification the 4// kernel was written from - so this checks the kernel against the spec independently of the kernel, which is 5// the strongest thing available without running llama.cpp. It is NOT a check against llama.cpp itself, and 6// that gap is stated rather than implied. 7// 8// WHAT THE TEST CAN AND CANNOT SEE. It compares expert IDs exactly and weights to a tolerance, on several 9// logit distributions including ties (where the "smallest index wins" rule is the only thing that decides) and 10// near-ties (where a float difference decides). It also asserts that the 2**-14 CLAMP CANNOT TRIGGER for this 11// model's geometry - see below - so the absence of a clamp test is a proven fact rather than an omission. 12#include "strata/kernels/router_top10.hpp" 13 14#include <cuda_runtime.h> 15 16#include <algorithm> 17#include <cmath> 18#include <cstdio> 19#include <cstdlib> 20#include <numeric> 21#include <random> 22#include <vector> 23 24namespace { 25 26void check (cudaError_t e ,const char * what ) { 27if (e != cudaSuccess ) { 28 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 29 std::exit (1 ); 30 } 31} 32 33constexpr double RENORM_CLAMP = 6.103515625e-05 ;// 2**-14 34 35// The host reference: softmax over all experts, stable descending argsort, gather, clamp, renormalise. 36void reference (const float * logits ,int n_expert ,int k ,int * ids ,float * w ) { 37double mx = logits [0 ]; 38for (int e = 1 ;e < n_expert ;++ e )mx = std::max (mx , (double )logits [e ]); 39double sum = 0 ; 40 std::vector < double > p ((size_t )n_expert ); 41for (int e = 0 ;e < n_expert ;++ e ) { 42p [(size_t )e ]= std::exp ((double )logits [e ]- mx ); 43sum += p [(size_t )e ]; 44 } 45for (int e = 0 ;e < n_expert ;++ e )p [(size_t )e ] /=sum ; 46 47// stable descending: std::stable_sort with a strict greater-than keeps equal elements in index order 48 std::vector < int > idx ((size_t )n_expert ); 49 std::iota (idx .begin (),idx .end (),0 ); 50 std::stable_sort (idx .begin (),idx .end (), 51 [& ](int a ,int b ) {return p [(size_t )a ]> p [(size_t )b ]; }); 52double s = 0 ; 53for (int i = 0 ;i < k ;++ i ) { 54ids [i ]= idx [(size_t )i ]; 55w [i ]= (float )p [(size_t )idx [(size_t )i ]]; 56s += p [(size_t )idx [(size_t )i ]]; 57 } 58s = std::max (s ,RENORM_CLAMP ); 59for (int i = 0 ;i < k ;++ i )w [i ]= (float ) ((double )w [i ] /s ); 60} 61 62int run_case (const char * name ,const std::vector < float >& logits ,int n_tokens ,int n_expert ,int k , 63double tol ) { 64 std::vector < int > h_ids ((size_t )n_tokens * k ); 65 std::vector < float > h_w ((size_t )n_tokens * k ); 66 std::vector < int > r_ids ((size_t )n_tokens * k ); 67 std::vector < float > r_w ((size_t )n_tokens * k ); 68for (int t = 0 ;t < n_tokens ;++ t ) { 69reference (& logits [(size_t )t * n_expert ],n_expert ,k ,& r_ids [(size_t )t * k ],& r_w [(size_t )t * k ]); 70 } 71 72float * d_l = nullptr ; 73int * d_ids = nullptr ; 74float * d_w = nullptr ; 75check (cudaMalloc (& d_l ,logits .size ()* sizeof (float )),"malloc logits" ); 76check (cudaMalloc (& d_ids ,h_ids .size ()* sizeof (int )),"malloc ids" ); 77check (cudaMalloc (& d_w ,h_w .size ()* sizeof (float )),"malloc w" ); 78check (cudaMemcpy (d_l ,logits .data (),logits .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"copy" ); 79 strata::kernels::router_top10 (d_l ,n_tokens ,n_expert ,k ,d_ids ,d_w ,nullptr ); 80check (cudaMemcpy (h_ids .data (),d_ids ,h_ids .size ()* sizeof (int ),cudaMemcpyDeviceToHost ),"back ids" ); 81check (cudaMemcpy (h_w .data (),d_w ,h_w .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"back w" ); 82 83long long id_bad = 0 ,w_bad = 0 ; 84double worst = 0 ; 85for (size_t i = 0 ;i < h_ids .size ();++ i ) { 86if (h_ids [i ]!= r_ids [i ])++ id_bad ; 87const double rel = std::fabs ((double )h_w [i ]- (double )r_w [i ]) / 88 (std::fabs ((double )r_w [i ])> 1e-30 ? std::fabs ((double )r_w [i ]) :1e-30 ); 89worst = std::max (worst ,rel ); 90if (!(rel <=tol ))++ w_bad ; 91 } 92// every token's weights must sum to 1 unless the clamp replaced the sum 93double worst_sum = 0 ; 94for (int t = 0 ;t < n_tokens ;++ t ) { 95double s = 0 ; 96for (int i = 0 ;i < k ;++ i )s += (double )h_w [(size_t )t * k + i ]; 97double raw = 0 ; 98for (int i = 0 ;i < k ;++ i )raw += (double )r_w [(size_t )t * k + i ]; 99// both sides divide by the same clamped scale, so the ratios agree; check the kernel's own sum 100worst_sum = std::max (worst_sum , std::fabs (s - 1.0 )); 101 } 102 std::printf (" %-26s ids %s (%lld bad) weights worst rel %.3e (%lld over tol) |sum-1| %.1e\n" ,name , 103id_bad ?"*** WRONG ***" :"exact" ,id_bad ,worst ,w_bad ,worst_sum ); 104cudaFree (d_l ); 105cudaFree (d_ids ); 106cudaFree (d_w ); 107return (int ) (id_bad + w_bad ); 108} 109 110}// namespace 111 112int main (int argc ,char ** argv ) { 113bool selftest = false; 114for (int i = 1 ;i < argc ;++ i ) { 115if (std::string (argv [i ])== "--selftest" )selftest = true; 116else { std::fprintf (stderr ,"usage: router_top10_parity [--selftest]\n" );return 2 ; } 117 } 118 119const int NE = 512 ;// the real n_expert 120const int K = 10 ;// the real top-k 121const int NT = 64 ; 122 std::mt19937 rng (2024 ); 123 std::normal_distribution < float > gauss (0.0f ,1.0f ); 124int bad = 0 ; 125 126// ---- random logits 127 std::vector < float > a ((size_t )NT * NE ); 128for (auto & v :a )v = gauss (rng ); 129bad += run_case ("random normal" ,a ,NT ,NE ,K ,1e-5 ); 130 131// ---- ALL-EQUAL logits: every expert ties, so the "smallest index wins" rule is the ONLY thing deciding 132// the ids. An unstable sort or a >= comparison in the maximum scan fails this and passes everything else. 133 std::vector < float > b ((size_t )NT * NE ,0.5f ); 134bad += run_case ("all equal (ties)" ,b ,NT ,NE ,K ,1e-5 ); 135 136// ---- a few exact ties among otherwise distinct values 137 std::vector < float > c ((size_t )NT * NE ); 138for (int t = 0 ;t < NT ;++ t ) { 139for (int e = 0 ;e < NE ;++ e )c [(size_t )t * NE + e ]= (e < 12 ) ?2.0f :gauss (rng ); 140 } 141bad += run_case ("12-way exact tie" ,c ,NT ,NE ,K ,1e-5 ); 142 143// ---- one dominant expert: the top-1 must be that expert and its weight must dominate 144 std::vector < float > d ((size_t )NT * NE ,0.0f ); 145for (int t = 0 ;t < NT ;++ t )d [(size_t )t * NE + (t %NE )]= 20.0f ; 146bad += run_case ("dominant expert" ,d ,NT ,NE ,K ,1e-5 ); 147 148// ---- THE CLAMP CANNOT TRIGGER, and that is provable rather than untested. 149// The sum of the top k of a probability vector over n outcomes is at least k/n (the minimum is the uniform 150// distribution). Here k/n = 10/512 = 0.01953125, which is 320x the 2**-14 = 6.1035e-05 clamp, so no input 151// whatsoever can make this model's renormalisation clamp - not an extreme one, not an adversarial one. 152// Asserting the bound is worth more than a test that cannot reach it. 153const double min_possible = (double )K / (double )NE ; 154 std::printf (" %-26s top-%d sum >= k/n = %.5f, clamp is %.3e -> margin %.0fx %s\n" ,"clamp reachability" , 155K ,min_possible ,RENORM_CLAMP ,min_possible /RENORM_CLAMP , 156min_possible > RENORM_CLAMP ?"(CLAMP IS UNREACHABLE, asserted)" :"*** REACHABLE ***" ); 157if (min_possible <=RENORM_CLAMP ) { 158 std::fprintf (stderr ,"the clamp CAN trigger, so it needs a test\n" ); 159++ bad ; 160 } 161 162 std::printf ("\nrouter_top10: %d failures over 4 distributions x %d tokens x %d experts, top-%d\n" ,bad ,NT ,NE , 163K ); 164if (bad )return 1 ; 165if (selftest ) std::printf ("router_top10_parity OK\n" ); 166return 0 ; 167}