okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6edbe9d6e
main
1// Is a hipHostGetDevicePointer alias usable on this stack? (#325) 2// 3// tests/hip/handoff.cpp fails on gfx1201/Windows with "separate copy/ring timeout": it does a 4// hipMemcpyAsync(..., hipMemcpyDeviceToDevice) into a hipHostGetDevicePointer alias, then waits for 5// a doorbell that copy should have published. This test pins down the properties that decide it. 6// 7// On Linux the alias is a device address of the pinned host memory, a D2D copy lands, the host sees 8// it, and a kernel reads it. On Windows (#325, RX 9070 XT) the runtime hands back the HOST pointer 9// itself (unified addressing; this test prints both addresses), and a D2D copy into it does not 10// publish - hip_handoff's timeout there. The engine itself runs on Windows (the reporter's serving 11// run gave correct answers), so kernels reading mapped memory through that address work; only the 12// copy-engine path does not. src/core/native_head.cpp keeps the token embedding in VRAM when the 13// alias is refused outright. 14// 15// Reporting, not gating: a Windows host is EXPECTED to fail some checks below, so this test exits 0 16// whenever it ran at all, and 77 (skipped) without a device. When the alias IS the host pointer the 17// D2D copy is not attempted (it may fault rather than report); the kernel read is, since that is 18// what the engine relies on. 19 20#include "strata/kernels/elementwise.hpp" 21 22#include <hip/hip_runtime.h> 23 24#include <cstdio> 25#include <vector> 26 27namespace { 28 29int not_held = 0 ; 30 31void report (const char * what ,bool holds ) { 32 std::printf (" %-44s %s\n" ,what ,holds ?"holds" :"DOES NOT HOLD" ); 33if (!holds )++ not_held ; 34} 35 36// A kernel reads `n` floats through `alias` into VRAM; the result is copied back and compared. 37bool kernel_reads (const float * alias ,const std::vector < float >& want ) { 38const int64_t n = (int64_t )want .size (); 39float * out = nullptr ; 40if (hipMalloc (reinterpret_cast < void **> (& out ),want .size ()* sizeof (float ))!= hipSuccess )return false; 41 strata::kernels::copy_from_mapped (out ,alias ,n ,nullptr ); 42 std::vector < float > got (want .size (),-1.0f ); 43const bool ran = hipDeviceSynchronize ()== hipSuccess && 44hipMemcpy (got .data (),out ,want .size ()* sizeof (float ),hipMemcpyDeviceToHost )== hipSuccess ; 45hipFree (out ); 46hipGetLastError (); 47return ran && got == want ; 48} 49 50}// namespace 51 52int main () { 53int runtime = 0 ; 54if (hipRuntimeGetVersion (& runtime )!= hipSuccess )return 1 ; 55int count = 0 ; 56if (hipGetDeviceCount (& count )!= hipSuccess || count < 1 ) { 57 std::printf ("no HIP device\n" ); 58return 77 ;// ctest "skipped" 59 } 60 std::printf ("HIP runtime %d, %d device(s)\n" ,runtime ,count ); 61 62const int64_t n = 4096 ;// enough elements for copy_from_mapped's float4 path 63const size_t bytes = size_t (n )* sizeof (float ); 64 std::vector < float > pattern (size_t (n ),0.0f ); 65for (int64_t i = 0 ;i < n ;++ i )pattern [size_t (i )]= float (i + 1 ); 66 67float * host = nullptr ; 68if (hipHostMalloc (reinterpret_cast < void **> (& host ),bytes ,hipHostMallocMapped )!= hipSuccess )return 1 ; 69void * alias = nullptr ; 70if (hipHostGetDevicePointer (& alias ,host ,0 )!= hipSuccess || alias == nullptr ) { 71 std::printf ("hipHostGetDevicePointer failed: native_head.cpp keeps the embedding in VRAM here\n" ); 72hipGetLastError (); 73hipHostFree (host ); 74return 0 ; 75 } 76const bool distinct = alias != reinterpret_cast < void *> (host ); 77 std::printf ("\n host %p\n alias %p\n -> %s\n\n" ,static_cast < void *> (host ),alias , 78distinct ?"a distinct device address" :"the host pointer itself (unified addressing)" ); 79report ("the alias is a distinct device address" ,distinct ); 80 81// What the engine relies on: a kernel reading mapped host memory through the alias. 82for (int64_t i = 0 ;i < n ;++ i )host [i ]= pattern [size_t (i )]; 83report ("a kernel reads the alias correctly" ,kernel_reads (reinterpret_cast < const float *> (alias ),pattern )); 84 85// What tests/hip/handoff.cpp attempts: a DeviceToDevice copy INTO the alias, seen by the host. 86if (distinct ) { 87float * device = nullptr ; 88 std::vector < float > twice (pattern ); 89for (float & v :twice )v *=2.0f ; 90if (hipMalloc (reinterpret_cast < void **> (& device ),bytes )== hipSuccess && 91hipMemcpy (device ,twice .data (),bytes ,hipMemcpyHostToDevice )== hipSuccess ) { 92const hipError_t copy = hipMemcpy (alias ,device ,bytes ,hipMemcpyDeviceToDevice ); 93report ("a D2D copy into the alias succeeds" ,copy == hipSuccess ); 94bool seen = hipDeviceSynchronize ()== hipSuccess ; 95for (int64_t i = 0 ;seen && i < n ;++ i )seen = host [i ]== twice [size_t (i )]; 96report ("the host sees a D2D copy into the alias" ,seen ); 97 } 98if (device )hipFree (device ); 99hipGetLastError (); 100 }else { 101 std::printf (" the D2D copy into the alias is not attempted: with alias == host it may fault rather than\n" 102" report (tests/hip/handoff's timeout on Windows)\n" ); 103 } 104 105 std::printf ("\n%d check(s) did not hold (a Windows host is expected to fail some)\n" ,not_held ); 106hipHostFree (host ); 107return 0 ; 108}