okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5438023a6
main
1// src/kernels/fused_gr_bench.cpp - fused_gr_read_multi: the AMD fast kernels (STRATA_GR_FAST) against the old 2// ones, bit for bit on every output (lo, rs, inject, mixed, the in-place R), and each one's time. 3// 4// build/fused_gr_bench [iters] [T min] [T max] 5// On SYCL the fast setter is a no-op; compare hashes across builds or environment settings. 6#define DPCT_PROFILING_ENABLED 7#include <sycl/sycl.hpp> 8#include <dpct/dpct.hpp> 9#include "strata/kernels/fused_gr.hpp" 10 11#include <cmath> 12#include <cstdint> 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <random> 17#include <vector> 18 19namespace K = strata::kernels; 20 21static uint16_t bf16 (float x ) { 22uint32_t u ; 23 std::memcpy (& u ,& x ,4 ); 24return (uint16_t ) ((u + 0x7fff + ((u >>16 )& 1 )) >>16 ); 25} 26 27int main (int argc ,char ** argv ) { 28setvbuf (stdout ,nullptr ,_IONBF ,0 ); 29const int iters = argc > 1 ? std::atoi (argv [1 ]) :500 ; 30const int t_lo = argc > 2 ? std::atoi (argv [2 ]) :1 ,t_hi = argc > 3 ? std::atoi (argv [3 ]) :6 ; 31const int N = 2560 ,HC = 4 ,D = N * HC ,LR = 320 ,TM = K ::kFusedGrMaxT ; 32 std::mt19937 rng (7 ); 33 std::normal_distribution < float > nd (0.f ,1.f ); 34auto fill = [& ](std::vector < float >& v ,float sc ) {for (auto & x :v )x = sc * nd (rng ); }; 35 std::vector < float > R ((size_t )TM * D ),bo ((size_t )TM * N ),inj ((size_t )TM * HC ),wn (D ); 36fill (R ,1.0f );fill (bo ,0.5f );fill (inj ,1.0f );fill (wn ,0.3f ); 37for (auto & x :wn )x += 1.0f ; 38 std::vector < uint16_t > wd ((size_t )LR * D ),wu ((size_t )D * LR ),wi ((size_t )HC * D ); 39for (auto & x :wd )x = bf16 (0.02f * nd (rng )); 40for (auto & x :wu )x = bf16 (0.05f * nd (rng )); 41for (auto & x :wi )x = bf16 (0.02f * nd (rng )); 42float * dR ,* dbo ,* dinj ,* dwn ,* dxn ,* dlo ,* drs ,* dio ,* dmix ; 43uint16_t * dwd ,* dwu ,* dwi ; 44dR = (float * )sycl::malloc_device (R .size ()* 4 , dpct::get_in_order_queue ()); 45dbo = 46 (float * )sycl::malloc_device (bo .size ()* 4 , dpct::get_in_order_queue ()); 47dinj = (float * )sycl::malloc_device (inj .size ()* 4 , 48 dpct::get_in_order_queue ()); 49dwn = 50 (float * )sycl::malloc_device (wn .size ()* 4 , dpct::get_in_order_queue ()); 51dxn = (float * )sycl::malloc_device ((size_t )TM * D * 4 , 52 dpct::get_in_order_queue ()); 53dlo = (float * )sycl::malloc_device ((size_t )TM * LR * 4 , 54 dpct::get_in_order_queue ()); 55drs = (float * )sycl::malloc_device ((size_t )TM * HC * 4 , 56 dpct::get_in_order_queue ()); 57dio = (float * )sycl::malloc_device ((size_t )TM * HC * 4 , 58 dpct::get_in_order_queue ()); 59dmix = (float * )sycl::malloc_device ((size_t )TM * N * 4 , 60 dpct::get_in_order_queue ()); 61dwd = (uint16_t * )sycl::malloc_device (wd .size ()* 2 , 62 dpct::get_in_order_queue ()); 63dwu = (uint16_t * )sycl::malloc_device (wu .size ()* 2 , 64 dpct::get_in_order_queue ()); 65dwi = (uint16_t * )sycl::malloc_device (wi .size ()* 2 , 66 dpct::get_in_order_queue ()); 67/* 68DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 69in the original code the source host memory is pageable memory. If the 70memory is not pageable, call wait() on event return by memcpy API to ensure 71synchronization behavior. 72*/ 73 dpct::get_in_order_queue ().memcpy (dbo ,bo .data (),bo .size ()* 4 ).wait (); 74/* 75DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 76in the original code the source host memory is pageable memory. If the 77memory is not pageable, call wait() on event return by memcpy API to ensure 78synchronization behavior. 79*/ 80 dpct::get_in_order_queue ().memcpy (dinj ,inj .data (),inj .size ()* 4 ).wait (); 81/* 82DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 83in the original code the source host memory is pageable memory. If the 84memory is not pageable, call wait() on event return by memcpy API to ensure 85synchronization behavior. 86*/ 87 dpct::get_in_order_queue ().memcpy (dwn ,wn .data (),wn .size ()* 4 ).wait (); 88/* 89DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 90in the original code the source host memory is pageable memory. If the 91memory is not pageable, call wait() on event return by memcpy API to ensure 92synchronization behavior. 93*/ 94 dpct::get_in_order_queue ().memcpy (dwd ,wd .data (),wd .size ()* 2 ).wait (); 95/* 96DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 97in the original code the source host memory is pageable memory. If the 98memory is not pageable, call wait() on event return by memcpy API to ensure 99synchronization behavior. 100*/ 101 dpct::get_in_order_queue ().memcpy (dwu ,wu .data (),wu .size ()* 2 ).wait (); 102/* 103DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 104in the original code the source host memory is pageable memory. If the 105memory is not pageable, call wait() on event return by memcpy API to ensure 106synchronization behavior. 107*/ 108 dpct::get_in_order_queue ().memcpy (dwi ,wi .data (),wi .size ()* 2 ).wait (); 109 dpct::queue_ptr s ; 110s = dpct::get_current_device ().create_queue (true); 111 dpct::event_ptr e0 ,e1 ; 112e0 = new sycl::event (); 113e1 = new sycl::event (); 114int failures = 0 ; 115for (int T = t_lo ;T <=t_hi ;++ T ) 116for (int apply = 0 ;apply < 2 ;++ apply ) 117for (int inject = 0 ;inject < 2 ;++ inject ) { 118K ::FusedGrArgs a [TM ]; 119for (int t = 0 ;t < T ;++ t ) { 120a [t ].R = dR + (size_t )t * D ;a [t ].R_out = dR + (size_t )t * D ;a [t ].apply = apply ; 121a [t ].bo_prev = dbo + (size_t )t * N ;a [t ].inj_prev = dinj + (size_t )t * HC ; 122a [t ].w_norm = dwn ;a [t ].w_down = dwd ;a [t ].w_up = dwu ;a [t ].w_inject = inject ?dwi :nullptr ; 123a [t ].lo = dlo + (size_t )t * LR ;a [t ].rs = drs + (size_t )t * HC ; 124a [t ].inject_out = dio + (size_t )t * HC ;a [t ].mixed = dmix + (size_t )t * N ; 125 } 126 std::vector < float > out [2 ]; 127for (int f = 0 ;f < 2 ;++ f ) { 128 dpct::get_in_order_queue () 129 .memcpy (dR ,R .data (),R .size ()* 4 ) 130 .wait (); 131 dpct::get_in_order_queue () 132 .memset (dlo ,0xff , (size_t )TM * LR * 4 ) 133 .wait (); 134 dpct::get_in_order_queue () 135 .memset (drs ,0xff , (size_t )TM * HC * 4 ) 136 .wait (); 137 dpct::get_in_order_queue () 138 .memset (dio ,0xff , (size_t )TM * HC * 4 ) 139 .wait (); 140 dpct::get_in_order_queue () 141 .memset (dmix ,0xff , (size_t )TM * N * 4 ) 142 .wait (); 143K ::fused_gr_set_fast (f ); 144K ::fused_gr_read_multi (a ,T ,dxn ,s ); 145s -> wait (); 146auto & o = out [f ]; 147o .resize ((size_t )TM * (D + LR + HC + HC + N )); 148float * p = o .data (); 149 dpct::get_in_order_queue () 150 .memcpy (p ,dR , (size_t )TM * D * 4 ) 151 .wait (); 152p += (size_t )TM * D ; 153 dpct::get_in_order_queue () 154 .memcpy (p ,dlo , (size_t )TM * LR * 4 ) 155 .wait (); 156p += (size_t )TM * LR ; 157 dpct::get_in_order_queue () 158 .memcpy (p ,drs , (size_t )TM * HC * 4 ) 159 .wait (); 160p += (size_t )TM * HC ; 161 dpct::get_in_order_queue () 162 .memcpy (p ,dio , (size_t )TM * HC * 4 ) 163 .wait (); 164p += (size_t )TM * HC ; 165 dpct::get_in_order_queue () 166 .memcpy (p ,dmix , (size_t )TM * N * 4 ) 167 .wait (); 168 } 169const bool same = std::memcmp (out [0 ].data (),out [1 ].data (),out [0 ].size ()* 4 )== 0 ; 170double us [2 ]= {1e30 ,1e30 }; 171for (int round = 0 ;round < 3 ;++ round ) 172for (int f = 0 ;f < 2 ;++ f ) { 173K ::fused_gr_set_fast (f ); 174for (int i = 0 ;i < 20 ;++ i )K ::fused_gr_read_multi (a ,T ,dxn ,s ); 175 dpct::sync_barrier (e0 ,s ); 176for (int i = 0 ;i < iters ;++ i )K ::fused_gr_read_multi (a ,T ,dxn ,s ); 177 dpct::sync_barrier (e1 ,s ); 178e1 -> wait_and_throw (); 179float ms = 0 ; 180ms = (e1 -> get_profiling_info < 181 sycl::info::event_profiling::command_end > ()- 182e0 -> get_profiling_info < 183 sycl::info::event_profiling:: 184command_start > ()) / 1851000000.0f ; 186us [f ]= std::fmin (us [f ],1e3 * ms /iters ); 187 } 188uint64_t hash = 1469598103934665603ull ; 189for (float value :out [1 ]) { 190uint32_t bits ; 191 std::memcpy (& bits ,& value ,sizeof (bits )); 192hash = (hash ^bits )* 1099511628211ull ; 193 } 194 std::printf ("T %d apply %d inject %d | old %6.1f us fast %6.1f us | %s | hash %016llx\n" , 195T ,apply ,inject ,us [0 ],us [1 ],same ?"bitwise equal" :"DIFFERS" , 196 (unsigned long long )hash ); 197if (!same )++ failures ; 198 } 199 std::printf ("fused_gr_bench: %d differing\n" ,failures ); 200return failures ?1 :0 ; 201}