okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8f70a52cf
main
1// fp64_cost: what does FP64 (emulated on Arc Alchemist and Battlemage) cost in the engine's small reduction kernels? 2// Same one-warp-per-row sum of squares + 1/sqrt as gdn_l2_norm / gdn_out_norm / the PLE rms norm, once with a double 3// accumulator (the engine today) and once in float, plus a silu over n values the way silu_inplace does it. 4// fp64_cost [launches=4000] (the A750 needs IGC_EnableDPEmulation=1 OverrideDefaultFP64Settings=1 for the double kernels) 5#include <sycl/sycl.hpp> 6#include <chrono> 7#include <cmath> 8#include <cstdio> 9#include <cstdlib> 10#include <vector> 11 12template < class F > 13static double us_per_launch (sycl::queue & q ,int n ,F && f ) { 14for (int i = 0 ;i < 50 ;++ i )f (); 15q .wait (); 16const auto t0 = std::chrono::steady_clock::now (); 17for (int i = 0 ;i < n ;++ i )f (); 18q .wait (); 19return std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count () /n ; 20} 21 22template < class Acc > 23static void l2norm (sycl::queue & q ,float * x ,int rows ,int cols ,float eps ) { 24q .parallel_for (sycl::nd_range < 1 > (rows * 32 ,32 ), [= ](sycl::nd_item < 1 > it ) [[sycl ::reqd_sub_group_size (32 )]] { 25const int row = it .get_group (0 ); 26float * p = x + (size_t )row * cols ; 27auto sg = it .get_sub_group (); 28Acc acc = 0 ; 29for (int i = it .get_local_id (0 );i < cols ;i += 32 )acc += (Acc )p [i ]* (Acc )p [i ]; 30for (int off = 16 ;off > 0 ;off >>=1 )acc += sycl::shift_group_left (sg ,acc ,off ); 31acc = sycl::group_broadcast (sg ,acc ,0 ); 32const float inv = (float ) ((Acc )1 / sycl::sqrt (acc + (Acc )eps )); 33for (int i = it .get_local_id (0 );i < cols ;i += 32 )p [i ] *=inv ; 34 }); 35} 36 37template < class Acc > 38static void silu (sycl::queue & q ,float * x ,int n ) { 39q .parallel_for (sycl::nd_range < 1 > ((n + 255 ) /256 * 256 ,256 ), [= ](sycl::nd_item < 1 > it ) { 40const int i = it .get_global_id (0 ); 41if (i >=n )return ; 42const Acc v = (Acc )x [i ]; 43x [i ]= (float ) (v / ((Acc )1 + sycl::exp (- v ))); 44 }); 45} 46 47 48// the sampler's tail: top_p cut and temperature draw over k kept logits, one warp (like sampled_tail_warp), 3 exps per element 49template < class Acc > 50static void sampler_tail (sycl::queue & q ,const float * sel ,int * out ,int k ,float top_p ,float u ) { 51q .parallel_for (sycl::nd_range < 1 > (32 ,32 ), [= ](sycl::nd_item < 1 > it ) [[sycl ::reqd_sub_group_size (32 )]] { 52const int lane = it .get_local_id (0 ); 53Acc sum = 0 ; 54float mx = sel [0 ]; 55for (int i = lane ;i < k ;i += 32 )sum += sycl::exp ((Acc )sel [i ]- (Acc )mx ); 56sum = sycl::reduce_over_group (it .get_sub_group (),sum , sycl::plus < Acc > ()); 57Acc cum = 0 ; 58int cut = k ; 59if (lane == 0 ) { 60for (int i = 0 ;i < k ;++ i ) {cum += sycl::exp ((Acc )sel [i ]- (Acc )mx ) /sum ;if (cum >= (Acc )top_p ) {cut = i + 1 ;break ; } } 61Acc c2 = 0 ;int pick = cut - 1 ; 62for (int i = 0 ;i < cut ;++ i ) {c2 += sycl::exp ((Acc )sel [i ]- (Acc )mx ) /sum ;if ((Acc )u < c2 ) {pick = i ;break ; } } 63out [0 ]= pick ; 64 } 65 }); 66} 67 68int main (int argc ,char ** argv ) { 69const int n = argc > 1 ? std::atoi (argv [1 ]) :4000 ; 70 sycl::queue q {sycl::gpu_selector_v , sycl::property::queue::in_order {}}; 71 std::printf ("device %s, fp64 %s\n" ,q .get_device ().get_info < sycl::info::device::name > ().c_str (), 72q .get_device ().has (sycl::aspect::fp64 ) ?"yes" :"NO (double kernels skipped)" ); 73const bool fp64 = q .get_device ().has (sycl::aspect::fp64 ); 74float * x = sycl::malloc_device < float > (1 <<20 ,q ); 75q .fill (x ,0.5f ,1 <<20 ).wait (); 76struct Shape {const char * what ;int rows ,cols ; }; 77const Shape shapes []= {{"gdn l2 norm (rows 48, cols 128)" ,48 ,128 }, {"gdn out norm (rows 48, cols 128)" ,48 ,128 }, 78 {"ple rms (rows 8, cols 2560)" ,8 ,2560 }, {"wide (rows 6, cols 2560)" ,6 ,2560 }}; 79for (const Shape & s :shapes ) { 80const double f = us_per_launch (q ,n , [& ] {l2norm < float > (q ,x ,s .rows ,s .cols ,1e-6f ); }); 81double d = -1 ; 82if (fp64 )d = us_per_launch (q ,n , [& ] {l2norm < double > (q ,x ,s .rows ,s .cols ,1e-6f ); }); 83 std::printf ("%-34s float %6.2f us double %6.2f us\n" ,s .what ,f ,d ); 84 } 85for (int cnt : {2560 ,10240 ,61440 }) { 86const double f = us_per_launch (q ,n , [& ] {silu < float > (q ,x ,cnt ); }); 87double d = -1 ; 88if (fp64 )d = us_per_launch (q ,n , [& ] {silu < double > (q ,x ,cnt ); }); 89 std::printf ("silu over %-24d float %6.2f us double %6.2f us\n" ,cnt ,f ,d ); 90 } 91 { 92float * sel = sycl::malloc_device < float > (64 ,q ); 93int * o = sycl::malloc_device < int > (1 ,q ); 94 std::vector < float > h (64 ); 95for (int i = 0 ;i < 64 ;++ i )h [i ]= 10.f - 0.3f * i ; 96q .memcpy (sel ,h .data (),64 * 4 ).wait (); 97for (int k : {20 ,64 }) { 98const double f = us_per_launch (q ,n , [& ] {sampler_tail < float > (q ,sel ,o ,k ,0.95f ,0.5f ); }); 99double d = -1 ; 100if (fp64 )d = us_per_launch (q ,n , [& ] {sampler_tail < double > (q ,sel ,o ,k ,0.95f ,0.5f ); }); 101 std::printf ("sampler tail, k=%-21d float %6.2f us double %6.2f us\n" ,k ,f ,d ); 102 } 103 } 104return 0 ; 105}