okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ddcc5f56d
main
1#define DPCT_COMPAT_RT_VERSION 12080 2// src/core/expert_cache.cpp - R4's slot storage and residency table. Read the 3// header first. 4#define DPCT_PROFILING_ENABLED 5#include <sycl/sycl.hpp> 6#include <dpct/dpct.hpp> 7#include "strata/sycl_queue.hpp" 8#include "strata/core/expert_cache.hpp" 9 10// #533's segmented cache uses CUDA's virtual memory management (cuMem*): not on HIP, neither the RDNA backend nor 11// the gfx906 compat build (PR #638), which compiles this file as HIP without STRATA_USE_HIP 12#define STRATA_EC_NO_VMM 1 // SYCL port: no driver virtual memory management on Level Zero (--vram-elastic is CUDA-only, #533) 13 14#if !defined(STRATA_EC_NO_VMM ) 15// #533: the virtual memory management types (the functions come through the 16// runtime's entry points) 17#endif 18 19#include <algorithm> 20#include <atomic> 21#include <set> 22#include <cstdio> 23#include <filesystem> 24#include <cstdlib> 25#include <utility> 26#include <cstring> 27 28namespace strata::core { 29 30// Local (card) memory this process holds, from the DRM clients' fdinfo (i915: drm-total-local0, xe: drm-total-vram0). 31// Level Zero's free-memory query reads the constant total on an Arc A750 (i915): the engine then sized its caches 32// from the whole card and over-allocated (UR_RESULT_ERROR_OUT_OF_HOST_MEMORY at the first launch). Other processes' 33// memory is not in this number, so it only ever LOWERS the figure the driver gave. 0 = not available. 34static unsigned long long own_drm_local_bytes () { 35#if defined(__linux__ ) 36unsigned long long total = 0 ; 37 std::error_code ec ; 38 std::set < unsigned long long > seen ;// one DRM client may appear under several fds 39for (const auto & e : std::filesystem::directory_iterator ("/proc/self/fdinfo" ,ec )) { 40 std::FILE * f = std::fopen (e .path ().string ().c_str (),"r" ); 41if (f == nullptr )continue ; 42char line [256 ]; 43bool drm = false; 44unsigned long long client = 0 ,bytes = 0 ; 45while (std::fgets (line ,sizeof line ,f )) { 46unsigned long long v = 0 ; 47char unit [8 ]= {0 }; 48if (std::strncmp (line ,"drm-driver:" ,11 )== 0 )drm = true; 49else if (std::sscanf (line ,"drm-client-id: %llu" ,& client )== 1 ) {} 50else if ((std::sscanf (line ,"drm-total-local0: %llu %7s" ,& v ,unit ) >=1 || 51 std::sscanf (line ,"drm-total-vram0: %llu %7s" ,& v ,unit ) >=1 )&& v > 0 ) { 52const unsigned long long mul = unit [0 ]== 'K' ?1024ull :unit [0 ]== 'M' ?1048576ull 53 :unit [0 ]== 'G' ?1073741824ull :1ull ; 54bytes += v * mul ; 55 } 56 } 57 std::fclose (f ); 58if (drm && bytes > 0 && seen .insert (client ).second )total += bytes ; 59 } 60return total ; 61#else 62return 0 ; 63#endif 64} 65 66// See the header. NVIDIA's DGX Spark Porting Guide (section 5.5, "Memory reporting on UMA systems") recommends the same: 67// not to rely on cudaMemGetInfo alone but to count the memory the OS can reclaim. Swap is not counted here (unlike 68// NVIDIA's reference snippet): an expert cache that pushes the system into swap would be far slower than a smaller one. 69size_t device_free_bytes ()try { 70size_t free_b = 0 ,total_b = 0 ; 71/* 72DPCT1106: 'cudaMemGetInfo' was migrated with the Intel extensions for 73device information which may not be supported by all compilers or runtimes. 74You may need to adjust the code. 75*/ 76 dpct::get_current_device ().get_memory_info (free_b ,total_b ); 77// An Arc on the xe driver whose Level Zero has no ext_intel_free_memory reports 0 free (Arc A770 16 GB, xe, #1545): 78// --expert-cache auto then sized its caches from 0 bytes. 0 with a known total means "the driver does not know", so on 79// Linux (xe is Linux-only) the card's total less what this process holds (the DRM fdinfo, as for the A750/i915 below) 80// is used instead. Like that figure it does not see other processes' memory. Elsewhere the 0 stands. 81#if defined(__linux__ ) 82if (free_b == 0 && total_b > 0 ) 83if (const unsigned long long own = own_drm_local_bytes ();own < total_b ) { 84static std::atomic < bool > said {false}; 85if (!said .exchange (true)) 86 std::fprintf (stderr ,"strata: the driver reports no free card memory (xe without ext_intel_free_memory); sizing from the DRM fdinfo (this process holds %.2f GiB)\n" , (double )own /1073741824.0 ); 87free_b = (size_t ) (total_b - own ); 88 } 89#endif 90// #1440: own_drm_local_bytes() sums every DRM fd of the process, i.e. both cards on a layer split, and would charge the 91// first card's cache to the second. With more than one GPU it runs only where the driver reports the whole card as 92// free (Arc A750/i915); a one-GPU run keeps the figure it always had. 93if (const unsigned long long own = (free_b + (16ull <<20 ) >=total_b || dpct::device_count () <=1 ) 94 ?own_drm_local_bytes () :0ull ; 95own > 0 && total_b > own && total_b - own < free_b ) { 96static std::atomic < bool > said {false}; 97if (free_b + (16ull <<20 ) >=total_b && !said .exchange (true)) 98 std::fprintf (stderr ,"strata: the driver reports the whole card as free although this process holds %.2f GiB of it (Arc A750/i915 does this); sizing from the DRM fdinfo instead\n" , (double )own /1073741824.0 ); 99free_b = (size_t ) (total_b - own ); 100 } 101#if defined(__linux__ )&& !defined(STRATA_HIP_GFX906 )// (the gfx906 compat layer has no cudaDevAttrIntegrated; that GPU is discrete) 102// per device: a box can mix an integrated GPU (an APU) with a discrete one, and the answer is the CURRENT device's 103static std::atomic < int > uma_cache [64 ];// 0 unknown, 1 integrated (unified memory), 2 discrete 104bool unified_memory = false; 105 { 106int dev = 0 ,v = 0 ; 107if (DPCT_CHECK_ERROR (dev = dpct::get_current_device_id ())== 0 && 108dev >=0 && dev < 64 ) { 109int c = uma_cache [dev ].load (std::memory_order_acquire ); 110if (c == 0 ) { 111c = DPCT_CHECK_ERROR ( 112v = dpct::get_device (dev ).get_integrated ())== 0 && 113v 114 ?1 115 :2 ; 116uma_cache [dev ].store (c , std::memory_order_release ); 117 } 118unified_memory = c == 1 ; 119 } 120 } 121if (unified_memory ) { 122if (FILE * m = std::fopen ("/proc/meminfo" ,"r" )) { 123char line [256 ]; 124unsigned long long kb = 0 ; 125while (std::fgets (line ,sizeof line ,m )) 126if (std::sscanf (line ,"MemAvailable: %llu kB" ,& kb )== 1 )break ; 127 std::fclose (m ); 128// STRATA_UMA_HEADROOM_GIB: a whole number of GiB, 0..1024; anything else keeps the default 6 (said once) 129static const long gib = [] { 130const char * h = std::getenv ("STRATA_UMA_HEADROOM_GIB" ); 131if (h == nullptr )return 6L ; 132char * end = nullptr ; 133const long v = std::strtol (h ,& end ,10 ); 134if (end != h && * end == '\0' && v >=0 && v <=1024 )return v ; 135 std::fprintf (stderr ,"strata: STRATA_UMA_HEADROOM_GIB=%s is not a whole number of GiB (0-1024): using 6\n" ,h ); 136return 6L ; 137 }(); 138const unsigned long long head = (unsigned long long )gib <<30 ; 139const unsigned long long avail = kb <<10 ; 140if (avail > head && avail - head > free_b )free_b = (size_t ) (avail - head ); 141 } 142 } 143#endif 144return free_b ; 145} 146catch (sycl::exception const & exc ) { 147 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 148 <<", line:" <<__LINE__ << std::endl ; 149 std::exit (1 ); 150} 151 152bool read_expert_profile (const std::string & path ,int64_t n_layers ,int64_t n_expert , 153 std::vector < std::pair < int32_t ,int32_t >>& ranked ,int64_t & slots , std::string & err ) { 154 std::FILE * f = std::fopen (path .c_str (),"rb" ); 155if (f == nullptr ) { 156err = "read_expert_profile: cannot open " + path ; 157return false; 158 } 159char magic [4 ]= {0 ,0 ,0 ,0 }; 160uint32_t hdr [5 ]= {0 ,0 ,0 ,0 ,0 }; 161if (std::fread (magic ,1 ,4 ,f )!= 4 || std::fread (hdr ,4 ,5 ,f )!= 5 ) { 162 std::fclose (f ); 163err = "read_expert_profile: " + path + " is too short to hold a header" ; 164return false; 165 } 166if (std::memcmp (magic ,"STRP" ,4 )!= 0 ) { 167 std::fclose (f ); 168err = "read_expert_profile: " + path + " does not start with STRP" ; 169return false; 170 } 171const uint32_t version = hdr [0 ],nl = hdr [1 ],ne = hdr [2 ],want = hdr [3 ],n_ranked = hdr [4 ]; 172if ((int64_t )nl != n_layers || (int64_t )ne != n_expert ) { 173 std::fclose (f ); 174char buf [256 ]; 175 std::snprintf (buf ,sizeof buf , 176"read_expert_profile: %s is %ux%u but this model is %lldx%lld - it is a profile for a " 177"different artifact" ,path .c_str (),nl ,ne , (long long )n_layers , (long long )n_expert ); 178err = buf ; 179return false; 180 } 181if (n_ranked > want ) { 182 std::fclose (f ); 183err = "read_expert_profile: the header claims more ranked pairs than slots" ; 184return false; 185 } 186ranked .assign (n_ranked , {0 ,0 }); 187 std::vector < uint16_t > raw ((size_t )n_ranked * 2 ); 188if (n_ranked > 0 && std::fread (raw .data (),2 , (size_t )n_ranked * 2 ,f )!= (size_t )n_ranked * 2 ) { 189 std::fclose (f ); 190err = "read_expert_profile: the ranked list is truncated" ; 191return false; 192 } 193 std::fclose (f ); 194for (uint32_t i = 0 ;i < n_ranked ;++ i ) { 195const int32_t l = (int32_t )raw [(size_t )i * 2 ],e = (int32_t )raw [(size_t )i * 2 + 1 ]; 196if (l < 0 || l >=n_layers || e < 0 || e >=n_expert ) { 197char buf [256 ]; 198 std::snprintf (buf ,sizeof buf ,"read_expert_profile: pair %u is (layer %d, expert %d), out of range" , 199i ,l ,e ); 200err = buf ; 201return false; 202 } 203ranked [(size_t )i ]= {l ,e }; 204 } 205slots = (int64_t )want ; 206 (void )version ;// a future format bumps it; the layout check above is what protects this reader today 207return true; 208} 209 210std::vector < std::pair < int32_t ,int32_t >> rank_learned_profile (int64_t n_layers ,int64_t n_expert , 211const std::vector < uint8_t >& resident , 212const std::vector < double >& heat , 213const std::vector < std::pair < int32_t ,int32_t >>& prior ) { 214const size_t n = (size_t ) (n_layers * n_expert ); 215 std::vector < int64_t > prior_rank (n ,INT64_MAX ); 216for (size_t r = 0 ;r < prior .size ();++ r ) { 217const auto [l ,e ]= prior [r ]; 218if (l >=0 && l < n_layers && e >=0 && e < n_expert ) { 219int64_t & pr = prior_rank [(size_t ) (l * n_expert + e )]; 220if (pr == INT64_MAX )pr = (int64_t )r ; 221 } 222 } 223 std::vector < int64_t > order (n ); 224for (size_t i = 0 ;i < n ;++ i )order [i ]= (int64_t )i ; 225auto res = [& ](int64_t i ) {return (size_t )i < resident .size ()&& resident [(size_t )i ]!= 0 ; }; 226auto ht = [& ](int64_t i ) {return (size_t )i < heat .size () ?heat [(size_t )i ] :0.0 ; }; 227 std::stable_sort (order .begin (),order .end (), [& ](int64_t a ,int64_t b ) { 228if (res (a )!= res (b ))return res (a ); 229if (ht (a )!= ht (b ))return ht (a )> ht (b ); 230if (prior_rank [(size_t )a ]!= prior_rank [(size_t )b ])return prior_rank [(size_t )a ]< prior_rank [(size_t )b ]; 231return a < b ; 232 }); 233 std::vector < std::pair < int32_t ,int32_t >> ranked (n ); 234for (size_t r = 0 ;r < n ;++ r ) 235ranked [r ]= {(int32_t ) (order [r ] /n_expert ), (int32_t ) (order [r ] %n_expert )}; 236return ranked ; 237} 238 239bool write_expert_profile (const std::string & path ,int64_t n_layers ,int64_t n_expert , 240const std::vector < std::pair < int32_t ,int32_t >>& ranked , std::string & err ) { 241if (n_layers <=0 || n_expert <=0 || n_layers > 65535 || n_expert > 65535 ) { 242err = "write_expert_profile: the model's layout does not fit the format" ; 243return false; 244 } 245 std::vector < int32_t > table ((size_t ) (n_layers * n_expert ),-1 ); 246 std::vector < uint16_t > pairs ; 247pairs .reserve (ranked .size ()* 2 ); 248for (size_t r = 0 ;r < ranked .size ();++ r ) { 249const auto [l ,e ]= ranked [r ]; 250if (l < 0 || l >=n_layers || e < 0 || e >=n_expert ) { 251err = "write_expert_profile: a ranked pair is out of range" ; 252return false; 253 } 254table [(size_t ) (l * n_expert + e )]= (int32_t )r ; 255pairs .push_back ((uint16_t )l ); 256pairs .push_back ((uint16_t )e ); 257 } 258const uint32_t hdr [5 ]= {1u , (uint32_t )n_layers , (uint32_t )n_expert , (uint32_t )ranked .size (), 259 (uint32_t )ranked .size ()}; 260const std::string tmp = path + ".tmp" ; 261 std::FILE * f = std::fopen (tmp .c_str (),"wb" ); 262if (f == nullptr ) { 263err = "write_expert_profile: cannot create " + tmp ; 264return false; 265 } 266// the format is little-endian (make_profile.py's "<"): so is every machine this engine runs on 267bool ok = std::fwrite ("STRP" ,1 ,4 ,f )== 4 && std::fwrite (hdr ,4 ,5 ,f )== 5 && 268 (pairs .empty ()|| std::fwrite (pairs .data (),2 ,pairs .size (),f )== pairs .size ())&& 269 std::fwrite (table .data (),4 ,table .size (),f )== table .size (); 270ok = (std::fclose (f )== 0 )&& ok ; 271 std::error_code ec ; 272if (ok ) std::filesystem::rename (tmp ,path ,ec );// replaces an existing file (MoveFileEx / rename(2)) 273if (!ok || ec ) { 274 std::filesystem::remove (tmp ,ec ); 275err = "write_expert_profile: cannot write " + path ; 276return false; 277 } 278return true; 279} 280 281ExpertCache ::~ExpertCache () {close (); } 282 283// ---- #533: the segmented arena (--vram-elastic). The driver API's virtual memory functions, looked up through the 284// runtime (no link against the driver library): one address range for the whole arena, backed by physical segments, 285// and the tail's segments unmapped / mapped again later. Nothing here runs unless a segment size was set. 286#if !defined(STRATA_EC_NO_VMM ) 287namespace { 288struct Vmm { 289int (CUDAAPI * device_get )(int * ,int )= nullptr ; 290int (CUDAAPI * attribute )(int * ,CUdevice_attribute ,int )= nullptr ; 291int (CUDAAPI * granularity )( 292size_t * ,const dpct::experimental::mem_prop * , 293 sycl::ext::oneapi::experimental::granularity_mode )= nullptr ; 294int (CUDAAPI * reserve )(dpct::device_ptr * ,size_t ,size_t , dpct::device_ptr , 295unsigned long long )= nullptr ; 296int (CUDAAPI * address_free )(dpct::device_ptr ,size_t )= nullptr ; 297int (CUDAAPI * create )(dpct::experimental::physical_mem_ptr * ,size_t , 298const dpct::experimental::mem_prop * , 299unsigned long long )= nullptr ; 300int (CUDAAPI * release )(dpct::experimental::physical_mem_ptr )= nullptr ; 301int (CUDAAPI * map )(dpct::device_ptr ,size_t ,size_t , 302 dpct::experimental::physical_mem_ptr , 303unsigned long long )= nullptr ; 304int (CUDAAPI * unmap )(dpct::device_ptr ,size_t )= nullptr ; 305int (CUDAAPI * set_access )(dpct::device_ptr ,size_t , 306const dpct::experimental::mem_access_desc * , 307size_t )= nullptr ; 308bool ok = false; 309}; 310 311template < class F > bool entry (const char * name ,F & f )try { 312void * p = nullptr ; 313cudaDriverEntryPointQueryResult q {}; 314#if DPCT_COMPAT_RT_VERSION >=12050 315/* 316DPCT1007: Migration of cudaGetDriverEntryPointByVersion is not 317supported. 318*/ 319const dpct::err0 e = cudaGetDriverEntryPointByVersion ( 320name ,& p ,12000 ,cudaEnableDefault ,& q ); 321#else 322const cudaError_t e = cudaGetDriverEntryPoint (name ,& p ,cudaEnableDefault ,& q ); 323#endif 324if (e != 0 || q != cudaDriverEntryPointSuccess || p == nullptr ) { 325/* 326DPCT1010: SYCL uses exceptions to report errors and does not use the 327error codes. The cudaGetLastError function call was replaced with 0. You 328need to rewrite this code. 329*/ 330 (void )0 ; 331return false; 332 } 333f = reinterpret_cast < F > (p ); 334return true; 335} 336catch (sycl::exception const & exc ) { 337 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 338 <<", line:" <<__LINE__ << std::endl ; 339 std::exit (1 ); 340} 341 342const Vmm & vmm () { 343static const Vmm v = [] { 344Vmm x ; 345x .ok = entry ("cuDeviceGet" ,x .device_get )&& entry ("cuDeviceGetAttribute" ,x .attribute )&& 346entry ("cuMemGetAllocationGranularity" ,x .granularity )&& entry ("cuMemAddressReserve" ,x .reserve )&& 347entry ("cuMemAddressFree" ,x .address_free )&& entry ("cuMemCreate" ,x .create )&& 348entry ("cuMemRelease" ,x .release )&& entry ("cuMemMap" ,x .map )&& entry ("cuMemUnmap" ,x .unmap )&& 349entry ("cuMemSetAccess" ,x .set_access ); 350return x ; 351 }(); 352return v ; 353} 354 355dpct::experimental::mem_prop device_prop (int dev ) { 356 dpct::experimental::mem_prop prop {}; 357prop .type = 0 ; 358prop .location .type = 1 ; 359prop .location .id = dev ; 360return prop ; 361} 362 363// one segment: a physical allocation mapped at `va`, readable and writable by this device 364bool map_segment (const Vmm & v ,int dev , dpct::device_ptr va ,size_t bytes , 365unsigned long long & handle ) { 366const dpct::experimental::mem_prop prop = device_prop (dev ); 367 dpct::experimental::physical_mem_ptr h = 0 ; 368if (v .create (& h ,bytes ,& prop ,0 )!= 0 )return false; 369if (v .map (va ,bytes ,0 ,h ,0 )!= 0 ) { 370v .release (h ); 371return false; 372 } 373 dpct::experimental::mem_access_desc access {}; 374access .location .type = 1 ; 375access .location .id = dev ; 376access .flags = 377 sycl::ext::oneapi::experimental::address_access_mode::read_write ; 378if (v .set_access (va ,bytes ,& access ,1 )!= 0 ) { 379v .unmap (va ,bytes ); 380v .release (h ); 381return false; 382 } 383handle = (unsigned long long )h ; 384return true; 385} 386}// namespace 387#endif 388 389bool ExpertCache ::open_segmented (uint64_t want , std::string & err )try { 390#if defined(STRATA_EC_NO_VMM ) 391 (void )want ; 392err = "ExpertCache: --vram-elastic (a segmented expert cache) is CUDA-only for now" ; 393return false; 394#else 395const Vmm & v = vmm (); 396int dev = 0 ,supported = 0 ; 397int cu = 0 ; 398if (!v .ok || DPCT_CHECK_ERROR (dev = dpct::get_current_device_id ())!= 0 || 399v .device_get (& cu ,dev )!= 0 || 400v .attribute (& supported , 401CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED , 402cu )!= 0 || 403 !supported ) { 404err = "ExpertCache: --vram-elastic needs the driver's virtual memory management, which this GPU or driver " 405"does not offer" ; 406return false; 407 } 408const dpct::experimental::mem_prop prop = device_prop (dev ); 409size_t gran = 0 ; 410if (v .granularity ( 411& gran ,& prop , 412 sycl::ext::oneapi::experimental::granularity_mode::recommended )!= 4130 || 414gran == 0 ) { 415err = "ExpertCache: cannot read the driver's allocation granularity" ; 416return false; 417 } 418const uint64_t g = (uint64_t )gran ; 419const uint64_t total = (want + g - 1 ) /g * g ; 420seg_ = (int64_t ) (((uint64_t )seg_req_ + g - 1 ) /g * g ); 421 dpct::device_ptr va = 0 ; 422if (v .reserve (& va , (size_t )total ,0 ,0 ,0 )!= 0 ) { 423err = "ExpertCache: cannot reserve the address range of the segmented expert cache" ; 424return false; 425 } 426base_ = reinterpret_cast < uint8_t *> (va ); 427reserved_ = total ; 428for (uint64_t at = 0 ;at < total ;at += (uint64_t )seg_ ) { 429segs_ .push_back (0 ); 430seg_size_ .push_back ((int64_t ) std::min < uint64_t > ((uint64_t )seg_ ,total - at )); 431 } 432for (size_t i = 0 ;i < segs_ .size ();++ i ) { 433if (!map_segment (v ,dev , 434va + (dpct::device_ptr )((uint64_t )i * (uint64_t )seg_ ), 435 (size_t )seg_size_ [i ],segs_ [i ])) { 436char buf [200 ]; 437 std::snprintf (buf ,sizeof buf ,"ExpertCache: cudaMalloc failed: segment %zu of %zu (%.2f GiB) of the " 438"segmented cache could not be allocated" ,i + 1 ,segs_ .size (), 439 (double )seg_size_ [i ] /1073741824.0 ); 440err = buf ;// "cudaMalloc failed": the auto cache's smaller-retry path reads it as an allocation failure 441release_segmented (); 442return false; 443 } 444mapped_segs_ = (int64_t )i + 1 ; 445 } 446// the reserved VA, so device_offset_end can place a borrowed prompt buffer or a resident expert inside it (the GEMM 447// stages an operand that lies more than 4 GiB in; oneMKL misreads such an operand's last partial tile) 448 strata::device_alloc_register (base_ , (size_t )reserved_ ); 449return true; 450#endif 451} 452catch (sycl::exception const & exc ) { 453 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 454 <<", line:" <<__LINE__ << std::endl ; 455 std::exit (1 ); 456} 457 458void ExpertCache ::release_segmented () { 459#if !defined(STRATA_EC_NO_VMM ) 460const Vmm & v = vmm (); 461if (base_ != nullptr ) dpct::get_current_device ().queues_wait_and_throw (); 462const dpct::device_ptr va = reinterpret_cast < dpct::device_ptr > (base_ ); 463for (size_t i = 0 ;i < segs_ .size ();++ i ) 464if (segs_ [i ]!= 0 ) { 465v .unmap (va + (dpct::device_ptr )((uint64_t )i * (uint64_t )seg_ ), 466 (size_t )seg_size_ [i ]); 467v .release ((dpct::experimental::physical_mem_ptr )segs_ [i ]); 468 } 469if (base_ != nullptr && reserved_ > 0 )v .address_free (va , (size_t )reserved_ ); 470#endif 471segs_ .clear (); 472seg_size_ .clear (); 473mapped_segs_ = 0 ; 474reserved_ = 0 ; 475 strata::device_alloc_unregister (base_ ); 476base_ = nullptr ; 477} 478 479int64_t ExpertCache ::mapped_bytes ()const { 480if (segs_ .empty ())return base_ != nullptr ?full_bytes () :0 ; 481int64_t b = 0 ; 482for (int64_t i = 0 ;i < mapped_segs_ ;++ i )b += seg_size_ [(size_t )i ]; 483return b ; 484} 485 486int64_t ExpertCache ::slots_within (int64_t bytes )const { 487if (bytes >=full_bytes ())return slots_ ; 488if (bytes <=0 )return 0 ; 489if (off_ .empty ())return blob_ > 0 ?bytes /blob_ :0 ; 490// off_[i + 1] is slot i's end: count the slots whose end is at most `bytes` 491return (int64_t ) (std::upper_bound (off_ .begin ()+ 1 ,off_ .end (), (uint64_t )bytes )- (off_ .begin ()+ 1 )); 492} 493 494bool ExpertCache ::shrink (int64_t keep_bytes , std::string & err )try { 495if (segs_ .empty ()) { 496err = "the expert cache is not segmented (the engine needs --vram-elastic)" ; 497return false; 498 } 499#if defined(STRATA_EC_NO_VMM ) 500 (void )keep_bytes ; 501return false; 502#else 503const Vmm & v = vmm (); 504int64_t keep = 0 ,at = 0 ;// the segments [0, keep) hold the first keep_bytes 505while (keep < (int64_t )segs_ .size ()&& at < keep_bytes )at += seg_size_ [(size_t )keep ++ ]; 506if (keep >=mapped_segs_ )return true; 507if (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ())!= 5080 ) { 509/* 510DPCT1009: SYCL reports errors using exceptions and does not use 511error codes. Please replace the "get_error_string_dummy(...)" with a 512real error-handling function. 513*/ 514/* 515DPCT1010: SYCL uses exceptions to report errors and does not use the 516error codes. The cudaGetLastError function call was replaced with 0. You 517need to rewrite this code. 518*/ 519err = std::string ("the device failed before the cache shrank: " )+ 520 dpct::get_error_string_dummy (0 ); 521return false; 522 } 523const dpct::device_ptr va = reinterpret_cast < dpct::device_ptr > (base_ ); 524for (int64_t i = mapped_segs_ - 1 ;i >=keep ;-- i ) { 525const dpct::device_ptr p = 526va + (dpct::device_ptr )((uint64_t )i * (uint64_t )seg_ ); 527if (v .unmap (p , (size_t )seg_size_ [(size_t )i ])!= 0 || 528v .release ((dpct::experimental::physical_mem_ptr )segs_ [(size_t )i ])!= 5290 ) { 530err = "the driver refused to release an expert-cache segment" ; 531live_slots_ = slots_within (mapped_bytes ()); 532return false; 533 } 534segs_ [(size_t )i ]= 0 ; 535mapped_segs_ = i ; 536 } 537live_slots_ = slots_within (mapped_bytes ()); 538return true; 539#endif 540} 541catch (sycl::exception const & exc ) { 542 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 543 <<", line:" <<__LINE__ << std::endl ; 544 std::exit (1 ); 545} 546 547bool ExpertCache ::grow (int64_t want_bytes , std::string & err ) { 548if (segs_ .empty ()) { 549err = "the expert cache is not segmented (the engine needs --vram-elastic)" ; 550return false; 551 } 552#if defined(STRATA_EC_NO_VMM ) 553 (void )want_bytes ; 554return false; 555#else 556const Vmm & v = vmm (); 557int dev = 0 ; 558dev = dpct::get_current_device_id (); 559const dpct::device_ptr va = reinterpret_cast < dpct::device_ptr > (base_ ); 560int64_t at = mapped_bytes (); 561while (mapped_segs_ < (int64_t )segs_ .size ()&& at + seg_size_ [(size_t )mapped_segs_ ] <=want_bytes ) { 562const size_t i = (size_t )mapped_segs_ ; 563if (!map_segment (v ,dev , 564va + (dpct::device_ptr )((uint64_t )i * (uint64_t )seg_ ), 565 (size_t )seg_size_ [i ],segs_ [i ])) { 566/* 567DPCT1010: SYCL uses exceptions to report errors and does not use 568the error codes. The cudaGetLastError function call was replaced 569with 0. You need to rewrite this code. 570*/ 571 (void )0 ; 572err = "the driver has no VRAM for another expert-cache segment" ; 573live_slots_ = slots_within (mapped_bytes ()); 574return false; 575 } 576at += seg_size_ [i ]; 577++ mapped_segs_ ; 578 } 579live_slots_ = slots_within (mapped_bytes ()); 580return true; 581#endif 582} 583 584#if defined(STRATA_USE_HIP ) 585bool ExpertCache ::ensure_blocking_staging (std::size_t bytes , std::string & err ) { 586if (bytes <=blocking_staging_bytes_ )return true; 587void * next = nullptr ; 588const cudaError_t status = cudaHostAlloc (& next ,bytes ,cudaHostAllocDefault ); 589if (status != cudaSuccess ) { 590err = std::string ("ExpertCache: HIP blocking staging allocation: " )+ cudaGetErrorString (status ); 591return false; 592 } 593if (blocking_staging_ ) (void )cudaFreeHost (blocking_staging_ ); 594blocking_staging_ = static_cast < uint8_t *> (next ); 595blocking_staging_bytes_ = bytes ; 596return true; 597} 598#endif 599 600namespace { 601bool g_cache_vmm = false;// set_vmm 602}// namespace 603 604void ExpertCache ::set_vmm (bool enabled ) {g_cache_vmm = enabled ; } 605 606bool ExpertCache ::open (int64_t n_slots ,int64_t n_layers ,int64_t n_expert ,int64_t blob_bytes , 607 std::string & err ) { 608close (); 609if (n_slots <=0 ) { 610err = "ExpertCache: n_slots must be positive" ; 611return false; 612 } 613if (n_layers <=0 || n_expert <=0 || blob_bytes <=0 ) { 614err = "ExpertCache: n_layers, n_expert and blob_bytes must all be positive" ; 615return false; 616 } 617 618const uint64_t want = (uint64_t )n_slots * (uint64_t )blob_bytes ; 619 620// ---- **THE ALLOCATION IS CHECKED AGAINST THE CARD, NOT AGAINST THE REQUEST.** 621// 622// `cudaMalloc` failing is the easy case. The one that matters is a machine where the weights already own 623// most of VRAM: the cache then takes what is left and `slots()` would report the number ASKED FOR while 624// `device_slot()` walks off the end. So the free-VRAM figure is read and compared BEFORE the allocation, 625// and the two numbers are named in the refusal. 626size_t free_b = 0 ,total_b = 0 ; 627/* 628DPCT1106: 'cudaMemGetInfo' was migrated with the Intel extensions for 629device information which may not be supported by all compilers or runtimes. 630You may need to adjust the code. 631*/ 632if (DPCT_CHECK_ERROR ( 633 dpct::get_current_device ().get_memory_info (free_b ,total_b ))== 0 ) { 634free_b = device_free_bytes ();// unified memory: what the OS can give back counts (see the header) 635if ((uint64_t )free_b < want ) { 636char buf [320 ]; 637 std::snprintf (buf ,sizeof buf , 638"ExpertCache: %lld slots x %lld B = %.2f GiB, but only %.2f GiB of VRAM is free " 639"(%.2f GiB of %.2f GiB total). Lower --expert-cache." , 640 (long long )n_slots , (long long )blob_bytes , (double )want /1073741824.0 , 641 (double )free_b /1073741824.0 , (double ) (total_b - free_b ) /1073741824.0 , 642 (double )total_b /1073741824.0 ); 643err = buf ; 644return false; 645 } 646 } 647 648if (seg_req_ > 0 ) {// #533: --vram-elastic: physical segments behind one address range (zeroed below) 649if (!open_segmented (want ,err ))return false; 650 }else if (g_cache_vmm && vmm_available ()) { 651// the elastic K/V: every chunk mapped now; the K/V may later take some of them (and give them back) 652auto r = std::make_unique < VmmRange > (); 653if (!r -> reserve (want )|| !r -> map_range (0 ,r -> chunks (), [] {return (VmmChunk )0 ; })) { 654char buf [256 ]; 655 std::snprintf (buf ,sizeof buf ,"ExpertCache: mapping %.2f GiB of VRAM failed: out of memory" , 656 (double )want /1073741824.0 ); 657err = buf ; 658return false; 659 } 660base_ = r -> base (); 661vmm_ = std::move (r ); 662 }else if (DPCT_CHECK_ERROR ( 663base_ = (uint8_t * )strata::malloc_device_guarded ( 664 (size_t )want , dpct::get_in_order_queue (),"expert cache arena" ))!= 0 || 665base_ == nullptr ) {// Level Zero answers a too-large allocation with null, not an exception 666// (an Arc A750 caps one allocation at 4 GiB unless UR_L0_ENABLE_RELAXED_ALLOCATION_LIMITS=1) 667base_ = nullptr ; 668char buf [256 ]; 669 std::snprintf ( 670buf ,sizeof buf ,"ExpertCache: cudaMalloc(%.2f GiB) failed: %s" , 671/* 672DPCT1009: SYCL reports errors using exceptions and does not use 673error codes. Please replace the "get_error_string_dummy(...)" with a 674real error-handling function. 675*/ 676/* 677DPCT1010: SYCL uses exceptions to report errors and does not 678use the error codes. The cudaGetLastError function call was replaced 679with 0. You need to rewrite this code. 680*/ 681 (double )want /1073741824.0 , dpct::get_error_string_dummy (0 )); 682err = buf ; 683return false; 684 } 685// Zeroed so a slot read before it is filled is a DETERMINISTIC wrong answer rather than whatever the 686// allocator handed back. A stale block of a previous process's memory would still sum to finite floats. 687if (DPCT_CHECK_ERROR ( 688 (strata::big_fill_zero (dpct::get_in_order_queue (),base_ , (size_t )want ),0 ))!= 6890 ) { 690err = "ExpertCache: cudaMemset of the slot arena failed" ; 691close (); 692return false; 693 } 694 695residency_ .assign ((size_t ) (n_layers * n_expert ),kNotResident ); 696slots_ = n_slots ; 697live_slots_ = n_slots ; 698n_layers_ = n_layers ; 699n_expert_ = n_expert ; 700blob_ = blob_bytes ; 701#if defined(STRATA_USE_HIP ) 702if (!ensure_blocking_staging ((std::size_t )blob_ ,err )) { 703close (); 704return false; 705 } 706#endif 707next_free_ = 0 ; 708fills_ = 0 ; 709admitted_ = 0 ; 710// R4.2g: each layer starts at the bottom of its own range. Built here rather than lazily so `admit` 711// stays allocation-free on the token path. 712layer_next_ .assign ((size_t ) (n_layers > 0 ?n_layers :0 ),0 ); 713for (int64_t l = 0 ;l < n_layers ;++ l ) { 714int64_t lo = 0 ,hi = 0 ; 715layer_slot_range (l ,lo ,hi ); 716layer_next_ [(size_t )l ]= (int32_t )lo ; 717 } 718return true; 719} 720 721bool ExpertCache ::open_sized (const std::vector < int64_t >& slot_bytes ,int64_t n_layers ,int64_t n_expert , 722 std::string & err ) { 723if (slot_bytes .empty ()) {err = "ExpertCache: no slots" ;return false; } 724int64_t mx = 0 ; 725 std::vector < uint64_t > off (slot_bytes .size ()+ 1 ,0 ); 726for (size_t i = 0 ;i < slot_bytes .size ();++ i ) { 727// 256-byte aligned slots, so every blob starts where the kernels' vector loads expect it 728off [i + 1 ]= off [i ]+ ((uint64_t )slot_bytes [i ]+ 255 ) /256 * 256 ; 729mx = slot_bytes [i ]> mx ?slot_bytes [i ] :mx ; 730 } 731// one allocation of the summed size, through the uniform path's checks: n "slots" of 1 byte 732if (!open ((int64_t )off .back (),n_layers ,n_expert ,1 ,err ))return false; 733slots_ = (int64_t )slot_bytes .size (); 734live_slots_ = slots_ ; 735blob_ = mx ; 736off_ = std::move (off ); 737#if defined(STRATA_USE_HIP ) 738if (!ensure_blocking_staging ((std::size_t )blob_ ,err )) { 739close (); 740return false; 741 } 742#endif 743// #369: each layer's cursor at the bottom of its own range, as open() seeds it - open() above ran on byte-sized 744// "slots", so its seeds are not slot indices 745layer_next_ .assign ((size_t ) (n_layers > 0 ?n_layers :0 ),0 ); 746for (int64_t l = 0 ;l < n_layers ;++ l ) { 747int64_t lo = 0 ,hi = 0 ; 748layer_slot_range (l ,lo ,hi ); 749layer_next_ [(size_t )l ]= (int32_t )lo ; 750 } 751return true; 752} 753 754void ExpertCache ::close () { 755#if defined(STRATA_USE_HIP ) 756if (blocking_staging_ ) (void )cudaFreeHost (blocking_staging_ ); 757blocking_staging_ = nullptr ; 758blocking_staging_bytes_ = 0 ; 759#endif 760off_ .clear (); 761if (!segs_ .empty ()) { 762release_segmented (); 763 }else if (vmm_ ) { 764vmm_ .reset ();// unmaps and frees every chunk it still holds 765 strata::device_alloc_unregister (base_ ); 766base_ = nullptr ; 767 }else if (base_ != nullptr ) { 768 strata::device_alloc_unregister (base_ ); 769 sycl::free (base_ , dpct::get_in_order_queue ()); 770base_ = nullptr ; 771 } 772residency_ .clear (); 773slots_ = 0 ; 774live_slots_ = 0 ; 775n_layers_ = 0 ; 776n_expert_ = 0 ; 777blob_ = 0 ; 778next_free_ = 0 ; 779fills_ = 0 ; 780admitted_ = 0 ; 781layer_next_ .clear (); 782} 783 784/// R4.2g. Layer `l` owns `[l*q, (l+1)*q)` with `q = slots_ / n_layers_`; the LAST layer takes whatever is 785/// left over, so the ranges always cover `0..slots_` exactly and no slot is orphaned by the division. 786void ExpertCache ::layer_slot_range (int64_t layer ,int64_t & lo ,int64_t & hi )const { 787lo = 0 ; 788hi = 0 ; 789if (n_layers_ <=0 || slots_ <=0 || layer < 0 || layer >=n_layers_ )return ; 790const int64_t q = slots_ /n_layers_ ; 791lo = layer * q ; 792hi = (layer == n_layers_ - 1 ) ?slots_ : (layer + 1 )* q ; 793} 794 795int32_t ExpertCache ::slot_of (int64_t layer ,int64_t expert )const { 796if (layer < 0 || layer >=n_layers_ || expert < 0 || expert >=n_expert_ )return kNotResident ; 797return residency_ [(size_t ) (layer * n_expert_ + expert )]; 798} 799 800int32_t ExpertCache ::admit (int64_t layer ,int64_t expert ) { 801if (layer < 0 || layer >=n_layers_ || expert < 0 || expert >=n_expert_ )return kNotResident ; 802const size_t at = (size_t ) (layer * n_expert_ + expert ); 803if (residency_ [at ]!= kNotResident )return residency_ [at ]; 804// R4.2g: THE PER-LAYER PATH. Same "no eviction" rule, but the ceiling is this layer's own range rather 805// than one counter shared by all 48 - which is what confined the measured hit rate to 2.97%. 806if (per_layer_ ) { 807if (layer_next_ .empty ())return kNotResident ; 808int64_t lo = 0 ,hi = 0 ; 809layer_slot_range (layer ,lo ,hi ); 810if ((int64_t )layer_next_ [(size_t )layer ] >=hi )return kNotResident ;// this layer's quota is full 811residency_ [at ]= layer_next_ [(size_t )layer ]++ ; 812++ admitted_ ; 813return residency_ [at ]; 814 } 815if (next_free_ >=slots_ )return kNotResident ;// full: no eviction, deliberately - see the header 816residency_ [at ]= (int32_t )next_free_ ; 817return (int32_t )next_free_ ++ ; 818} 819 820uint8_t * ExpertCache ::device_slot (int32_t slot ) { 821if (slot < 0 || slot >=slots_ )return nullptr ; 822if (!off_ .empty ())return base_ + off_ [(size_t )slot ]; 823return base_ + (size_t )slot * (size_t )blob_ ; 824} 825 826const uint8_t * ExpertCache ::device_slot (int32_t slot )const { 827if (slot < 0 || slot >=slots_ )return nullptr ; 828if (!off_ .empty ())return base_ + off_ [(size_t )slot ]; 829return base_ + (size_t )slot * (size_t )blob_ ; 830} 831 832bool ExpertCache ::fill_slot (int32_t slot ,const uint8_t * host_blob , 833void * stream , std::string & err ,int64_t bytes )try { 834const size_t n = (size_t ) (bytes > 0 && bytes <=blob_ ?bytes :blob_ ); 835uint8_t * dst = device_slot (slot ); 836if (dst == nullptr ) { 837err = "ExpertCache::fill_slot: slot " + std::to_string (slot )+ " is outside 0.." + 838 std::to_string (slots_ - 1 ); 839return false; 840 } 841if (host_blob == nullptr ) { 842err = "ExpertCache::fill_slot: the host blob is null" ; 843return false; 844 } 845/* 846DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While 847the origin API might be synchronous, it depends on the type of operand 848memory, so you may need to call wait() on event return by memcpy API to 849ensure synchronization behavior. 850*/ 851const dpct::err0 e = 852DPCT_CHECK_ERROR (strata::q_of (stream )-> memcpy (dst ,host_blob ,n )); 853/* 854DPCT1000: Error handling if-stmt was detected but could not be 855rewritten. 856*/ 857if (e != 0 ) { 858/* 859DPCT1009: SYCL reports errors using exceptions and does not use 860error codes. Please replace the "get_error_string_dummy(...)" with a 861real error-handling function. 862*/ 863/* 864DPCT1001: The statement could not be removed. 865*/ 866err = std::string ("ExpertCache::fill_slot: " )+ 867 dpct::get_error_string_dummy (e ); 868return false; 869 } 870++ fills_ ; 871return true; 872} 873catch (sycl::exception const & exc ) { 874 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 875 <<", line:" <<__LINE__ << std::endl ; 876 std::exit (1 ); 877} 878 879bool ExpertCache ::fill_slot_blocking (int32_t slot ,const uint8_t * host_blob , 880 std::string & err ,int64_t bytes )try { 881const size_t n = (size_t ) (bytes > 0 && bytes <=blob_ ?bytes :blob_ ); 882uint8_t * dst = device_slot (slot ); 883if (dst == nullptr ) { 884err = "ExpertCache::fill_slot_blocking: slot outside the arena" ; 885return false; 886 } 887if (host_blob == nullptr ) { 888err = "ExpertCache::fill_slot_blocking: the host blob is null" ; 889return false; 890 } 891#if defined(STRATA_USE_HIP ) 892// Bound HIP's pageable-source staging to one expert instead of repeatedly 893// registering regions of the mmap. The blocking copy completes before reuse. 894if (!blocking_staging_ || n > blocking_staging_bytes_ ) { 895err = "ExpertCache::fill_slot_blocking: HIP staging buffer is too small" ; 896return false; 897 } 898 std::memcpy (blocking_staging_ ,host_blob ,n ); 899const cudaError_t e = cudaMemcpy (dst ,blocking_staging_ ,n ,cudaMemcpyHostToDevice ); 900#else 901/* 902DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 903in the original code the source host memory is pageable memory. If the 904memory is not pageable, call wait() on event return by memcpy API to ensure 905synchronization behavior. 906*/ 907const dpct::err0 e = 908DPCT_CHECK_ERROR (dpct::get_in_order_queue ().memcpy (dst ,host_blob ,n ).wait ()); 909#endif 910/* 911DPCT1000: Error handling if-stmt was detected but could not be 912rewritten. 913*/ 914if (e != 0 ) { 915/* 916DPCT1009: SYCL reports errors using exceptions and does not use 917error codes. Please replace the "get_error_string_dummy(...)" with a 918real error-handling function. 919*/ 920/* 921DPCT1001: The statement could not be removed. 922*/ 923err = std::string ("ExpertCache::fill_slot_blocking: " )+ 924 dpct::get_error_string_dummy (e ); 925return false; 926 } 927++ fills_ ; 928return true; 929} 930catch (sycl::exception const & exc ) { 931 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 932 <<", line:" <<__LINE__ << std::endl ; 933 std::exit (1 ); 934} 935 936bool ExpertCache ::fill_slot_queued (int32_t slot ,const uint8_t * host_blob , 937 std::string & err ,int64_t bytes )try { 938const size_t n = (size_t ) (bytes > 0 && bytes <=blob_ ?bytes :blob_ ); 939uint8_t * dst = device_slot (slot ); 940if (dst == nullptr || host_blob == nullptr ) { 941err = dst == nullptr ?"ExpertCache::fill_slot_queued: slot outside the arena" 942 :"ExpertCache::fill_slot_queued: the host blob is null" ; 943return false; 944 } 945/* 946DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While 947the origin API might be synchronous, it depends on the type of operand 948memory, so you may need to call wait() on event return by memcpy API to 949ensure synchronization behavior. 950*/ 951const dpct::err0 e = 952DPCT_CHECK_ERROR (dpct::get_in_order_queue ().memcpy (dst ,host_blob ,n ).wait ()); 953/* 954DPCT1000: Error handling if-stmt was detected but could not be 955rewritten. 956*/ 957if (e != 0 ) { 958/* 959DPCT1009: SYCL reports errors using exceptions and does not use 960error codes. Please replace the "get_error_string_dummy(...)" with a 961real error-handling function. 962*/ 963/* 964DPCT1001: The statement could not be removed. 965*/ 966err = std::string ("ExpertCache::fill_slot_queued: " )+ 967 dpct::get_error_string_dummy (e ); 968return false; 969 } 970++ fills_ ; 971return true; 972} 973catch (sycl::exception const & exc ) { 974 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 975 <<", line:" <<__LINE__ << std::endl ; 976 std::exit (1 ); 977} 978 979bool ExpertCache ::sync_queued (std::string & err )try { 980const dpct::err0 e = DPCT_CHECK_ERROR (dpct::get_in_order_queue ().wait ()); 981/* 982DPCT1000: Error handling if-stmt was detected but could not be 983rewritten. 984*/ 985if (e != 0 ) { 986/* 987DPCT1009: SYCL reports errors using exceptions and does not use 988error codes. Please replace the "get_error_string_dummy(...)" with a 989real error-handling function. 990*/ 991/* 992DPCT1001: The statement could not be removed. 993*/ 994err = std::string ("ExpertCache::sync_queued: " )+ 995 dpct::get_error_string_dummy (e ); 996return false; 997 } 998return true; 999} 1000catch (sycl::exception const & exc ) { 1001 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 1002 <<", line:" <<__LINE__ << std::endl ; 1003 std::exit (1 ); 1004} 1005 1006bool ExpertCache ::verify_slot (int32_t slot ,const uint8_t * host_blob , 1007 std::string & err ,int64_t bytes )try { 1008const int64_t nb = bytes > 0 && bytes <=blob_ ?bytes :blob_ ; 1009const uint8_t * src = device_slot (slot ); 1010if (src == nullptr ) { 1011err = "ExpertCache::verify_slot: slot outside the arena" ; 1012return false; 1013 } 1014// `cudaMemcpy` and not `cudaMemcpyAsync`: this is a startup check, and a check that can be read before it 1015// has happened is not a check. It also synchronises the fills queued before it, which is what makes the 1016// comparison meaningful. 1017 std::vector < uint8_t > got ((size_t )nb ); 1018const dpct::err0 e = DPCT_CHECK_ERROR ( 1019 dpct::get_in_order_queue ().memcpy (got .data (),src , (size_t )nb ).wait ()); 1020/* 1021DPCT1000: Error handling if-stmt was detected but could not be 1022rewritten. 1023*/ 1024if (e != 0 ) { 1025/* 1026DPCT1009: SYCL reports errors using exceptions and does not use 1027error codes. Please replace the "get_error_string_dummy(...)" with a 1028real error-handling function. 1029*/ 1030/* 1031DPCT1001: The statement could not be removed. 1032*/ 1033err = std::string ("ExpertCache::verify_slot: " )+ 1034 dpct::get_error_string_dummy (e ); 1035return false; 1036 } 1037if (std::memcmp (got .data (),host_blob , (size_t )nb )!= 0 ) { 1038size_t first = 0 ; 1039while (first < (size_t )nb && got [first ]== host_blob [first ])++ first ; 1040char buf [384 ]; 1041 std::snprintf (buf ,sizeof buf , 1042"ExpertCache::verify_slot: slot %d differs from the arena at byte %llu (of %lld): device %02x%02x%02x%02x, arena %02x%02x%02x%02x" , 1043 (int )slot , (unsigned long long )first , (long long )blob_ ,first < (size_t )nb ?got [first ] :0 , 1044first + 1 < (size_t )nb ?got [first + 1 ] :0 ,first + 2 < (size_t )nb ?got [first + 2 ] :0 , 1045first + 3 < (size_t )nb ?got [first + 3 ] :0 ,first < (size_t )nb ?host_blob [first ] :0 , 1046first + 1 < (size_t )nb ?host_blob [first + 1 ] :0 ,first + 2 < (size_t )nb ?host_blob [first + 2 ] :0 , 1047first + 3 < (size_t )nb ?host_blob [first + 3 ] :0 ); 1048err = buf ; 1049return false; 1050 } 1051return true; 1052} 1053catch (sycl::exception const & exc ) { 1054 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 1055 <<", line:" <<__LINE__ << std::endl ; 1056 std::exit (1 ); 1057} 1058 1059}// namespace strata::core