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