okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a8a51b280
main
1#include "b60_layout_state.hpp" 2// Private phase-boundary adapter for the measured same-payload layouts. 3#include <sycl/sycl.hpp> 4#include <cstdint> 5#include <cstdio> 6#include <stdexcept> 7namespace strata::kernels { 8template < int B > inline int raw_to_packed (int i ,int blocks ){ 9const int row = i /(blocks * B ),kb = (i /B )%blocks ,x = i %B ,base = kb * 16 * B ; 10if constexpr (B == 56 ){ 11const int r = row %8 ,b = (row /8 )* 8 * blocks * 56 + kb * 448 ; 12if (x < 32 )return b + (r * 8 + x /4 )* 4 + x %4 ; 13if (x < 48 )return b + 256 + (r * 8 + (x - 32 )/2 )* 2 + (x - 32 )%2 ; 14return b + 384 + r * 8 + x - 48 ; 15 } 16if constexpr (B == 66 ){ 17const int r = row %8 ,b = (row /8 )* 8 * blocks * 66 + kb * 528 ; 18if (x < 2 )return b + 512 + r * 2 + x ; 19const int p = (x - 2 )/8 ,z = (x - 2 )%8 ; 20return b + (z < 4 ?0 :256 )+ (r * 8 + p )* 4 + z %4 ; 21 } 22if constexpr (B == 136 )return base + (x /4 )* 64 + row * 4 + x %4 ; 23if constexpr (B == 82 ){ 24if (x < 2 )return base + 1280 + row * 2 + x ; 25if (x < 34 )return base + ((x - 2 )/4 )* 160 + row * 4 + (x - 2 )%4 ; 26if (x < 66 )return base + ((x - 34 )/4 )* 160 + 64 + row * 4 + (x - 34 )%4 ; 27if (x < 74 )return base + (x - 66 )* 160 + 128 + row * 2 ; 28return base + (x - 74 )* 160 + 129 + row * 2 ; 29 } 30if constexpr (B == 18 ){ 31if (x < 2 )return base + 256 + row * 2 + x ; 32const int z = x - 2 ;return base + (z /8 )* 128 + ((z %8 )/4 )* 64 + row * 4 + z %4 ; 33 } 34} 35template < int B > inline int packed_to_raw (int i ,int blocks ){ 36if constexpr (B == 56 ){ 37const int sub = i /(8 * blocks * 56 ),rem = i %(8 * blocks * 56 ),kb = rem /448 ,z = rem %448 ; 38int r ,x ; 39if (z < 256 ){const int cell = z /4 ;r = cell /8 ;x = (cell %8 )* 4 + z %4 ;} 40else if (z < 384 ){const int cell = (z - 256 )/2 ;r = cell /8 ;x = 32 + (cell %8 )* 2 + (z - 256 )%2 ;} 41else {r = (z - 384 )/8 ;x = 48 + (z - 384 )%8 ;} 42return ((sub * 8 + r )* blocks + kb )* 56 + x ; 43 } 44if constexpr (B == 66 ){ 45const int sub = i /(8 * blocks * 66 ),rem = i %(8 * blocks * 66 ),kb = rem /528 ,z = rem %528 ; 46int r ,x ; 47if (z >=512 ){r = (z - 512 )/2 ;x = (z - 512 )%2 ;} 48else {const int cell = (z %256 )/4 ;r = cell /8 ;x = 2 + (cell %8 )* 8 + (z >=256 ?4 :0 )+ z %4 ;} 49return ((sub * 8 + r )* blocks + kb )* 66 + x ; 50 } 51const int kb = i /(16 * B ),z = i %(16 * B );int row ,x ; 52if constexpr (B == 136 ){row = (z %64 )/4 ;x = (z /64 )* 4 + z %4 ;} 53if constexpr (B == 82 ){ 54if (z >=1280 ){row = (z - 1280 )/2 ;x = (z - 1280 )%2 ;} 55else {const int p = z /160 ,r = z %160 ; 56if (r < 64 ){row = r /4 ;x = 2 + p * 4 + r %4 ;} 57else if (r < 128 ){row = (r - 64 )/4 ;x = 34 + p * 4 + (r - 64 )%4 ;} 58else {row = (r - 128 )/2 ;x = ((r - 128 )%2 ?74 :66 )+ p ;} 59 } 60 } 61if constexpr (B == 18 ){ 62if (z >=256 ){row = (z - 256 )/2 ;x = (z - 256 )%2 ;} 63else {const int p = z /128 ,r = z %128 ;row = (r %64 )/4 ;x = 2 + p * 8 + (r /64 )* 4 + r %4 ;} 64 } 65return (row * blocks + kb )* B + x ; 66} 67template < int B ,int Blocks > class B60WeightPermute ; 68template < int B ,int Blocks > void launch_permute (void * const * matrices ,int count ,int rows ,bool inverse ,sycl::queue & q ){ 69constexpr int blocks = Blocks ,words = 16 * Blocks * B /4 ; 70if (size_t (words )* 4 > q .get_device ().get_info < sycl::info::device::local_mem_size > ())throw std::invalid_argument ("weight tile exceeds local memory" ); 71q .submit ([& ](sycl::handler & h ){ 72 sycl::local_accessor < uint32_t ,1 > tile (words ,h ); 73h .parallel_for < B60WeightPermute < B ,Blocks >> (sycl::nd_range < 3 > (sycl::range < 3 > (1 ,count ,(rows /16 )* 256 ),sycl::range < 3 > (1 ,1 ,256 )),[= ](sycl::nd_item < 3 > it ){ 74auto * p = static_cast < uint32_t *> (matrices [it .get_group (1 )]); 75const int t = it .get_local_id (2 );const size_t base = it .get_group (2 )* size_t (words ); 76for (int w = t ;w < words ;w += 256 )tile [w ]= p [base + w ]; 77it .barrier (sycl::access::fence_space::local_space ); 78for (int w = t ;w < words ;w += 256 ){ 79uint32_t out = 0 ; 80for (int byte = 0 ;byte < 4 ;++ byte ){const int j = inverse ?raw_to_packed < B > (w * 4 + byte ,blocks ):packed_to_raw < B > (w * 4 + byte ,blocks ); 81out |=((tile [j /4 ]>>(8 * (j %4 )))& 255u )<<(8 * byte ); 82 } 83p [base + w ]= out ; 84 } 85 }); 86 }); 87} 88void b60_weight_permute_many (void * const * matrices ,int count ,int rows ,int blocks ,int type ,bool inverse ,void * stream ){ 89if (!matrices || count <=0 || rows <=0 || rows %16 || blocks <=0 || !stream )throw std::invalid_argument ("weight permutation geometry" ); 90auto & q =* static_cast < sycl::queue *> (stream );if (!q .is_in_order ())throw std::invalid_argument ("weight permutation needs ordered phase boundary" ); 91if (type == 29 && blocks == 10 )launch_permute < 56 ,10 > (matrices ,count ,rows ,inverse ,q ); 92else if (type == 16 && blocks == 10 )launch_permute < 66 ,10 > (matrices ,count ,rows ,inverse ,q ); 93else if (type == 22 && blocks == 10 )launch_permute < 82 ,10 > (matrices ,count ,rows ,inverse ,q ); 94else if (type == 42 && blocks == 10 )launch_permute < 18 ,10 > (matrices ,count ,rows ,inverse ,q ); 95else if (type == 23 && blocks == 10 )launch_permute < 136 ,10 > (matrices ,count ,rows ,inverse ,q ); 96else if (type == 23 && blocks == 24 )launch_permute < 136 ,24 > (matrices ,count ,rows ,inverse ,q ); 97else throw std::invalid_argument ("unsupported packed weight type" ); 98} 99} 100 101#include <atomic> 102namespace strata::kernels { 103namespace {std::atomic < bool > b60_packed_state {false};thread_localbool b60_main_actor_state = false;} 104bool b60_weights_packed (){return b60_packed_state .load (std::memory_order_acquire );} 105void b60_set_weights_packed (bool v ){b60_packed_state .store (v ,std::memory_order_release );} 106bool b60_main_actor (){return b60_main_actor_state ;} 107void b60_set_main_actor (bool v ){b60_main_actor_state = v ;} 108} 109 110// Exact out-of-place caches. Original raw pointers remain valid for every other consumer. 111#include "b60_weight_cache.hpp" 112#include <map> 113#include <mutex> 114#include <utility> 115namespace strata::kernels { 116namespace { 117struct B60CachedWeight {void * allocation ;void * packed ;int rows ,cols ,device ;}; 118std::mutex b60_cache_mutex ; 119std::map < std::pair < uintptr_t ,int > ,B60CachedWeight > b60_weight_caches ; 120inline size_t b60_iq3_cache_byte (size_t i ,int blocks ){ 121const size_t row = i /(blocks * 110 ),kb = (i /110 )%blocks ,x = i %110 ; 122const size_t r = row %16 ,base = (row /16 )* 16 * blocks * 110 + kb * 1760 ; 123if (x < 2 )return base + 1728 + r * 2 + x ; 124if (x < 66 ){const size_t p = (x - 2 )/8 ,z = (x - 2 )%8 ;return base + p * 208 + (z < 4 ?0 :64 )+ r * 4 + z %4 ;} 125if (x < 74 )return base + (x - 66 )* 208 + 192 + r ; 126if (x < 106 )return base + ((x - 74 )/4 )* 208 + 128 + r * 4 + (x - 74 )%4 ; 127return base + 1664 + r * 4 + x - 106 ; 128} 129template < int Kind > class B60SeparateWeightCache ; 130} 131const void * b60_cached_weight (const void * original ,int kind ){ 132 std::lock_guard < std::mutex > lock (b60_cache_mutex ); 133auto it = b60_weight_caches .find ({reinterpret_cast < uintptr_t > (original ),kind }); 134return it == b60_weight_caches .end ()?nullptr :it -> second .packed ; 135} 136void b60_make_weight_cache (const void * original ,int rows ,int cols ,int kind ,int device ,void * stream ){ 137if (!original || !stream || rows <=0 || (rows %16 && kind != 24 )|| cols <=0 )throw std::invalid_argument ("B60 cache geometry/pointer" ); 138const bool iq3 = kind == 21 && ((cols == 2560 && rows == 6144 )|| (cols == 6144 && rows == 2560 )); 139const bool iq4 = kind == 23 && cols == 2560 && (rows == 6144 || rows == 12288 ); 140const bool head = kind == 24 && cols == 2560 && (rows == 248320 || rows == 106299 ); 141const bool gr = kind == 30 && ((rows == 320 && cols == 10240 )|| (rows == 10240 && cols == 320 )); 142if (!iq3 && !iq4 && !head && !gr )throw std::invalid_argument ("B60 unsupported cache shape/kind" ); 143auto & q =* static_cast < sycl::queue *> (stream ); 144if (!q .is_in_order ())throw std::invalid_argument ("B60 cache preparation requires ordered queue" ); 145const size_t raw_bytes = gr ?size_t (rows )* cols * 2 :size_t (rows )* (cols /256 )* (iq3 ?110 :136 ); 146const size_t bytes = head ?size_t ((rows + 15 )/16 * 16 )* (cols /256 )* 136 :raw_bytes ; 147constexpr size_t guard = kB60WeightCacheGuard ; 148void * allocation = sycl::malloc_device (bytes + 2 * guard ,q );if (!allocation )throw std::bad_alloc (); 149void * result = static_cast < uint8_t *> (allocation )+ guard ; 150try { 151q .fill (static_cast < uint8_t *> (allocation ),uint8_t (0xa7 ),bytes + 2 * guard ); 152if (iq3 ){ 153const auto * src = static_cast < const uint8_t *> (original );auto * dst = static_cast < uint8_t *> (result );const int blocks = cols /256 ; 154q .parallel_for < B60SeparateWeightCache < 21 >> (sycl::nd_range < 1 > (((bytes + 255 )/256 )* 256 ,256 ),[= ](sycl::nd_item < 1 > it ){ 155const size_t i = it .get_global_id (0 );if (i < bytes )dst [b60_iq3_cache_byte (i ,blocks )]= src [i ]; 156 }); 157 }else if (iq4 || head ){ 158const auto * src = static_cast < const uint8_t *> (original );auto * dst = static_cast < uint8_t *> (result );const int blocks = cols /256 ;const size_t tile = 16 * blocks * 136 ; 159if (head )q .fill (dst ,uint8_t (0 ),bytes ); 160q .parallel_for < B60SeparateWeightCache < 23 >> (sycl::nd_range < 1 > (((bytes + 255 )/256 )* 256 ,256 ),[= ](sycl::nd_item < 1 > it ){ 161const size_t i = it .get_global_id (0 );if (i < raw_bytes ){const size_t base = (i /tile )* tile ;dst [base + raw_to_packed < 136 > (int (i %tile ),blocks )]= src [i ];} 162 }); 163 }else { 164const auto * src = static_cast < const uint32_t *> (original );auto * dst = static_cast < uint32_t *> (result );const size_t words = bytes /4 ;const int cw = cols /2 ; 165q .parallel_for < B60SeparateWeightCache < 30 >> (sycl::nd_range < 1 > (((words + 255 )/256 )* 256 ,256 ),[= ](sycl::nd_item < 1 > it ){ 166const size_t i = it .get_global_id (0 );if (i < words ){const size_t row = i /cw ,k = i %cw ;dst [(row /16 )* 16 * cw + k * 16 + row %16 ]= src [i ];} 167 }); 168 } 169q .wait_and_throw (); 170 std::lock_guard < std::mutex > lock (b60_cache_mutex ); 171auto [it ,inserted ]= b60_weight_caches .emplace (std::make_pair (reinterpret_cast < uintptr_t > (original ),kind ),B60CachedWeight {allocation ,result ,rows ,cols ,device }); 172if (!inserted )throw std::logic_error ("B60 cache duplicate raw address/kind, including cross-device aliases" ); 173 }catch (...){sycl::free (allocation ,q );throw ;} 174} 175void b60_release_weight_cache (const void * original ,int kind ,int device ,void * stream ){ 176void * allocation = nullptr ; 177 { 178 std::lock_guard < std::mutex > lock (b60_cache_mutex ); 179auto it = b60_weight_caches .find ({reinterpret_cast < uintptr_t > (original ),kind }); 180if (it == b60_weight_caches .end ()|| it -> second .device != device )throw std::logic_error ("B60 cache release ownership" ); 181allocation = it -> second .allocation ;b60_weight_caches .erase (it ); 182 } 183 sycl::free (allocation ,* static_cast < sycl::queue *> (stream )); 184} 185} 186 187// Independent registry: existing cache APIs, raw pointers and object ABI stay intact. 188#include "b60_mtp_q8_cache.hpp" 189namespace strata::kernels { 190namespace { 191struct B60MtpQ8Cache {void * allocation ;void * packed ;int rows ,cols ,device ;unsigned token_mask ;}; 192std::mutex b60_mtp_q8_mutex ; 193std::map < uintptr_t ,B60MtpQ8Cache > b60_mtp_q8_caches ; 194class B60MtpQ8ByteCache ; 195} 196bool b60_has_mtp_q8_cache (const void * raw ){ 197 std::lock_guard < std::mutex > lock (b60_mtp_q8_mutex ); 198return b60_mtp_q8_caches .count (reinterpret_cast < uintptr_t > (raw ))!= 0 ; 199} 200const void * b60_cached_mtp_q8_weight (const void * raw ,int rows ,int cols ,int tokens ,int device ){ 201 std::lock_guard < std::mutex > lock (b60_mtp_q8_mutex ); 202auto it = b60_mtp_q8_caches .find (reinterpret_cast < uintptr_t > (raw )); 203if (it == b60_mtp_q8_caches .end ())return nullptr ; 204const auto & c = it -> second ; 205if (c .rows != rows || c .cols != cols || c .device != device )throw std::logic_error ("B60 MTP Q8 cached pointer geometry/device" ); 206return tokens >=1 && tokens <=8 && (c .token_mask & (1u <<tokens ))?c .packed :nullptr ; 207} 208void b60_make_mtp_q8_cache (const void * raw ,int rows ,int cols ,unsigned mask ,int device ,void * stream ){ 209const bool shape = (cols == 2560 && (rows == 2560 || rows == 12288 || rows == 512 || rows == 640 ))|| 210 (rows == 2560 && (cols == 6144 || cols == 640 )); 211if (!raw || !stream || !shape || !mask || (mask & ~((1u <<1 )|(1u <<4 )|(1u <<8 ))))throw std::invalid_argument ("B60 MTP Q8 cache shape/mask" ); 212auto & q =* static_cast < sycl::queue *> (stream ); 213if (!q .is_in_order ()|| device < 0 || 214 sycl::get_pointer_type (raw ,q .get_context ())!= sycl::usm::alloc::device || sycl::get_pointer_device (raw ,q .get_context ())!= q .get_device ()) 215throw std::invalid_argument ("B60 MTP Q8 cache ownership/queue" ); 216const size_t bytes = size_t (rows )* (cols /32 )* 34 ;constexpr size_t guard = 128 ; 217void * allocation = sycl::malloc_device (bytes + 2 * guard ,q );if (!allocation )throw std::bad_alloc (); 218auto * result = static_cast < uint8_t *> (allocation )+ guard ; 219try { 220q .fill (static_cast < uint8_t *> (allocation ),uint8_t (0xa7 ),bytes + 2 * guard ); 221const auto * src = static_cast < const uint8_t *> (raw );const int blocks = cols /32 ; 222q .parallel_for < B60MtpQ8ByteCache > (sycl::nd_range < 1 > (((bytes + 255 )/256 )* 256 ,256 ),[= ](sycl::nd_item < 1 > it ){ 223const size_t i = it .get_global_id (0 ); 224if (i < bytes ){ 225const size_t row = i /(size_t (blocks )* 34 ),kb = (i /34 )%blocks ,x = i %34 ,r = row %16 ; 226const size_t base = (row /16 )* size_t (blocks )* 544 + kb * 544 ; 227const size_t dst = x < 2 ?base + r * 2 + x :base + 32 + ((x - 2 )/4 )* 64 + r * 4 + (x - 2 )%4 ; 228result [dst ]= src [i ]; 229 } 230 }); 231q .wait_and_throw (); 232 std::lock_guard < std::mutex > lock (b60_mtp_q8_mutex ); 233auto [it ,inserted ]= b60_mtp_q8_caches .emplace (reinterpret_cast < uintptr_t > (raw ),B60MtpQ8Cache {allocation ,result ,rows ,cols ,device ,mask }); 234if (!inserted )throw std::logic_error ("B60 duplicate MTP Q8 cache raw pointer" ); 235 }catch (...){sycl::free (allocation ,q );throw ;} 236} 237void b60_release_mtp_q8_cache (const void * raw ,int device ,void * stream ){ 238void * allocation ; 239 { 240 std::lock_guard < std::mutex > lock (b60_mtp_q8_mutex ); 241auto it = b60_mtp_q8_caches .find (reinterpret_cast < uintptr_t > (raw )); 242if (it == b60_mtp_q8_caches .end ()|| it -> second .device != device )throw std::logic_error ("B60 MTP Q8 cache release ownership" ); 243allocation = it -> second .allocation ;b60_mtp_q8_caches .erase (it ); 244 } 245 sycl::free (allocation ,* static_cast < sycl::queue *> (stream )); 246} 247} 248 249// Single-device decode: a device-built plan mixes packed resident slots with raw 250// pinned mirrors. Convert only mirror groups into one reusable device arena. 251// Neither the host mirror nor the model file is modified. The two ordered nodes 252// finish every copy before publishing new pointers to the unchanged FP readers. 253namespace strata::kernels { 254namespace { 255struct SingleExpertStage {uint8_t * data = nullptr ;sycl::queue * queue = nullptr ;size_t stride = 0 ;int cap = 0 ;uintptr_t begin = 0 ,end = 0 ;}; 256SingleExpertStage single_stage ; 257class B60SingleExpertStageCopy ; 258class B60SingleExpertStagePublish ; 259class B60SingleExpertMirrorExchangeCoalesced ; 260inline size_t single_source_byte (size_t i ,int gu ,size_t up ,size_t down ,bool pack16 ,bool pack29 ){ 261const size_t base = i < up ?0 :i < down ?up :down ; 262const size_t z = i - base ; 263const int B = i >=down ?18 :gu == 22 ?82 :gu == 16 && pack16 ?66 :gu == 29 && pack29 ?56 :0 ; 264if (!B )return i ; 265const size_t tile = size_t (16 )* 10 * B ,b = (z /tile )* tile ;const int j = int (z %tile ); 266if (B == 18 )return base + b + packed_to_raw < 18 > (j ,10 ); 267if (B == 82 )return base + b + packed_to_raw < 82 > (j ,10 ); 268if (B == 66 )return base + b + packed_to_raw < 66 > (j ,10 ); 269return base + b + packed_to_raw < 56 > (j ,10 ); 270} 271inline size_t single_destination_byte (size_t i ,int gu ,size_t up ,size_t down ,bool pack16 ,bool pack29 ){ 272const size_t base = i < up ?0 :i < down ?up :down ,z = i - base ; 273const int B = i >=down ?18 :gu == 22 ?82 :gu == 16 && pack16 ?66 :gu == 29 && pack29 ?56 :0 ; 274if (!B )return i ; 275const size_t tile = size_t (16 )* 10 * B ,b = (z /tile )* tile ;const int j = int (z %tile ); 276if (B == 18 )return base + b + raw_to_packed < 18 > (j ,10 ); 277if (B == 82 )return base + b + raw_to_packed < 82 > (j ,10 ); 278if (B == 66 )return base + b + raw_to_packed < 66 > (j ,10 ); 279return base + b + raw_to_packed < 56 > (j ,10 ); 280} 281} 282void b60_single_staging_open (int cap ,size_t blob ,void * stream ){ 283if (single_stage .data || cap <=0 || !blob || blob %64 || !stream )throw std::invalid_argument ("B60 single staging ownership/geometry" ); 284auto * q = static_cast < sycl::queue *> (stream );auto * p = sycl::malloc_device < uint8_t > (size_t (cap )* blob ,* q );if (!p )throw std::bad_alloc (); 285single_stage = {p ,q ,blob ,cap ,0 ,0 };q -> fill (p ,uint8_t (0 ),size_t (cap )* blob ).wait_and_throw (); 286 std::fprintf (stderr ,"B60_SINGLE_STAGE cap=%d stride=%zu bytes=%zu device=0\n" ,cap ,blob ,size_t (cap )* blob ); 287} 288void b60_single_staging_bind (const void * base ,size_t bytes ){ 289if (!single_stage .data || !base || !bytes || single_stage .begin )throw std::invalid_argument ("B60 single staging cache range" ); 290single_stage .begin = reinterpret_cast < uintptr_t > (base );single_stage .end = single_stage .begin + bytes ; 291} 292void b60_single_staging_close (){ 293if (single_stage .data ){single_stage .queue -> wait_and_throw ();sycl::free (single_stage .data ,* single_stage .queue );single_stage = {};} 294} 295void b60_exchange_single_expert (uint8_t * device ,uint8_t * mirror ,int gu ,size_t up ,size_t down ,size_t bytes ,void * stream ){ 296const size_t expected_up = gu == 22 ?524800 :gu == 16 ?422400 :358400 ; 297if (!device || !mirror || !stream || bytes %4 || up != expected_up || up * 2 != down || down + 460800 != bytes || 298 !(gu == 22 || gu == 16 || gu == 29 )|| (reinterpret_cast < uintptr_t > (device )& 63 )|| 299 (reinterpret_cast < uintptr_t > (mirror )& 63 )) 300throw std::invalid_argument ("B60 packed mirror exchange geometry" ); 301auto & q =* static_cast < sycl::queue *> (stream ); 302if (!q .is_in_order ()|| sycl::get_pointer_type (device ,q .get_context ())!= sycl::usm::alloc::device || 303 sycl::get_pointer_type (mirror ,q .get_context ())!= sycl::usm::alloc::host || 304 sycl::get_pointer_device (device ,q .get_context ())!= q .get_device ()) 305throw std::invalid_argument ("B60 packed mirror exchange ownership" ); 306const bool pack16 = [] {const char * p = std::getenv ("STRATA_PRIVATE_GU_XXS_PACKED" );return p && p [0 ]== '1' ;}(); 307const bool pack29 = [] {const char * p = std::getenv ("STRATA_PRIVATE_GU_IQ1_PACKED" );return p && p [0 ]== '1' ;}(); 308// Own a contiguous raw mirror word: all PCIe accesses coalesce. The four 309// scattered resident bytes are in VRAM and belong only to this work-item. 310// The bijection permits in-place exchange without a temporary expert blob. 311auto * host_words = reinterpret_cast < uint32_t *> (mirror ); 312q .parallel_for < B60SingleExpertMirrorExchangeCoalesced > ( 313 sycl::nd_range < 1 > (((bytes /4 + 255 )/256 )* 256 ,256 ), 314 [= ](sycl::nd_item < 1 > it ) [[sycl ::reqd_sub_group_size (32 )]] { 315const size_t w = it .get_global_id (0 );if (w >=bytes /4 )return ; 316const uint32_t incoming = host_words [w ];uint32_t outgoing = 0 ; 317for (int b = 0 ;b < 4 ;++ b ){ 318const size_t packed = single_destination_byte (w * 4 + b ,gu ,up ,down ,pack16 ,pack29 ); 319outgoing |=uint32_t (device [packed ])<<(8 * b ); 320device [packed ]= uint8_t (incoming >>(8 * b )); 321 } 322host_words [w ]= outgoing ; 323 }); 324q .wait_and_throw (); 325} 326void b60_stage_single_experts (unsigned long long * ptrs ,const int32_t * count ,int cap ,int gu ,size_t up ,size_t down ,size_t bytes ,void * stream ){ 327const auto s = single_stage ;auto & q =* static_cast < sycl::queue *> (stream ); 328if (!s .data || !s .begin || !q .is_in_order ()|| q .get_device ()!= s .queue -> get_device ()|| cap < 1 || cap > s .cap || bytes > s .stride || bytes %4 || down + 460800 != bytes || up * 2 != down || !(gu == 22 || gu == 16 || gu == 29 ))throw std::invalid_argument ("B60 single staging plan/geometry" ); 329const bool pack16 = [] {const char * p = std::getenv ("STRATA_PRIVATE_GU_XXS_PACKED" );return p && p [0 ]== '1' ;}(); 330const bool pack29 = [] {const char * p = std::getenv ("STRATA_PRIVATE_GU_IQ1_PACKED" );return p && p [0 ]== '1' ;}(); 331// Each 16 KiB stripe owns disjoint words. All stripes see the original ptrs. 332const size_t stripes = (bytes + 16383 )/16384 ; 333q .parallel_for < B60SingleExpertStageCopy > (sycl::nd_range < 2 > (sycl::range < 2 > (size_t (cap ),stripes * 256 ),sycl::range < 2 > (1 ,256 )),[= ](sycl::nd_item < 2 > it ){ 334const int g = int (it .get_group (0 ));if (g >=* count )return ;const uintptr_t src = ptrs [g ];if (src >=s .begin && src < s .end )return ; 335const auto * p = reinterpret_cast < const uint8_t *> (src );auto * dst = reinterpret_cast < uint32_t *> (s .data + size_t (g )* s .stride ); 336const size_t lo = it .get_group (1 )* 4096 ,hi = sycl::min (lo + 4096 ,bytes /4 ); 337for (size_t w = lo + it .get_local_id (1 );w < hi ;w += 256 ){uint32_t out = 0 ; 338for (int b = 0 ;b < 4 ;++ b )out |=uint32_t (p [single_source_byte (w * 4 + b ,gu ,up ,down ,pack16 ,pack29 )])<<(8 * b ); 339dst [w ]= out ; 340 } 341 }); 342q .parallel_for < B60SingleExpertStagePublish > (sycl::range < 1 > (size_t (cap )),[= ](sycl::id < 1 > it ){const int g = int (it [0 ]);if (g <* count ){const uintptr_t src = ptrs [g ];if (src < s .begin || src >=s .end )ptrs [g ]= reinterpret_cast < uintptr_t > (s .data + size_t (g )* s .stride );}}); 343} 344}