char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d5d3e05bf
char/aliceai-t5-moe
1#ifndef GGML_SYCL_CPY_HPP 2#define GGML_SYCL_CPY_HPP 3 4#include "common.hpp" 5#include <float.h> 6 7typedef void (* cpy_kernel_t )(const char * cx ,char * cdst ); 8 9__dpct_inline__ int best_index_int8 (int n ,const int8_t * val ,float x ) { 10if (x <=val [0 ]) { 11return 0 ; 12 } 13if (x >=val [n - 1 ]) { 14return n - 1 ; 15 } 16int ml = 0 ,mu = n - 1 ; 17while (mu - ml > 1 ) { 18int mav = (ml + mu ) /2 ; 19if (x < val [mav ]) { 20mu = mav ; 21 }else { 22ml = mav ; 23 } 24 } 25return x - val [mu - 1 ]< val [mu ]- x ?mu - 1 :mu ; 26} 27 28inline void cpy_blck_f32_q8_0 (const char * cxi ,char * cdsti ) { 29const float * xi = (const float * )cxi ; 30block_q8_0 * dsti = (block_q8_0 * )cdsti ; 31 32float amax = 0.0f ;// absolute max 33 34for (int j = 0 ;j < QK8_0 ;j ++ ) { 35const float v = xi [j ]; 36amax = sycl::fmax (amax , sycl::fabs ((float )v )); 37 } 38 39const float d = amax / ((1 <<7 )- 1 ); 40const float id = d ?1.0f /d :0.0f ; 41 42dsti -> d = d ; 43 44for (int j = 0 ;j < QK8_0 ;++ j ) { 45const float x0 = xi [j ]* id ; 46 47dsti -> qs [j ]= sycl::round ((float )x0 ); 48 } 49} 50 51inline void cpy_blck_f32_q1_0 (const char * cxi ,char * cdsti ) { 52const float * xi = (const float * )cxi ; 53block_q1_0 * dsti = (block_q1_0 * )cdsti ; 54 55float sum_abs = 0.0f ; 56for (int j = 0 ;j < QK1_0 ;++ j ) { 57sum_abs += sycl::fabs ((float )xi [j ]); 58 } 59 60dsti -> d = sum_abs /QK1_0 ; 61 62for (int j = 0 ;j < QK1_0 /8 ;++ j ) { 63dsti -> qs [j ]= 0 ; 64 } 65 66for (int j = 0 ;j < QK1_0 ;++ j ) { 67if (xi [j ] >=0.0f ) { 68dsti -> qs [j /8 ] |= (1u << (j %8 )); 69 } 70 } 71} 72 73inline int round_nearest_int (float x ) { 74return (int )(x >=0.0f ?x + 0.5f :x - 0.5f ); 75} 76 77inline void cpy_blck_f32_q2_0 (const char * cxi ,char * cdsti ) { 78const float * xi = (const float * )cxi ; 79block_q2_0 * dsti = (block_q2_0 * )cdsti ; 80 81float amax = 0.0f ; 82 83for (int j = 0 ;j < QK2_0 ;++ j ) { 84amax = sycl::fmax (amax , sycl::fabs ((float )xi [j ])); 85 } 86 87const float d = amax ; 88const float id = d > 0.0f ?1.0f /d :0.0f ; 89 90dsti -> d = d ; 91 92for (int j = 0 ;j < QK2_0 /4 ;++ j ) { 93dsti -> qs [j ]= 0 ; 94 } 95 96for (int j = 0 ;j < QK2_0 ;++ j ) { 97int q = round_nearest_int (xi [j ]* id )+ 1 ; 98q = dpct::max (0 , dpct::min (3 ,q )); 99 100const int byte_index = j /4 ; 101const int bit_offset = (j %4 )* 2 ; 102dsti -> qs [byte_index ] |= (uint8_t )q <<bit_offset ; 103 } 104} 105 106inline int best_index_mxfp4 (const float x ,const float e ) { 107int best_index = 0 ; 108float best_err = sycl::fabs ((float ) (kvalues_mxfp4 [0 ]* e - x )); 109for (int i = 1 ;i < 16 ;++ i ) { 110const float err = sycl::fabs ((float ) (kvalues_mxfp4 [i ]* e - x )); 111if (err < best_err ) { 112best_index = i ; 113best_err = err ; 114 } 115 } 116return best_index ; 117} 118 119inline int nearest_int_sycl (float x ) { 120const float val = x + 12582912.0f ; 121int i ; 122memcpy (& i ,& val ,sizeof (int )); 123return (i & 0x007fffff )- 0x00400000 ; 124} 125 126inline int nearest_int_ggml_sycl (float x ) { 127return (int ) sycl::round ((float )x ); 128} 129 130inline uint8_t clamp_u8 (const int x ,const int lo ,const int hi ) { 131return (uint8_t ) dpct::max (lo , dpct::min (hi ,x )); 132} 133 134inline int8_t clamp_i8 (const int x ,const int lo ,const int hi ) { 135return (int8_t ) dpct::max (lo , dpct::min (hi ,x )); 136} 137 138constexpr float GROUP_MAX_EPS_SYCL = 1e-15f ; 139 140inline float make_qx_quants_sycl (int n ,int nmax ,const float * x ,int8_t * L ,int rmse_type ,const float * qw ) { 141float max = 0.0f ; 142float amax = 0.0f ; 143for (int i = 0 ;i < n ;++ i ) { 144const float ax = sycl::fabs (x [i ]); 145if (ax > amax ) { 146amax = ax ; 147max = x [i ]; 148 } 149 } 150if (amax < GROUP_MAX_EPS_SYCL ) { 151for (int i = 0 ;i < n ;++ i ) { 152L [i ]= 0 ; 153 } 154return 0.0f ; 155 } 156 157float iscale = - nmax /max ; 158if (rmse_type == 0 ) { 159for (int i = 0 ;i < n ;++ i ) { 160int l = nearest_int_ggml_sycl (iscale * x [i ]); 161L [i ]= (int8_t ) (nmax + dpct::max (- nmax , dpct::min (nmax - 1 ,l ))); 162 } 163return 1.0f /iscale ; 164 } 165 166bool return_early = false; 167if (rmse_type < 0 ) { 168rmse_type = - rmse_type ; 169return_early = true; 170 } 171 172float sumlx = 0.0f ; 173float suml2 = 0.0f ; 174for (int i = 0 ;i < n ;++ i ) { 175int l = nearest_int_ggml_sycl (iscale * x [i ]); 176l = dpct::max (- nmax , dpct::min (nmax - 1 ,l )); 177L [i ]= (int8_t ) (l + nmax ); 178 179const float w = qw ?qw [i ] : (rmse_type == 1 ?x [i ]* x [i ] : 180rmse_type == 2 ?1.0f :rmse_type == 3 ? sycl::fabs (x [i ]) : sycl::sqrt (sycl::fabs (x [i ]))); 181 182sumlx += w * x [i ]* l ; 183suml2 += w * l * l ; 184 } 185 186float scale = suml2 ?sumlx /suml2 :0.0f ; 187if (return_early ) { 188return suml2 > 0.0f ?0.5f * (scale + 1.0f /iscale ) :1.0f /iscale ; 189 } 190 191float best = scale * sumlx ; 192for (int is = -9 ;is <=9 ;++ is ) { 193if (is == 0 ) { 194continue ; 195 } 196iscale = - (nmax + 0.1f * is ) /max ; 197sumlx = 0.0f ; 198suml2 = 0.0f ; 199for (int i = 0 ;i < n ;++ i ) { 200int l = nearest_int_ggml_sycl (iscale * x [i ]); 201l = dpct::max (- nmax , dpct::min (nmax - 1 ,l )); 202const float w = qw ?qw [i ] : (rmse_type == 1 ?x [i ]* x [i ] : 203rmse_type == 2 ?1.0f :rmse_type == 3 ? sycl::fabs (x [i ]) : sycl::sqrt (sycl::fabs (x [i ]))); 204sumlx += w * x [i ]* l ; 205suml2 += w * l * l ; 206 } 207 208if (suml2 > 0.0f && sumlx * sumlx > best * suml2 ) { 209for (int i = 0 ;i < n ;++ i ) { 210int l = nearest_int_ggml_sycl (iscale * x [i ]); 211L [i ]= (int8_t ) (nmax + dpct::max (- nmax , dpct::min (nmax - 1 ,l ))); 212 } 213scale = sumlx /suml2 ; 214best = scale * sumlx ; 215 } 216 } 217 218return scale ; 219} 220 221inline float make_q3_quants_sycl (int n ,int nmax ,const float * x ,int8_t * L ,bool do_rmse ) { 222float max = 0.0f ; 223float amax = 0.0f ; 224for (int i = 0 ;i < n ;++ i ) { 225const float ax = sycl::fabs (x [i ]); 226if (ax > amax ) { 227amax = ax ; 228max = x [i ]; 229 } 230 } 231 232if (amax < GROUP_MAX_EPS_SYCL ) { 233for (int i = 0 ;i < n ;++ i ) { 234L [i ]= 0 ; 235 } 236return 0.0f ; 237 } 238 239const float iscale = - nmax /max ; 240if (do_rmse ) { 241float sumlx = 0.0f ; 242float suml2 = 0.0f ; 243for (int i = 0 ;i < n ;++ i ) { 244int l = nearest_int_ggml_sycl (iscale * x [i ]); 245l = dpct::max (- nmax , dpct::min (nmax - 1 ,l )); 246L [i ]= (int8_t )l ; 247const float w = x [i ]* x [i ]; 248sumlx += w * x [i ]* l ; 249suml2 += w * l * l ; 250 } 251 252for (int itry = 0 ;itry < 5 ;++ itry ) { 253int n_changed = 0 ; 254for (int i = 0 ;i < n ;++ i ) { 255const float w = x [i ]* x [i ]; 256float slx = sumlx - w * x [i ]* L [i ]; 257if (slx > 0.0f ) { 258float sl2 = suml2 - w * L [i ]* L [i ]; 259int new_l = nearest_int_ggml_sycl (x [i ]* sl2 /slx ); 260new_l = dpct::max (- nmax , dpct::min (nmax - 1 ,new_l )); 261if (new_l != L [i ]) { 262slx += w * x [i ]* new_l ; 263sl2 += w * new_l * new_l ; 264if (sl2 > 0.0f && slx * slx * suml2 > sumlx * sumlx * sl2 ) { 265L [i ]= (int8_t )new_l ; 266sumlx = slx ; 267suml2 = sl2 ; 268++ n_changed ; 269 } 270 } 271 } 272 } 273if (!n_changed ) { 274break ; 275 } 276 } 277 278for (int i = 0 ;i < n ;++ i ) { 279L [i ]+= nmax ; 280 } 281return suml2 > 0.0f ?sumlx /suml2 :0.0f ; 282 } 283 284for (int i = 0 ;i < n ;++ i ) { 285int l = nearest_int_ggml_sycl (iscale * x [i ]); 286l = dpct::max (- nmax , dpct::min (nmax - 1 ,l )); 287L [i ]= (int8_t ) (l + nmax ); 288 } 289 290return 1.0f /iscale ; 291} 292 293inline void set_scale_min_k4 (int j ,uint8_t * q ,uint8_t d ,uint8_t m ) { 294if (j < 4 ) { 295q [j ]= (q [j ]& 0xC0 ) | (d & 0x3F ); 296q [j + 4 ]= (q [j + 4 ]& 0xC0 ) | (m & 0x3F ); 297 }else { 298q [j + 4 ]= (d & 0x0F ) | ((m & 0x0F ) <<4 ); 299q [j - 4 ]= (q [j - 4 ]& 0x3F ) | ((d >>4 ) <<6 ); 300q [j - 0 ]= (q [j - 0 ]& 0x3F ) | ((m >>4 ) <<6 ); 301 } 302} 303 304inline void get_scale_min_k4_local (int j ,const uint8_t * q ,uint8_t & d ,uint8_t & m ) { 305if (j < 4 ) { 306d = q [j ]& 63 ; 307m = q [j + 4 ]& 63 ; 308 }else { 309d = (q [j + 4 ]& 0xF ) | ((q [j - 4 ] >>6 ) <<4 ); 310m = (q [j + 4 ] >>4 ) | ((q [j - 0 ] >>6 ) <<4 ); 311 } 312} 313 314inline void cpy_blck_f32_mxfp4 (const char * cxi ,char * cdsti ) { 315const float * xi = (const float * )cxi ; 316block_mxfp4 * dsti = (block_mxfp4 * )cdsti ; 317 318float amax = 0.0f ; 319for (int j = 0 ;j < QK_MXFP4 ;++ j ) { 320amax = sycl::fmax (amax , sycl::fabs ((float )xi [j ])); 321 } 322 323const uint8_t e = amax > 0.0f ? (uint8_t ) (sycl::floor (sycl::log2 (amax ))- 2 + 127 ) :0 ; 324const float d = GGML_E8M0_TO_FP32_HALF (e ); 325 326dsti -> e = e ; 327 328for (int j = 0 ;j < QK_MXFP4 /2 ;++ j ) { 329const uint8_t x0 = best_index_mxfp4 (xi [0 + j ],d ); 330const uint8_t x1 = best_index_mxfp4 (xi [QK_MXFP4 /2 + j ],d ); 331 332dsti -> qs [j ]= x0 ; 333dsti -> qs [j ] |=x1 <<4 ; 334 } 335} 336 337inline void cpy_blck_f32_nvfp4 (const char * cxi ,char * cdsti ) { 338const float * xi = (const float * )cxi ; 339block_nvfp4 * dsti = (block_nvfp4 * )cdsti ; 340 341constexpr int n_sub = QK_NVFP4 /QK_NVFP4_SUB ; 342 343for (int s = 0 ;s < n_sub ;++ s ) { 344const float * xb = xi + s * QK_NVFP4_SUB ; 345 346float amax = 0.0f ; 347for (int j = 0 ;j < QK_NVFP4_SUB ;++ j ) { 348amax = sycl::fmax (amax , sycl::fabs ((float )xb [j ])); 349 } 350 351const uint8_t ue = ggml_fp32_to_ue4m3 (amax /6.0f ); 352dsti -> d [s ]= ue ; 353const float d = ggml_sycl_ue4m3_to_fp32 (ue ); 354 355for (int j = 0 ;j < QK_NVFP4_SUB /2 ;++ j ) { 356const uint8_t x0 = best_index_mxfp4 (xb [0 + j ],d ); 357const uint8_t x1 = best_index_mxfp4 (xb [QK_NVFP4_SUB /2 + j ],d ); 358 359dsti -> qs [s * (QK_NVFP4_SUB /2 )+ j ]= x0 | (x1 <<4 ); 360 } 361 } 362} 363 364 365inline void cpy_blck_f32_q4_0 (const char * cxi ,char * cdsti ) { 366const float * xi = (const float * )cxi ; 367block_q4_0 * dsti = (block_q4_0 * )cdsti ; 368 369float amax = 0.0f ; 370float vmax = 0.0f ; 371 372for (int j = 0 ;j < QK4_0 ;++ j ) { 373const float v = xi [j ]; 374if (amax < sycl::fabs ((float )v )) { 375amax = sycl::fabs ((float )v ); 376vmax = v ; 377 } 378 } 379 380const float d = vmax /-8 ; 381const float id = d ?1.0f /d :0.0f ; 382 383dsti -> d = d ; 384 385for (int j = 0 ;j < QK4_0 /2 ;++ j ) { 386const float x0 = xi [0 + j ]* id ; 387const float x1 = xi [QK4_0 /2 + j ]* id ; 388 389const uint8_t xi0 = dpct::min (15 , (int8_t ) (x0 + 8.5f )); 390const uint8_t xi1 = dpct::min (15 , (int8_t ) (x1 + 8.5f )); 391 392dsti -> qs [j ]= xi0 ; 393dsti -> qs [j ] |=xi1 <<4 ; 394 } 395} 396 397inline void cpy_blck_f32_q4_1 (const char * cxi ,char * cdsti ) { 398const float * xi = (const float * )cxi ; 399block_q4_1 * dsti = (block_q4_1 * )cdsti ; 400 401float vmin = FLT_MAX ; 402float vmax = - FLT_MAX ; 403 404for (int j = 0 ;j < QK4_1 ;++ j ) { 405const float v = xi [j ]; 406 407vmin = sycl::min (v ,vmin ); 408vmax = sycl::max (v ,vmax ); 409 } 410 411const float d = (vmax - vmin ) / ((1 <<4 )- 1 ); 412const float id = d ?1.0f /d :0.0f ; 413 414dsti -> dm .x ()= d ; 415dsti -> dm .y ()= vmin ; 416 417for (int j = 0 ;j < QK4_1 /2 ;++ j ) { 418const float x0 = (xi [0 + j ]- vmin )* id ; 419const float x1 = (xi [QK4_1 /2 + j ]- vmin )* id ; 420 421const uint8_t xi0 = dpct::min (15 , (int8_t ) (x0 + 0.5f )); 422const uint8_t xi1 = dpct::min (15 , (int8_t ) (x1 + 0.5f )); 423 424dsti -> qs [j ]= xi0 ; 425dsti -> qs [j ] |=xi1 <<4 ; 426 } 427} 428 429inline void cpy_blck_f32_q5_0 (const char * cxi ,char * cdsti ) { 430const float * xi = (const float * )cxi ; 431block_q5_0 * dsti = (block_q5_0 * )cdsti ; 432 433float amax = 0.0f ; 434float vmax = 0.0f ; 435 436for (int j = 0 ;j < QK5_0 ;++ j ) { 437const float v = xi [j ]; 438if (amax < sycl::fabs ((float )v )) { 439amax = sycl::fabs ((float )v ); 440vmax = v ; 441 } 442 } 443 444const float d = vmax /-16 ; 445const float id = d ?1.0f /d :0.0f ; 446 447dsti -> d = d ; 448 449uint32_t qh = 0 ; 450for (int j = 0 ;j < QK5_0 /2 ;++ j ) { 451const float x0 = xi [0 + j ]* id ; 452const float x1 = xi [QK5_0 /2 + j ]* id ; 453 454const uint8_t xi0 = dpct::min (31 , (int8_t ) (x0 + 16.5f )); 455const uint8_t xi1 = dpct::min (31 , (int8_t ) (x1 + 16.5f )); 456 457dsti -> qs [j ]= (xi0 & 0xf ) | ((xi1 & 0xf ) <<4 ); 458qh |= ((xi0 & 0x10u ) >>4 ) << (j + 0 ); 459qh |= ((xi1 & 0x10u ) >>4 ) << (j + QK5_0 /2 ); 460 } 461memcpy (dsti -> qh ,& qh ,sizeof (qh )); 462} 463 464inline void cpy_blck_f32_q5_1 (const char * cxi ,char * cdsti ) { 465const float * xi = (const float * )cxi ; 466block_q5_1 * dsti = (block_q5_1 * )cdsti ; 467 468float min = xi [0 ]; 469float max = xi [0 ]; 470 471for (int j = 1 ;j < QK5_1 ;++ j ) { 472const float v = xi [j ]; 473min = v < min ?v :min ; 474max = v > max ?v :max ; 475 } 476 477const float d = (max - min ) /31 ; 478const float id = d ?1.0f /d :0.0f ; 479 480dsti -> dm .x ()= d ; 481dsti -> dm .y ()= min ; 482 483uint32_t qh = 0 ; 484for (int j = 0 ;j < QK5_1 /2 ;++ j ) { 485const float x0 = (xi [0 + j ]- min )* id ; 486const float x1 = (xi [QK5_1 /2 + j ]- min )* id ; 487 488const uint8_t xi0 = (uint8_t ) (x0 + 0.5f ); 489const uint8_t xi1 = (uint8_t ) (x1 + 0.5f ); 490 491dsti -> qs [j ]= (xi0 & 0xf ) | ((xi1 & 0xf ) <<4 ); 492qh |= ((xi0 & 0x10u ) >>4 ) << (j + 0 ); 493qh |= ((xi1 & 0x10u ) >>4 ) << (j + QK5_1 /2 ); 494 } 495memcpy (dsti -> qh ,& qh ,sizeof (qh )); 496} 497 498inline void cpy_blck_f32_iq4_nl (const char * cxi ,char * cdsti ) { 499const float * xi = (const float * )cxi ; 500block_iq4_nl * dsti = (block_iq4_nl * )cdsti ; 501 502float amax = 0.0f ; 503float vmax = 0.0f ; 504 505for (int j = 0 ;j < QK4_NL ;++ j ) { 506const float v = xi [j ]; 507if (amax < sycl::fabs ((float )v )) { 508amax = sycl::fabs ((float )v ); 509vmax = v ; 510 } 511 } 512 513float d = vmax /kvalues_iq4nl [0 ]; 514const float id = d ?1.0f /d :0.0f ; 515 516float sumqx = 0 ,sumq2 = 0 ; 517for (int j = 0 ;j < QK4_NL /2 ;++ j ) { 518const float x0 = xi [0 + j ]* id ; 519const float x1 = xi [QK4_NL /2 + j ]* id ; 520const uint8_t xi0 = best_index_int8 (16 ,kvalues_iq4nl ,x0 ); 521const uint8_t xi1 = best_index_int8 (16 ,kvalues_iq4nl ,x1 ); 522dsti -> qs [j ]= xi0 | (xi1 <<4 ); 523const float v0 = kvalues_iq4nl [xi0 ]; 524const float v1 = kvalues_iq4nl [xi1 ]; 525const float w0 = xi [0 + j ]* xi [0 + j ]; 526const float w1 = xi [QK4_NL /2 + j ]* xi [QK4_NL /2 + j ]; 527sumqx += w0 * v0 * xi [j ]+ w1 * v1 * xi [QK4_NL /2 + j ]; 528sumq2 += w0 * v0 * v0 + w1 * v1 * v1 ; 529 } 530 531dsti -> d = sumq2 > 0 ?sumqx /sumq2 :d ; 532} 533 534void ggml_sycl_cpy (ggml_backend_sycl_context & ctx ,const ggml_tensor * src0 ,const ggml_tensor * src1 ); 535void ggml_sycl_dup (ggml_backend_sycl_context & ctx ,ggml_tensor * dst ); 536 537#endif // GGML_SYCL_CPY_HPP