char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
749f688fc
char/aliceai-t5-moe
1#include "rope.hpp" 2#include "convert.hpp" 3#include "ggml-sycl/common.hpp" 4#include "ggml.h" 5 6struct rope_corr_dims { 7float v [2 ]; 8}; 9 10struct mrope_sections { 11int v [4 ]; 12}; 13 14static float rope_yarn_ramp (const float low ,const float high ,const int i0 ) { 15const float y = (i0 /2 - low ) / sycl::max (0.001f ,high - low ); 16return 1.0f - sycl::min (1.0f , sycl::max (0.0f ,y )); 17} 18 19template < bool forward > 20static void rope_yarn (const float theta_extrap ,const float freq_scale , 21const rope_corr_dims corr_dims ,const int64_t i0 , 22const float ext_factor ,float mscale ,float & cos_theta , 23float & sin_theta ) { 24float theta_interp = freq_scale * theta_extrap ; 25float theta = theta_interp ; 26if (ext_factor != 0.0f ) { 27float ramp_mix = 28rope_yarn_ramp (corr_dims .v [0 ],corr_dims .v [1 ],i0 )* ext_factor ; 29theta = theta_interp * (1 - ramp_mix )+ theta_extrap * ramp_mix ; 30 31mscale *=1.0f + 0.1f * sycl::log (1.0f /freq_scale ); 32 } 33cos_theta = sycl::cos (theta )* mscale ; 34sin_theta = sycl::sin (theta )* mscale ; 35if (!forward ) { 36sin_theta *=-1.0f ; 37 } 38} 39 40template < bool forward ,bool has_ff ,typename T ,typename D > 41static void rope_norm (const T * x ,D * dst ,const int ne00 ,const int ne01 , 42const int ne02 ,const int s01 ,const int s02 , 43const int s03 ,const int s1 ,const int s2 ,const int s3 , 44const int n_dims ,const int n_offs ,const int32_t * pos , 45const float freq_scale ,const float ext_factor , 46const float attn_factor ,const rope_corr_dims corr_dims , 47const float theta_scale ,const float * freq_factors , 48const int64_t * row_indices ,const int set_rows_stride ) { 49auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 50const int i0 = 2 * (item_ct1 .get_local_range (1 )* item_ct1 .get_group (1 )+ 51item_ct1 .get_local_id (1 )); 52 53if (i0 >=ne00 ) { 54return ; 55 } 56 57const int row_dst = item_ct1 .get_local_range (2 )* item_ct1 .get_group (2 )+ 58item_ct1 .get_local_id (2 ); 59 60const uint32_t i3 = row_dst / (ne01 * ne02 ); 61const uint32_t i2 = (row_dst - i3 * ne01 * ne02 ) /ne01 ; 62const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01 ; 63 64int idst = i0 + i1 * s1 + i2 * s2 + i3 * s3 ; 65const int ix = i0 + i1 * s01 + i2 * s02 + i3 * s03 ; 66 67if (set_rows_stride != 0 ) { 68idst = i1 * s1 + i0 ; 69idst += row_indices [i2 ]* set_rows_stride ; 70 } 71 72const auto & store_coaelsced = [& ](float x0 ,float x1 ) { 73if constexpr (std::is_same_v < float ,D > ) { 74 sycl::float2 v = sycl::float2 (x0 ,x1 ); 75ggml_sycl_memcpy_1 < 8 > (dst + idst ,& v ); 76 }else if constexpr (std::is_same_v < sycl::half ,D > ) { 77 sycl::half2 v = sycl::half2 (x0 ,x1 ); 78ggml_sycl_memcpy_1 < 4 > (dst + idst ,& v ); 79 } 80 }; 81if (i0 < n_offs || i0 >=n_offs + n_dims ) { 82store_coaelsced (x [ix + 0 ],x [ix + 1 ]); 83return ; 84 } 85 86const int iw = i0 - n_offs ;// relative idx 87 88const float theta_base = pos [i2 ]* dpct::pow (theta_scale ,iw /2.0f ); 89 90const float freq_factor = has_ff ?freq_factors [iw /2 ] :1.0f ; 91 92float cos_theta ; 93float sin_theta ; 94 95rope_yarn < forward > (theta_base /freq_factor ,freq_scale ,corr_dims ,iw , 96ext_factor ,attn_factor ,cos_theta ,sin_theta ); 97 98const float x0 = x [ix + 0 ]; 99const float x1 = x [ix + 1 ]; 100 101store_coaelsced (x0 * cos_theta - x1 * sin_theta , 102x0 * sin_theta + x1 * cos_theta ); 103} 104 105template < bool forward ,bool has_ff ,typename T ,typename D > 106static void rope_neox (const T * x ,D * dst ,const int ne00 ,const int ne01 , 107const int ne02 ,const int s01 ,const int s02 , 108const int s03 ,const int s1 ,const int s2 ,const int s3 , 109const int n_dims ,const int n_offs ,const int32_t * pos , 110const float freq_scale ,const float ext_factor , 111const float attn_factor ,const rope_corr_dims corr_dims , 112const float theta_scale ,const float * freq_factors , 113const int64_t * row_indices ,const int set_rows_stride ) { 114auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 115const int i0 = 2 * (item_ct1 .get_local_range (1 )* item_ct1 .get_group (1 )+ 116item_ct1 .get_local_id (1 )); 117 118if (i0 >=ne00 ) { 119return ; 120 } 121 122const int row_dst = item_ct1 .get_local_range (2 )* item_ct1 .get_group (2 )+ 123item_ct1 .get_local_id (2 ); 124 125const uint32_t i3 = row_dst / (ne01 * ne02 ); 126const uint32_t i2 = (row_dst - i3 * ne01 * ne02 ) /ne01 ; 127const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01 ; 128 129int idst = i0 /2 + i1 * s1 + i2 * s2 + i3 * s3 ; 130const int ix = i0 /2 + i1 * s01 + i2 * s02 + i3 * s03 ; 131 132if (set_rows_stride != 0 ) { 133idst = i1 * s1 + i0 /2 ; 134idst += row_indices [i2 ]* set_rows_stride ; 135 } 136 137if (i0 < n_offs || i0 >=n_offs + n_dims ) { 138dst [idst + i0 /2 + 0 ]= ggml_sycl_cast < D > (x [ix + i0 /2 + 0 ]); 139dst [idst + i0 /2 + 1 ]= ggml_sycl_cast < D > (x [ix + i0 /2 + 1 ]); 140 141return ; 142 } 143 144const int iw = i0 - n_offs ;// relative idx 145 146const float theta_base = pos [i2 ]* dpct::pow (theta_scale ,iw /2.0f ); 147 148const float freq_factor = has_ff ?freq_factors [iw /2 ] :1.0f ; 149 150float cos_theta ; 151float sin_theta ; 152 153rope_yarn < forward > (theta_base /freq_factor ,freq_scale ,corr_dims ,iw , 154ext_factor ,attn_factor ,cos_theta ,sin_theta ); 155 156// idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2 157const float x0 = x [ix + n_offs /2 + 0 ]; 158const float x1 = x [ix + n_offs /2 + n_dims /2 ]; 159 160dst [idst + n_offs /2 + 0 ]= ggml_sycl_cast < D > (x0 * cos_theta - x1 * sin_theta ); 161dst [idst + n_offs /2 + n_dims /2 ]= ggml_sycl_cast < D > (x0 * sin_theta + x1 * cos_theta ); 162} 163 164template < bool forward ,bool has_ff ,typename T > 165static void rope_multi (const T * x ,T * dst ,const int ne00 ,const int ne01 , 166const int ne02 ,const int s01 ,const int s02 , 167const int s03 ,const int s1 ,const int s2 ,const int s3 , 168const int n_dims ,const int n_offs ,const int32_t * pos , 169const float freq_scale ,const float ext_factor , 170const float attn_factor ,const rope_corr_dims corr_dims , 171const float theta_scale ,const float * freq_factors , 172const mrope_sections sections ,const bool is_imrope ) { 173auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 174const int i0 = 2 * (item_ct1 .get_local_range (1 )* item_ct1 .get_group (1 )+ 175item_ct1 .get_local_id (1 )); 176 177if (i0 >=ne00 ) { 178return ; 179 } 180 181const int row_dst = item_ct1 .get_local_range (2 )* item_ct1 .get_group (2 )+ 182item_ct1 .get_local_id (2 ); 183 184const uint32_t i3 = row_dst / (ne01 * ne02 ); 185const uint32_t i2 = (row_dst - i3 * ne01 * ne02 ) /ne01 ; 186const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01 ; 187 188int idst = i0 /2 + i1 * s1 + i2 * s2 + i3 * s3 ; 189const int ix = i0 /2 + i1 * s01 + i2 * s02 + i3 * s03 ; 190 191if (i0 < n_offs || i0 >=n_offs + n_dims ) { 192dst [idst + i0 /2 + 0 ]= x [ix + i0 /2 + 0 ]; 193dst [idst + i0 /2 + 1 ]= x [ix + i0 /2 + 1 ]; 194 195return ; 196 } 197 198const int iw = i0 - n_offs ;// relative idx 199 200const int sect_dims = 201sections .v [0 ]+ sections .v [1 ]+ sections .v [2 ]+ sections .v [3 ]; 202const int sec_w = sections .v [1 ]+ sections .v [0 ]; 203const int sector = (iw /2 ) %sect_dims ; 204 205float theta_base = 0.0 ; 206if (is_imrope ) { 207if (sector %3 == 1 && sector < 3 * sections .v [1 ]) {// h 208theta_base = pos [i2 + ne02 * 1 ]* dpct::pow (theta_scale ,iw /2.0f ); 209 }else if (sector %3 == 2 && sector < 3 * sections .v [2 ]) {// w 210theta_base = pos [i2 + ne02 * 2 ]* dpct::pow (theta_scale ,iw /2.0f ); 211 }else if (sector %3 == 0 && sector < 3 * sections .v [0 ]) {// t 212theta_base = pos [i2 ]* dpct::pow (theta_scale ,iw /2.0f ); 213 }else { 214theta_base = pos [i2 + ne02 * 3 ]* dpct::pow (theta_scale ,iw /2.0f ); 215 } 216 }else { 217if (sector < sections .v [0 ]) { 218theta_base = pos [i2 ]* dpct::pow (theta_scale ,iw /2.0f ); 219 }else if (sector >=sections .v [0 ]&& sector < sec_w ) { 220theta_base = pos [i2 + ne02 * 1 ]* dpct::pow (theta_scale ,iw /2.0f ); 221 }else if (sector >=sec_w && sector < sec_w + sections .v [2 ]) { 222theta_base = pos [i2 + ne02 * 2 ]* dpct::pow (theta_scale ,iw /2.0f ); 223 }else if (sector >=sec_w + sections .v [2 ]) { 224theta_base = pos [i2 + ne02 * 3 ]* dpct::pow (theta_scale ,iw /2.0f ); 225 } 226 } 227 228const float freq_factor = has_ff ?freq_factors [iw /2 ] :1.0f ; 229 230float cos_theta ; 231float sin_theta ; 232 233rope_yarn < forward > (theta_base /freq_factor ,freq_scale ,corr_dims ,iw , 234ext_factor ,attn_factor ,cos_theta ,sin_theta ); 235 236// idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2 237const float x0 = x [ix + n_offs /2 + 0 ]; 238const float x1 = x [ix + n_offs /2 + n_dims /2 ]; 239 240dst [idst + n_offs /2 + 0 ]= x0 * cos_theta - x1 * sin_theta ; 241dst [idst + n_offs /2 + n_dims /2 ]= x0 * sin_theta + x1 * cos_theta ; 242} 243 244template < bool forward ,bool has_ff ,typename T > 245static void rope_vision (const T * x ,T * dst ,const int ne00 ,const int ne01 , 246const int ne02 ,const int s01 ,const int s02 , 247const int s03 ,const int s1 ,const int s2 ,const int s3 , 248const int n_dims ,const int32_t * pos , 249const float freq_scale ,const float ext_factor , 250const float attn_factor ,const rope_corr_dims corr_dims , 251const float theta_scale ,const float * freq_factors , 252const mrope_sections sections ) { 253auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 254const int i0 = 2 * (item_ct1 .get_local_range (1 )* item_ct1 .get_group (1 )+ 255item_ct1 .get_local_id (1 )); 256 257if (i0 >=ne00 ) { 258return ; 259 } 260 261const int row_dst = item_ct1 .get_local_range (2 )* item_ct1 .get_group (2 )+ 262item_ct1 .get_local_id (2 ); 263 264const uint32_t i3 = row_dst / (ne01 * ne02 ); 265const uint32_t i2 = (row_dst - i3 * ne01 * ne02 ) /ne01 ; 266const uint32_t i1 = row_dst - i3 * ne01 * ne02 - i2 * ne01 ; 267 268int idst = i0 /2 + i1 * s1 + i2 * s2 + i3 * s3 ; 269const int ix = i0 /2 + i1 * s01 + i2 * s02 + i3 * s03 ; 270 271const int sect_dims = sections .v [0 ]+ sections .v [1 ]; 272const int sec_w = sections .v [1 ]+ sections .v [0 ]; 273const int sector = (i0 /2 ) %sect_dims ; 274 275float theta_base = 0.0 ; 276if (sector < sections .v [0 ]) { 277const int p = sector ; 278theta_base = pos [i2 ]* dpct::pow (theta_scale ,p ); 279 }else if (sector >=sections .v [0 ]&& sector < sec_w ) { 280const int p = sector - sections .v [0 ]; 281theta_base = pos [i2 + ne02 ]* dpct::pow (theta_scale ,p ); 282 } 283 284const float freq_factor = has_ff ?freq_factors [i0 /2 ] :1.0f ; 285 286float cos_theta ; 287float sin_theta ; 288 289rope_yarn < forward > (theta_base /freq_factor ,freq_scale ,corr_dims ,i0 , 290ext_factor ,attn_factor ,cos_theta ,sin_theta ); 291 292const float x0 = x [ix + 0 ]; 293const float x1 = x [ix + n_dims ]; 294 295dst [idst + 0 ]= x0 * cos_theta - x1 * sin_theta ; 296dst [idst + n_dims ]= x0 * sin_theta + x1 * cos_theta ; 297} 298 299template < bool forward ,typename T ,typename D > 300static void 301rope_norm_sycl (const T * x ,D * dst ,const int ne00 ,const int ne01 , 302const int ne02 ,const int s01 ,const int s02 ,const int s03 , 303const int s1 ,const int s2 ,const int s3 ,const int n_dims , 304const int n_offs ,const int nr ,const int32_t * pos ,const float freq_scale , 305const float freq_base ,const float ext_factor , 306const float attn_factor ,const rope_corr_dims corr_dims , 307const float * freq_factors ,const int64_t * row_indices , 308const int set_rows_stride , dpct::queue_ptr stream ) { 309GGML_ASSERT (ne00 %2 == 0 ); 310const dpct::dim3 block_dims (1 ,SYCL_ROPE_BLOCK_SIZE ,1 ); 311const int n_blocks_x = 312 (ne00 + 2 * SYCL_ROPE_BLOCK_SIZE - 1 ) / (2 * SYCL_ROPE_BLOCK_SIZE ); 313const dpct::dim3 block_nums (nr ,n_blocks_x ,1 ); 314 315const float theta_scale = powf (freq_base ,-2.0f /n_dims ); 316 317if (freq_factors == nullptr ) { 318stream -> parallel_for ( 319 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 320 [= ](sycl::nd_item < 3 > item_ct1 ) { 321GGML_UNUSED (item_ct1 ); 322rope_norm < forward , false> ( 323x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 324n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 325theta_scale ,freq_factors ,row_indices ,set_rows_stride ); 326 }); 327 }else { 328stream -> parallel_for ( 329 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 330 [= ](sycl::nd_item < 3 > item_ct1 ) { 331GGML_UNUSED (item_ct1 ); 332rope_norm < forward , true> ( 333x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 334n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 335theta_scale ,freq_factors ,row_indices ,set_rows_stride ); 336 }); 337 } 338} 339 340template < bool forward ,typename T ,typename D > 341static void 342rope_neox_sycl (const T * x ,D * dst ,const int ne00 ,const int ne01 , 343const int ne02 ,const int s01 ,const int s02 ,const int s03 , 344const int s1 ,const int s2 ,const int s3 ,const int n_dims , 345const int n_offs ,const int nr ,const int32_t * pos ,const float freq_scale , 346const float freq_base ,const float ext_factor , 347const float attn_factor ,const rope_corr_dims corr_dims , 348const float * freq_factors ,const int64_t * row_indices , 349const int set_rows_stride , dpct::queue_ptr stream ) { 350GGML_ASSERT (ne00 %2 == 0 ); 351const dpct::dim3 block_dims (1 ,SYCL_ROPE_BLOCK_SIZE ,1 ); 352const int n_blocks_x = 353 (ne00 + 2 * SYCL_ROPE_BLOCK_SIZE - 1 ) / (2 * SYCL_ROPE_BLOCK_SIZE ); 354const dpct::dim3 block_nums (nr ,n_blocks_x ,1 ); 355 356const float theta_scale = powf (freq_base ,-2.0f /n_dims ); 357 358if (freq_factors == nullptr ) { 359stream -> parallel_for ( 360 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 361 [= ](sycl::nd_item < 3 > item_ct1 ) { 362GGML_UNUSED (item_ct1 ); 363rope_neox < forward , false> ( 364x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 365n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 366theta_scale ,freq_factors ,row_indices ,set_rows_stride ); 367 }); 368 }else { 369stream -> parallel_for ( 370 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 371 [= ](sycl::nd_item < 3 > item_ct1 ) { 372GGML_UNUSED (item_ct1 ); 373rope_neox < forward , true> ( 374x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 375n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 376theta_scale ,freq_factors ,row_indices ,set_rows_stride ); 377 }); 378 } 379} 380 381template < bool forward ,typename T > 382static void 383rope_multi_sycl (const T * x ,T * dst ,const int ne00 ,const int ne01 , 384const int ne02 ,const int s01 ,const int s02 ,const int s03 , 385const int s1 ,const int s2 ,const int s3 ,const int n_dims , 386const int n_offs ,const int nr ,const int32_t * pos ,const float freq_scale , 387const float freq_base ,const float ext_factor , 388const float attn_factor ,const rope_corr_dims corr_dims , 389const float * freq_factors ,const mrope_sections sections , 390const bool is_imrope , dpct::queue_ptr stream ) { 391GGML_ASSERT (ne00 %2 == 0 ); 392const dpct::dim3 block_dims (1 ,SYCL_ROPE_BLOCK_SIZE ,1 ); 393const int n_blocks_x = 394 (ne00 + 2 * SYCL_ROPE_BLOCK_SIZE - 1 ) / (2 * SYCL_ROPE_BLOCK_SIZE ); 395const dpct::dim3 block_nums (nr ,n_blocks_x ,1 ); 396 397const float theta_scale = powf (freq_base ,-2.0f /n_dims ); 398 399if (freq_factors == nullptr ) { 400stream -> parallel_for ( 401 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 402 [= ](sycl::nd_item < 3 > item_ct1 ) { 403GGML_UNUSED (item_ct1 ); 404rope_multi < forward , false,T > ( 405x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 406n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 407theta_scale ,freq_factors ,sections ,is_imrope ); 408 }); 409 }else { 410stream -> parallel_for ( 411 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 412 [= ](sycl::nd_item < 3 > item_ct1 ) { 413GGML_UNUSED (item_ct1 ); 414rope_multi < forward , true,T > ( 415x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 416n_offs ,pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 417theta_scale ,freq_factors ,sections ,is_imrope ); 418 }); 419 } 420} 421 422template < bool forward ,typename T > 423static void 424rope_vision_sycl (const T * x ,T * dst ,const int ne00 ,const int ne01 , 425const int ne02 ,const int s01 ,const int s02 ,const int s03 , 426const int s1 ,const int s2 ,const int s3 ,const int n_dims , 427const int nr ,const int32_t * pos ,const float freq_scale , 428const float freq_base ,const float ext_factor , 429const float attn_factor ,const rope_corr_dims corr_dims , 430const float * freq_factors ,const mrope_sections sections , 431 dpct::queue_ptr stream ) { 432GGML_ASSERT (ne00 %2 == 0 ); 433const dpct::dim3 block_dims (1 ,SYCL_ROPE_BLOCK_SIZE ,1 ); 434const int n_blocks_x = 435 (ne00 + 2 * SYCL_ROPE_BLOCK_SIZE - 1 ) / (2 * SYCL_ROPE_BLOCK_SIZE ); 436const dpct::dim3 block_nums (nr ,n_blocks_x ,1 ); 437 438const float theta_scale = powf (freq_base ,-2.0f /n_dims ); 439 440if (freq_factors == nullptr ) { 441stream -> parallel_for ( 442 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 443 [= ](sycl::nd_item < 3 > item_ct1 ) { 444GGML_UNUSED (item_ct1 ); 445rope_vision < forward , false,T > ( 446x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 447pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 448theta_scale ,freq_factors ,sections ); 449 }); 450 }else { 451stream -> parallel_for ( 452 sycl::nd_range < 3 > (block_nums * block_dims ,block_dims ), 453 [= ](sycl::nd_item < 3 > item_ct1 ) { 454GGML_UNUSED (item_ct1 ); 455rope_vision < forward , true,T > ( 456x ,dst ,ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims , 457pos ,freq_scale ,ext_factor ,attn_factor ,corr_dims , 458theta_scale ,freq_factors ,sections ); 459 }); 460 } 461} 462 463template < bool forward > 464void ggml_sycl_op_rope_impl (ggml_backend_sycl_context & ctx ,ggml_tensor * dst , 465const ggml_tensor * set_rows = nullptr ) { 466const ggml_tensor * src0 = dst -> src [0 ]; 467const ggml_tensor * src1 = dst -> src [1 ]; 468const ggml_tensor * src2 = dst -> src [2 ]; 469 470const float * src0_d = (const float * )src0 -> data ; 471const float * src1_d = (const float * )src1 -> data ; 472 473void * dst_d = dst -> data ; 474const int64_t * row_indices = nullptr ; 475ggml_type dst_type = dst -> type ; 476int set_rows_stride = 0 ; 477 478if (set_rows != nullptr ) { 479GGML_ASSERT (forward ); 480dst_d = set_rows -> data ; 481row_indices = (const int64_t * )set_rows -> src [1 ]-> data ; 482dst_type = set_rows -> type ; 483set_rows_stride = set_rows -> nb [1 ] /ggml_type_size (set_rows -> type ); 484 } 485 dpct::queue_ptr stream = ctx .stream (); 486 487GGML_ASSERT (src0 -> type == GGML_TYPE_F32 || src0 -> type == GGML_TYPE_F16 ); 488GGML_ASSERT (dst -> type == GGML_TYPE_F32 || dst -> type == GGML_TYPE_F16 ); 489GGML_ASSERT (src0 -> type == dst -> type || 490 (src0 -> type == GGML_TYPE_F32 && dst -> type == GGML_TYPE_F16 )); 491 492const int64_t ne00 = src0 -> ne [0 ];// head dims 493const int64_t ne01 = src0 -> ne [1 ];// num heads 494const int64_t ne02 = src0 -> ne [2 ];// num heads 495const int64_t nr = ggml_nrows (src0 ); 496 497const size_t s01 = src0 -> nb [1 ] /ggml_type_size (src0 -> type ); 498const size_t s02 = src0 -> nb [2 ] /ggml_type_size (src0 -> type ); 499const size_t s03 = src0 -> nb [3 ] /ggml_type_size (src0 -> type ); 500 501const size_t s1 = dst -> nb [1 ] /ggml_type_size (dst -> type ); 502const size_t s2 = dst -> nb [2 ] /ggml_type_size (dst -> type ); 503const size_t s3 = dst -> nb [3 ] /ggml_type_size (dst -> type ); 504 505const int n_dims = ((int32_t * )dst -> op_params )[1 ]; 506const int mode = ((int32_t * )dst -> op_params )[2 ]; 507const int n_ctx_orig = ((int32_t * )dst -> op_params )[4 ]; 508const int n_offs = ((int32_t * )dst -> op_params )[15 ]; 509mrope_sections sections ; 510 511float freq_base ; 512float freq_scale ; 513float ext_factor ; 514float attn_factor ; 515float beta_fast ; 516float beta_slow ; 517 518memcpy (& freq_base , (int32_t * )dst -> op_params + 5 ,sizeof (float )); 519memcpy (& freq_scale , (int32_t * )dst -> op_params + 6 ,sizeof (float )); 520memcpy (& ext_factor , (int32_t * )dst -> op_params + 7 ,sizeof (float )); 521memcpy (& attn_factor , (int32_t * )dst -> op_params + 8 ,sizeof (float )); 522memcpy (& beta_fast , (int32_t * )dst -> op_params + 9 ,sizeof (float )); 523memcpy (& beta_slow , (int32_t * )dst -> op_params + 10 ,sizeof (float )); 524memcpy (& sections .v , (int32_t * )dst -> op_params + 11 ,sizeof (int )* 4 ); 525 526const bool is_neox = mode & GGML_ROPE_TYPE_NEOX ; 527const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE ; 528const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE ; 529const bool is_vision = mode == GGML_ROPE_TYPE_VISION ; 530 531if (is_mrope ) { 532GGML_ASSERT (sections .v [0 ]> 0 || sections .v [1 ]> 0 || 533sections .v [2 ]> 0 ); 534 } 535 536if (is_vision ) { 537GGML_ASSERT (n_dims == ne00 /2 ); 538GGML_ASSERT (n_offs == 0 );// offset not supported for vision, as the rotated pairs span the whole row 539 } 540 541const int32_t * pos = (const int32_t * )src1_d ; 542 543const float * freq_factors = nullptr ; 544if (src2 != nullptr ) { 545freq_factors = (const float * )src2 -> data ; 546 } 547 548rope_corr_dims corr_dims ; 549ggml_rope_yarn_corr_dims (n_dims ,n_ctx_orig ,freq_base ,beta_fast , 550beta_slow ,corr_dims .v ); 551 552// compute 553if (is_neox ) { 554GGML_SYCL_DEBUG ("%s: neox path\n" ,__func__ ); 555if (src0 -> type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F32 ) { 556rope_neox_sycl < forward ,float ,float > ( 557 (const float * )src0_d , (float * )dst_d ,ne00 ,ne01 ,ne02 ,s01 , 558s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale ,freq_base , 559ext_factor ,attn_factor ,corr_dims ,freq_factors ,row_indices , 560set_rows_stride ,stream ); 561 }else if (src0 -> type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F16 ) { 562rope_neox_sycl < forward ,float , sycl::half > ( 563 (const float * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 ,ne02 , 564s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale , 565freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 566row_indices ,set_rows_stride ,stream ); 567 }else if (src0 -> type == GGML_TYPE_F16 && dst_type == GGML_TYPE_F16 ) { 568rope_neox_sycl < forward , sycl::half , sycl::half > ( 569 (const sycl::half * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 , 570ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale , 571freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 572row_indices ,set_rows_stride ,stream ); 573 }else { 574GGML_ABORT ("Fatal error: Tensor type unsupported!" ); 575 } 576 }else if (is_mrope && !is_vision ) { 577GGML_SYCL_DEBUG ("%s: mrope path\n" ,__func__ ); 578if (src0 -> type == GGML_TYPE_F32 ) { 579rope_multi_sycl < forward > ((const float * )src0_d , (float * )dst_d , 580ne00 ,ne01 ,ne02 ,s01 ,s02 ,s03 ,s1 ,s2 , 581s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale ,freq_base , 582ext_factor ,attn_factor ,corr_dims , 583freq_factors ,sections ,is_imrope ,stream ); 584 }else if (src0 -> type == GGML_TYPE_F16 ) { 585rope_multi_sycl < forward > ( 586 (const sycl::half * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 , 587ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale , 588freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 589sections ,is_imrope ,stream ); 590 }else { 591GGML_ABORT ("Fatal error: Tensor type unsupported!" ); 592 } 593 }else if (is_vision ) { 594GGML_SYCL_DEBUG ("%s: vision path\n" ,__func__ ); 595if (src0 -> type == GGML_TYPE_F32 ) { 596rope_vision_sycl < forward > ( 597 (const float * )src0_d , (float * )dst_d ,ne00 ,ne01 ,ne02 ,s01 , 598s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,nr ,pos ,freq_scale ,freq_base , 599ext_factor ,attn_factor ,corr_dims ,freq_factors ,sections , 600stream ); 601 }else if (src0 -> type == GGML_TYPE_F16 ) { 602rope_vision_sycl < forward > ( 603 (const sycl::half * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 , 604ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,nr ,pos ,freq_scale , 605freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 606sections ,stream ); 607 }else { 608GGML_ABORT ("Fatal error: Tensor type unsupported!" ); 609 } 610 }else { 611GGML_SYCL_DEBUG ("%s: norm path\n" ,__func__ ); 612if (src0 -> type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F32 ) { 613rope_norm_sycl < forward ,float ,float > ( 614 (const float * )src0_d , (float * )dst_d ,ne00 ,ne01 ,ne02 ,s01 , 615s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale ,freq_base , 616ext_factor ,attn_factor ,corr_dims ,freq_factors ,row_indices , 617set_rows_stride ,stream ); 618 }else if (src0 -> type == GGML_TYPE_F32 && dst_type == GGML_TYPE_F16 ) { 619rope_norm_sycl < forward ,float , sycl::half > ( 620 (const float * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 ,ne02 , 621s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale , 622freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 623row_indices ,set_rows_stride ,stream ); 624 }else if (src0 -> type == GGML_TYPE_F16 && dst_type == GGML_TYPE_F16 ) { 625rope_norm_sycl < forward , sycl::half , sycl::half > ( 626 (const sycl::half * )src0_d , (sycl::half * )dst_d ,ne00 ,ne01 , 627ne02 ,s01 ,s02 ,s03 ,s1 ,s2 ,s3 ,n_dims ,n_offs ,nr ,pos ,freq_scale , 628freq_base ,ext_factor ,attn_factor ,corr_dims ,freq_factors , 629row_indices ,set_rows_stride ,stream ); 630 }else { 631GGML_ABORT ("Fatal error: Tensor type unsupported!" ); 632 } 633 } 634} 635 636void ggml_sycl_rope (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 637scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 3 ); 638 639ggml_sycl_op_rope_impl < true> (ctx ,dst ); 640} 641 642void ggml_sycl_rope_back (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ) { 643scope_op_debug_print scope_dbg_print (__func__ ,dst ,/*num_src=*/ 3 ); 644ggml_sycl_op_rope_impl < false> (ctx ,dst ); 645} 646 647void ggml_sycl_rope_fused (ggml_backend_sycl_context & ctx ,ggml_tensor * rope , 648ggml_tensor * set_rows ) { 649scope_op_debug_print scope_dbg_print (__func__ ,rope ,/*num_src=*/ 3 ); 650ggml_sycl_op_rope_impl < true> (ctx ,rope ,set_rows ); 651}