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/convert.cpp # 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 / convert.cpp` 823 lines ยท 34934 bytes Raw Blame History 1 #include "convert.hpp" 2 #include "dequantize.hpp" 3 #include "presets.hpp" 4 5 template 6 static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k, 7 const sycl::nd_item<3> &item_ct1) { 8 const int64_t i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) + 9 item_ct1.get_local_id(2)); 10 11 if (i >= k) { 12 return; 13 } 14 15 const int64_t ib = i/qk; // block index 16 const int64_t iqs = (i%qk)/qr; // quant index 17 const int64_t iybs = i - i%qk; // y block start index 18 const int64_t y_offset = qr == 1 ? 1 : qk/2; 19 20 // dequantize 21 dfloat2 v; 22 dequantize_kernel(vx, ib, iqs, v); 23 24 y[iybs + iqs + 0] = v.x(); 25 y[iybs + iqs + y_offset] = v.y(); 26 } 27 28 template 29 static void dequantize_block_sycl(const void *__restrict__ vx, 30 dst_t *__restrict__ y, const int64_t k, 31 dpct::queue_ptr stream) { 32 const int64_t num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE); 33 { 34 dpct::has_capability_or_fail(stream->get_device(), 35 {sycl::aspect::fp16}); 36 stream->parallel_for( 37 sycl::nd_range<3>( 38 sycl::range<3>(1, 1, num_blocks) * 39 sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE), 40 sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)), 41 [=](sycl::nd_item<3> item_ct1) { 42 dequantize_block(vx, y, k, item_ct1); 43 }); 44 } 45 } 46 47 template 48 static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int64_t k, 49 dpct::queue_ptr stream) { 50 const int64_t nb = k / QK_K; 51 #if QK_K == 256 52 { 53 dpct::has_capability_or_fail(stream->get_device(), 54 {sycl::aspect::fp16}); 55 56 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 57 sycl::range<3>(1, 1, 64), 58 sycl::range<3>(1, 1, 64)), 59 [=](sycl::nd_item<3> item_ct1) { 60 dequantize_block_q2_K(vx, y, item_ct1); 61 }); 62 } 63 #else 64 { 65 dpct::has_capability_or_fail(stream->get_device(), 66 {sycl::aspect::fp16}); 67 68 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 69 sycl::range<3>(1, 1, 32), 70 sycl::range<3>(1, 1, 32)), 71 [=](sycl::nd_item<3> item_ct1) { 72 dequantize_block_q2_K(vx, y, item_ct1); 73 }); 74 } 75 76 #endif 77 } 78 79 template 80 static void dequantize_row_q2_K_sycl_reorder(const void *vx, dst_t *y, const int64_t k, 81 dpct::queue_ptr stream) { 82 const int64_t nb = k / QK_K; 83 84 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 85 stream->parallel_for( 86 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 64), sycl::range<3>(1, 1, 64)), 87 [=](sycl::nd_item<3> item_ct1) { 88 dequantize_block_q2_K_reorder(vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), item_ct1, nb); 89 }); 90 } 91 92 template 93 static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int64_t k, 94 dpct::queue_ptr stream) { 95 const int64_t nb = k / QK_K; 96 #if QK_K == 256 97 { 98 dpct::has_capability_or_fail(stream->get_device(), 99 {sycl::aspect::fp16}); 100 101 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 102 sycl::range<3>(1, 1, 64), 103 sycl::range<3>(1, 1, 64)), 104 [=](sycl::nd_item<3> item_ct1) { 105 dequantize_block_q3_K(vx, y, item_ct1); 106 }); 107 } 108 #else 109 { 110 dpct::has_capability_or_fail(stream->get_device(), 111 {sycl::aspect::fp16}); 112 113 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 114 sycl::range<3>(1, 1, 32), 115 sycl::range<3>(1, 1, 32)), 116 [=](sycl::nd_item<3> item_ct1) { 117 dequantize_block_q3_K(vx, y, item_ct1); 118 }); 119 } 120 #endif 121 } 122 123 template 124 static void dequantize_row_q3_K_sycl_reorder(const void *vx, dst_t *y, const int64_t k, 125 dpct::queue_ptr stream) { 126 const int64_t nb = k / QK_K; 127 128 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 129 stream->parallel_for( 130 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 64), sycl::range<3>(1, 1, 64)), 131 [=](sycl::nd_item<3> item_ct1) { 132 dequantize_block_q3_K_reorder(vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), item_ct1, nb); 133 }); 134 } 135 136 template 137 static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int64_t k, 138 dpct::queue_ptr stream) { 139 const int64_t nb32 = k / 32; 140 const int64_t nb = (k + 255) / 256; 141 { 142 dpct::has_capability_or_fail(stream->get_device(), 143 {sycl::aspect::fp16}); 144 145 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 146 sycl::range<3>(1, 1, 32), 147 sycl::range<3>(1, 1, 32)), 148 [=](sycl::nd_item<3> item_ct1) { 149 dequantize_block_q4_0(vx, y, nb32, item_ct1); 150 }); 151 } 152 } 153 154 template 155 static void dequantize_row_q4_0_sycl_reorder(const void *vx, dst_t *y, const int64_t k, 156 dpct::queue_ptr stream) { 157 158 dpct::has_capability_or_fail(stream->get_device(), 159 {sycl::aspect::fp16}); 160 161 // flat range<1>, four qs bytes (8 outputs) per item: two vectorized stores. 162 // k % QK4_0 == 0 gives whole 32-element blocks, so the k/8 launch covers every row exactly. 163 GGML_ASSERT(k % QK4_0 == 0); 164 stream->parallel_for(sycl::range<1>((size_t) (k / 8)), 165 [=](sycl::id<1> idx) { 166 dequantize_block_q4_0_reorder_wide(vx, y, k, idx); 167 }); 168 } 169 170 template 171 static void dequantize_row_q8_0_sycl_reorder(const void *vx, dst_t *y, const int64_t k, 172 dpct::queue_ptr stream) { 173 174 dpct::has_capability_or_fail(stream->get_device(), 175 {sycl::aspect::fp16}); 176 177 // flat range<1>, two pairs (4 elements) per item: no nd_range/group math, one vectorized store. 178 // k % QK8_0 == 0 implies k % 4 == 0, so the wide launch covers the row exactly (no tail). 179 GGML_ASSERT(k % QK8_0 == 0); 180 stream->parallel_for(sycl::range<1>((size_t) (k / 4)), 181 [=](sycl::id<1> idx) { 182 dequantize_block_q8_0_reorder_wide(vx, y, k, idx); 183 }); 184 } 185 186 template 187 static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int64_t k, 188 dpct::queue_ptr stream) { 189 const int64_t nb32 = k / 32; 190 const int64_t nb = (k + 255) / 256; 191 { 192 dpct::has_capability_or_fail(stream->get_device(), 193 {sycl::aspect::fp16}); 194 195 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 196 sycl::range<3>(1, 1, 32), 197 sycl::range<3>(1, 1, 32)), 198 [=](sycl::nd_item<3> item_ct1) { 199 dequantize_block_q4_1(vx, y, nb32, item_ct1); 200 }); 201 } 202 } 203 204 205 template 206 static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int64_t k, 207 dpct::queue_ptr stream) { 208 const int64_t nb = k / QK_K; 209 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 210 stream->parallel_for(sycl::nd_range<1>(sycl::range<1>((size_t) nb * SYCL_DEQK_WI_PER_BLOCK), 211 sycl::range<1>((size_t) SYCL_DEQK_WI_PER_BLOCK)), 212 [=](sycl::nd_item<1> it) { dequantize_block_q4_K_wide(vx, y, nb, it); }); 213 } 214 215 template 216 static void dequantize_row_q4_K_sycl_reorder(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 217 const int64_t nb = k / QK_K; 218 const size_t local_size = 32; 219 const size_t global_size = nb * local_size; 220 221 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 222 223 stream->submit([&](sycl::handler & cgh) { 224 sycl::local_accessor scale_local_acc(sycl::range<1>(12), cgh); 225 226 cgh.parallel_for(sycl::nd_range<1>(sycl::range<1>(global_size), sycl::range<1>(local_size)), 227 [=](sycl::nd_item<1> item_ct1) { 228 dequantize_block_q4_K_reorder(vx, y + item_ct1.get_group(0) * QK_K, item_ct1.get_group(0), get_pointer(scale_local_acc), item_ct1, nb); 229 }); 230 }); 231 } 232 233 template 234 static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int64_t k, 235 dpct::queue_ptr stream) { 236 const int64_t nb = k / QK_K; 237 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 238 stream->parallel_for(sycl::nd_range<1>(sycl::range<1>((size_t) nb * SYCL_DEQK_WI_PER_BLOCK), 239 sycl::range<1>((size_t) SYCL_DEQK_WI_PER_BLOCK)), 240 [=](sycl::nd_item<1> it) { dequantize_block_q5_K_wide(vx, y, nb, it); }); 241 } 242 243 template 244 static void dequantize_row_q5_K_sycl_reorder(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 245 const int64_t nb = k / QK_K; 246 247 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 248 249 stream->submit([&](sycl::handler & cgh) { 250 sycl::local_accessor scale_local_acc(sycl::range<1>(K_SCALE_SIZE), cgh); 251 252 cgh.parallel_for( 253 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 32), sycl::range<3>(1, 1, 32)), 254 [=](sycl::nd_item<3> item_ct1) { 255 dequantize_block_q5_K_reorder(vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), get_pointer(scale_local_acc), item_ct1, nb); 256 }); 257 }); 258 } 259 260 template 261 static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int64_t k, 262 dpct::queue_ptr stream) { 263 const int64_t nb = k / QK_K; 264 #if QK_K == 256 265 { 266 dpct::has_capability_or_fail(stream->get_device(), 267 {sycl::aspect::fp16}); 268 269 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 270 sycl::range<3>(1, 1, 64), 271 sycl::range<3>(1, 1, 64)), 272 [=](sycl::nd_item<3> item_ct1) { 273 dequantize_block_q6_K(vx, y, item_ct1); 274 }); 275 } 276 #else 277 { 278 dpct::has_capability_or_fail(stream->get_device(), 279 {sycl::aspect::fp16}); 280 281 stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 282 sycl::range<3>(1, 1, 32), 283 sycl::range<3>(1, 1, 32)), 284 [=](sycl::nd_item<3> item_ct1) { 285 dequantize_block_q6_K(vx, y, item_ct1); 286 }); 287 } 288 289 #endif 290 } 291 292 template 293 static void dequantize_row_q6_K_sycl_reorder(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 294 const int64_t nb = k / QK_K; 295 296 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 297 298 stream->parallel_for( 299 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 64), sycl::range<3>(1, 1, 64)), 300 [=](sycl::nd_item<3> item_ct1) { dequantize_block_q6_K_reorder(vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), item_ct1, nb); }); 301 } 302 303 template 304 static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int64_t k, 305 dpct::queue_ptr stream) { 306 const int64_t nb = k / QK_K; 307 { 308 dpct::has_capability_or_fail(stream->get_device(), 309 {sycl::aspect::fp16}); 310 311 stream->submit([&](sycl::handler &cgh) { 312 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 313 sycl::range<3>(1, 1, 32), 314 sycl::range<3>(1, 1, 32)), 315 [=](sycl::nd_item<3> item_ct1) { 316 dequantize_block_iq1_s( 317 vx, y, item_ct1, iq1s_grid_gpu 318 ); 319 }); 320 }); 321 } 322 } 323 324 template 325 static void dequantize_row_iq1_m_sycl(const void *vx, dst_t *y, const int64_t k, 326 dpct::queue_ptr stream) { 327 const int64_t nb = k / QK_K; 328 { 329 dpct::has_capability_or_fail(stream->get_device(), 330 {sycl::aspect::fp16}); 331 332 stream->submit([&](sycl::handler &cgh) { 333 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 334 sycl::range<3>(1, 1, 32), 335 sycl::range<3>(1, 1, 32)), 336 [=](sycl::nd_item<3> item_ct1) { 337 dequantize_block_iq1_m( 338 vx, y, item_ct1, iq1s_grid_gpu 339 ); 340 }); 341 }); 342 } 343 } 344 345 template 346 static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int64_t k, 347 dpct::queue_ptr stream) { 348 const int64_t nb = k / QK_K; 349 { 350 dpct::has_capability_or_fail(stream->get_device(), 351 {sycl::aspect::fp16}); 352 353 stream->submit([&](sycl::handler &cgh) { 354 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 355 sycl::range<3>(1, 1, 32), 356 sycl::range<3>(1, 1, 32)), 357 [=](sycl::nd_item<3> item_ct1) { 358 dequantize_block_iq2_xxs( 359 vx, y, item_ct1, iq2xxs_grid, 360 ksigns_iq2xs, kmask_iq2xs); 361 }); 362 }); 363 } 364 } 365 366 template 367 static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int64_t k, 368 dpct::queue_ptr stream) { 369 const int64_t nb = k / QK_K; 370 { 371 dpct::has_capability_or_fail(stream->get_device(), 372 {sycl::aspect::fp16}); 373 374 stream->submit([&](sycl::handler &cgh) { 375 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 376 sycl::range<3>(1, 1, 32), 377 sycl::range<3>(1, 1, 32)), 378 [=](sycl::nd_item<3> item_ct1) { 379 dequantize_block_iq2_xs( 380 vx, y, item_ct1, iq2xs_grid, 381 ksigns_iq2xs, kmask_iq2xs); 382 }); 383 }); 384 } 385 } 386 387 template 388 static void dequantize_row_iq2_s_sycl(const void *vx, dst_t *y, const int64_t k, 389 dpct::queue_ptr stream) { 390 const int64_t nb = k / QK_K; 391 { 392 dpct::has_capability_or_fail(stream->get_device(), 393 {sycl::aspect::fp16}); 394 395 stream->submit([&](sycl::handler &cgh) { 396 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 397 sycl::range<3>(1, 1, 32), 398 sycl::range<3>(1, 1, 32)), 399 [=](sycl::nd_item<3> item_ct1) { 400 dequantize_block_iq2_s(vx, y, item_ct1); 401 }); 402 }); 403 } 404 } 405 406 407 template 408 static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int64_t k, 409 dpct::queue_ptr stream) { 410 const int64_t nb = k / QK_K; 411 { 412 dpct::has_capability_or_fail(stream->get_device(), 413 {sycl::aspect::fp16}); 414 415 stream->submit([&](sycl::handler &cgh) { 416 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 417 sycl::range<3>(1, 1, 32), 418 sycl::range<3>(1, 1, 32)), 419 [=](sycl::nd_item<3> item_ct1) { 420 dequantize_block_iq3_xxs( 421 vx, y, item_ct1, iq3xxs_grid, 422 ksigns_iq2xs, kmask_iq2xs); 423 }); 424 }); 425 } 426 } 427 428 template 429 static void dequantize_row_iq3_xxs_sycl_reorder(const void * vx, dst_t * y, const int64_t k, 430 dpct::queue_ptr stream) { 431 const int64_t nb = k / QK_K; 432 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 433 stream->parallel_for( 434 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 32), sycl::range<3>(1, 1, 32)), 435 [=](sycl::nd_item<3> item_ct1) { 436 dequantize_block_iq3_xxs_reorder( 437 vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), item_ct1, nb, 438 iq3xxs_grid, ksigns_iq2xs, kmask_iq2xs); 439 }); 440 } 441 442 template 443 static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int64_t k, 444 dpct::queue_ptr stream) { 445 const int64_t nb = k / QK_K; 446 { 447 dpct::has_capability_or_fail(stream->get_device(), 448 {sycl::aspect::fp16}); 449 450 stream->submit([&](sycl::handler &cgh) { 451 cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 452 sycl::range<3>(1, 1, 32), 453 sycl::range<3>(1, 1, 32)), 454 [=](sycl::nd_item<3> item_ct1) { 455 dequantize_block_iq3_s( 456 vx, y, item_ct1, kmask_iq2xs, iq3s_grid); 457 }); 458 }); 459 } 460 } 461 462 template 463 static void dequantize_row_iq3_s_sycl_reorder(const void * vx, dst_t * y, const int64_t k, 464 dpct::queue_ptr stream) { 465 const int64_t nb = k / QK_K; 466 dpct::has_capability_or_fail(stream->get_device(), { sycl::aspect::fp16 }); 467 stream->parallel_for( 468 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 32), sycl::range<3>(1, 1, 32)), 469 [=](sycl::nd_item<3> item_ct1) { 470 dequantize_block_iq3_s_reorder(vx, y + item_ct1.get_group(2) * QK_K, item_ct1.get_group(2), 471 item_ct1, nb, kmask_iq2xs, iq3s_grid); 472 }); 473 } 474 475 template 476 static void dequantize_row_iq4_xs_sycl(const void *vx, dst_t *y, const int64_t k, 477 dpct::queue_ptr stream) { 478 const int64_t nb = (k + QK_K - 1) / QK_K; 479 #if QK_K == 64 480 dequantize_row_iq4_nl_sycl(vx, y, k, stream); 481 #else 482 { 483 dpct::has_capability_or_fail(stream->get_device(), 484 {sycl::aspect::fp16}); 485 486 stream->submit([&](sycl::handler &cgh) { 487 cgh.parallel_for( 488 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 489 sycl::range<3>(1, 1, 32), 490 sycl::range<3>(1, 1, 32)), 491 [=](sycl::nd_item<3> item_ct1) { 492 dequantize_block_iq4_xs(vx, y, item_ct1); 493 }); 494 }); 495 } 496 #endif 497 } 498 499 template 500 static void dequantize_row_iq4_nl_sycl(const void *vx, dst_t *y, const int64_t k, 501 dpct::queue_ptr stream) { 502 const int64_t nb = (k + QK_K - 1) / QK_K; 503 { 504 dpct::has_capability_or_fail(stream->get_device(), 505 {sycl::aspect::fp16}); 506 507 stream->submit([&](sycl::handler &cgh) { 508 cgh.parallel_for( 509 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * 510 sycl::range<3>(1, 1, 32), 511 sycl::range<3>(1, 1, 32)), 512 [=](sycl::nd_item<3> item_ct1) { 513 dequantize_block_iq4_nl(vx, y, item_ct1); 514 }); 515 }); 516 } 517 } 518 519 template 520 static void dequantize_row_mxfp4_sycl(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 521 const int nb = (k + QK_K - 1) / QK_K; 522 stream->parallel_for( 523 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 32), sycl::range<3>(1, 1, 32)), 524 [=](sycl::nd_item<3> item_ct1) { 525 dequantize_block_mxfp4(vx, y, item_ct1); 526 }); 527 } 528 529 template 530 static void dequantize_row_mxfp4_sycl_reorder(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 531 // flat range<1>, four qs bytes (8 outputs) per item: two vectorized stores. 532 // Each 32-element block holds QK_MXFP4/2 = 16 qs bytes = 4 items, so the 533 // k/8 launch covers the row exactly (k % QK_MXFP4 == 0 asserted). 534 GGML_ASSERT(k % QK_MXFP4 == 0); 535 stream->parallel_for(sycl::range<1>((size_t) (k / 8)), 536 [=](sycl::id<1> idx) { 537 dequantize_block_mxfp4_reorder_wide(vx, y, k, idx); 538 }); 539 } 540 541 template 542 static void dequantize_row_nvfp4_sycl(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr stream) { 543 GGML_ASSERT(k % QK_NVFP4 == 0); 544 const int nb = k / QK_NVFP4; 545 stream->parallel_for( 546 sycl::nd_range<3>(sycl::range<3>(1, 1, nb) * sycl::range<3>(1, 1, 32), sycl::range<3>(1, 1, 32)), 547 [=](sycl::nd_item<3> /*item_ct1*/) { 548 dequantize_block_nvfp4(vx, y, k); 549 }); 550 } 551 552 553 template 554 static void dequantize_block_nc(const void * __restrict__ vx, dst_t * __restrict__ y, 555 const int64_t ne00, const int64_t ne01, const int64_t ne02, 556 const int64_t s01, const int64_t s02, const int64_t s03) { 557 auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); 558 const int64_t i00 = 2 * (int64_t(item_ct1.get_local_range(2)) * item_ct1.get_group(2) + item_ct1.get_local_id(2)); 559 560 if (i00 >= ne00) { 561 return; 562 } 563 564 const int64_t i01 = item_ct1.get_group(1); 565 const int64_t i02 = item_ct1.get_group(0) % ne02; 566 const int64_t i03 = item_ct1.get_group(0) / ne02; 567 568 const int64_t ibx0 = i03*s03 + i02*s02 + i01*s01; 569 570 const int64_t ib = ibx0 + i00/qk; // block index 571 const int64_t iqs = (i00%qk)/qr; // quant index 572 const int64_t iybs = i00 - i00%qk; // y block start index 573 const int64_t y_offset = qr == 1 ? 1 : qk/2; 574 575 // dequantize 576 #ifdef GGML_SYCL_F16 577 sycl::half2 v; 578 #else 579 sycl::float2 v; 580 #endif 581 582 dequantize_kernel(vx, ib, iqs, v); 583 584 const int64_t iy0 = ((i03*ne02 + i02)*ne01 + i01)*ne00 + iybs + iqs; 585 y[iy0 + 0] = ggml_sycl_cast(v.x()); 586 y[iy0 + y_offset] = ggml_sycl_cast(v.y()); 587 } 588 589 590 template 591 static void dequantize_block_nc_sycl(const void * vx, 592 dst_t * y, 593 const int64_t ne00, 594 const int64_t ne01, 595 const int64_t ne02, 596 const int64_t ne03, 597 const int64_t s01, 598 const int64_t s02, 599 const int64_t s03, 600 dpct::queue_ptr stream) { 601 const dpct::dim3 num_blocks((ne00 + 2 * SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2 * SYCL_DEQUANTIZE_BLOCK_SIZE), ne01, 602 ne02 * ne03); 603 stream->parallel_for(sycl::nd_range<3>(num_blocks * sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE), 604 sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)), 605 [=](sycl::nd_item<3> item_ct1) { 606 GGML_UNUSED(item_ct1); 607 dequantize_block_nc(vx, y, ne00, ne01, ne02, s01, s02, s03); 608 }); 609 } 610 template 611 static void convert_unary_nc(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t ne00, const int64_t ne01, 612 const int64_t ne02, const int64_t s01, const int64_t s02, const int64_t s03, 613 const sycl::nd_item<3> & item_ct1) { 614 615 const int64_t work_group_size = item_ct1.get_local_range(2); 616 const int64_t global_id = item_ct1.get_local_id(2) + work_group_size * item_ct1.get_group(2); 617 618 const int64_t i01 = item_ct1.get_group(1); 619 const int64_t i02 = item_ct1.get_group(0) % ne02; 620 const int64_t i03 = item_ct1.get_group(0) / ne02; 621 622 // make each work-item deal with more elements since sycl global range can not exceed max int 623 const src_t * x = static_cast(vx); 624 const int64_t ix = i03 * s03 + i02 * s02 + i01 * s01; 625 const int64_t iy = ((i03 * ne02 + i02) * ne01 + i01) * ne00; 626 627 #pragma unroll 628 for (int64_t i00 = global_id; i00 < ne00; i00 += work_group_size * item_ct1.get_group_range(2)) { 629 y[iy + i00] = static_cast(x[ix + i00]); 630 } 631 } 632 633 template 634 static void convert_unary_nc_sycl(const void * __restrict__ vx, dst_t * __restrict__ y, 635 const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03, 636 const int64_t s01, const int64_t s02, const int64_t s03, dpct::queue_ptr queue) { 637 dpct::has_capability_or_fail(queue->get_device(), { sycl::aspect::fp16 }); 638 639 sycl::range<3> global_size(ne02 * ne03, ne01, ceil_div(ne00, SYCL_DEQUANTIZE_BLOCK_SIZE)); 640 641 // decrease global range when it exceeds the max int 642 // TODO: Downsample logic is separated from the kernel, a rewrite is desirable 643 int64_t downsized_workgroup = downsample_sycl_global_range(global_size[0], SYCL_DEQUANTIZE_BLOCK_SIZE); 644 sycl::range<3> workgroup_size(1, 1, downsized_workgroup); 645 646 queue->parallel_for(sycl::nd_range<3>(global_size * workgroup_size, workgroup_size), [=](sycl::nd_item<3> item_ct1) { 647 convert_unary_nc(vx, y, ne00, ne01, ne02, s01, s02, s03, item_ct1); 648 }); 649 } 650 651 template 652 static void convert_unary_sycl(const void * vx, dst_t * y, const int64_t k, dpct::queue_ptr queue) { 653 convert_unary_nc_sycl(vx, y, k, 1, 1, 1, k, k, k, queue); 654 } 655 656 657 static bool is_reordered_src0(const ggml_tensor * dst) { 658 return dst->src[0]->extra && 659 ((ggml_tensor_extra_gpu *) dst->src[0]->extra)->optimized_feature.reorder; 660 } 661 662 template 663 static to_t_sycl_t ggml_get_dequant_sycl(ggml_type type, ggml_tensor * dst) { 664 switch (type) { 665 case GGML_TYPE_Q1_0: 666 return dequantize_block_sycl; 667 case GGML_TYPE_Q2_0: 668 return dequantize_block_sycl; 669 case GGML_TYPE_Q4_0: 670 if (is_reordered_src0(dst)) { 671 return dequantize_row_q4_0_sycl_reorder; 672 } else if constexpr (std::is_same_v) { 673 return dequantize_row_q4_0_sycl; 674 } else { 675 return dequantize_block_sycl; 676 } 677 case GGML_TYPE_Q4_1: 678 if constexpr (std::is_same_v) { 679 return dequantize_row_q4_1_sycl; 680 } else { 681 return dequantize_block_sycl; 682 } 683 case GGML_TYPE_Q5_0: 684 return dequantize_block_sycl; 685 case GGML_TYPE_Q5_1: 686 return dequantize_block_sycl; 687 case GGML_TYPE_Q8_0: 688 if (is_reordered_src0(dst)) { 689 return dequantize_row_q8_0_sycl_reorder; 690 } else { 691 return dequantize_block_sycl; 692 } 693 case GGML_TYPE_Q2_K: 694 if (is_reordered_src0(dst)) { 695 return dequantize_row_q2_K_sycl_reorder; 696 } else { 697 return dequantize_row_q2_K_sycl; 698 } 699 case GGML_TYPE_Q3_K: 700 if (is_reordered_src0(dst)) { 701 return dequantize_row_q3_K_sycl_reorder; 702 } else { 703 return dequantize_row_q3_K_sycl; 704 } 705 case GGML_TYPE_Q4_K: 706 if (is_reordered_src0(dst)) { 707 return dequantize_row_q4_K_sycl_reorder; 708 } else { 709 return dequantize_row_q4_K_sycl; 710 } 711 case GGML_TYPE_Q5_K: 712 if (is_reordered_src0(dst)) { 713 return dequantize_row_q5_K_sycl_reorder; 714 } else { 715 return dequantize_row_q5_K_sycl; 716 } 717 case GGML_TYPE_Q6_K: 718 if (is_reordered_src0(dst)) { 719 return dequantize_row_q6_K_sycl_reorder; 720 } else { 721 return dequantize_row_q6_K_sycl; 722 } 723 case GGML_TYPE_IQ1_S: 724 return dequantize_row_iq1_s_sycl; 725 case GGML_TYPE_IQ1_M: 726 return dequantize_row_iq1_m_sycl; 727 case GGML_TYPE_IQ2_XXS: 728 return dequantize_row_iq2_xxs_sycl; 729 case GGML_TYPE_IQ2_XS: 730 return dequantize_row_iq2_xs_sycl; 731 case GGML_TYPE_IQ2_S: 732 return dequantize_row_iq2_s_sycl; 733 case GGML_TYPE_IQ3_XXS: 734 if (is_reordered_src0(dst)) { 735 return dequantize_row_iq3_xxs_sycl_reorder; 736 } else { 737 return dequantize_row_iq3_xxs_sycl; 738 } 739 case GGML_TYPE_IQ3_S: 740 if (is_reordered_src0(dst)) { 741 return dequantize_row_iq3_s_sycl_reorder; 742 } else { 743 return dequantize_row_iq3_s_sycl; 744 } 745 case GGML_TYPE_IQ4_XS: 746 return dequantize_row_iq4_xs_sycl; 747 case GGML_TYPE_IQ4_NL: 748 return dequantize_row_iq4_nl_sycl; 749 case GGML_TYPE_MXFP4: 750 if (is_reordered_src0(dst)) { 751 return dequantize_row_mxfp4_sycl_reorder; 752 } 753 return dequantize_row_mxfp4_sycl; 754 case GGML_TYPE_NVFP4: 755 return dequantize_row_nvfp4_sycl; 756 case GGML_TYPE_F32: 757 if constexpr (std::is_same_v) { 758 return convert_unary_sycl; 759 } 760 break; 761 case GGML_TYPE_F16: 762 if constexpr (std::is_same_v) { 763 return convert_unary_sycl; 764 } 765 break; 766 #ifdef GGML_SYCL_HAS_BF16 767 case GGML_TYPE_BF16: 768 return convert_unary_sycl; 769 #endif 770 default: 771 break; 772 } 773 GGML_ABORT("fatal error: unsupport data type=%s\n", ggml_type_name(type)); 774 } 775 776 to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type, ggml_tensor * dst) { 777 return ggml_get_dequant_sycl(type, dst); 778 } 779 780 to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type, ggml_tensor *dst) { 781 return ggml_get_dequant_sycl(type, dst); 782 } 783 784 785 #ifdef GGML_SYCL_HAS_BF16 786 to_bf16_sycl_t ggml_get_to_bf16_sycl(ggml_type type, ggml_tensor * /*dst*/) { 787 switch (type) { 788 case GGML_TYPE_F32: 789 return convert_unary_sycl; 790 case GGML_TYPE_F16: 791 return convert_unary_sycl; 792 case GGML_TYPE_BF16: 793 return convert_unary_sycl; 794 default: 795 GGML_ABORT("fatal error: unsupport data type=%s\n", ggml_type_name(type)); 796 } 797 } 798 #endif 799 800 to_fp16_nc_sycl_t ggml_get_to_fp16_nc_sycl(ggml_type type) { 801 switch (type) { 802 case GGML_TYPE_F32: 803 return convert_unary_nc_sycl; 804 #ifdef GGML_SYCL_HAS_BF16 805 case GGML_TYPE_BF16: 806 return convert_unary_nc_sycl; 807 #endif 808 case GGML_TYPE_Q1_0: 809 return dequantize_block_nc_sycl; 810 case GGML_TYPE_Q4_0: 811 return dequantize_block_nc_sycl; 812 case GGML_TYPE_Q4_1: 813 return dequantize_block_nc_sycl; 814 case GGML_TYPE_Q5_0: 815 return dequantize_block_nc_sycl; 816 case GGML_TYPE_Q5_1: 817 return dequantize_block_nc_sycl; 818 case GGML_TYPE_Q8_0: 819 return dequantize_block_nc_sycl; 820 default: 821 return nullptr; 822 } 823 }