okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
36ef9d6e6
main
1// Real asynchronous CPU/GPU handoff and graph replay, including changing payloads. 2#include <hip/hip_runtime.h> 3#include "strata/kernels/elementwise.hpp" 4#include <atomic> 5#include <chrono> 6#include <cstdio> 7#include <thread> 8#include <vector> 9#define CHECK (x ) do {auto e=(x);if(e!=hipSuccess){std::fprintf(stderr,"%s: %s\n",#x,hipGetErrorString(e));return 2;}} while(0) 10int main (){ 11constexpr int N = 4096 ,rounds = 100 ; 12float * host = nullptr ,* mapped = nullptr ,* device = nullptr ; 13uint32_t * flag = nullptr ,* seq = nullptr ,* dflag = nullptr ,* dseq = nullptr ; 14CHECK (hipHostMalloc ((void ** )& host ,N * 4 ,hipHostMallocMapped )); 15CHECK (hipHostMalloc ((void ** )& flag ,4 ,hipHostMallocMapped )); 16CHECK (hipHostMalloc ((void ** )& seq ,4 ,hipHostMallocMapped )); 17CHECK (hipHostGetDevicePointer ((void ** )& mapped ,host ,0 )); 18CHECK (hipHostGetDevicePointer ((void ** )& dflag ,flag ,0 )); 19CHECK (hipHostGetDevicePointer ((void ** )& dseq ,seq ,0 )); 20CHECK (hipMalloc ((void ** )& device ,N * 4 )); 21hipStream_t stream ;CHECK (hipStreamCreateWithFlags (& stream ,hipStreamNonBlocking )); 22hipGraph_t graph ;hipGraphExec_t exec ; 23* flag = 0 ;* seq = 1 ; 24CHECK (hipStreamBeginCapture (stream ,hipStreamCaptureModeThreadLocal )); 25 strata::kernels::doorbell_wait (dflag ,dseq ,(void * )stream ); 26 strata::kernels::copy_from_mapped (device ,mapped ,N ,(void * )stream ); 27 strata::kernels::scale_inplace (device ,N ,2.0f ,(void * )stream ); 28CHECK (hipStreamEndCapture (stream ,& graph ));CHECK (hipGraphInstantiateWithFlags (& exec ,graph ,0 )); 29 std::vector < float > got (N ); 30for (int r = 1 ;r <=rounds ;r ++ ){ 31* seq = r ; 32CHECK (hipGraphLaunch (exec ,stream )); 33// Producer deliberately arrives after the GPU consumer; a missing wait must fail. 34 std::this_thread::sleep_for (std::chrono::microseconds (50 + (r %17 )* 10 )); 35for (int i = 0 ;i < N ;i ++ )host [i ]= (float )(r * 10000 + i ); 36__atomic_store_n (flag ,(uint32_t )r ,__ATOMIC_RELEASE ); 37CHECK (hipStreamSynchronize (stream )); 38CHECK (hipMemcpy (got .data (),device ,N * 4 ,hipMemcpyDeviceToHost )); 39for (int i = 0 ;i < N ;i ++ )if (got [i ]!= (float )(r * 10000 + i )* 2.0f ){std::fprintf (stderr ,"stale payload r=%d i=%d got=%f\n" ,r ,i ,got [i ]);return 1 ;} 40 } 41CHECK (hipGraphExecDestroy (exec ));CHECK (hipGraphDestroy (graph )); 42// The production `moe_route` fallback records three device-to-device copies into 43// mapped pinned buffers, then launches `doorbell_ring` as a separate graph node. 44// Prove that seeing the increment publishes the preceding payload to the CPU. 45float * copy_out = nullptr ,* mapped_copy_out = nullptr ,* copy_weights = nullptr ,* mapped_copy_weights = nullptr ; 46int32_t * copy_ids = nullptr ,* mapped_copy_ids = nullptr ,* copy_ids_device = nullptr ; 47float * copy_weights_device = nullptr ; 48uint32_t * copy_seq = nullptr ,* device_copy_seq = nullptr ; 49CHECK (hipHostMalloc ((void ** )& copy_out ,N * 4 ,hipHostMallocMapped )); 50CHECK (hipHostMalloc ((void ** )& copy_ids ,10 * 4 ,hipHostMallocMapped )); 51CHECK (hipHostMalloc ((void ** )& copy_weights ,10 * 4 ,hipHostMallocMapped )); 52CHECK (hipHostMalloc ((void ** )& copy_seq ,4 ,hipHostMallocMapped )); 53CHECK (hipHostGetDevicePointer ((void ** )& mapped_copy_out ,copy_out ,0 )); 54CHECK (hipHostGetDevicePointer ((void ** )& mapped_copy_ids ,copy_ids ,0 )); 55CHECK (hipHostGetDevicePointer ((void ** )& mapped_copy_weights ,copy_weights ,0 )); 56CHECK (hipHostGetDevicePointer ((void ** )& device_copy_seq ,copy_seq ,0 )); 57CHECK (hipMalloc ((void ** )& copy_ids_device ,10 * 4 )); 58CHECK (hipMalloc ((void ** )& copy_weights_device ,10 * 4 )); 59* copy_seq = 0 ; 60 std::vector < int32_t > copy_route_ids (10 );std::vector < float > copy_route_weights (10 ); 61CHECK (hipStreamBeginCapture (stream ,hipStreamCaptureModeThreadLocal )); 62CHECK (hipMemcpyAsync (mapped_copy_out ,device ,N * 4 ,hipMemcpyDeviceToDevice ,stream )); 63CHECK (hipMemcpyAsync (mapped_copy_ids ,copy_ids_device ,10 * 4 ,hipMemcpyDeviceToDevice ,stream )); 64CHECK (hipMemcpyAsync (mapped_copy_weights ,copy_weights_device ,10 * 4 ,hipMemcpyDeviceToDevice ,stream )); 65 strata::kernels::doorbell_ring (device_copy_seq ,(void * )stream ); 66CHECK (hipStreamEndCapture (stream ,& graph ));CHECK (hipGraphInstantiateWithFlags (& exec ,graph ,0 )); 67for (int r = 1 ;r <=rounds ;r ++ ){ 68for (int i = 0 ;i < N ;i ++ )got [i ]= (float )(r * 20000 + i ); 69for (int i = 0 ;i < 10 ;i ++ ){copy_route_ids [i ]= r * 10 + i ;copy_route_weights [i ]= r + i * .125f ;} 70CHECK (hipMemcpyAsync (device ,got .data (),N * 4 ,hipMemcpyHostToDevice ,stream )); 71CHECK (hipMemcpyAsync (copy_ids_device ,copy_route_ids .data (),10 * 4 ,hipMemcpyHostToDevice ,stream )); 72CHECK (hipMemcpyAsync (copy_weights_device ,copy_route_weights .data (),10 * 4 ,hipMemcpyHostToDevice ,stream )); 73CHECK (hipGraphLaunch (exec ,stream )); 74const auto deadline = std::chrono::steady_clock::now ()+ std::chrono::seconds (2 ); 75// Do not query or synchronize the stream before validating the mapped payload. 76while (__atomic_load_n (copy_seq ,__ATOMIC_ACQUIRE )!= (uint32_t )r ){ 77if (std::chrono::steady_clock::now ()> deadline ){std::fprintf (stderr ,"separate copy/ring timeout\n" );return 10 ;} 78 std::this_thread::yield (); 79 } 80for (int i = 0 ;i < N ;i ++ )if (copy_out [i ]!= (float )(r * 20000 + i )){std::fprintf (stderr ,"stale separate-copy payload r=%d i=%d\n" ,r ,i );return 11 ;} 81for (int i = 0 ;i < 10 ;i ++ )if (copy_ids [i ]!= copy_route_ids [i ]|| copy_weights [i ]!= copy_route_weights [i ]){std::fprintf (stderr ,"stale separate-copy route payload r=%d i=%d\n" ,r ,i );return 12 ;} 82 } 83CHECK (hipStreamSynchronize (stream )); 84CHECK (hipGraphExecDestroy (exec ));CHECK (hipGraphDestroy (graph )); 85float * published = nullptr ,* dpublished = nullptr ,* weights = nullptr ,* dweights = nullptr ,* gw = nullptr ; 86int32_t * ids = nullptr ,* dids = nullptr ,* gi = nullptr ; 87CHECK (hipHostMalloc ((void ** )& published ,N * 4 ,hipHostMallocMapped )); 88CHECK (hipHostMalloc ((void ** )& weights ,40 ,hipHostMallocMapped )); 89CHECK (hipHostMalloc ((void ** )& ids ,40 ,hipHostMallocMapped )); 90CHECK (hipHostGetDevicePointer ((void ** )& dpublished ,published ,0 )); 91CHECK (hipHostGetDevicePointer ((void ** )& dweights ,weights ,0 )); 92CHECK (hipHostGetDevicePointer ((void ** )& dids ,ids ,0 )); 93CHECK (hipMalloc ((void ** )& gw ,40 ));CHECK (hipMalloc ((void ** )& gi ,40 )); 94 std::vector < int32_t > ids_src (10 );std::vector < float > ws (10 ); 95* flag = 0 ; 96CHECK (hipStreamBeginCapture (stream ,hipStreamCaptureModeThreadLocal )); 97 strata::kernels::doorbell_publish (device ,gi ,gw ,N ,10 ,dpublished ,dids ,dweights ,dflag ,(void * )stream ); 98CHECK (hipStreamEndCapture (stream ,& graph ));CHECK (hipGraphInstantiateWithFlags (& exec ,graph ,0 )); 99for (int r = 1 ;r <=rounds ;r ++ ){ 100for (int i = 0 ;i < N ;i ++ )got [i ]= (float )(r * 10000 + i ); 101for (int i = 0 ;i < 10 ;i ++ ){ids_src [i ]= r * 10 + i ;ws [i ]= r + i * .125f ;} 102CHECK (hipMemcpyAsync (device ,got .data (),N * 4 ,hipMemcpyHostToDevice ,stream )); 103CHECK (hipMemcpyAsync (gi ,ids_src .data (),40 ,hipMemcpyHostToDevice ,stream )); 104CHECK (hipMemcpyAsync (gw ,ws .data (),40 ,hipMemcpyHostToDevice ,stream )); 105CHECK (hipGraphLaunch (exec ,stream )); 106const auto deadline = std::chrono::steady_clock::now ()+ std::chrono::seconds (2 ); 107// No driver query or stream sync may make the payload visible to this observer. 108while (__atomic_load_n (flag ,__ATOMIC_ACQUIRE )!= (uint32_t )r ){ 109if (std::chrono::steady_clock::now ()> deadline ){std::fprintf (stderr ,"publish timeout\n" );return 7 ;} 110 std::this_thread::yield (); 111 } 112for (int i = 0 ;i < N ;i ++ )if (published [i ]!= got [i ]){std::fprintf (stderr ,"stale GPU publication\n" );return 8 ;} 113for (int i = 0 ;i < 10 ;i ++ )if (ids [i ]!= ids_src [i ]|| weights [i ]!= ws [i ])return 9 ; 114CHECK (hipStreamSynchronize (stream )); 115 } 116CHECK (hipGraphExecDestroy (exec ));CHECK (hipGraphDestroy (graph ));CHECK (hipStreamDestroy (stream )); 117CHECK (hipFree (gi ));CHECK (hipFree (gw ));CHECK (hipHostFree (published ));CHECK (hipHostFree (ids ));CHECK (hipHostFree (weights )); 118CHECK (hipFree (device ));CHECK (hipHostFree (host ));CHECK (hipHostFree (flag ));CHECK (hipHostFree (seq )); 119CHECK (hipFree (copy_weights_device ));CHECK (hipFree (copy_ids_device )); 120CHECK (hipHostFree (copy_out ));CHECK (hipHostFree (copy_ids ));CHECK (hipHostFree (copy_weights ));CHECK (hipHostFree (copy_seq )); 121 std::puts ("PASS asynchronous mapped-memory handoff: 100 CPU-to-GPU waits, 100 separate copy/rings, 100 fused publishes; 1232800 values checked" ); 122}