okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5438023a6
main
1// src/kernels/native_expert_bench.cpp - the grouped native experts on real GGUF rows: one AMD layout against 2// another, bit for bit, and each one's time. 3// 4// build/native_expert_bench <shard1.gguf> <layer[,layer...]> <groups> <tokens per group> <ref mode> <mode> [iters] 5// 6// Modes are STRATA_EXP_MODE values. A group is one expert (distinct blobs, ~2 MB each, so G >= 8 does not fit 7// in L2); its entries read distinct tokens of an 8-token window. The output must be bitwise equal to the 8// reference mode's: the verify window's text depends on it. 9#define DPCT_PROFILING_ENABLED 10#include <sycl/sycl.hpp> 11#include <dpct/dpct.hpp> 12#include "strata/artifact/gguf_reader.hpp" 13#include "strata/kernels/cpu/native_expert.hpp" 14#include "strata/kernels/iq_kernels.hpp" 15 16#include "ggml.h" 17 18#include <chrono> 19#include <cmath> 20#include <cstdint> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24#include <random> 25#include <sstream> 26#include <string> 27#include <vector> 28 29namespace cpu= strata::kernels::cpu; 30namespace K = strata::kernels; 31 32uint64_t fnv1a (const void * data ,size_t bytes ) { 33const auto * p = static_cast < const uint8_t *> (data ); 34uint64_t h = 1469598103934665603ull ; 35for (size_t i = 0 ;i < bytes ;++ i ) { 36h ^=p [i ]; 37h *=1099511628211ull ; 38 } 39return h ; 40} 41 42int main (int argc ,char ** argv ) { 43setvbuf (stdout ,nullptr ,_IONBF ,0 ); 44if (argc < 7 ) { 45 std::fprintf (stderr ,"usage: native_expert_bench <shard1.gguf> <layer[,layer]> <groups> <tokens> <ref mode> <mode> [iters]\n" ); 46return 2 ; 47 } 48 strata::GgufFile gguf (argv [1 ]); 49 std::vector < int > layers ; 50 { 51 std::stringstream ss (argv [2 ]); 52 std::string t ; 53while (std::getline (ss ,t ,',' ))layers .push_back (std::atoi (t .c_str ())); 54 } 55const int G = std::atoi (argv [3 ]),T = std::atoi (argv [4 ]),mref = std::atoi (argv [5 ]),mode = std::atoi (argv [6 ]); 56const int iters = argc > 7 ? std::atoi (argv [7 ]) :200 ; 57const int NTOK = 8 ; 58if (T < 1 || T > NTOK || G < 1 ) { std::fprintf (stderr ,"tokens 1..8, groups >= 1\n" );return 2 ; } 59const int64_t H = 2560 ,FF_FULL = 640 ,FF = FF_FULL ;// Flash-Next's expert geometry 60int failures = 0 ; 61 dpct::queue_ptr s ; 62s = dpct::get_current_device ().create_queue (true); 63// SYCL port: the queue carries an exception handler, which would swallow a failing launch and leave the timing 64// at "the kernel did nothing" (0.3 us for a group of experts). Every wait goes through this, so a device-side 65// error is printed instead of silently producing an empty measurement. 66auto sync = [& ](const char * what ) { 67try { 68s -> wait_and_throw (); 69return true; 70 }catch (std::exception const & e ) { 71 std::printf ("%s: %s\n" ,what ,e .what ()); 72return false; 73 } 74 }; 75for (int l :layers ) { 76const strata::TensorInfo * t [3 ]= {}; 77const char * roles [3 ]= {"gate" ,"up" ,"down" }; 78for (const auto & ti :gguf .tensors ()) 79for (int r = 0 ;r < 3 ;++ r ) 80if (ti .name == "blk." + std::to_string (l )+ ".ffn_" + roles [r ]+ "_exps.weight" )t [r ]= & ti ; 81if (!t [0 ]|| !t [1 ]|| !t [2 ]) { std::printf ("layer %d: no expert tensors\n" ,l );++ failures ;continue ; } 82 cpu::NativeFmt f ; 83 std::string err ; 84if (!cpu::native_fmt ((int )t [0 ]-> type , (int )t [2 ]-> type ,H ,FF ,f ,err )) { 85 std::printf ("layer %d: %s\n" ,l ,err .c_str ());++ failures ;continue ; 86 } 87 cpu::NativeFmt ff ;// the full expert's geometry, for strides into the GGUF 88if (!cpu::native_fmt ((int )t [0 ]-> type , (int )t [2 ]-> type ,H ,FF_FULL ,ff ,err )) { 89 std::printf ("layer %d: %s\n" ,l ,err .c_str ());++ failures ;continue ; 90 } 91const size_t dsz = f .bytes - f .down_off ,dsz_full = ff .bytes - ff .down_off ; 92const size_t drow = dsz /H ,drow_full = dsz_full /H ; 93 std::vector < uint8_t > blobs ((size_t )G * f .bytes ); 94for (int g = 0 ;g < G ;++ g ) { 95const size_t E = (size_t ) ((g * 37 + 5 ) %256 ); 96uint8_t * b = blobs .data ()+ (size_t )g * f .bytes ; 97 std::memcpy (b ,gguf .tensor_data (* t [0 ])+ E * ff .up_off ,f .up_off ); 98 std::memcpy (b + f .up_off ,gguf .tensor_data (* t [1 ])+ E * ff .up_off ,f .up_off ); 99for (int64_t r = 0 ;r < H ;++ r ) 100 std::memcpy (b + f .down_off + r * drow ,gguf .tensor_data (* t [2 ])+ E * dsz_full + r * drow_full ,drow ); 101 } 102 std::mt19937 rng (17 + l ); 103 std::normal_distribution < float > nd (0.f ,1.f ); 104 std::vector < float > x ((size_t )NTOK * H ); 105for (auto & v :x )v = nd (rng ); 106const int NE = G * T ; 107 std::vector < unsigned long long > ptr (G ); 108 std::vector < int32_t > start (G + 1 ),dst (NE ),tok (NE ); 109const auto L = K ::native_expert_layout (f .gu_type ,f .d_type ,H ,FF ); 110// SYCL port: this bench was never a build target, so its CUDA calls were never migrated. Device buffers are 111// USM allocations on the bench's own in-order queue, and every copy is ordered on that queue. 112uint8_t * dblob = (uint8_t * ) sycl::malloc_device (blobs .size (),* s ); 113void * dx = sycl::malloc_device ((size_t )NTOK * H * 4 ,* s ); 114void * dxq = sycl::malloc_device ((size_t )NTOK * H /32 * 36 ,* s ); 115void * dscr = sycl::malloc_device (K ::native_expert_scratch_bytes (NE ,FF ),* s ); 116float * dout = (float * ) sycl::malloc_device ((size_t )NE * H * 4 ,* s ); 117unsigned long long * dptr = (unsigned long long * ) sycl::malloc_device ((size_t )G * 8 ,* s ); 118int32_t * dstart = (int32_t * ) sycl::malloc_device ((size_t ) (G + 1 )* 4 ,* s ); 119int32_t * dn = (int32_t * ) sycl::malloc_device (4 ,* s ); 120int32_t * ddst = (int32_t * ) sycl::malloc_device ((size_t )NE * 4 ,* s ); 121int32_t * dtok = (int32_t * ) sycl::malloc_device ((size_t )NE * 4 ,* s ); 122for (int g = 0 ;g < G ;++ g ) { 123ptr [g ]= (unsigned long long ) (dblob + (size_t )g * f .bytes ); 124start [g ]= g * T ; 125for (int j = 0 ;j < T ;++ j ) { 126dst [g * T + j ]= g * T + j ; 127tok [g * T + j ]= (g * 3 + j ) %NTOK ; 128 } 129 } 130start [G ]= NE ; 131s -> memcpy (dblob ,blobs .data (),blobs .size ()).wait (); 132s -> memcpy (dx ,x .data (),x .size ()* 4 ).wait (); 133s -> memcpy (dptr ,ptr .data (), (size_t )G * 8 ).wait (); 134s -> memcpy (dstart ,start .data (), (size_t ) (G + 1 )* 4 ).wait (); 135s -> memcpy (dn ,& G ,4 ).wait (); 136s -> memcpy (ddst ,dst .data (), (size_t )NE * 4 ).wait (); 137s -> memcpy (dtok ,tok .data (), (size_t )NE * 4 ).wait (); 138K ::quantize_q8_1_rows ((const float * )dx ,NTOK ,H ,dxq ,s ); 139s -> wait_and_throw (); 140auto run = [& ](int m ,int phase ) { 141K ::native_expert_set_mode (m ,phase ); 142K ::native_expert_grouped (L ,dptr ,dstart ,dn ,ddst ,dtok ,G ,NE ,dxq ,dscr ,dout ,s ); 143 }; 144 std::vector < float > ref ((size_t )NE * H ),got ((size_t )NE * H ); 145s -> memset (dout ,0xff ,got .size ()* 4 ).wait (); 146run (mref ,0 ); 147if (!sync ("reference window" ))return 1 ; 148 149s -> memcpy (ref .data (),dout ,ref .size ()* 4 ).wait (); 150// SYCL port: a launch that returns without enqueueing leaves the 0xff fill intact, and then the mode and the 151// reference agree bitwise on an untouched buffer - a "0 differing" that measures nothing. The fill is NaN as 152// a float, so a reference window with no finite value anywhere did no work: say so instead of timing it. 153 { 154size_t written = 0 ; 155for (size_t i = 0 ;i < ref .size ();++ i ) { 156float v ; 157 std::memcpy (& v ,& ref [i ],4 ); 158if (std::isfinite (v ))++ written ; 159 } 160if (written == 0 ) { 161 std::printf ("layer %d: the expert launch wrote nothing (%zu outputs, none finite) - refusing to time it\n" , 162 (int )l ,ref .size ()); 163++ failures ; 164continue ; 165 } 166 } 167s -> memset (dout ,0xff ,got .size ()* 4 ).wait (); 168run (mode ,0 ); 169if (!sync ("mode window" ))return 1 ; 170s -> memcpy (got .data (),dout ,got .size ()* 4 ).wait (); 171size_t ndiff = 0 ; 172double worst = 0 ; 173for (size_t i = 0 ;i < ref .size ();++ i ) 174if (std::memcmp (& ref [i ],& got [i ],4 )!= 0 ) { 175++ ndiff ; 176worst = std::fmax (worst , std::fabs ((double )ref [i ]- got [i ]) / (std::fabs ((double )ref [i ])+ 1e-6 )); 177 } 178auto time = [& ](int m ,int phase ) { 179for (int i = 0 ;i < 10 ;++ i )run (m ,phase ); 180s -> wait (); 181const auto t0 = std::chrono::steady_clock::now (); 182for (int i = 0 ;i < iters ;++ i )run (m ,phase ); 183sync ("timing loop" ); 184const double ms = 185 std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 186// SYCL port: the printout below and the GB/s it derives both assume MICROseconds ("all %7.1f us", 187// bytes / us / 1e3), but this returned milliseconds - so every published number was 1000x too small in 188// time and 1000x too large in bandwidth (0.1 "us" and 148,713 GB/s for a launch that takes ~100 us and 189// moves 19.7 MB at ~198 GB/s). The wall clock always agreed with the real work: 200 iterations 618 ms, 190// 2,000 iterations 3,566 ms. Return microseconds. 191return ms * 1e3 /iters ; 192 }; 193const double gu_b = (double )G * 2 * f .up_off ,d_b = (double )G * dsz ; 194double us [2 ][3 ]= {{1e30 ,1e30 ,1e30 }, {1e30 ,1e30 ,1e30 }}; 195for (int round = 0 ;round < 3 ;++ round )// interleaved, the minimum: the clocks wander between runs 196for (int k = 0 ;k < 2 ;++ k ) 197for (int p = 0 ;p < 3 ;++ p )us [k ][p ]= std::fmin (us [k ][p ],time (k ?mode :mref ,p )); 198 std::printf ("layer %2d %-7s/%-6s G %d T %d | mode %d: all %7.1f us gu %7.1f us (%3.0f GB/s) down %6.1f us (%3.0f GB/s)" 199" | mode %d: all %7.1f us gu %7.1f us (%3.0f GB/s) down %6.1f us (%3.0f GB/s)" 200" | hash %016llx | %s\n" , 201l ,ggml_type_name ((ggml_type )f .gu_type ),ggml_type_name ((ggml_type )f .d_type ),G ,T , 202mref ,us [0 ][0 ],us [0 ][1 ],gu_b /us [0 ][1 ] /1e3 ,us [0 ][2 ],d_b /us [0 ][2 ] /1e3 , 203mode ,us [1 ][0 ],us [1 ][1 ],gu_b /us [1 ][1 ] /1e3 ,us [1 ][2 ],d_b /us [1 ][2 ] /1e3 , 204 (unsigned long long )fnv1a (got .data (),got .size ()* sizeof (float )), 205ndiff ?"DIFFERS" :"bitwise equal" ); 206if (ndiff ) { 207 std::printf (" %zu of %zu outputs differ, worst rel %.2e\n" ,ndiff ,ref .size (),worst ); 208++ failures ; 209 } 210K ::native_expert_set_mode (-1 ,0 ); 211for (void * p : {(void * )dblob , (void * )dx , (void * )dxq , (void * )dscr , (void * )dout , (void * )dptr , 212 (void * )dstart , (void * )dn , (void * )ddst , (void * )dtok }) 213 sycl::free (p ,* s ); 214 } 215 std::printf ("native_expert_bench: %d differing\n" ,failures ); 216return failures ?1 :0 ; 217}