okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
main
1// src/kernels/resident_plan_parity.cpp - the verify window's device-built expert plan (#783 PR-e: the parallel 2// grouping and the prefix scan that replaced thread 0's serial pass) against a host replay of its contract: 3// distinct experts in routing order, each with its entries ascending, `ptr[g]` = the expert's slot, counts / start / 4// dst / tok / start2 in the host pool's layout. 5// 6// n = 1..80 entries (a window of up to kVerifyMaxT tokens x 10 experts), ids drawn from pools of 3..512 experts so 7// duplicates run from "every entry the same expert" to "all distinct", with and without a slot table (`slot_off`). 8// Both modes are run: with a `skip` word (ring when the plan was built, 0 when an expert was not resident - and 9// then the plan is untouched) and without one (the all-resident graph: a non-resident expert gives an empty plan and 10// sets *plan_err). 11// Every word of the plan the host pool would read is compared with memcmp. GPU, synthetic, no model. 12#define DPCT_PROFILING_ENABLED 13#include <sycl/sycl.hpp> 14#include <dpct/dpct.hpp> 15#include "strata/kernels/verify_kernels.hpp" 16 17#include <cstdint> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <random> 22#include <vector> 23 24namespace k= strata::kernels; 25 26namespace { 27void ck (dpct::err0 e ,const char * w ) { 28} 29}// namespace 30 31int main () { 32const int K = 10 ,NE = 512 ; 33const long long capx = (long long ) k::kVerifyMaxT * K ; 34const long long ptr_off = ((4 + (capx + 1 )+ 2 * capx )+ 1 )& ~1ll ; 35const size_t plan_words = (size_t ) (ptr_off + 4 * capx + capx + 16 ); 36 std::mt19937 rng (783 ); 37const long long blob = 1337 ; 38uint8_t * const cache_base = reinterpret_cast < uint8_t *> (uintptr_t (0x40000000 ));// never dereferenced 39 40int32_t * d_ids ,* d_res ,* d_plan ; 41unsigned long long * d_off ; 42uint32_t * d_skip ; 43ck (DPCT_CHECK_ERROR (d_ids = (int32_t * )sycl::malloc_device ( 44128 * 4 , dpct::get_in_order_queue ())), 45"ids" ); 46ck (DPCT_CHECK_ERROR (d_res = (int32_t * )sycl::malloc_device ( 47NE * 4 , dpct::get_in_order_queue ())), 48"res" ); 49ck (DPCT_CHECK_ERROR (d_off = (unsigned long long * )sycl::malloc_device ( 501024 * 8 , dpct::get_in_order_queue ())), 51"off" ); 52ck (DPCT_CHECK_ERROR (d_plan = (int32_t * )sycl::malloc_device ( 53plan_words * 4 , dpct::get_in_order_queue ())), 54"plan" ); 55ck (DPCT_CHECK_ERROR (d_skip = (uint32_t * )sycl::malloc_device ( 564 , dpct::get_in_order_queue ())), 57"skip" ); 58uint32_t * d_err = nullptr ; 59ck (DPCT_CHECK_ERROR (d_err = (uint32_t * )sycl::malloc_device ( 604 , dpct::get_in_order_queue ())), 61"err" ); 62 63 std::vector < int32_t > res (NE ); 64for (int e = 0 ;e < NE ;++ e )res [e ]= (e * 37 + 11 ) %1000 ;// distinct slots, not the identity 65 std::vector < unsigned long long > off (1024 ); 66for (int s = 0 ;s < 1024 ;++ s )off [s ]= (unsigned long long )s * 4096 + (s %7 )* 64 ; 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*/ 73ck (DPCT_CHECK_ERROR ( 74 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_res ,res .data (),NE * 4 ).wait ()), 75"res" ); 76/* 77DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 78in the original code the source host memory is pageable memory. If the 79memory is not pageable, call wait() on event return by memcpy API to ensure 80synchronization behavior. 81*/ 82ck (DPCT_CHECK_ERROR ( 83 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_off ,off .data (),1024 * 8 ).wait ()), 84"off" ); 85 86int cases = 0 ,bad = 0 ; 87for (int n = 1 ;n <=80 ;++ n ) { 88for (int variant = 0 ;variant < 4 ;++ variant ) { 89const int pool = variant == 0 ?3 : (variant == 1 ?12 : (variant == 2 ?40 :512 )); 90 std::vector < int32_t > ids (n ); 91for (int i = 0 ;i < n ;++ i )ids [i ]= (int32_t ) ((rng () %pool )* (512 /pool )); 92const bool use_off = (n + variant ) %2 == 1 ; 93/* 94DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 95assuming in the original code the source host memory is pageable 96memory. If the memory is not pageable, call wait() on event return 97by memcpy API to ensure synchronization behavior. 98*/ 99ck (DPCT_CHECK_ERROR ( 100 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_ids ,ids .data (),n * 4 ).wait ()), 101"ids" ); 102 103// host replay 104 std::vector < int32_t > counts = {0 ,n ,0 },start ,dst (n ),tok (n ); 105 std::vector < unsigned long long > ptr ; 106 std::vector < int > first_seen ;// group -> expert 107for (int i = 0 ;i < n ;++ i ) { 108int g = -1 ; 109for (size_t q = 0 ;q < first_seen .size ();++ q ) 110if (first_seen [q ]== ids [i ])g = (int )q ; 111if (g < 0 ) {first_seen .push_back (ids [i ]);g = (int )first_seen .size ()- 1 ; } 112 } 113const int groups = (int )first_seen .size (); 114counts [0 ]= groups ; 115start .assign (groups + 1 ,0 ); 116int pos = 0 ; 117for (int g = 0 ;g < groups ;++ g ) { 118start [g ]= pos ; 119ptr .push_back ((unsigned long long ) (uintptr_t )cache_base + 120 (use_off ?off [res [first_seen [g ]]] : (unsigned long long )res [first_seen [g ]]* (unsigned long long )blob )); 121for (int i = 0 ;i < n ;++ i ) 122if (ids [i ]== first_seen [g ]) {dst [pos ]= i ;tok [pos ]= i /K ;++ pos ; } 123 } 124start [groups ]= n ; 125 126for (int mode = 0 ;mode < 2 ;++ mode ) {// 0: general kernel with a skip word, 1: all resident 127ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 128 .memset (d_plan ,0xAB ,plan_words * 4 ) 129 .wait ()), 130"fill" ); 131ck (DPCT_CHECK_ERROR ( 132 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (d_skip ,0 ,4 ).wait ()), 133"skip0" ); 134ck (DPCT_CHECK_ERROR ( 135 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (d_err ,0 ,4 ).wait ()), 136"err0" ); 137 k::resident_plan (d_ids ,n ,K ,d_res ,NE ,cache_base ,use_off ?d_off :nullptr ,blob ,d_plan ,capx , 138mode == 0 ?d_skip :nullptr ,7u ,nullptr ,d_err ); 139ck (DPCT_CHECK_ERROR ( 140 dpct::get_current_device ().queues_wait_and_throw ()), 141"sync" ); 142 std::vector < int32_t > plan (plan_words ); 143ck (DPCT_CHECK_ERROR ( 144 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 145 .memcpy (plan .data (),d_plan ,plan_words * 4 ) 146 .wait ()), 147"plan" ); 148uint32_t skip = 0 ; 149ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 150 .memcpy (& skip ,d_skip ,4 ) 151 .wait ()), 152"skipr" ); 153bool ok = mode == 1 || skip == 7u ; 154ok = ok && std::memcmp (plan .data (),counts .data (),3 * 4 )== 0 ; 155ok = ok && std::memcmp (plan .data ()+ 4 ,start .data (), (size_t ) (groups + 1 )* 4 )== 0 ; 156const int32_t * pdst = plan .data ()+ 4 + capx + 1 ; 157ok = ok && std::memcmp (pdst ,dst .data (), (size_t )n * 4 )== 0 ; 158ok = ok && std::memcmp (pdst + capx ,tok .data (), (size_t )n * 4 )== 0 ; 159ok = ok && std::memcmp (plan .data ()+ ptr_off ,ptr .data (), (size_t )groups * 8 )== 0 ; 160ok = ok && plan [(size_t ) (ptr_off + 4 * capx )]== n ; 161++ cases ; 162if (!ok ) { 163 std::printf (" *** n=%d pool=%d %s %s: the plan differs from the host replay ***\n" ,n ,pool , 164use_off ?"slot_off" :"blob" ,mode ?"all_resident" :"general" ); 165++ bad ; 166 } 167 } 168 } 169 } 170 171// a routed expert that is not resident: the general kernel leaves the plan alone and zeroes *skip 172 { 173 std::vector < int32_t > res2 = res ; 174res2 [5 ]= -1 ; 175/* 176DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 177assuming in the original code the source host memory is pageable memory. 178If the memory is not pageable, call wait() on event return by memcpy API 179to ensure synchronization behavior. 180*/ 181ck (DPCT_CHECK_ERROR ( 182 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_res ,res2 .data (),NE * 4 ).wait ()), 183"res2" ); 184const int32_t ids [10 ]= {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 }; 185/* 186DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 187assuming in the original code the source host memory is pageable memory. 188If the memory is not pageable, call wait() on event return by memcpy API 189to ensure synchronization behavior. 190*/ 191ck (DPCT_CHECK_ERROR ( 192 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_ids ,ids ,sizeof ids ).wait ()), 193"ids" ); 194ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 195 .memset (d_plan ,0xAB ,plan_words * 4 ) 196 .wait ()), 197"fill" ); 198const uint32_t seven = 7 ; 199ck (DPCT_CHECK_ERROR ( 200 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_skip ,& seven ,4 ).wait ()), 201"skip7" ); 202 k::resident_plan (d_ids ,10 ,K ,d_res ,NE ,cache_base ,nullptr ,blob ,d_plan ,capx ,d_skip ,7u ,nullptr ,d_err ); 203ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 204"sync" ); 205 std::vector < int32_t > plan (plan_words ); 206ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 207 .memcpy (plan .data (),d_plan ,plan_words * 4 ) 208 .wait ()), 209"plan" ); 210uint32_t skip = 99 ; 211ck (DPCT_CHECK_ERROR ( 212 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (& skip ,d_skip ,4 ).wait ()), 213"skipr" ); 214bool untouched = true; 215for (int32_t w :plan )untouched = untouched && (uint32_t )w == 0xABABABABu ; 216++ cases ; 217if (skip != 0 || !untouched ) { 218 std::printf (" *** a non-resident expert: skip=%u untouched=%d (want 0 / 1) ***\n" ,skip , (int )untouched ); 219++ bad ; 220 } 221 } 222 223 std::printf ("resident_plan: %d cases, %d failures\n" ,cases ,bad ); 224if (bad )return 1 ; 225 std::printf ("resident_plan_parity OK\n" ); 226return 0 ; 227}