okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aebfd69a2
main
1// S6: the AMD fast router (router_top10's default on HIP for 64 < n_expert <= 512, k <= 32) against the portable 2// kernel, BITWISE: ids and weights of 65,536 rows of 512 logits - normal rows, rows with many exact ties, near-ties, 3// a wide spread (tiny probabilities), all-equal rows and rows with a NaN - plus the fast kernel forced onto its 4// serial-sum path, and a 256-expert / k=8 geometry. Then the time per single-token call of each. 5#include <hip/hip_runtime.h> 6#include "strata/kernels/router_top10.hpp" 7 8#include <cmath> 9#include <cstdio> 10#include <cstring> 11#include <random> 12#include <vector> 13 14#define CHECK (call ) \ 15 do { \ 16 const hipError_t error = (call); \ 17 if (error != hipSuccess) { \ 18 std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(error)); \ 19 return 2; \ 20 } \ 21 } while (0) 22 23namespace k= strata::kernels; 24 25static int compare (int n_expert ,int topk ,int rows ,unsigned seed ) { 26 std::mt19937 rng (seed ); 27 std::normal_distribution < float > nd (0.f ,2.f ); 28 std::vector < float > lg ((size_t )rows * n_expert ); 29for (size_t i = 0 ;i < lg .size ();++ i ) { 30const size_t r = i /n_expert ; 31float x = nd (rng )* (r %7 == 5 ?40.0f :1.0f ); 32if (r %3 == 1 )x = std::round (x * 4.0f ) /4.0f ; 33if (r %3 == 2 )x = std::round (x * 64.0f ) /64.0f ; 34if (r %1000 == 999 && (i %n_expert )== 77 )x = NAN ; 35if (r %1000 == 998 )x = 1.0f ; 36lg [i ]= x ; 37 } 38const size_t out = (size_t )rows * topk ; 39float * d_l = nullptr ,* d_w [3 ]= {}; 40int * d_i [3 ]= {}; 41CHECK (hipMalloc (& d_l ,lg .size ()* 4 )); 42CHECK (hipMemcpy (d_l ,lg .data (),lg .size ()* 4 ,hipMemcpyHostToDevice )); 43 std::vector < int > ids [3 ]; 44 std::vector < float > w [3 ]; 45for (int v = 0 ;v < 3 ;++ v ) { 46CHECK (hipMalloc (& d_w [v ],out * 4 )); 47CHECK (hipMalloc (& d_i [v ],out * 4 )); 48CHECK (hipMemset (d_w [v ],0x3c ,out * 4 ));// what an unwritten rank keeps (NaN rows) 49CHECK (hipMemset (d_i [v ],0x11 ,out * 4 )); 50if (!k::router_top10_variant (d_l ,rows ,n_expert ,topk ,d_i [v ],d_w [v ],nullptr ,v )) { 51 std::fprintf (stderr ,"router_top10_variant %d refused\n" ,v ); 52return 2 ; 53 } 54CHECK (hipDeviceSynchronize ()); 55ids [v ].resize (out ); 56w [v ].resize (out ); 57CHECK (hipMemcpy (ids [v ].data (),d_i [v ],out * 4 ,hipMemcpyDeviceToHost )); 58CHECK (hipMemcpy (w [v ].data (),d_w [v ],out * 4 ,hipMemcpyDeviceToHost )); 59 } 60int fails = 0 ; 61for (int v = 1 ;v < 3 ;++ v ) { 62int bad = 0 ; 63for (int r = 0 ;r < rows ;++ r ) 64if (std::memcmp (& ids [0 ][(size_t )r * topk ],& ids [v ][(size_t )r * topk ],topk * 4 )|| 65 std::memcmp (& w [0 ][(size_t )r * topk ],& w [v ][(size_t )r * topk ],topk * 4 )) { 66if (bad < 3 ) 67 std::printf (" row %d differs: id0 %d/%d w0 %.9g/%.9g\n" ,r ,ids [0 ][(size_t )r * topk ], 68ids [v ][(size_t )r * topk ],w [0 ][(size_t )r * topk ],w [v ][(size_t )r * topk ]); 69++ bad ; 70 } 71 std::printf ("%s %d experts, k %d, %d rows: %s vs the portable kernel: %d rows differ\n" ,bad ?"FAIL" :"PASS" , 72n_expert ,topk ,rows ,v == 1 ?"fast kernel" :"fast kernel, serial path" ,bad ); 73fails += bad != 0 ; 74 } 75// time: single-token calls back to back 76hipEvent_t e0 ,e1 ; 77CHECK (hipEventCreate (& e0 )); 78CHECK (hipEventCreate (& e1 )); 79for (int v = 0 ;v < 2 ;++ v ) { 80const int reps = 2000 ; 81CHECK (hipEventRecord (e0 )); 82for (int i = 0 ;i < reps ;++ i ) 83 k::router_top10_variant (d_l + (size_t ) (i %64 )* n_expert ,1 ,n_expert ,topk ,d_i [v ],d_w [v ],nullptr ,v ); 84CHECK (hipEventRecord (e1 )); 85CHECK (hipEventSynchronize (e1 )); 86float ms = 0 ; 87CHECK (hipEventElapsedTime (& ms ,e0 ,e1 )); 88 std::printf (" %s: %.2f us per single-token call\n" ,v ?"fast" :"portable" ,1000.0f * ms /reps ); 89 } 90for (int v = 0 ;v < 3 ;++ v ) { (void )hipFree (d_w [v ]); (void )hipFree (d_i [v ]); } 91 (void )hipFree (d_l ); 92return fails ; 93} 94 95int main () { 96int fails = 0 ; 97fails += compare (512 ,10 ,65536 ,17 ); 98fails += compare (256 ,8 ,8192 ,5 ); 99 std::printf ("FAILURES: %d\n" ,fails ); 100return fails ?1 :0 ; 101}