okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// Adapted from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d: 2// src/models/qwen4exp.cpp; ggml/src/ggml-cuda/{set-rows.cu,norm.cu,rope.cu}. 3// MIT License 4// Copyright (c) 2023-2026 The ggml authors 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to deal 7// in the Software without restriction, including without limitation the rights 8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9// copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// The above copyright notice and this permission notice shall be included in all 12// copies or substantial portions of the Software. 13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19// SOFTWARE. 20 21#define DPCT_PROFILING_ENABLED 22#include <sycl/sycl.hpp> 23#include "strata/kprof.hpp" 24#include "strata/core/geometry.hpp" 25#include <dpct/dpct.hpp> 26#include "strata/sycl_queue.hpp" 27#include "strata/kernels/native_qsa_indexer.hpp" 28#include "strata/kernels/mrope.hpp" 29#include <atomic> 30#include <cmath> 31#include <cstddef> 32#include <cstdint> 33#include <limits> 34#include <stdexcept> 35 36namespace strata::kernels { 37namespace { 38std::atomic < bool > enabled {false}; 39constexpr int D = 128 ,R = 4 ,ROT = 64 ,THREADS = 256 ; 40static_assert(D == strata::core::kCompiledGeometry .idx_key_dim && R == strata::core::kCompiledGeometry .idx_block && ROT == strata::core::kCompiledGeometry .n_rot , 41"native_qsa_indexer is compiled for core/geometry.hpp" ); 42inline float warp_sum (float x ) { 43#pragma unroll 44for (int offset = 16 ;offset ;offset >>=1 ) 45/* 46DPCT1108: '__shfl_xor_sync' was migrated with the experimental 47feature masked sub_group function which may not be supported by all 48compilers or runtimes. You may need to adjust the code. 49*/ 50x += dpct::experimental::permute_sub_group_by_xor ( 510xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (),x , 52offset ); 53return x ; 54} 55// TAB (#280, STRATA_ROPE_TABLE=1): the angles from the session's float64 table. The host launches <false> whenever 56// no table applies - the default - so the default kernel is 0.1.31's code exactly (the table read is not in it; 57// with it merely skipped at run time, the compiled default path changed its results). 58template < bool TAB > 59__dpct_inline__ void 60append (const float * __restrict__raw ,const int32_t * __restrict__pos_dev , 61int pos_stride ,int n_steps ,int pos_base , 62const float * __restrict__gamma ,float epsilon ,float * __restrict__tail , 63float * __restrict__dead ,float * __restrict__pooled , 64int32_t * __restrict__block_pos ,int max_cells ,float theta_scale , 65float freq_scale ,float corr_low ,float corr_high ,float ext_factor , 66float mscale ,const int32_t * __restrict__mtab ,RopeTab rt ) { 67auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 68auto & values = * sycl::ext::oneapi::group_local_memory_for_overwrite < float [D ]> ( 69 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 70auto & partials = 71* sycl::ext::oneapi::group_local_memory_for_overwrite < float [32 ]> ( 72 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 73const int d = item_ct1 .get_local_id (2 ); 74for (int step_idx = 0 ;step_idx < n_steps ;++ step_idx ) { 75const int pos = pos_dev [(std::size_t )step_idx * pos_stride ]; 76if (pos >=0 && pos < max_cells ) { 77const float * raw_step = raw + (std::size_t )step_idx * D ; 78const int slot = pos %R ; 79float incoming = 0.0f ; 80if (d < D ) { 81// SET_ROWS stores F16; GET_ROWS expands those exact values to F32. 82incoming = 83 sycl::vec < sycl::half ,1 > ( 84 sycl::vec < float ,1 > (raw_step [d ]) 85 .convert < sycl::half , sycl::rounding_mode::rte > ()[0 ]) 86 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 87if (slot < R - 1 )tail [slot * D + d ]= incoming ; 88 } 89if (pos == 0 || slot == R - 1 ) { 90float mean = 0.0f ; 91if (d < D ) { 92// The spare's four gather indices all name cell zero. Completed blocks 93// use chronological slices; each graph ADD materializes an F32 sum. 94float sum = pos == 0 ?incoming :tail [d ]; 95#pragma unroll 96for (int j = 1 ;j < R ;++ j ) 97/* 98DPCT1013: The rounding mode could not be specified 99and the generated code may have different accuracy than 100the original code. Verify the correctness. SYCL math 101built-in function rounding mode is aligned with OpenCL 102C 1.2 standard. 103*/ 104sum = sum + (pos == 0 || j == R - 1 ?incoming 105 :tail [j * D + d ]); 106/* 107DPCT1013: The rounding mode could not be specified and 108the generated code may have different accuracy than the 109original code. Verify the correctness. SYCL math built-in 110function rounding mode is aligned with OpenCL C 1.2 111standard. 112*/ 113mean = sycl::fma (0.25f ,sum , 1140.0f );// SCALE includes a zero bias. 115 } 116float square_sum = 0.0f ; 117if (d < D )square_sum += mean * mean ; 118square_sum = warp_sum (square_sum ); 119const int lane = d %32 ; 120if (lane == 0 )partials [d /32 ]= square_sum ; 121/* 122DPCT1118: SYCL group functions and algorithms must be 123encountered in converged control flow. You may need to adjust 124the code. 125*/ 126/* 127DPCT1065: Consider replacing sycl::nd_item::barrier() with 128sycl::nd_item::barrier(sycl::access::fence_space::local_space) 129for better performance if there is no access to global memory. 130*/ 131item_ct1 .barrier (); 132square_sum = lane < THREADS /32 ?partials [lane ] :0.0f ; 133square_sum = warp_sum (square_sum ); 134const float scale = sycl::rsqrt (square_sum /D + epsilon ); 135if (d < D )values [d ]= scale * mean * gamma [d ]; 136/* 137DPCT1118: SYCL group functions and algorithms must be 138encountered in converged control flow. You may need to adjust 139the code. 140*/ 141/* 142DPCT1065: Consider replacing sycl::nd_item::barrier() with 143sycl::nd_item::barrier(sycl::access::fence_space::local_space) 144for better performance if there is no access to global memory. 145*/ 146item_ct1 .barrier (); 147if (d < D ) { 148const int b = pos /R ; 149const int rope_pos = pos == 0 ?0 :pos_base + R * b ; 150float y = values [d ]; 151if (d < ROT ) { 152const int pair = d % (ROT /2 ); 153// The spare (pos == 0) keeps its zero angle; under YaRN the mscale magnitude still rides in 154// through cos(0) - which is exactly what the queries are scaled by, so the top-k is unmoved. 155float c ,s ; 156if (!(TAB && rope_tab_cs (rt ,pos == 0 ?0 :mrope_pos (mtab ,rope_pos ,pair ),pair ,c ,s ))) { 157const float theta_extrap = 158 (pos == 0 ?0 159 :mrope_pos (mtab ,rope_pos ,pair ))* 160 dpct::pow (theta_scale ,float (pair )); 161rope_scaled_angle (theta_extrap ,freq_scale ,corr_low ,corr_high ,ext_factor ,mscale ,pair ,c ,s ); 162 } 163const float a = values [pair ],z = values [pair + ROT /2 ]; 164y = d < ROT /2 ?a * c - z * s :a * s + z * c ; 165 } 166pooled [std::size_t (b )* D + d ]= y ; 167if (pos == 0 )dead [d ]= y ; 168else pooled [std::size_t (b + 1 )* D + d ]= dead [d ]; 169if (d == 0 && pos != 0 )* block_pos = rope_pos ; 170 } 171 } 172 } 173/* 174DPCT1118: SYCL group functions and algorithms must be encountered in 175converged control flow. You may need to adjust the code. 176*/ 177/* 178DPCT1065: Consider replacing sycl::nd_item::barrier() with 179sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 180better performance if there is no access to global memory. 181*/ 182if (n_steps > 1 )item_ct1 .barrier (); 183 } 184} 185// ---- C-2: the batched append. The pooled key of a completed block b (its last cell pos = 4b+3), computed with 186// the single append's arithmetic: keys rounded through F16, summed tail[0]+tail[1]+tail[2]+incoming in that order, 187// the RMS norm, gamma, the rotation at pos_base + 4b. `k(j)` is the key of the block's cell j: a row of this batch 188// (cell >= p0) or the tail the previous batch left (cell < p0). Only the batch's last completed block writes the 189// spare after it (every earlier one's is overwritten by the next block in order). 190template < bool TAB > 191__dpct_inline__ float 192pooled_value (const float * values ,int d ,int rope_pos ,float theta_scale , 193float freq_scale ,float corr_low ,float corr_high , 194float ext_factor ,float mscale ,const int32_t * mtab ,bool zero_pos , 195const RopeTab & rt ) { 196float y = values [d ]; 197if (d < ROT ) { 198const int pair = d % (ROT /2 ); 199// The same rotation the single append applies, rope_scaling.hpp's helper shared: under none this is 200// today's cosf/sinf; under linear/YaRN the pooled keys move with the cache's K, and the spare's zero 201// angle carries the mscale magnitude through cos(0) exactly as the single append's does. 202float c ,s ; 203if (!(TAB && rope_tab_cs (rt ,zero_pos ?0 :mrope_pos (mtab ,rope_pos ,pair ),pair ,c ,s ))) { 204const float theta_extrap = 205 (zero_pos ?0 :mrope_pos (mtab ,rope_pos ,pair ))* 206 dpct::pow (theta_scale ,float (pair )); 207rope_scaled_angle (theta_extrap ,freq_scale ,corr_low ,corr_high ,ext_factor ,mscale ,pair ,c ,s ); 208 } 209const float a = values [pair ],z = values [pair + ROT /2 ]; 210y = d < ROT /2 ?a * c - z * s :a * s + z * c ; 211 } 212return y ; 213} 214__dpct_inline__ float norm_scale (float mean ,float * partials ,int d ) { 215float square_sum = d < D ?mean * mean :0.0f ; 216square_sum = warp_sum (square_sum ); 217const int lane = d %32 ; 218if (lane == 0 )partials [d /32 ]= square_sum ; 219/* 220DPCT1065: Consider replacing sycl::nd_item::barrier() with 221sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 222performance if there is no access to global memory. 223*/ 224 sycl::ext::oneapi::this_work_item::get_nd_item < 3 > ().barrier (); 225square_sum = lane < THREADS /32 ?partials [lane ] :0.0f ; 226square_sum = warp_sum (square_sum ); 227return square_sum ; 228} 229// cell 0 of a sequence: the spare (every gather index names cell 0), written to pooled[0] and dead 230template < bool TAB > 231__dpct_inline__ void 232append_first (const float * __restrict__raw ,const float * __restrict__gamma , 233float epsilon ,float * __restrict__dead , 234float * __restrict__pooled ,float theta_scale ,float freq_scale , 235float corr_low ,float corr_high ,float ext_factor ,float mscale , 236const int32_t * __restrict__mtab ,RopeTab rt ) { 237auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 238const int d = item_ct1 .get_local_id (2 ); 239auto & values = 240* sycl::ext::oneapi::group_local_memory_for_overwrite < float [D ]> ( 241 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 242auto & partials = 243* sycl::ext::oneapi::group_local_memory_for_overwrite < float [32 ]> ( 244 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 245float mean = 0.0f ; 246if (d < D ) { 247const float incoming = 248 sycl::vec < sycl::half ,1 > ( 249 sycl::vec < float ,1 > (raw [d ]) 250 .convert < sycl::half , sycl::rounding_mode::rte > ()[0 ]) 251 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 252float sum = incoming ; 253#pragma unroll 254/* 255DPCT1013: The rounding mode could not be specified and the generated 256code may have different accuracy than the original code. Verify the 257correctness. SYCL math built-in function rounding mode is aligned with 258OpenCL C 1.2 standard. 259*/ 260for (int j = 1 ;j < R ;++ j )sum = sum + incoming ; 261/* 262DPCT1013: The rounding mode could not be specified and the generated 263code may have different accuracy than the original code. Verify the 264correctness. SYCL math built-in function rounding mode is aligned with 265OpenCL C 1.2 standard. 266*/ 267mean = sycl::fma (0.25f ,sum ,0.0f ); 268 } 269const float square_sum = norm_scale (mean ,partials ,d ); 270const float scale = sycl::rsqrt (square_sum /D + epsilon ); 271if (d < D )values [d ]= scale * mean * gamma [d ]; 272/* 273DPCT1065: Consider replacing sycl::nd_item::barrier() with 274sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 275performance if there is no access to global memory. 276*/ 277item_ct1 .barrier (); 278if (d >=D )return ; 279const float y = pooled_value < TAB > (values ,d ,0 ,theta_scale ,freq_scale ,corr_low ,corr_high ,ext_factor ,mscale , 280mtab , true,rt ); 281pooled [d ]= y ; 282dead [d ]= y ; 283} 284template < bool TAB > 285__dpct_inline__ void 286append_blocks (const float * __restrict__raw ,int64_t n ,int64_t p0 , 287int pos_base ,const float * __restrict__gamma ,float epsilon , 288const float * __restrict__tail ,const float * __restrict__dead , 289float * __restrict__pooled ,int32_t * __restrict__block_pos , 290int64_t first_block ,int64_t last_block ,float theta_scale , 291float freq_scale ,float corr_low ,float corr_high , 292float ext_factor ,float mscale ,const int32_t * __restrict__mtab , 293RopeTab rt ) { 294auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 295const int64_t b = first_block + item_ct1 .get_group (2 ); 296const int d = item_ct1 .get_local_id (2 ); 297auto & values = 298* sycl::ext::oneapi::group_local_memory_for_overwrite < float [D ]> ( 299 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 300auto & partials = 301* sycl::ext::oneapi::group_local_memory_for_overwrite < float [32 ]> ( 302 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 303float mean = 0.0f ; 304if (d < D ) { 305auto key = [& ](int j )-> float { 306const int64_t cell = b * R + j ; 307return cell >=p0 308 ? sycl::vec < sycl::half ,1 > ( 309 sycl::vec < float ,1 > (raw [(cell - p0 )* D + d ]) 310 .convert < sycl::half , 311 sycl::rounding_mode::rte > ()[0 ]) 312 .convert < float , 313 sycl::rounding_mode::automatic > ()[0 ] 314 :tail [j * D + d ]; 315 }; 316float sum = key (0 ); 317#pragma unroll 318/* 319DPCT1013: The rounding mode could not be specified and the generated 320code may have different accuracy than the original code. Verify the 321correctness. SYCL math built-in function rounding mode is aligned with 322OpenCL C 1.2 standard. 323*/ 324for (int j = 1 ;j < R ;++ j )sum = sum + key (j ); 325/* 326DPCT1013: The rounding mode could not be specified and the generated 327code may have different accuracy than the original code. Verify the 328correctness. SYCL math built-in function rounding mode is aligned with 329OpenCL C 1.2 standard. 330*/ 331mean = sycl::fma (0.25f ,sum ,0.0f ); 332 } 333const float square_sum = norm_scale (mean ,partials ,d ); 334const float scale = sycl::rsqrt (square_sum /D + epsilon ); 335if (d < D )values [d ]= scale * mean * gamma [d ]; 336/* 337DPCT1065: Consider replacing sycl::nd_item::barrier() with 338sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 339performance if there is no access to global memory. 340*/ 341item_ct1 .barrier (); 342if (d >=D )return ; 343const int rope_pos = pos_base + R * (int )b ; 344pooled [std::size_t (b )* D + d ]= pooled_value < TAB > (values ,d ,rope_pos ,theta_scale ,freq_scale ,corr_low ,corr_high , 345ext_factor ,mscale ,mtab , false,rt ); 346if (b == last_block ) { 347pooled [std::size_t (b + 1 )* D + d ]= dead [d ]; 348if (d == 0 )* block_pos = rope_pos ; 349 } 350} 351// the tail after the batch: slot s holds the key of the batch's last cell with cell % 4 == s (s < 3), if any 352__dpct_inline__ void append_tail (const float * __restrict__raw ,int64_t n , 353int64_t p0 ,float * __restrict__tail ) { 354auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 355const int s = item_ct1 .get_group (2 ),d = item_ct1 .get_local_id (2 ); 356if (d >=D )return ; 357const int64_t last = p0 + n - 1 ; 358int64_t cell = last - ((last %R )- s + R ) %R ;// the last cell <= last with cell % 4 == s 359if (cell < p0 )return ; 360tail [s * D + d ]= 361 sycl::vec < sycl::half ,1 > ( 362 sycl::vec < float ,1 > (raw [(cell - p0 )* D + d ]) 363 .convert < sycl::half , sycl::rounding_mode::rte > ()[0 ]) 364 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 365} 366struct Span {const void * p ; std::size_t n ; }; 367void validate (Span s ) { 368const auto p = reinterpret_cast < std::uintptr_t > (s .p ); 369if (!p || p %4 || s .n > UINTPTR_MAX - p ) 370throw std::invalid_argument ("native QSA indexer requires aligned bounded spans" ); 371} 372bool overlaps (Span a ,Span b ) { 373const auto x = reinterpret_cast < std::uintptr_t > (a .p ),y = reinterpret_cast < std::uintptr_t > (b .p ); 374return x < y + b .n && y < x + a .n ; 375} 376}// namespace 377 378void native_qsa_indexer_set_enabled (bool value ) {enabled .store (value , std::memory_order_relaxed ); } 379bool native_qsa_indexer_enabled () {return enabled .load (std::memory_order_relaxed ); } 380void native_qsa_indexer_append_steps (const float * raw ,const int32_t * relative_pos_device , 381int pos_stride ,int n_steps ,int32_t pos_base , 382const float * gamma ,float epsilon ,const QsaIndexerBuffers & b , 383const QsaShapes & s ,int64_t max_cells ,const RopeScaling & scaling ,void * stream ) { 384if (n_steps <=0 )return ; 385if (!stream || s .idx_dim != D || s .idx_block != R || s .n_rot != ROT || 386max_cells < 1 || max_cells > INT32_MAX || pos_base < 0 || pos_base %R || 387int64_t (pos_base )+ max_cells > INT32_MAX || !std::isfinite (epsilon )|| epsilon <=0.0f || 388rope_scaling_invalid (scaling )!= nullptr ) 389throw std::invalid_argument ("native QSA indexer requires fixed geometry, aligned position base, positive capacity/epsilon, valid frequency/scaling and explicit stream" ); 390const std::size_t pos_span = (n_steps == 1 || pos_stride <=0 ) ?4 : (std::size_t (n_steps - 1 )* pos_stride + 1 )* 4 ; 391const Span spans []= {{raw ,std::size_t (n_steps )* D * 4 },{relative_pos_device ,pos_span },{gamma ,D * 4 },{b .tail ,(R - 1 )* D * 4 }, 392 {b .dead ,D * 4 },{b .pooled ,std::size_t (max_cells /R + 1 )* D * 4 },{b .block_pos ,4 }}; 393for (const auto & span :spans )validate (span ); 394for (int i = 0 ;i < 7 ;++ i )for (int j = i + 1 ;j < 7 ;++ j ) 395if (overlaps (spans [i ],spans [j ]))throw std::invalid_argument ("native QSA indexer buffers overlap" ); 396const float theta_scale = powf ((float )scaling .freq_base ,-2.0f /ROT ); 397const RopeKernelArgs k = scaling .kernel_args (ROT );// none: the identity constants 398const RopeTab rt = rope_table_for (scaling ); 399if (rt .cos != nullptr ) 400 { 401auto exp_props = sycl::ext::oneapi::experimental::properties { 402 sycl::ext::oneapi::experimental::use_root_sync }; 403 404 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 405-> submit ([& ](sycl::handler & cgh ) { 406auto mrope_table_ct18 = mrope_table (); 407 408cgh .parallel_for < dpct_kernel_name < class append_182f95 , 409dpct_kernel_scalar < true>>> ( 410 sycl::nd_range < 3 > (sycl::range (1 ,1 ,THREADS ), 411 sycl::range (1 ,1 ,THREADS )), 412exp_props , 413 [= ](sycl::nd_item < 3 > item_ct1 ) 414 [[sycl ::reqd_sub_group_size (32 )]] { 415append < true> (raw ,relative_pos_device ,pos_stride , 416n_steps ,pos_base ,gamma ,epsilon , 417b .tail ,b .dead ,b .pooled ,b .block_pos , 418int (max_cells ),theta_scale , 419k .freq_scale ,k .corr_low ,k .corr_high , 420k .ext_factor ,k .attn_factor , 421mrope_table_ct18 ,rt ); 422 }); 423 }); 424 }else { 425auto exp_props = sycl::ext::oneapi::experimental::properties { 426 sycl::ext::oneapi::experimental::use_root_sync }; 427 428 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 429-> submit ([& ](sycl::handler & cgh ) { 430auto mrope_table_ct18 = mrope_table (); 431 432cgh .parallel_for < dpct_kernel_name < class append_182f95 , 433dpct_kernel_scalar < false>>> ( 434 sycl::nd_range < 3 > (sycl::range (1 ,1 ,THREADS ), 435 sycl::range (1 ,1 ,THREADS )), 436exp_props , 437 [= ](sycl::nd_item < 3 > item_ct1 ) 438 [[sycl ::reqd_sub_group_size (32 )]] { 439append < false> (raw ,relative_pos_device ,pos_stride , 440n_steps ,pos_base ,gamma ,epsilon , 441b .tail ,b .dead ,b .pooled ,b .block_pos , 442int (max_cells ),theta_scale , 443k .freq_scale ,k .corr_low ,k .corr_high , 444k .ext_factor ,k .attn_factor , 445mrope_table_ct18 ,rt ); 446 }); 447 }); 448 } 449/* 450DPCT1010: SYCL uses exceptions to report errors and does not use the 451error codes. The cudaGetLastError function call was replaced with 0. You 452need to rewrite this code. 453*/ 454const auto error = 0 ; 455/* 456DPCT1009: SYCL reports errors using exceptions and does not use error 457codes. Please replace the "get_error_string_dummy(...)" with a real 458error-handling function. 459*/ 460/* 461DPCT1001: The statement could not be removed. 462*/ 463/* 464DPCT1000: Error handling if-stmt was detected but could not be 465rewritten. 466*/ 467if (error != 4680 )throw std::runtime_error (dpct::get_error_string_dummy (error )); 469} 470 471void native_qsa_indexer_append (const float * raw ,const int32_t * relative_pos_device ,int32_t pos_base , 472const float * gamma ,float epsilon ,const QsaIndexerBuffers & b , 473const QsaShapes & s ,int64_t max_cells ,const RopeScaling & scaling ,void * stream ) { 474native_qsa_indexer_append_steps (raw ,relative_pos_device ,0 ,1 ,pos_base ,gamma ,epsilon ,b ,s ,max_cells ,scaling ,stream ); 475} 476void native_qsa_indexer_append_batch (const float * raw ,int64_t n ,int64_t p0 ,int32_t pos_base ,const float * gamma , 477float epsilon ,const QsaIndexerBuffers & b ,const QsaShapes & s ,int64_t max_cells , 478const RopeScaling & scaling ,void * stream ) { 479if (n <=0 )return ; 480if (!stream || s .idx_dim != D || s .idx_block != R || s .n_rot != ROT || p0 < 0 || p0 + n > max_cells || 481max_cells > INT32_MAX || pos_base < 0 || pos_base %R || int64_t (pos_base )+ max_cells > INT32_MAX || 482 !std::isfinite (epsilon )|| epsilon <=0.0f || rope_scaling_invalid (scaling )!= nullptr ) 483throw std::invalid_argument ("native QSA indexer (batch): bad geometry, positions, parameters or scaling" ); 484const float theta_scale = powf ((float )scaling .freq_base ,-2.0f /ROT ); 485const RopeKernelArgs k = scaling .kernel_args (ROT );// none: the identity constants 486const float fsf = k .freq_scale ,cl = k .corr_low ,ch = k .corr_high ,ef = k .ext_factor ,ms = k .attn_factor ; 487const dpct::queue_ptr st = strata::q_of (stream ); 488const int32_t * mtab = mrope_table (); 489const RopeTab rt = rope_table_for (scaling ); 490if (p0 == 0 ) { 491if (rt .cos != nullptr ) 492 { 493auto exp_props = sycl::ext::oneapi::experimental::properties { 494 sycl::ext::oneapi::experimental::use_root_sync }; 495 496 strata::kprof::kp (st ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class append_first_ea2d1f , 497dpct_kernel_scalar < true>>> ( 498 sycl::nd_range < 3 > (sycl::range (1 ,1 ,THREADS ), 499 sycl::range (1 ,1 ,THREADS )), 500exp_props , 501 [= ](sycl::nd_item < 3 > item_ct1 ) 502 [[sycl ::reqd_sub_group_size (32 )]] { 503append_first < true> (raw ,gamma ,epsilon ,b .dead , 504b .pooled ,theta_scale ,fsf ,cl ,ch , 505ef ,ms ,mtab ,rt ); 506 }); 507 }else { 508auto exp_props = sycl::ext::oneapi::experimental::properties { 509 sycl::ext::oneapi::experimental::use_root_sync }; 510 511 strata::kprof::kp (st ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class append_first_ea2d1f , 512dpct_kernel_scalar < false>>> ( 513 sycl::nd_range < 3 > (sycl::range (1 ,1 ,THREADS ), 514 sycl::range (1 ,1 ,THREADS )), 515exp_props , 516 [= ](sycl::nd_item < 3 > item_ct1 ) 517 [[sycl ::reqd_sub_group_size (32 )]] { 518append_first < false> (raw ,gamma ,epsilon ,b .dead , 519b .pooled ,theta_scale ,fsf ,cl ,ch , 520ef ,ms ,mtab ,rt ); 521 }); 522 } 523 } 524// completed blocks: those whose last cell (4b+3) lies in [p0, p0 + n) 525const int64_t first = p0 <=R - 1 ?0 : (p0 - (R - 1 )+ R - 1 ) /R ;// the smallest b with 4b+3 >= p0 526const int64_t hi = p0 + n - 1 >=R - 1 ? (p0 + n - 1 - (R - 1 )) /R :-1 ;// the largest b with 4b+3 <= p0+n-1 527if (hi >=first && rt .cos != nullptr ) 528 { 529auto exp_props = sycl::ext::oneapi::experimental::properties { 530 sycl::ext::oneapi::experimental::use_root_sync }; 531 532 strata::kprof::kp (st ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class append_blocks_a6c94e , 533dpct_kernel_scalar < true>>> ( 534 sycl::nd_range < 3 > (sycl::range (1 ,1 , (unsigned )(hi - first + 1 ))* 535 sycl::range (1 ,1 ,THREADS ), 536 sycl::range (1 ,1 ,THREADS )), 537exp_props , 538 [= ](sycl::nd_item < 3 > item_ct1 ) [[sycl ::reqd_sub_group_size (32 )]] { 539append_blocks < true> (raw ,n ,p0 ,pos_base ,gamma ,epsilon , 540b .tail ,b .dead ,b .pooled ,b .block_pos , 541first ,hi ,theta_scale ,fsf ,cl ,ch ,ef ,ms , 542mtab ,rt ); 543 }); 544 }else if (hi >=first ) 545 { 546auto exp_props = sycl::ext::oneapi::experimental::properties { 547 sycl::ext::oneapi::experimental::use_root_sync }; 548 549 strata::kprof::kp (st ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class append_blocks_a6c94e , 550dpct_kernel_scalar < false>>> ( 551 sycl::nd_range < 3 > (sycl::range (1 ,1 , (unsigned )(hi - first + 1 ))* 552 sycl::range (1 ,1 ,THREADS ), 553 sycl::range (1 ,1 ,THREADS )), 554exp_props , 555 [= ](sycl::nd_item < 3 > item_ct1 ) [[sycl ::reqd_sub_group_size (32 )]] { 556append_blocks < false> (raw ,n ,p0 ,pos_base ,gamma ,epsilon , 557b .tail ,b .dead ,b .pooled ,b .block_pos , 558first ,hi ,theta_scale ,fsf ,cl ,ch ,ef , 559ms ,mtab ,rt ); 560 }); 561 } 562 { 563auto exp_props = sycl::ext::oneapi::experimental::properties { 564 sycl::ext::oneapi::experimental::use_root_sync }; 565 566 strata::kprof::kp (st ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class append_tail_e0658e >> ( 567 sycl::nd_range < 3 > (sycl::range (1 ,1 ,R - 1 )* sycl::range (1 ,1 ,D ), 568 sycl::range (1 ,1 ,D )), 569exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 570append_tail (raw ,n ,p0 ,b .tail ); 571 }); 572 } 573/* 574DPCT1010: SYCL uses exceptions to report errors and does not use the 575error codes. The cudaGetLastError function call was replaced with 0. You 576need to rewrite this code. 577*/ 578const auto error = 0 ; 579/* 580DPCT1009: SYCL reports errors using exceptions and does not use error 581codes. Please replace the "get_error_string_dummy(...)" with a real 582error-handling function. 583*/ 584/* 585DPCT1001: The statement could not be removed. 586*/ 587/* 588DPCT1000: Error handling if-stmt was detected but could not be 589rewritten. 590*/ 591if (error != 5920 )throw std::runtime_error (dpct::get_error_string_dummy (error )); 593} 594}// namespace strata::kernels