ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / dequantize.hpp
| 1 | // |
| 2 | // MIT license |
| 3 | // Copyright (C) 2024 Intel Corporation |
| 4 | // SPDX-License-Identifier: MIT |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 9 | // See https://llvm.org/LICENSE.txt for license information. |
| 10 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 11 | // |
| 12 | |
| 13 | #ifndef GGML_SYCL_DEQUANTIZE_HPP |
| 14 | #define GGML_SYCL_DEQUANTIZE_HPP |
| 15 | |
| 16 | #include "common.hpp" |
| 17 | #include "convert.hpp" |
| 18 | |
| 19 | typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, dfloat2 & v); |
| 20 | typedef void (*dequantize_kernel_t_reorder)(const void *d, const int64_t ib, const void *qs, |
| 21 | const int iqs, dfloat2 &v); |
| 22 | typedef void (*dequantize_kernel_f32_t)(const void * vx, const int64_t ib, const int iqs, float & v0, float & v1); |
| 23 | |
| 24 | #if QK_K == 256 |
| 25 | static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m); |
| 26 | #endif |
| 27 | |
| 28 | static __dpct_inline__ void dequantize_q2_0(const void *vx, const int64_t ib, |
| 29 | const int iqs, dfloat2 &v) { |
| 30 | const block_q2_0 * x = (const block_q2_0 *) vx; |
| 31 | |
| 32 | const dfloat d = x[ib].d; |
| 33 | |
| 34 | const int byte_idx = iqs / 4; |
| 35 | const int shift = (iqs % 4) * 2; |
| 36 | const uint8_t vui = x[ib].qs[byte_idx]; |
| 37 | |
| 38 | v.x() = (vui >> shift) & 3; |
| 39 | v.y() = (vui >> (shift + 2)) & 3; |
| 40 | |
| 41 | #ifdef GGML_SYCL_F16 |
| 42 | v.s0() = ((dfloat)v.s0() - 1.0f) * d; |
| 43 | v.s1() = ((dfloat)v.s1() - 1.0f) * d; |
| 44 | #else |
| 45 | v.x() = ((dfloat)v.x() - 1.0f) * d; |
| 46 | v.y() = ((dfloat)v.y() - 1.0f) * d; |
| 47 | #endif // GGML_SYCL_F16 |
| 48 | } |
| 49 | |
| 50 | static __dpct_inline__ void dequantize_q4_0(const void *vx, const int64_t ib, |
| 51 | const int iqs, dfloat2 &v) { |
| 52 | const block_q4_0 * x = (const block_q4_0 *) vx; |
| 53 | |
| 54 | const dfloat d = x[ib].d; |
| 55 | |
| 56 | const int vui = x[ib].qs[iqs]; |
| 57 | |
| 58 | v.x() = vui & 0xF; |
| 59 | v.y() = vui >> 4; |
| 60 | |
| 61 | #ifdef GGML_SYCL_F16 |
| 62 | // v = v - {8.0f, 8.0f}; |
| 63 | // v = v * {d, d}; |
| 64 | v.s0() = (v.s0() - 8.0f) * d; |
| 65 | v.s1() = (v.s1() - 8.0f) * d; |
| 66 | |
| 67 | #else |
| 68 | v.x() = (v.x() - 8.0f) * d; |
| 69 | v.y() = (v.y() - 8.0f) * d; |
| 70 | #endif // GGML_SYCL_F16 |
| 71 | } |
| 72 | |
| 73 | static __dpct_inline__ void dequantize_q4_0_reorder(const void *d_ptr, const int64_t ib, const void *qs, |
| 74 | const int iqs, dfloat2 &v) { |
| 75 | // const block_q4_0 * x = (const block_q4_0 *) vx; |
| 76 | |
| 77 | const dfloat d = (const dfloat)*((const sycl::half*)d_ptr+ib); |
| 78 | |
| 79 | const int vui = *((const uint8_t *)qs+iqs); |
| 80 | |
| 81 | v.x() = vui & 0xF; |
| 82 | v.y() = vui >> 4; |
| 83 | |
| 84 | #ifdef GGML_SYCL_F16 |
| 85 | // v = v - {8.0f, 8.0f}; |
| 86 | // v = v * {d, d}; |
| 87 | v.s0() = (v.s0() - 8.0f) * d; |
| 88 | v.s1() = (v.s1() - 8.0f) * d; |
| 89 | |
| 90 | #else |
| 91 | v.x() = (v.x() - 8.0f) * d; |
| 92 | v.y() = (v.y() - 8.0f) * d; |
| 93 | #endif // GGML_SYCL_F16 |
| 94 | } |
| 95 | |
| 96 | static __dpct_inline__ void dequantize_q1_0_reorder(const void *d_ptr, const int64_t ib, const void *qs, |
| 97 | const int iqs, dfloat2 &v) { |
| 98 | // Q1_0 reorder layout: scale values followed by quantized bits |
| 99 | const dfloat d = (const dfloat)*((const sycl::half*)d_ptr+ib); |
| 100 | |
| 101 | const int bit_index_0 = iqs + 0; |
| 102 | const int bit_index_1 = iqs + 1; |
| 103 | |
| 104 | const int bit_0 = (*((const uint8_t *)qs + bit_index_0 / 8) >> (bit_index_0 % 8)) & 1; |
| 105 | const int bit_1 = (*((const uint8_t *)qs + bit_index_1 / 8) >> (bit_index_1 % 8)) & 1; |
| 106 | |
| 107 | v.x() = (2 * bit_0 - 1) * d; |
| 108 | v.y() = (2 * bit_1 - 1) * d; |
| 109 | } |
| 110 | |
| 111 | static __dpct_inline__ void dequantize_q1_0(const void *vx, const int64_t ib, |
| 112 | const int iqs, dfloat2 &v) { |
| 113 | const block_q1_0 * x = (const block_q1_0 *) vx; |
| 114 | const dfloat d = x[ib].d; |
| 115 | |
| 116 | const int bit_index_0 = iqs + 0; |
| 117 | const int bit_index_1 = iqs + 1; |
| 118 | |
| 119 | const int bit_0 = (x[ib].qs[bit_index_0 / 8] >> (bit_index_0 % 8)) & 1; |
| 120 | const int bit_1 = (x[ib].qs[bit_index_1 / 8] >> (bit_index_1 % 8)) & 1; |
| 121 | |
| 122 | v.x() = (2 * bit_0 - 1) * d; |
| 123 | v.y() = (2 * bit_1 - 1) * d; |
| 124 | } |
| 125 | |
| 126 | static __dpct_inline__ void dequantize_q4_1(const void *vx, const int64_t ib, |
| 127 | const int iqs, dfloat2 &v) { |
| 128 | const block_q4_1 * x = (const block_q4_1 *) vx; |
| 129 | |
| 130 | const dfloat d = x[ib].dm[0]; |
| 131 | const dfloat m = x[ib].dm[1]; |
| 132 | |
| 133 | const int vui = x[ib].qs[iqs]; |
| 134 | |
| 135 | v.x() = vui & 0xF; |
| 136 | v.y() = vui >> 4; |
| 137 | |
| 138 | #ifdef GGML_SYCL_F16 |
| 139 | // v = v * {d, d}; |
| 140 | // v = v + {m, m}; |
| 141 | v.s0() = sycl::fma(v.s0(), d, m); |
| 142 | v.s1() = sycl::fma(v.s1(), d, m); |
| 143 | |
| 144 | #else |
| 145 | v.x() = sycl::fma(v.x(), d, m); |
| 146 | v.y() = sycl::fma(v.y(), d, m); |
| 147 | #endif // GGML_SYCL_F16 |
| 148 | } |
| 149 | |
| 150 | static __dpct_inline__ void dequantize_q4_K(const void *vx, const int64_t ib, |
| 151 | const int iqs, dfloat2 &v) { |
| 152 | #if QK_K == 256 |
| 153 | const block_q4_K * x = (const block_q4_K *) vx; |
| 154 | const sycl::half2 dm = x[ib].dm; |
| 155 | const float dall = dm[0]; |
| 156 | const float dmin = dm[1]; |
| 157 | |
| 158 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 159 | const int il = idx / 64; |
| 160 | const int in = idx % 64; |
| 161 | const int is = 2 * il + (in >= 32 ? 1 : 0); |
| 162 | const int off = in & 31; |
| 163 | const int qsi = 32 * il + off; |
| 164 | |
| 165 | uint8_t sc; |
| 166 | uint8_t m; |
| 167 | get_scale_min_k4(is, x[ib].scales, sc, m); |
| 168 | |
| 169 | const uint8_t q = x[ib].qs[qsi]; |
| 170 | const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF); |
| 171 | return sycl::fma((dfloat) qv, (dfloat) (dall * sc), (dfloat) (-dmin * m)); |
| 172 | }; |
| 173 | |
| 174 | v.x() = dequantize_one(iqs + 0); |
| 175 | v.y() = dequantize_one(iqs + 1); |
| 176 | #else |
| 177 | GGML_ABORT("Q4_K dequantize not supported for QK_K != 256"); |
| 178 | #endif |
| 179 | } |
| 180 | |
| 181 | static __dpct_inline__ void dequantize_q4_K_f32(const void *vx, const int64_t ib, |
| 182 | const int iqs, float &v0, float &v1) { |
| 183 | #if QK_K == 256 |
| 184 | const block_q4_K * x = (const block_q4_K *) vx; |
| 185 | const sycl::half2 dm = x[ib].dm; |
| 186 | const float dall = dm[0]; |
| 187 | const float dmin = dm[1]; |
| 188 | |
| 189 | auto dequantize_one = [&](const int idx) -> float { |
| 190 | const int il = idx / 64; |
| 191 | const int in = idx % 64; |
| 192 | const int is = 2 * il + (in >= 32 ? 1 : 0); |
| 193 | const int qsi = 32 * il + (in & 31); |
| 194 | |
| 195 | uint8_t sc; |
| 196 | uint8_t m; |
| 197 | get_scale_min_k4(is, x[ib].scales, sc, m); |
| 198 | |
| 199 | const float d = dall * sc; |
| 200 | const float mn = dmin * m; |
| 201 | const uint8_t q = x[ib].qs[qsi]; |
| 202 | const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF); |
| 203 | |
| 204 | return d * qv - mn; |
| 205 | }; |
| 206 | |
| 207 | v0 = dequantize_one(iqs + 0); |
| 208 | v1 = dequantize_one(iqs + 1); |
| 209 | #else |
| 210 | GGML_ABORT("Q4_K dequantize not supported for QK_K != 256"); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | static __dpct_inline__ void dequantize_q2_K(const void *vx, const int64_t ib, |
| 215 | const int iqs, dfloat2 &v) { |
| 216 | #if QK_K == 256 |
| 217 | const block_q2_K * x = (const block_q2_K *) vx; |
| 218 | const float dall = x[ib].dm[0]; |
| 219 | const float dmin = x[ib].dm[1]; |
| 220 | |
| 221 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 222 | const int n = idx / 128; |
| 223 | const int r = idx % 128; |
| 224 | const int g = r / 32; |
| 225 | const int l = r % 32; |
| 226 | const int is = 8 * n + l / 16; |
| 227 | |
| 228 | const uint8_t q = x[ib].qs[32 * n + l]; |
| 229 | const uint8_t sc = x[ib].scales[is + 2 * g]; |
| 230 | const float d = dall * (sc & 0xF); |
| 231 | const float m = dmin * (sc >> 4); |
| 232 | |
| 233 | return (dfloat) d * (dfloat) ((q >> (2 * g)) & 3) - (dfloat) m; |
| 234 | }; |
| 235 | |
| 236 | v.x() = dequantize_one(iqs + 0); |
| 237 | v.y() = dequantize_one(iqs + 1); |
| 238 | #else |
| 239 | GGML_ABORT("Q2_K dequantize not supported for QK_K != 256"); |
| 240 | #endif |
| 241 | } |
| 242 | |
| 243 | static __dpct_inline__ void dequantize_q2_K_f32(const void *vx, const int64_t ib, |
| 244 | const int iqs, float &v0, float &v1) { |
| 245 | #if QK_K == 256 |
| 246 | const block_q2_K * x = (const block_q2_K *) vx; |
| 247 | const float dall = x[ib].dm[0]; |
| 248 | const float dmin = x[ib].dm[1]; |
| 249 | |
| 250 | auto dequantize_one = [&](const int idx) -> float { |
| 251 | const int n = idx / 128; |
| 252 | const int r = idx % 128; |
| 253 | const int g = r / 32; |
| 254 | const int l = r % 32; |
| 255 | const int is = 8 * n + l / 16; |
| 256 | |
| 257 | const uint8_t q = x[ib].qs[32 * n + l]; |
| 258 | const uint8_t sc = x[ib].scales[is + 2 * g]; |
| 259 | const float d = dall * (sc & 0xF); |
| 260 | const float m = dmin * (sc >> 4); |
| 261 | |
| 262 | return d * ((q >> (2 * g)) & 3) - m; |
| 263 | }; |
| 264 | |
| 265 | v0 = dequantize_one(iqs + 0); |
| 266 | v1 = dequantize_one(iqs + 1); |
| 267 | #else |
| 268 | GGML_ABORT("Q2_K dequantize not supported for QK_K != 256"); |
| 269 | #endif |
| 270 | } |
| 271 | |
| 272 | static __dpct_inline__ void dequantize_q3_K(const void *vx, const int64_t ib, |
| 273 | const int iqs, dfloat2 &v) { |
| 274 | #if QK_K == 256 |
| 275 | const block_q3_K * x = (const block_q3_K *) vx; |
| 276 | const float d_all = x[ib].d; |
| 277 | |
| 278 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 279 | const int n = idx / 128; |
| 280 | const int r = idx % 128; |
| 281 | const int j = r / 32; |
| 282 | const int l = r % 32; |
| 283 | |
| 284 | const int is0 = l / 16; |
| 285 | const int is = 8 * n + 2 * j + is0; |
| 286 | const int shift = 2 * j; |
| 287 | const uint8_t m = 1 << (4 * n + j); |
| 288 | |
| 289 | const int8_t us = is < 4 ? (x[ib].scales[is - 0] & 0xF) | (((x[ib].scales[is + 8] >> 0) & 3) << 4) : |
| 290 | is < 8 ? (x[ib].scales[is - 0] & 0xF) | (((x[ib].scales[is + 4] >> 2) & 3) << 4) : |
| 291 | is < 12 ? (x[ib].scales[is - 8] >> 4) | (((x[ib].scales[is + 0] >> 4) & 3) << 4) : |
| 292 | (x[ib].scales[is - 8] >> 4) | (((x[ib].scales[is - 4] >> 6) & 3) << 4); |
| 293 | |
| 294 | const float dl = d_all * (us - 32); |
| 295 | const uint8_t q = x[ib].qs[32 * n + l]; |
| 296 | const uint8_t h = x[ib].hmask[l]; |
| 297 | const int8_t qv = ((q >> shift) & 3) - ((h & m) ? 0 : 4); |
| 298 | |
| 299 | return (dfloat) (dl * qv); |
| 300 | }; |
| 301 | |
| 302 | v.x() = dequantize_one(iqs + 0); |
| 303 | v.y() = dequantize_one(iqs + 1); |
| 304 | #else |
| 305 | GGML_ABORT("Q3_K dequantize not supported for QK_K != 256"); |
| 306 | #endif |
| 307 | } |
| 308 | |
| 309 | static __dpct_inline__ void dequantize_q5_K(const void *vx, const int64_t ib, |
| 310 | const int iqs, dfloat2 &v) { |
| 311 | #if QK_K == 256 |
| 312 | const block_q5_K * x = (const block_q5_K *) vx; |
| 313 | const float dall = x[ib].dm[0]; |
| 314 | const float dmin = x[ib].dm[1]; |
| 315 | |
| 316 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 317 | const int il = idx / 64; |
| 318 | const int in = idx % 64; |
| 319 | const int is = 2 * il + (in >= 32 ? 1 : 0); |
| 320 | const int ir = (in & 31) / 2; |
| 321 | const int iq = in & 1; |
| 322 | |
| 323 | const uint8_t q = x[ib].qs[32 * il + 2 * ir + iq]; |
| 324 | const uint8_t h = x[ib].qh[2 * ir + iq]; |
| 325 | const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF); |
| 326 | |
| 327 | uint8_t sc; |
| 328 | uint8_t m; |
| 329 | get_scale_min_k4(is, x[ib].scales, sc, m); |
| 330 | |
| 331 | const float d = dall * sc; |
| 332 | const float mn = dmin * m; |
| 333 | const uint8_t hm = 1 << (2 * il + (in >= 32 ? 1 : 0)); |
| 334 | |
| 335 | return sycl::fma((dfloat) (qv + ((h & hm) ? 16 : 0)), (dfloat) d, (dfloat) (-mn)); |
| 336 | }; |
| 337 | |
| 338 | v.x() = dequantize_one(iqs + 0); |
| 339 | v.y() = dequantize_one(iqs + 1); |
| 340 | #else |
| 341 | GGML_ABORT("Q5_K dequantize not supported for QK_K != 256"); |
| 342 | #endif |
| 343 | } |
| 344 | |
| 345 | static __dpct_inline__ void dequantize_q5_K_f32(const void *vx, const int64_t ib, |
| 346 | const int iqs, float &v0, float &v1) { |
| 347 | #if QK_K == 256 |
| 348 | const block_q5_K * x = (const block_q5_K *) vx; |
| 349 | const float dall = x[ib].dm[0]; |
| 350 | const float dmin = x[ib].dm[1]; |
| 351 | |
| 352 | auto dequantize_one = [&](const int idx) -> float { |
| 353 | const int il = idx / 64; |
| 354 | const int in = idx % 64; |
| 355 | const int is = 2 * il + (in >= 32 ? 1 : 0); |
| 356 | const int ir = (in & 31) / 2; |
| 357 | const int iq = in & 1; |
| 358 | |
| 359 | const uint8_t q = x[ib].qs[32 * il + 2 * ir + iq]; |
| 360 | const uint8_t h = x[ib].qh[2 * ir + iq]; |
| 361 | const uint8_t qv = (in >= 32) ? (q >> 4) : (q & 0xF); |
| 362 | |
| 363 | uint8_t sc; |
| 364 | uint8_t m; |
| 365 | get_scale_min_k4(is, x[ib].scales, sc, m); |
| 366 | |
| 367 | const float d = dall * sc; |
| 368 | const float mn = dmin * m; |
| 369 | const uint8_t hm = 1 << (2 * il + (in >= 32 ? 1 : 0)); |
| 370 | |
| 371 | return (qv + ((h & hm) ? 16 : 0)) * d - mn; |
| 372 | }; |
| 373 | |
| 374 | v0 = dequantize_one(iqs + 0); |
| 375 | v1 = dequantize_one(iqs + 1); |
| 376 | #else |
| 377 | GGML_ABORT("Q5_K dequantize not supported for QK_K != 256"); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | static __dpct_inline__ void dequantize_q6_K(const void *vx, const int64_t ib, |
| 382 | const int iqs, dfloat2 &v) { |
| 383 | #if QK_K == 256 |
| 384 | const block_q6_K * x = (const block_q6_K *) vx; |
| 385 | const float d = x[ib].d; |
| 386 | |
| 387 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 388 | const int ip = idx / 128; |
| 389 | const int in = idx % 128; |
| 390 | const int il = in & 31; |
| 391 | const int ig = in / 32; |
| 392 | const int is = 8 * ip + il / 16; |
| 393 | |
| 394 | const uint8_t ql0 = x[ib].ql[64 * ip + il]; |
| 395 | const uint8_t ql1 = x[ib].ql[64 * ip + il + 32]; |
| 396 | const uint8_t qh = x[ib].qh[32 * ip + il]; |
| 397 | const int8_t * sc = x[ib].scales + is; |
| 398 | |
| 399 | uint8_t qv; |
| 400 | int8_t scale; |
| 401 | if (ig == 0) { |
| 402 | qv = (ql0 & 0xF) | (((qh >> 0) & 3) << 4); |
| 403 | scale = sc[0]; |
| 404 | } else if (ig == 1) { |
| 405 | qv = (ql1 & 0xF) | (((qh >> 2) & 3) << 4); |
| 406 | scale = sc[2]; |
| 407 | } else if (ig == 2) { |
| 408 | qv = (ql0 >> 4) | (((qh >> 4) & 3) << 4); |
| 409 | scale = sc[4]; |
| 410 | } else { |
| 411 | qv = (ql1 >> 4) | (((qh >> 6) & 3) << 4); |
| 412 | scale = sc[6]; |
| 413 | } |
| 414 | |
| 415 | return (dfloat) (d * scale * ((int8_t) qv - 32)); |
| 416 | }; |
| 417 | |
| 418 | v.x() = dequantize_one(iqs + 0); |
| 419 | v.y() = dequantize_one(iqs + 1); |
| 420 | #else |
| 421 | GGML_ABORT("Q6_K dequantize not supported for QK_K != 256"); |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | static __dpct_inline__ void dequantize_mxfp4(const void *vx, const int64_t ib, |
| 426 | const int iqs, dfloat2 &v) { |
| 427 | const block_mxfp4 * x = (const block_mxfp4 *) vx; |
| 428 | const float d = ggml_sycl_e8m0_to_fp32(x[ib].e); |
| 429 | const uint8_t q = x[ib].qs[iqs]; |
| 430 | |
| 431 | v.x() = d * kvalues_mxfp4[q & 0xF] * 0.5f; |
| 432 | v.y() = d * kvalues_mxfp4[q >> 4] * 0.5f; |
| 433 | } |
| 434 | |
| 435 | static __dpct_inline__ void dequantize_nvfp4(const void *vx, const int64_t ib, |
| 436 | const int iqs, dfloat2 &v) { |
| 437 | const block_nvfp4 & xb = ((const block_nvfp4 *) vx)[ib]; |
| 438 | |
| 439 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 440 | const int sub = idx / QK_NVFP4_SUB; |
| 441 | const int j = idx % QK_NVFP4_SUB; |
| 442 | const int jh = j % (QK_NVFP4_SUB / 2); |
| 443 | |
| 444 | const float d = ggml_sycl_ue4m3_to_fp32(xb.d[sub]); |
| 445 | const uint8_t q = xb.qs[sub * (QK_NVFP4_SUB / 2) + jh]; |
| 446 | const uint8_t qv = (j < (QK_NVFP4_SUB / 2)) ? (q & 0x0F) : (q >> 4); |
| 447 | |
| 448 | return d * kvalues_mxfp4[qv]; |
| 449 | }; |
| 450 | |
| 451 | v.x() = dequantize_one(iqs + 0); |
| 452 | v.y() = dequantize_one(iqs + 1); |
| 453 | } |
| 454 | |
| 455 | static __dpct_inline__ void dequantize_iq2_xxs(const void *vx, const int64_t ib, |
| 456 | const int iqs, dfloat2 &v) { |
| 457 | #if QK_K == 256 |
| 458 | const block_iq2_xxs * x = (const block_iq2_xxs *) vx; |
| 459 | |
| 460 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 461 | const int ib8 = idx / 32; |
| 462 | const int r = idx % 32; |
| 463 | const int il = r / 8; |
| 464 | const int j = r % 8; |
| 465 | |
| 466 | const uint16_t * q2 = x[ib].qs + 4 * ib8; |
| 467 | const uint8_t * aux8 = (const uint8_t *) q2; |
| 468 | const uint8_t * grid = (const uint8_t *) (iq2xxs_grid + aux8[il]); |
| 469 | const uint32_t aux32 = q2[2] | (q2[3] << 16); |
| 470 | const float d = (float) x[ib].d * (0.5f + (aux32 >> 28)) * 0.25f; |
| 471 | const uint8_t signs = ksigns_iq2xs[(aux32 >> (7 * il)) & 127]; |
| 472 | |
| 473 | return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f); |
| 474 | }; |
| 475 | |
| 476 | v.x() = dequantize_one(iqs + 0); |
| 477 | v.y() = dequantize_one(iqs + 1); |
| 478 | #else |
| 479 | GGML_ABORT("IQ2_XXS dequantize not supported for QK_K != 256"); |
| 480 | #endif |
| 481 | } |
| 482 | |
| 483 | static __dpct_inline__ void dequantize_iq2_xs(const void *vx, const int64_t ib, |
| 484 | const int iqs, dfloat2 &v) { |
| 485 | #if QK_K == 256 |
| 486 | const block_iq2_xs * x = (const block_iq2_xs *) vx; |
| 487 | |
| 488 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 489 | const int ib8 = idx / 32; |
| 490 | const int r = idx % 32; |
| 491 | const int il = r / 8; |
| 492 | const int j = r % 8; |
| 493 | |
| 494 | const uint16_t * q2 = x[ib].qs + 4 * ib8; |
| 495 | const uint8_t * grid = (const uint8_t *) (iq2xs_grid + (q2[il] & 511)); |
| 496 | const float d = (float) x[ib].d * (0.5f + ((x[ib].scales[ib8] >> (4 * (il / 2))) & 0xf)) * 0.25f; |
| 497 | const uint8_t signs = ksigns_iq2xs[q2[il] >> 9]; |
| 498 | |
| 499 | return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f); |
| 500 | }; |
| 501 | |
| 502 | v.x() = dequantize_one(iqs + 0); |
| 503 | v.y() = dequantize_one(iqs + 1); |
| 504 | #else |
| 505 | GGML_ABORT("IQ2_XS dequantize not supported for QK_K != 256"); |
| 506 | #endif |
| 507 | } |
| 508 | |
| 509 | static __dpct_inline__ void dequantize_iq2_s(const void *vx, const int64_t ib, |
| 510 | const int iqs, dfloat2 &v) { |
| 511 | #if QK_K == 256 |
| 512 | const block_iq2_s * x = (const block_iq2_s *) vx; |
| 513 | |
| 514 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 515 | const int ib8 = idx / 32; |
| 516 | const int r = idx % 32; |
| 517 | const int il = r / 8; |
| 518 | const int j = r % 8; |
| 519 | |
| 520 | const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | ((x[ib].qh[ib8] << (8 - 2 * il)) & 0x300); |
| 521 | const uint8_t * grid = (const uint8_t *) (iq2s_grid + grid_id); |
| 522 | const float d = (float) x[ib].d * (0.5f + ((x[ib].scales[ib8] >> (4 * (il / 2))) & 0xf)) * 0.25f; |
| 523 | const uint8_t signs = x[ib].qs[QK_K / 8 + 4 * ib8 + il]; |
| 524 | |
| 525 | return d * grid[j] * ((signs & kmask_iq2xs[j]) ? -1.f : 1.f); |
| 526 | }; |
| 527 | |
| 528 | v.x() = dequantize_one(iqs + 0); |
| 529 | v.y() = dequantize_one(iqs + 1); |
| 530 | #else |
| 531 | GGML_ABORT("IQ2_S dequantize not supported for QK_K != 256"); |
| 532 | #endif |
| 533 | } |
| 534 | |
| 535 | static __dpct_inline__ void dequantize_iq3_xxs(const void *vx, const int64_t ib, |
| 536 | const int iqs, dfloat2 &v) { |
| 537 | #if QK_K == 256 |
| 538 | const block_iq3_xxs * x = (const block_iq3_xxs *) vx; |
| 539 | |
| 540 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 541 | const int ib8 = idx / 32; |
| 542 | const int r = idx % 32; |
| 543 | const int il = r / 8; |
| 544 | const int j = r % 8; |
| 545 | |
| 546 | const uint8_t * q3 = x[ib].qs + 8 * ib8; |
| 547 | const uint16_t * gas = (const uint16_t *) (x[ib].qs + QK_K / 4) + 2 * ib8; |
| 548 | const uint8_t * grid1 = (const uint8_t *) (iq3xxs_grid + q3[2 * il + 0]); |
| 549 | const uint8_t * grid2 = (const uint8_t *) (iq3xxs_grid + q3[2 * il + 1]); |
| 550 | const uint32_t aux32 = gas[0] | (gas[1] << 16); |
| 551 | const float d = (float) x[ib].d * (0.5f + (aux32 >> 28)) * 0.5f; |
| 552 | const uint8_t signs = ksigns_iq2xs[(aux32 >> (7 * il)) & 127]; |
| 553 | |
| 554 | if (j < 4) { |
| 555 | return d * grid1[j] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f); |
| 556 | } |
| 557 | return d * grid2[j - 4] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f); |
| 558 | }; |
| 559 | |
| 560 | v.x() = dequantize_one(iqs + 0); |
| 561 | v.y() = dequantize_one(iqs + 1); |
| 562 | #else |
| 563 | GGML_ABORT("IQ3_XXS dequantize not supported for QK_K != 256"); |
| 564 | #endif |
| 565 | } |
| 566 | |
| 567 | static __dpct_inline__ void dequantize_iq3_s(const void *vx, const int64_t ib, |
| 568 | const int iqs, dfloat2 &v) { |
| 569 | #if QK_K == 256 |
| 570 | const block_iq3_s * x = (const block_iq3_s *) vx; |
| 571 | |
| 572 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 573 | const int ib8 = idx / 32; |
| 574 | const int r = idx % 32; |
| 575 | const int il = r / 8; |
| 576 | const int j = r % 8; |
| 577 | |
| 578 | const uint8_t * qs = x[ib].qs + 8 * ib8; |
| 579 | const uint16_t grid1_id = qs[2 * il + 0] | ((x[ib].qh[ib8] << (8 - 2 * il)) & 256); |
| 580 | const uint16_t grid2_id = qs[2 * il + 1] | ((x[ib].qh[ib8] << (7 - 2 * il)) & 256); |
| 581 | const uint8_t * grid1 = (const uint8_t *) (iq3s_grid + grid1_id); |
| 582 | const uint8_t * grid2 = (const uint8_t *) (iq3s_grid + grid2_id); |
| 583 | const float d = (float) x[ib].d * (1 + 2 * ((x[ib].scales[ib8 / 2] >> (4 * (ib8 % 2))) & 0xf)); |
| 584 | const uint8_t signs = x[ib].signs[4 * ib8 + il]; |
| 585 | |
| 586 | if (j < 4) { |
| 587 | return d * grid1[j] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f); |
| 588 | } |
| 589 | return d * grid2[j - 4] * ((signs & kmask_iq2xs[j + 0]) ? -1.f : 1.f); |
| 590 | }; |
| 591 | |
| 592 | v.x() = dequantize_one(iqs + 0); |
| 593 | v.y() = dequantize_one(iqs + 1); |
| 594 | #else |
| 595 | GGML_ABORT("IQ3_S dequantize not supported for QK_K != 256"); |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | static __dpct_inline__ void dequantize_iq1_s(const void *vx, const int64_t ib, |
| 600 | const int iqs, dfloat2 &v) { |
| 601 | #if QK_K == 256 |
| 602 | const block_iq1_s * x = (const block_iq1_s *) vx; |
| 603 | |
| 604 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 605 | const int ib8 = idx / 32; |
| 606 | const int r = idx % 32; |
| 607 | const int il = r / 8; |
| 608 | const int j = r % 8; |
| 609 | |
| 610 | const float delta = (x[ib].qh[ib8] & 0x8000) ? (-1.f - IQ1S_DELTA) : (-1.f + IQ1S_DELTA); |
| 611 | const float d = (float) x[ib].d * (2 * ((x[ib].qh[ib8] >> 12) & 7) + 1); |
| 612 | const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | (((x[ib].qh[ib8] >> (3 * il)) & 7) << 8); |
| 613 | const uint32_t g = iq1s_grid_gpu[grid_id]; |
| 614 | const int8_t qv = (j < 4) ? ((g >> (8 * j)) & 0x0F) : ((g >> (8 * (j - 4) + 4)) & 0x0F); |
| 615 | |
| 616 | return d * (qv + delta); |
| 617 | }; |
| 618 | |
| 619 | v.x() = dequantize_one(iqs + 0); |
| 620 | v.y() = dequantize_one(iqs + 1); |
| 621 | #else |
| 622 | GGML_ABORT("IQ1_S dequantize not supported for QK_K != 256"); |
| 623 | #endif |
| 624 | } |
| 625 | |
| 626 | static __dpct_inline__ void dequantize_iq1_m(const void *vx, const int64_t ib, |
| 627 | const int iqs, dfloat2 &v) { |
| 628 | #if QK_K == 256 |
| 629 | const block_iq1_m * x = (const block_iq1_m *) vx; |
| 630 | |
| 631 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 632 | const int ib8 = idx / 32; |
| 633 | const int r = idx % 32; |
| 634 | const int il = r / 8; |
| 635 | const int j = r % 8; |
| 636 | |
| 637 | const uint16_t * sc = (const uint16_t *) x[ib].scales; |
| 638 | iq1m_scale_t scale; |
| 639 | scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); |
| 640 | |
| 641 | const int ib16 = 2 * ib8 + il / 2; |
| 642 | const float d = (float) scale.f16 * (2 * ((sc[ib16 / 4] >> (3 * (ib16 % 4))) & 0x7) + 1); |
| 643 | |
| 644 | const uint8_t qh = x[ib].qh[2 * ib8 + il / 2]; |
| 645 | const float delta = (qh & (0x08 << (4 * (il % 2)))) ? (-1.f - IQ1M_DELTA) : (-1.f + IQ1M_DELTA); |
| 646 | |
| 647 | const uint16_t grid_id = x[ib].qs[4 * ib8 + il] | (((qh >> (4 * (il % 2))) & 7) << 8); |
| 648 | const uint32_t g = iq1s_grid_gpu[grid_id]; |
| 649 | const int8_t qv = (j < 4) ? ((g >> (8 * j)) & 0x0F) : ((g >> (8 * (j - 4) + 4)) & 0x0F); |
| 650 | |
| 651 | return d * (qv + delta); |
| 652 | }; |
| 653 | |
| 654 | v.x() = dequantize_one(iqs + 0); |
| 655 | v.y() = dequantize_one(iqs + 1); |
| 656 | #else |
| 657 | GGML_ABORT("IQ1_M dequantize not supported for QK_K != 256"); |
| 658 | #endif |
| 659 | } |
| 660 | |
| 661 | static __dpct_inline__ void dequantize_iq4_nl(const void *vx, const int64_t ib, |
| 662 | const int iqs, dfloat2 &v) { |
| 663 | const block_iq4_nl * x = (const block_iq4_nl *) vx; |
| 664 | const float d = (float) x[ib].d; |
| 665 | |
| 666 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 667 | if (idx < 16) { |
| 668 | return d * kvalues_iq4nl[x[ib].qs[idx] & 0xF]; |
| 669 | } |
| 670 | return d * kvalues_iq4nl[x[ib].qs[idx - 16] >> 4]; |
| 671 | }; |
| 672 | |
| 673 | v.x() = dequantize_one(iqs + 0); |
| 674 | v.y() = dequantize_one(iqs + 1); |
| 675 | } |
| 676 | |
| 677 | static __dpct_inline__ void dequantize_iq4_xs(const void *vx, const int64_t ib, |
| 678 | const int iqs, dfloat2 &v) { |
| 679 | #if QK_K == 256 |
| 680 | const block_iq4_xs * x = (const block_iq4_xs *) vx; |
| 681 | |
| 682 | auto dequantize_one = [&](const int idx) -> dfloat { |
| 683 | const int ib8 = idx / 32; |
| 684 | const int r = idx % 32; |
| 685 | const int byte_idx = (r < 16) ? r : (r - 16); |
| 686 | const uint8_t q = x[ib].qs[16 * ib8 + byte_idx]; |
| 687 | const uint8_t qv = (r < 16) ? (q & 0x0F) : (q >> 4); |
| 688 | |
| 689 | const float d = (float) x[ib].d * ((((x[ib].scales_l[ib8 / 2] >> (4 * (ib8 % 2))) & 0xf) | |
| 690 | (((x[ib].scales_h >> (2 * ib8)) & 3) << 4)) - 32); |
| 691 | return d * kvalues_iq4nl[qv]; |
| 692 | }; |
| 693 | |
| 694 | v.x() = dequantize_one(iqs + 0); |
| 695 | v.y() = dequantize_one(iqs + 1); |
| 696 | #else |
| 697 | GGML_ABORT("IQ4_XS dequantize not supported for QK_K != 256"); |
| 698 | #endif |
| 699 | } |
| 700 | |
| 701 | static __dpct_inline__ void dequantize_q5_0(const void *vx, const int64_t ib, |
| 702 | const int iqs, dfloat2 &v) { |
| 703 | const block_q5_0 * x = (const block_q5_0 *) vx; |
| 704 | |
| 705 | const dfloat d = x[ib].d; |
| 706 | |
| 707 | uint32_t qh; |
| 708 | memcpy(&qh, x[ib].qh, sizeof(qh)); |
| 709 | |
| 710 | const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10; |
| 711 | const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10; |
| 712 | |
| 713 | v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0); |
| 714 | v.y() = ((x[ib].qs[iqs] >> 4) | xh_1); |
| 715 | |
| 716 | #ifdef GGML_SYCL_F16 |
| 717 | // v = v - {16.0f, 16.0f}; |
| 718 | // v = v * {d, d}; |
| 719 | v.s0() = (v.s0() - 16.0f) * d; |
| 720 | v.s1() = (v.s1() - 16.0f) * d; |
| 721 | |
| 722 | #else |
| 723 | v.x() = (v.x() - 16.0f) * d; |
| 724 | v.y() = (v.y() - 16.0f) * d; |
| 725 | #endif // GGML_SYCL_F16 |
| 726 | } |
| 727 | |
| 728 | static __dpct_inline__ void dequantize_q5_1(const void *vx, const int64_t ib, |
| 729 | const int iqs, dfloat2 &v) { |
| 730 | const block_q5_1 * x = (const block_q5_1 *) vx; |
| 731 | |
| 732 | const dfloat d = x[ib].dm[0]; |
| 733 | const dfloat m = x[ib].dm[1]; |
| 734 | |
| 735 | uint32_t qh; |
| 736 | memcpy(&qh, x[ib].qh, sizeof(qh)); |
| 737 | |
| 738 | const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10; |
| 739 | const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10; |
| 740 | |
| 741 | v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0); |
| 742 | v.y() = ((x[ib].qs[iqs] >> 4) | xh_1); |
| 743 | |
| 744 | #ifdef GGML_SYCL_F16 |
| 745 | // v = v * {d, d}; |
| 746 | // v = v + {m, m}; |
| 747 | v.s0() = sycl::fma(v.s0(), d, m); |
| 748 | v.s1() = sycl::fma(v.s1(), d, m); |
| 749 | #else |
| 750 | v.x() = sycl::fma(v.x(), d, m); |
| 751 | v.y() = sycl::fma(v.y(), d, m); |
| 752 | #endif // GGML_SYCL_F16 |
| 753 | } |
| 754 | |
| 755 | static __dpct_inline__ void dequantize_q8_0_reorder(const void *d_ptr, const int64_t ib, const void *qs, |
| 756 | const int iqs, dfloat2 &v) { |
| 757 | const dfloat d = (const dfloat)*((const sycl::half*)d_ptr + ib); |
| 758 | |
| 759 | v.x() = ((const int8_t *)qs)[iqs + 0]; |
| 760 | v.y() = ((const int8_t *)qs)[iqs + 1]; |
| 761 | |
| 762 | #ifdef GGML_SYCL_F16 |
| 763 | v.s0() *= d; |
| 764 | v.s1() *= d; |
| 765 | #else |
| 766 | v.x() *= d; |
| 767 | v.y() *= d; |
| 768 | #endif // GGML_SYCL_F16 |
| 769 | } |
| 770 | |
| 771 | static __dpct_inline__ void dequantize_q8_0(const void *vx, const int64_t ib, |
| 772 | const int iqs, dfloat2 &v) { |
| 773 | const block_q8_0 * x = (const block_q8_0 *) vx; |
| 774 | |
| 775 | const dfloat d = x[ib].d; |
| 776 | |
| 777 | v.x() = x[ib].qs[iqs + 0]; |
| 778 | v.y() = x[ib].qs[iqs + 1]; |
| 779 | |
| 780 | #ifdef GGML_SYCL_F16 |
| 781 | // v = v * {d, d}; |
| 782 | v.s0() *= d; |
| 783 | v.s1() *= d; |
| 784 | #else |
| 785 | v.x() *= d; |
| 786 | v.y() *= d; |
| 787 | #endif // GGML_SYCL_F16 |
| 788 | } |
| 789 | |
| 790 | template<typename dst_t> |
| 791 | static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t nb32, |
| 792 | const sycl::nd_item<3> &item_ct1) { |
| 793 | |
| 794 | const int64_t i = item_ct1.get_group(2); |
| 795 | |
| 796 | // assume 32 threads |
| 797 | const int64_t tid = item_ct1.get_local_id(2); |
| 798 | const int64_t il = tid/8; |
| 799 | const int64_t ir = tid%8; |
| 800 | const int64_t ib = 8*i + ir; |
| 801 | if (ib >= nb32) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | dst_t * y = yy + 256*i + 32*ir + 4*il; |
| 806 | |
| 807 | const block_q4_0 * x = (const block_q4_0 *)vx + ib; |
| 808 | const float d = sycl::vec<sycl::half, 1>(x->d) |
| 809 | .convert<float, sycl::rounding_mode::automatic>()[0]; |
| 810 | const float dm = -8*d; |
| 811 | |
| 812 | const uint8_t * q = x->qs + 4*il; |
| 813 | |
| 814 | for (int l = 0; l < 4; ++l) { |
| 815 | y[l+ 0] = d * (q[l] & 0xF) + dm; |
| 816 | y[l+16] = d * (q[l] >> 4) + dm; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // four qs bytes per item, all inside one 32-element block: two vec<dst_t,4> stores. |
| 821 | // float math, same products as the scalar kernel; nibble order kept (lo at +0, hi at +16). |
| 822 | template<typename dst_t> |
| 823 | static void dequantize_block_q4_0_reorder_wide(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t k, |
| 824 | const sycl::id<1> idx) { |
| 825 | const int64_t i = (int64_t) idx[0]; |
| 826 | const int64_t ib = i / 4; |
| 827 | const int64_t iqs = 4 * (i % 4); |
| 828 | |
| 829 | const uint8_t * qs = (const uint8_t *) vx; |
| 830 | const sycl::half * d_ptr = (const sycl::half *) ((const uint8_t *) vx + k/2); |
| 831 | |
| 832 | const float d = float(d_ptr[ib]); |
| 833 | const uint8_t * q = qs + ib*(QK4_0/2) + iqs; |
| 834 | |
| 835 | sycl::vec<float, 4> lo, hi; |
| 836 | lo[0] = d * ((q[0] & 0xF) - 8); |
| 837 | lo[1] = d * ((q[1] & 0xF) - 8); |
| 838 | lo[2] = d * ((q[2] & 0xF) - 8); |
| 839 | lo[3] = d * ((q[3] & 0xF) - 8); |
| 840 | hi[0] = d * ((q[0] >> 4) - 8); |
| 841 | hi[1] = d * ((q[1] >> 4) - 8); |
| 842 | hi[2] = d * ((q[2] >> 4) - 8); |
| 843 | hi[3] = d * ((q[3] >> 4) - 8); |
| 844 | |
| 845 | dst_t * yb = yy + ib*QK4_0 + iqs; |
| 846 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb) = lo.convert<dst_t, sycl::rounding_mode::automatic>(); |
| 847 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb + QK4_0/2) = hi.convert<dst_t, sycl::rounding_mode::automatic>(); |
| 848 | } |
| 849 | |
| 850 | // Dequantize Q8_0 from reorder layout: [all qs (k bytes)][all d values] |
| 851 | template<typename dst_t> |
| 852 | static void dequantize_block_q8_0_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t k, |
| 853 | const sycl::nd_item<3> &item_ct1) { |
| 854 | // two values per thread, like dequantize_block<dequantize_q8_0>; the reorder layout keeps all qs contiguous |
| 855 | // grid-stride loop because the launcher may shrink the work-group to keep the global range below INT_MAX |
| 856 | // float math, not dequantize_q8_0_reorder: that helper computes in half with GGML_SYCL_F16, which changes f32 output |
| 857 | const int8_t * qs = (const int8_t *) vx; |
| 858 | const sycl::half * d_ptr = (const sycl::half *) ((const uint8_t *) vx + k); |
| 859 | const int64_t stride = 2 * item_ct1.get_local_range(2) * item_ct1.get_group_range(2); |
| 860 | |
| 861 | for (int64_t i = 2 * item_ct1.get_global_id(2); i < k; i += stride) { |
| 862 | const float d = float(d_ptr[i / QK8_0]); |
| 863 | |
| 864 | yy[i + 0] = d * qs[i + 0]; |
| 865 | yy[i + 1] = d * qs[i + 1]; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | template<typename dst_t> |
| 870 | static void dequantize_block_q8_0_reorder_wide(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t k, |
| 871 | const sycl::id<1> idx) { |
| 872 | const int64_t i = 4 * (int64_t) idx[0]; |
| 873 | |
| 874 | const int8_t * qs = (const int8_t *) vx; |
| 875 | const sycl::half * d_ptr = (const sycl::half *) ((const uint8_t *) vx + k); |
| 876 | |
| 877 | const float d0 = float(d_ptr[i / QK8_0]); |
| 878 | const float d1 = float(d_ptr[(i + 2) / QK8_0]); |
| 879 | |
| 880 | sycl::vec<float, 4> p; |
| 881 | p[0] = d0 * (float) qs[i + 0]; |
| 882 | p[1] = d0 * (float) qs[i + 1]; |
| 883 | p[2] = d1 * (float) qs[i + 2]; |
| 884 | p[3] = d1 * (float) qs[i + 3]; |
| 885 | |
| 886 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yy + i) = p.convert<dst_t, sycl::rounding_mode::automatic>(); |
| 887 | } |
| 888 | |
| 889 | template<typename dst_t> |
| 890 | static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t nb32, |
| 891 | const sycl::nd_item<3> &item_ct1) { |
| 892 | |
| 893 | const int64_t i = item_ct1.get_group(2); |
| 894 | |
| 895 | // assume 32 threads |
| 896 | const int64_t tid = item_ct1.get_local_id(2); |
| 897 | const int64_t il = tid/8; |
| 898 | const int64_t ir = tid%8; |
| 899 | const int64_t ib = 8*i + ir; |
| 900 | if (ib >= nb32) { |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | dst_t * y = yy + 256*i + 32*ir + 4*il; |
| 905 | |
| 906 | const block_q4_1 * x = (const block_q4_1 *)vx + ib; |
| 907 | const sycl::float2 d = |
| 908 | x->dm.convert<float, sycl::rounding_mode::automatic>(); |
| 909 | |
| 910 | const uint8_t * q = x->qs + 4*il; |
| 911 | |
| 912 | for (int l = 0; l < 4; ++l) { |
| 913 | y[l + 0] = d.x() * (q[l] & 0xF) + d.y(); |
| 914 | y[l + 16] = d.x() * (q[l] >> 4) + d.y(); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | |
| 919 | //================================== k-quants |
| 920 | |
| 921 | template<typename dst_t> |
| 922 | static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 923 | const sycl::nd_item<3> &item_ct1) { |
| 924 | |
| 925 | const int64_t i = item_ct1.get_group(2); |
| 926 | const block_q2_K * x = (const block_q2_K *) vx; |
| 927 | |
| 928 | const int64_t tid = item_ct1.get_local_id(2); |
| 929 | #if QK_K == 256 |
| 930 | const int64_t n = tid/32; |
| 931 | const int64_t l = tid - 32*n; |
| 932 | const int64_t is = 8*n + l/16; |
| 933 | |
| 934 | const uint8_t q = x[i].qs[32*n + l]; |
| 935 | dst_t * y = yy + i*QK_K + 128*n; |
| 936 | |
| 937 | float dall = x[i].dm[0]; |
| 938 | float dmin = x[i].dm[1]; |
| 939 | y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4); |
| 940 | y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4); |
| 941 | y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4); |
| 942 | y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4); |
| 943 | #else |
| 944 | const int64_t is = tid/16; // 0 or 1 |
| 945 | const int64_t il = tid%16; // 0...15 |
| 946 | const uint8_t q = x[i].qs[il] >> (2*is); |
| 947 | dst_t * y = yy + i*QK_K + 16*is + il; |
| 948 | |
| 949 | float dall = x[i].dm[0]; |
| 950 | float dmin = x[i].dm[1]; |
| 951 | y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4); |
| 952 | y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4); |
| 953 | #endif |
| 954 | |
| 955 | } |
| 956 | |
| 957 | template<typename dst_t> |
| 958 | static void dequantize_block_q2_K_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t i, |
| 959 | const sycl::nd_item<3> & item_ct1, int64_t n_blocks) { |
| 960 | #if QK_K == 256 |
| 961 | if (i >= n_blocks) { |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 966 | const size_t qs_offset = i * (QK_K / 4); |
| 967 | const size_t scales_offset = n_blocks * (QK_K / 4) + i * (QK_K / 16); |
| 968 | const size_t dm_offset = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 16) + i * sizeof(ggml_half2); |
| 969 | |
| 970 | const uint8_t * qs = base + qs_offset; |
| 971 | const uint8_t * scales = base + scales_offset; |
| 972 | const ggml_half2 * dm = reinterpret_cast<const ggml_half2 *>(base + dm_offset); |
| 973 | |
| 974 | const int64_t tid = item_ct1.get_local_id(2); |
| 975 | const int64_t n = tid / 32; // 128-element chunk: 0 or 1 |
| 976 | const int64_t eh0 = 4 * (tid - 32 * n); // base element within the chunk |
| 977 | |
| 978 | const int shift = 2 * (eh0 / 32); // 2-bit lane: 0/2/4/6 |
| 979 | const uint8_t * q4 = qs + 32 * n + (eh0 & 31); |
| 980 | const uint8_t sc0 = scales[8 * n + eh0 / 16]; |
| 981 | |
| 982 | const float dall = (*dm)[0]; |
| 983 | const float dmin = (*dm)[1]; |
| 984 | |
| 985 | // remapped decomposition: one aligned vec<dst_t,4> per thread instead of four |
| 986 | // scalars strided 32 apart; a 4-run shares one scale byte and one 2-bit lane, |
| 987 | // so the per-element math and output are identical to the scalar kernel. |
| 988 | sycl::vec<dst_t, 4> v; |
| 989 | #pragma unroll |
| 990 | for (int j = 0; j < 4; ++j) { |
| 991 | v[j] = dall * (sc0 & 0xF) * ((q4[j] >> shift) & 3) - dmin * (sc0 >> 4); |
| 992 | } |
| 993 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yy + 128 * n + eh0) = v; |
| 994 | #else |
| 995 | GGML_UNUSED(vx); |
| 996 | GGML_UNUSED(yy); |
| 997 | GGML_UNUSED(i); |
| 998 | GGML_UNUSED(item_ct1); |
| 999 | GGML_UNUSED(n_blocks); |
| 1000 | GGML_ABORT("Q2_K reorder dequantize not supported for QK_K != 256"); |
| 1001 | #endif |
| 1002 | } |
| 1003 | |
| 1004 | template<typename dst_t> |
| 1005 | static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1006 | const sycl::nd_item<3> &item_ct1) { |
| 1007 | |
| 1008 | const int64_t i = item_ct1.get_group(2); |
| 1009 | const block_q3_K * x = (const block_q3_K *) vx; |
| 1010 | |
| 1011 | #if QK_K == 256 |
| 1012 | const int64_t r = item_ct1.get_local_id(2) / 4; |
| 1013 | const int64_t tid = r/2; |
| 1014 | const int64_t is0 = r%2; |
| 1015 | const int64_t l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4); |
| 1016 | const int64_t n = tid / 4; |
| 1017 | const int64_t j = tid - 4*n; |
| 1018 | |
| 1019 | uint8_t m = 1 << (4*n + j); |
| 1020 | int64_t is = 8*n + 2*j + is0; |
| 1021 | int shift = 2*j; |
| 1022 | |
| 1023 | int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) : |
| 1024 | is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) : |
| 1025 | is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) : |
| 1026 | (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4); |
| 1027 | float d_all = x[i].d; |
| 1028 | float dl = d_all * (us - 32); |
| 1029 | |
| 1030 | dst_t * y = yy + i*QK_K + 128*n + 32*j; |
| 1031 | const uint8_t * q = x[i].qs + 32*n; |
| 1032 | const uint8_t * hm = x[i].hmask; |
| 1033 | |
| 1034 | for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4)); |
| 1035 | #else |
| 1036 | const int64_t tid = item_ct1.get_local_id(2); |
| 1037 | const int64_t is = tid/16; // 0 or 1 |
| 1038 | const int64_t il = tid%16; // 0...15 |
| 1039 | const int64_t im = il/8; // 0...1 |
| 1040 | const int64_t in = il%8; // 0...7 |
| 1041 | |
| 1042 | dst_t * y = yy + i*QK_K + 16*is + il; |
| 1043 | |
| 1044 | const uint8_t q = x[i].qs[il] >> (2*is); |
| 1045 | const uint8_t h = x[i].hmask[in] >> (2*is + im); |
| 1046 | const float d = (float)x[i].d; |
| 1047 | |
| 1048 | if (is == 0) { |
| 1049 | y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4)); |
| 1050 | y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4)); |
| 1051 | } else { |
| 1052 | y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4)); |
| 1053 | y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4)); |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
| 1057 | } |
| 1058 | |
| 1059 | template<typename dst_t> |
| 1060 | static void dequantize_block_q3_K_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t i, |
| 1061 | const sycl::nd_item<3> & item_ct1, int64_t n_blocks) { |
| 1062 | #if QK_K == 256 |
| 1063 | if (i >= n_blocks) { |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 1068 | const size_t qs_offset = i * (QK_K / 4); |
| 1069 | const size_t hmask_offset = n_blocks * (QK_K / 4) + i * (QK_K / 8); |
| 1070 | const size_t scales_offset = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 8) + i * 12; |
| 1071 | const size_t d_offset = n_blocks * (QK_K / 4) + n_blocks * (QK_K / 8) + n_blocks * 12 + |
| 1072 | i * sizeof(ggml_half); |
| 1073 | |
| 1074 | const uint8_t * qs = base + qs_offset; |
| 1075 | const uint8_t * hmask = base + hmask_offset; |
| 1076 | const uint8_t * scales = base + scales_offset; |
| 1077 | const float d_all = static_cast<float>(*reinterpret_cast<const ggml_half *>(base + d_offset)); |
| 1078 | |
| 1079 | const int64_t r = item_ct1.get_local_id(2) / 4; |
| 1080 | const int64_t tid = r / 2; |
| 1081 | const int64_t is0 = r % 2; |
| 1082 | const int64_t l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4); |
| 1083 | const int64_t n = tid / 4; |
| 1084 | const int64_t j = tid - 4 * n; |
| 1085 | const int64_t is = 8 * n + 2 * j + is0; |
| 1086 | const int shift = 2 * j; |
| 1087 | uint8_t m = 1 << (4 * n + j); |
| 1088 | |
| 1089 | uint8_t us = is < 4 |
| 1090 | ? (scales[is - 0] & 0xF) | (((scales[is + 8] >> 0) & 3) << 4) |
| 1091 | : is < 8 |
| 1092 | ? (scales[is - 0] & 0xF) | (((scales[is + 4] >> 2) & 3) << 4) |
| 1093 | : is < 12 |
| 1094 | ? (scales[is - 8] >> 4) | (((scales[is + 0] >> 4) & 3) << 4) |
| 1095 | : (scales[is - 8] >> 4) | (((scales[is - 4] >> 6) & 3) << 4); |
| 1096 | |
| 1097 | const float dl = d_all * (us - 32); |
| 1098 | |
| 1099 | dst_t * y = yy + 128 * n + 32 * j; |
| 1100 | const uint8_t * q = qs + 32 * n; |
| 1101 | const uint8_t * hm = hmask; |
| 1102 | |
| 1103 | sycl::vec<dst_t, 4> v; |
| 1104 | #pragma unroll |
| 1105 | for (int jj = 0; jj < 4; ++jj) { |
| 1106 | v[jj] = dl * ((int8_t) ((q[l0 + jj] >> shift) & 3) - ((hm[l0 + jj] & m) ? 0 : 4)); |
| 1107 | } |
| 1108 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + l0) = v; |
| 1109 | #else |
| 1110 | GGML_UNUSED(vx); |
| 1111 | GGML_UNUSED(yy); |
| 1112 | GGML_UNUSED(i); |
| 1113 | GGML_UNUSED(item_ct1); |
| 1114 | GGML_UNUSED(n_blocks); |
| 1115 | GGML_ABORT("Q3_K reorder dequantize not supported for QK_K != 256"); |
| 1116 | #endif |
| 1117 | } |
| 1118 | |
| 1119 | #if QK_K == 256 |
| 1120 | static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) { |
| 1121 | if (j < 4) { |
| 1122 | d = q[j] & 63; |
| 1123 | m = q[j + 4] & 63; |
| 1124 | } else { |
| 1125 | d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4); |
| 1126 | m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4); |
| 1127 | } |
| 1128 | } |
| 1129 | #endif |
| 1130 | |
| 1131 | // Work-items that cover one 256-weight K-quant block: four 64-element chunks, 8 work-items |
| 1132 | // per chunk, 4 qs bytes per work-item. |
| 1133 | static constexpr int SYCL_DEQK_WI_PER_BLOCK = 32; |
| 1134 | |
| 1135 | template <typename dst_t> |
| 1136 | static inline void dequantize_q4_K_wide_one(dst_t * __restrict__ y, const uint8_t * __restrict__ qs, |
| 1137 | const uint8_t * __restrict__ scales, const float dall, |
| 1138 | const float dmin, const int tid) { |
| 1139 | const int il = tid >> 3; // 64-element chunk, 0..3 |
| 1140 | const int ir = (tid & 7) * 4; // 4 qs bytes within the chunk |
| 1141 | |
| 1142 | uint8_t sc, m; |
| 1143 | get_scale_min_k4(2 * il + 0, scales, sc, m); |
| 1144 | const float d1 = dall * sc; |
| 1145 | const float m1 = dmin * m; |
| 1146 | get_scale_min_k4(2 * il + 1, scales, sc, m); |
| 1147 | const float d2 = dall * sc; |
| 1148 | const float m2 = dmin * m; |
| 1149 | |
| 1150 | const sycl::vec<uint8_t, 4> q = vec_aligned_load<uint8_t, 4>(qs + 32 * il + ir); |
| 1151 | |
| 1152 | sycl::vec<dst_t, 4> lo, hi; |
| 1153 | #pragma unroll |
| 1154 | for (int l = 0; l < 4; ++l) { |
| 1155 | lo[l] = d1 * (q[l] & 0xF) - m1; |
| 1156 | hi[l] = d2 * (q[l] >> 4) - m2; |
| 1157 | } |
| 1158 | dst_t * yb = y + 64 * il + ir; |
| 1159 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb + 0) = lo; |
| 1160 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb + 32) = hi; |
| 1161 | } |
| 1162 | |
| 1163 | template <typename dst_t> |
| 1164 | static void dequantize_block_q4_K_wide(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t nb, |
| 1165 | const sycl::nd_item<1> & it) { |
| 1166 | static_assert(QK_K == 256, "wide K-quant dequant assumes QK_K == 256"); |
| 1167 | const int64_t g = it.get_global_id(0); |
| 1168 | const int64_t b = g / SYCL_DEQK_WI_PER_BLOCK; |
| 1169 | const int tid = (int) (g % SYCL_DEQK_WI_PER_BLOCK); |
| 1170 | if (b >= nb) { |
| 1171 | return; |
| 1172 | } |
| 1173 | const block_q4_K * x = (const block_q4_K *) vx + b; |
| 1174 | dequantize_q4_K_wide_one(yy + b * QK_K, x->qs, x->scales, (float) x->dm[0], (float) x->dm[1], tid); |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | template <typename dst_t> |
| 1179 | inline void dequantize_q4_K_common(dst_t * __restrict__ y, const uint8_t * __restrict__ qs_ptr, const float dall, |
| 1180 | const float dmin, uint8_t * __restrict__ scales_local, int il, int ir) { |
| 1181 | const int is = 2 * il; |
| 1182 | constexpr int n = 4; |
| 1183 | |
| 1184 | uint8_t sc, m; |
| 1185 | get_scale_min_k4(is + 0, scales_local, sc, m); |
| 1186 | const float d1 = dall * sc; |
| 1187 | const float m1 = dmin * m; |
| 1188 | |
| 1189 | get_scale_min_k4(is + 1, scales_local, sc, m); |
| 1190 | const float d2 = dall * sc; |
| 1191 | const float m2 = dmin * m; |
| 1192 | |
| 1193 | sycl::vec<uint8_t, n> q_vec = vec_aligned_load<uint8_t, n>(qs_ptr + 32 * il + n * ir); |
| 1194 | |
| 1195 | sycl::vec<dst_t, n> lo, hi; |
| 1196 | for (int l = 0; l < n; ++l) { |
| 1197 | lo[l] = d1 * (q_vec[l] & 0xF) - m1; |
| 1198 | hi[l] = d2 * (q_vec[l] >> 4) - m2; |
| 1199 | } |
| 1200 | *reinterpret_cast<sycl::vec<dst_t, n> *>(y + 0) = lo; |
| 1201 | *reinterpret_cast<sycl::vec<dst_t, n> *>(y + 32) = hi; |
| 1202 | } |
| 1203 | |
| 1204 | template <typename dst_t> |
| 1205 | static void dequantize_block_q4_K_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t i, |
| 1206 | uint8_t * scales_local, const sycl::nd_item<1> & item_ct1, int64_t nb) { |
| 1207 | const int64_t tid = item_ct1.get_local_id(0); // thread index within block |
| 1208 | const int64_t il = tid / 8; |
| 1209 | const int64_t ir = tid % 8; |
| 1210 | |
| 1211 | dst_t * y = yy + 64 * il + 4 * ir; |
| 1212 | |
| 1213 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 1214 | const size_t qs_offset = i * (QK_K / 2); |
| 1215 | const size_t scales_offset = nb * (QK_K / 2) + i * K_SCALE_SIZE; |
| 1216 | const size_t dm_offset = nb * (QK_K / 2) + nb * K_SCALE_SIZE + i * sizeof(ggml_half2); |
| 1217 | |
| 1218 | const uint8_t * qs_ptr = base + qs_offset; |
| 1219 | const uint8_t * scales_ptr = base + scales_offset; |
| 1220 | ggml_half2 dm_values = *reinterpret_cast<const ggml_half2 *>(base + dm_offset); |
| 1221 | |
| 1222 | const float dall = dm_values.x(); |
| 1223 | const float dmin = dm_values.y(); |
| 1224 | |
| 1225 | if (tid < 12) { |
| 1226 | scales_local[tid] = scales_ptr[tid]; |
| 1227 | } |
| 1228 | |
| 1229 | item_ct1.barrier(sycl::access::fence_space::local_space); |
| 1230 | dequantize_q4_K_common(y, qs_ptr, dall, dmin, scales_local, il, ir); |
| 1231 | } |
| 1232 | |
| 1233 | template <typename dst_t> |
| 1234 | static inline void dequantize_q5_K_wide_one(dst_t * __restrict__ y, const uint8_t * __restrict__ qs, |
| 1235 | const uint8_t * __restrict__ qh_ptr, |
| 1236 | const uint8_t * __restrict__ scales, const float dall, |
| 1237 | const float dmin, const int tid) { |
| 1238 | const int il = tid >> 3; // 64-element chunk, 0..3 |
| 1239 | const int ir = (tid & 7) * 4; // 4 qs bytes within the chunk; qh holds the 5th bit |
| 1240 | |
| 1241 | uint8_t sc, m; |
| 1242 | get_scale_min_k4(2 * il + 0, scales, sc, m); |
| 1243 | const float d1 = dall * sc; |
| 1244 | const float m1 = dmin * m; |
| 1245 | get_scale_min_k4(2 * il + 1, scales, sc, m); |
| 1246 | const float d2 = dall * sc; |
| 1247 | const float m2 = dmin * m; |
| 1248 | |
| 1249 | const sycl::vec<uint8_t, 4> q = vec_aligned_load<uint8_t, 4>(qs + 32 * il + ir); |
| 1250 | const sycl::vec<uint8_t, 4> h = vec_aligned_load<uint8_t, 4>(qh_ptr + ir); |
| 1251 | |
| 1252 | const uint8_t hm1 = 1 << (2 * il); |
| 1253 | const uint8_t hm2 = hm1 << 1; |
| 1254 | |
| 1255 | sycl::vec<dst_t, 4> lo, hi; |
| 1256 | #pragma unroll |
| 1257 | for (int l = 0; l < 4; ++l) { |
| 1258 | lo[l] = d1 * ((q[l] & 0xF) + ((h[l] & hm1) ? 16 : 0)) - m1; |
| 1259 | hi[l] = d2 * ((q[l] >> 4) + ((h[l] & hm2) ? 16 : 0)) - m2; |
| 1260 | } |
| 1261 | dst_t * yb = y + 64 * il + ir; |
| 1262 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb + 0) = lo; |
| 1263 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yb + 32) = hi; |
| 1264 | } |
| 1265 | |
| 1266 | template <typename dst_t> |
| 1267 | static void dequantize_block_q5_K_wide(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t nb, |
| 1268 | const sycl::nd_item<1> & it) { |
| 1269 | static_assert(QK_K == 256, "wide K-quant dequant assumes QK_K == 256"); |
| 1270 | const int64_t g = it.get_global_id(0); |
| 1271 | const int64_t b = g / SYCL_DEQK_WI_PER_BLOCK; |
| 1272 | const int tid = (int) (g % SYCL_DEQK_WI_PER_BLOCK); |
| 1273 | if (b >= nb) { |
| 1274 | return; |
| 1275 | } |
| 1276 | const block_q5_K * x = (const block_q5_K *) vx + b; |
| 1277 | dequantize_q5_K_wide_one(yy + b * QK_K, x->qs, x->qh, x->scales, (float) x->dm[0], (float) x->dm[1], tid); |
| 1278 | } |
| 1279 | |
| 1280 | template <typename dst_t> |
| 1281 | static void dequantize_block_q5_K_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t ib, |
| 1282 | uint8_t * scales_local, const sycl::nd_item<3> & item_ct1, int64_t n_blocks) { |
| 1283 | |
| 1284 | #if QK_K == 256 |
| 1285 | // remapped decomposition: 32 threads, each emitting two aligned vec<dst_t,4> |
| 1286 | // from four consecutive ql/qh bytes; a 4-element run shares one scale pair, |
| 1287 | // so the per-element math and output are identical to the 64-thread kernel. |
| 1288 | const int64_t tid = item_ct1.get_local_id(2); |
| 1289 | const int64_t il = tid / 8; // 0...3 |
| 1290 | const int64_t ir = tid % 8; // 0...7 |
| 1291 | const int64_t is = 2 * il; |
| 1292 | |
| 1293 | dst_t * y = yy + 64 * il + 4 * ir; |
| 1294 | |
| 1295 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 1296 | |
| 1297 | // Reordered layout: [qs (QK_K/2 per block)] [qh (QK_K/8 per block)] [scales (K_SCALE_SIZE per block)] [dm (half2 per block)] |
| 1298 | const size_t qs_offset = ib * (QK_K / 2); |
| 1299 | const size_t qh_offset = n_blocks * (QK_K / 2) + ib * (QK_K / 8); |
| 1300 | const size_t scales_offset = n_blocks * (QK_K / 2) + n_blocks * (QK_K / 8) + ib * K_SCALE_SIZE; |
| 1301 | const size_t dm_offset = n_blocks * (QK_K / 2) + n_blocks * (QK_K / 8) + n_blocks * K_SCALE_SIZE + ib * sizeof(ggml_half2); |
| 1302 | |
| 1303 | const uint8_t * qs_ptr = base + qs_offset; |
| 1304 | const uint8_t * qh_ptr = base + qh_offset; |
| 1305 | const uint8_t * scales_ptr = base + scales_offset; |
| 1306 | const ggml_half2 dm_values = *reinterpret_cast<const ggml_half2 *>(base + dm_offset); |
| 1307 | |
| 1308 | const float dall = dm_values.x(); |
| 1309 | const float dmin = dm_values.y(); |
| 1310 | |
| 1311 | const uint8_t * ql = qs_ptr + 32 * il + 4 * ir; // four consecutive bytes |
| 1312 | const uint8_t * qh = qh_ptr + 4 * ir; // four consecutive bytes |
| 1313 | |
| 1314 | if (tid < K_SCALE_SIZE) { |
| 1315 | scales_local[tid] = scales_ptr[tid]; |
| 1316 | } |
| 1317 | |
| 1318 | item_ct1.barrier(sycl::access::fence_space::local_space); |
| 1319 | |
| 1320 | uint8_t sc, m; |
| 1321 | get_scale_min_k4(is + 0, scales_local, sc, m); |
| 1322 | const float d1 = dall * sc; const float m1 = dmin * m; |
| 1323 | get_scale_min_k4(is + 1, scales_local, sc, m); |
| 1324 | const float d2 = dall * sc; const float m2 = dmin * m; |
| 1325 | |
| 1326 | const uint8_t hm_lo = 1 << (2 * il); |
| 1327 | const uint8_t hm_hi = 1 << (2 * il + 1); |
| 1328 | |
| 1329 | sycl::vec<dst_t, 4> lo, hi; |
| 1330 | #pragma unroll |
| 1331 | for (int j = 0; j < 4; ++j) { |
| 1332 | lo[j] = d1 * ((ql[j] & 0xF) + (qh[j] & hm_lo ? 16 : 0)) - m1; |
| 1333 | hi[j] = d2 * ((ql[j] >> 4) + (qh[j] & hm_hi ? 16 : 0)) - m2; |
| 1334 | } |
| 1335 | |
| 1336 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 0) = lo; |
| 1337 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 32) = hi; |
| 1338 | #else |
| 1339 | GGML_UNUSED(ib); GGML_UNUSED(tid); GGML_UNUSED(yy); GGML_UNUSED(scales_local); GGML_UNUSED(n_blocks); |
| 1340 | GGML_ABORT("Q5_K reorder dequantize not supported for QK_K != 256"); |
| 1341 | #endif |
| 1342 | } |
| 1343 | |
| 1344 | template<typename dst_t> |
| 1345 | static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1346 | const sycl::nd_item<3> &item_ct1) { |
| 1347 | const block_q6_K * x = (const block_q6_K *) vx; |
| 1348 | |
| 1349 | const int64_t i = item_ct1.get_group(2); |
| 1350 | #if QK_K == 256 |
| 1351 | |
| 1352 | // assume 64 threads - this is very slightly better than the one below |
| 1353 | const int64_t tid = item_ct1.get_local_id(2); |
| 1354 | const int64_t ip = tid/32; // ip is 0 or 1 |
| 1355 | const int64_t il = tid - 32*ip; // 0...32 |
| 1356 | const int64_t is = 8*ip + il/16; |
| 1357 | |
| 1358 | dst_t * y = yy + i*QK_K + 128*ip + il; |
| 1359 | |
| 1360 | const float d = x[i].d; |
| 1361 | |
| 1362 | const uint8_t * ql = x[i].ql + 64*ip + il; |
| 1363 | const uint8_t qh = x[i].qh[32*ip + il]; |
| 1364 | const int8_t * sc = x[i].scales + is; |
| 1365 | |
| 1366 | y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32); |
| 1367 | y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32); |
| 1368 | y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32); |
| 1369 | y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32); |
| 1370 | #else |
| 1371 | |
| 1372 | // assume 32 threads |
| 1373 | const int64_t tid = item_ct1.get_local_id(2); |
| 1374 | const int64_t ip = tid/16; // 0 or 1 |
| 1375 | const int64_t il = tid - 16*ip; // 0...15 |
| 1376 | |
| 1377 | dst_t * y = yy + i*QK_K + 16*ip + il; |
| 1378 | |
| 1379 | const float d = x[i].d; |
| 1380 | |
| 1381 | const uint8_t ql = x[i].ql[16*ip + il]; |
| 1382 | const uint8_t qh = x[i].qh[il] >> (2*ip); |
| 1383 | const int8_t * sc = x[i].scales; |
| 1384 | |
| 1385 | y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32); |
| 1386 | y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32); |
| 1387 | #endif |
| 1388 | } |
| 1389 | |
| 1390 | template <typename dst_t> |
| 1391 | static void dequantize_block_q6_K_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t ib, |
| 1392 | const sycl::nd_item<3> & item_ct1, int64_t n_blocks) { |
| 1393 | // remapped decomposition: each of the 64 threads emits one aligned vec<dst_t,4> |
| 1394 | // instead of four scalars strided 32 apart. The four elements of a run share one |
| 1395 | // 16-element scale group, one ql nibble source and one qh bit pair, so the |
| 1396 | // per-element math and the output are identical to the scalar kernel. |
| 1397 | const int64_t tid = item_ct1.get_local_id(2); |
| 1398 | const int64_t ip = tid / 32; // 128-element half: 0 or 1 |
| 1399 | const int64_t eh0 = 4 * (tid - 32 * ip); // base element within the half |
| 1400 | |
| 1401 | const uint8_t * base_ptr = static_cast<const uint8_t *>(vx); |
| 1402 | const auto ql_offset = ib * (QK_K / 2); |
| 1403 | const auto qh_offset = (QK_K / 2) * n_blocks + (QK_K / 4) * ib; |
| 1404 | const auto base_scales_offset = (QK_K / 2) * n_blocks + (QK_K / 4) * n_blocks + (QK_K / 16) * ib; |
| 1405 | const auto base_d_offset = ((QK_K / 2) + (QK_K / 4) + (QK_K / 16)) * n_blocks; |
| 1406 | const uint8_t * ql_ptr = base_ptr + ql_offset; |
| 1407 | const uint8_t * qh_ptr = base_ptr + qh_offset; |
| 1408 | const uint8_t * scales_ptr = base_ptr + base_scales_offset; |
| 1409 | const ggml_half * d = (const ggml_half *) (base_ptr + base_d_offset) + ib; |
| 1410 | |
| 1411 | const int shift = eh0 >= 64 ? 4 : 0; // low or high ql nibble |
| 1412 | const int qb = 2 * (eh0 / 32); // qh bit pair: 0/2/4/6 |
| 1413 | const int8_t sc0 = reinterpret_cast<const int8_t *>(scales_ptr + 8 * ip + eh0 / 16)[0]; |
| 1414 | const uint8_t * ql4 = ql_ptr + 64 * ip + (eh0 & 63); |
| 1415 | const uint8_t * qh4 = qh_ptr + 32 * ip + (eh0 & 31); |
| 1416 | |
| 1417 | sycl::vec<dst_t, 4> v; |
| 1418 | #pragma unroll |
| 1419 | for (int j = 0; j < 4; ++j) { |
| 1420 | v[j] = *d * sc0 * ((int8_t) (((ql4[j] >> shift) & 0xF) | ((((qh4[j] >> qb) & 3)) << 4)) - 32); |
| 1421 | } |
| 1422 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(yy + 128 * ip + eh0) = v; |
| 1423 | } |
| 1424 | |
| 1425 | template<typename dst_t> |
| 1426 | static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1427 | const sycl::nd_item<3> &item_ct1, |
| 1428 | const uint64_t *iq2xxs_grid_ptr, |
| 1429 | const uint8_t *ksigns_iq2xs_ptr, |
| 1430 | const uint8_t *kmask_iq2xs_ptr) { |
| 1431 | |
| 1432 | const int64_t i = item_ct1.get_group(2); |
| 1433 | const block_iq2_xxs * x = (const block_iq2_xxs *) vx; |
| 1434 | |
| 1435 | const int64_t tid = item_ct1.get_local_id(2); |
| 1436 | #if QK_K == 256 |
| 1437 | const int64_t il = tid/8; // 0...3 |
| 1438 | const int64_t ib = tid%8; // 0...7 |
| 1439 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1440 | const uint16_t * q2 = x[i].qs + 4*ib; |
| 1441 | const uint8_t * aux8 = (const uint8_t *)q2; |
| 1442 | const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]); |
| 1443 | const uint32_t aux32 = q2[2] | (q2[3] << 16); |
| 1444 | const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f; |
| 1445 | const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127]; |
| 1446 | for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f); |
| 1447 | #else |
| 1448 | assert(false); |
| 1449 | #endif |
| 1450 | |
| 1451 | } |
| 1452 | |
| 1453 | template<typename dst_t> |
| 1454 | static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1455 | const sycl::nd_item<3> &item_ct1, |
| 1456 | const uint64_t *iq2xs_grid, |
| 1457 | const uint8_t *ksigns_iq2xs, |
| 1458 | const uint8_t *kmask_iq2xs) { |
| 1459 | |
| 1460 | const int64_t i = item_ct1.get_group(2); |
| 1461 | const block_iq2_xs * x = (const block_iq2_xs *) vx; |
| 1462 | |
| 1463 | const int64_t tid = item_ct1.get_local_id(2); |
| 1464 | #if QK_K == 256 |
| 1465 | const int64_t il = tid/8; // 0...3 |
| 1466 | const int64_t ib = tid%8; // 0...7 |
| 1467 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1468 | const uint16_t * q2 = x[i].qs + 4*ib; |
| 1469 | const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511)); |
| 1470 | const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f; |
| 1471 | const uint8_t signs = ksigns_iq2xs[q2[il] >> 9]; |
| 1472 | for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f); |
| 1473 | #else |
| 1474 | assert(false); |
| 1475 | #endif |
| 1476 | |
| 1477 | } |
| 1478 | |
| 1479 | template <typename dst_t> |
| 1480 | __dpct_inline__ static void |
| 1481 | dequantize_block_iq2_s(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1482 | const sycl::nd_item<3> &item_ct1) { |
| 1483 | |
| 1484 | const int64_t i = item_ct1.get_group(2); |
| 1485 | const block_iq2_s * x = (const block_iq2_s *) vx; |
| 1486 | |
| 1487 | const int64_t tid = item_ct1.get_local_id(2); |
| 1488 | #if QK_K == 256 |
| 1489 | const int64_t il = tid/8; // 0...3 |
| 1490 | const int64_t ib = tid%8; // 0...7 |
| 1491 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1492 | const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300))); |
| 1493 | const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f; |
| 1494 | const uint8_t signs = x[i].qs[QK_K/8+4*ib+il]; |
| 1495 | #pragma unroll |
| 1496 | for (int j = 0; j < 8; ++j) |
| 1497 | y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f); |
| 1498 | #else |
| 1499 | assert(false); |
| 1500 | |
| 1501 | #endif |
| 1502 | |
| 1503 | } |
| 1504 | |
| 1505 | template<typename dst_t> |
| 1506 | static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1507 | const sycl::nd_item<3> &item_ct1, |
| 1508 | const uint32_t *iq3xxs_grid, |
| 1509 | const uint8_t *ksigns_iq2xs, |
| 1510 | const uint8_t *kmask_iq2xs) { |
| 1511 | |
| 1512 | const int64_t i = item_ct1.get_group(2); |
| 1513 | const block_iq3_xxs * x = (const block_iq3_xxs *) vx; |
| 1514 | |
| 1515 | const int64_t tid = item_ct1.get_local_id(2); |
| 1516 | #if QK_K == 256 |
| 1517 | const int64_t il = tid/8; // 0...3 |
| 1518 | const int64_t ib = tid%8; // 0...7 |
| 1519 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1520 | const uint8_t * q3 = x[i].qs + 8*ib; |
| 1521 | const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib; |
| 1522 | const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]); |
| 1523 | const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]); |
| 1524 | const uint32_t aux32 = gas[0] | (gas[1] << 16); |
| 1525 | const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f; |
| 1526 | const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127]; |
| 1527 | for (int j = 0; j < 4; ++j) { |
| 1528 | y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f); |
| 1529 | y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f); |
| 1530 | } |
| 1531 | #else |
| 1532 | assert(false); |
| 1533 | #endif |
| 1534 | |
| 1535 | } |
| 1536 | |
| 1537 | template<typename dst_t> |
| 1538 | static void dequantize_block_iq3_xxs_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t i, |
| 1539 | const sycl::nd_item<3> & item_ct1, const int64_t nb, |
| 1540 | const uint32_t * iq3xxs_grid, |
| 1541 | const uint8_t * ksigns_iq2xs, |
| 1542 | const uint8_t * kmask_iq2xs) { |
| 1543 | const int64_t tid = item_ct1.get_local_id(2); |
| 1544 | #if QK_K == 256 |
| 1545 | const int64_t il = tid / 8; |
| 1546 | const int64_t ib = tid % 8; |
| 1547 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 1548 | const uint8_t * qs = base + i * (3 * QK_K / 8); |
| 1549 | const ggml_half * d = reinterpret_cast<const ggml_half *>(base + nb * (3 * QK_K / 8)); |
| 1550 | const uint8_t * q3 = qs + 8 * ib; |
| 1551 | const uint16_t * gas = reinterpret_cast<const uint16_t *>(qs + QK_K / 4) + 2 * ib; |
| 1552 | const uint8_t * grid1 = reinterpret_cast<const uint8_t *>(iq3xxs_grid + q3[2*il+0]); |
| 1553 | const uint8_t * grid2 = reinterpret_cast<const uint8_t *>(iq3xxs_grid + q3[2*il+1]); |
| 1554 | const uint32_t aux32 = gas[0] | (gas[1] << 16); |
| 1555 | const float dl = (float) d[i] * (0.5f + (aux32 >> 28)) * 0.5f; |
| 1556 | const uint8_t signs = ksigns_iq2xs[(aux32 >> (7 * il)) & 127]; |
| 1557 | dst_t * y = yy + 32 * ib + 8 * il; |
| 1558 | sycl::vec<dst_t, 4> lo, hi; |
| 1559 | #pragma unroll |
| 1560 | for (int j = 0; j < 4; ++j) { |
| 1561 | lo[j] = dl * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f); |
| 1562 | hi[j] = dl * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f); |
| 1563 | } |
| 1564 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 0) = lo; |
| 1565 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 4) = hi; |
| 1566 | #else |
| 1567 | GGML_UNUSED(vx); |
| 1568 | GGML_UNUSED(yy); |
| 1569 | GGML_UNUSED(i); |
| 1570 | GGML_UNUSED(item_ct1); |
| 1571 | GGML_UNUSED(nb); |
| 1572 | GGML_UNUSED(iq3xxs_grid); |
| 1573 | GGML_UNUSED(ksigns_iq2xs); |
| 1574 | GGML_UNUSED(kmask_iq2xs); |
| 1575 | GGML_ABORT("IQ3_XXS reorder dequantization requires QK_K == 256"); |
| 1576 | #endif |
| 1577 | } |
| 1578 | |
| 1579 | template <typename dst_t> |
| 1580 | __dpct_inline__ static void |
| 1581 | dequantize_block_iq3_s(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1582 | const sycl::nd_item<3> &item_ct1, |
| 1583 | const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) { |
| 1584 | |
| 1585 | const int64_t i = item_ct1.get_group(2); |
| 1586 | const block_iq3_s * x = (const block_iq3_s *) vx; |
| 1587 | |
| 1588 | const int64_t tid = item_ct1.get_local_id(2); |
| 1589 | #if QK_K == 256 |
| 1590 | const int64_t il = tid/8; // 0...3 |
| 1591 | const int64_t ib = tid%8; // 0...7 |
| 1592 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1593 | const uint8_t * qs = x[i].qs + 8*ib; |
| 1594 | const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256))); |
| 1595 | const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256))); |
| 1596 | const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf)); |
| 1597 | const uint8_t signs = x[i].signs[4*ib + il]; |
| 1598 | #pragma unroll |
| 1599 | for (int j = 0; j < 4; ++j) { |
| 1600 | y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f); |
| 1601 | y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f); |
| 1602 | } |
| 1603 | #else |
| 1604 | assert(false); |
| 1605 | #endif |
| 1606 | |
| 1607 | } |
| 1608 | |
| 1609 | template <typename dst_t> |
| 1610 | __dpct_inline__ static void |
| 1611 | dequantize_block_iq3_s_reorder(const void * __restrict__ vx, dst_t * __restrict__ yy, const int64_t i, |
| 1612 | const sycl::nd_item<3> & item_ct1, const int64_t nb, |
| 1613 | const uint8_t * kmask_iq2xs, const uint32_t * iq3s_grid) { |
| 1614 | const int64_t tid = item_ct1.get_local_id(2); |
| 1615 | #if QK_K == 256 |
| 1616 | const int64_t il = tid / 8; |
| 1617 | const int64_t ib = tid % 8; |
| 1618 | const uint8_t * base = static_cast<const uint8_t *>(vx); |
| 1619 | const uint8_t * qs = base + i * (QK_K / 4); |
| 1620 | const uint8_t * qh = base + nb * (QK_K / 4) + i * (QK_K / 32); |
| 1621 | const uint8_t * signs_base = base + nb * (QK_K / 4 + QK_K / 32) + i * (QK_K / 8); |
| 1622 | const uint8_t * metadata = base + nb * (QK_K / 4 + QK_K / 32 + QK_K / 8) + |
| 1623 | i * (sizeof(ggml_half) + IQ3S_N_SCALE); |
| 1624 | const ggml_half d = *reinterpret_cast<const ggml_half *>(metadata); |
| 1625 | const uint8_t * scales = metadata + sizeof(ggml_half); |
| 1626 | const uint8_t * qs_ib = qs + 8 * ib; |
| 1627 | const uint8_t * grid1 = reinterpret_cast<const uint8_t *>( |
| 1628 | iq3s_grid + (qs_ib[2*il+0] | ((qh[ib] << (8 - 2*il)) & 256))); |
| 1629 | const uint8_t * grid2 = reinterpret_cast<const uint8_t *>( |
| 1630 | iq3s_grid + (qs_ib[2*il+1] | ((qh[ib] << (7 - 2*il)) & 256))); |
| 1631 | const float dl = (float) d * (1 + 2 * ((scales[ib/2] >> (4 * (ib%2))) & 0xf)); |
| 1632 | const uint8_t signs = signs_base[4 * ib + il]; |
| 1633 | dst_t * y = yy + 32 * ib + 8 * il; |
| 1634 | sycl::vec<dst_t, 4> lo, hi; |
| 1635 | #pragma unroll |
| 1636 | for (int j = 0; j < 4; ++j) { |
| 1637 | lo[j] = dl * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f); |
| 1638 | hi[j] = dl * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f); |
| 1639 | } |
| 1640 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 0) = lo; |
| 1641 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + 4) = hi; |
| 1642 | #else |
| 1643 | GGML_UNUSED(vx); |
| 1644 | GGML_UNUSED(yy); |
| 1645 | GGML_UNUSED(i); |
| 1646 | GGML_UNUSED(item_ct1); |
| 1647 | GGML_UNUSED(nb); |
| 1648 | GGML_UNUSED(kmask_iq2xs); |
| 1649 | GGML_UNUSED(iq3s_grid); |
| 1650 | GGML_ABORT("IQ3_S reorder dequantization requires QK_K == 256"); |
| 1651 | #endif |
| 1652 | } |
| 1653 | |
| 1654 | template <typename dst_t> |
| 1655 | __dpct_inline__ static void |
| 1656 | dequantize_block_iq1_s(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1657 | const sycl::nd_item<3> &item_ct1, |
| 1658 | const uint32_t *iq1s_grid_gpu) { |
| 1659 | |
| 1660 | const int64_t i = item_ct1.get_group(2); |
| 1661 | const block_iq1_s * x = (const block_iq1_s *) vx; |
| 1662 | |
| 1663 | const int64_t tid = item_ct1.get_local_id(2); |
| 1664 | #if QK_K == 256 |
| 1665 | const int64_t il = tid/8; // 0...3 |
| 1666 | const int64_t ib = tid%8; // 0...7 |
| 1667 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1668 | const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA; |
| 1669 | const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1); |
| 1670 | uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32; |
| 1671 | grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)]; |
| 1672 | grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f; |
| 1673 | grid32[0] &= 0x0f0f0f0f; |
| 1674 | #pragma unroll |
| 1675 | for (int j = 0; j < 8; ++j) { |
| 1676 | y[j] = d * (q[j] + delta); |
| 1677 | } |
| 1678 | #else |
| 1679 | assert(false); |
| 1680 | #endif |
| 1681 | |
| 1682 | } |
| 1683 | |
| 1684 | template <typename dst_t> |
| 1685 | __dpct_inline__ static void |
| 1686 | dequantize_block_iq1_m(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1687 | const sycl::nd_item<3> &item_ct1, |
| 1688 | const uint32_t *iq1s_grid_gpu) { |
| 1689 | |
| 1690 | const int64_t i = item_ct1.get_group(2); |
| 1691 | const block_iq1_m * x = (const block_iq1_m *) vx; |
| 1692 | |
| 1693 | const int64_t tid = item_ct1.get_local_id(2); |
| 1694 | #if QK_K == 256 |
| 1695 | const int64_t il = tid/8; // 0...3 |
| 1696 | const int64_t ib = tid%8; // 0...7 |
| 1697 | dst_t * y = yy + i*QK_K + 32*ib + 8*il; |
| 1698 | const uint16_t * sc = (const uint16_t *)x[i].scales; |
| 1699 | iq1m_scale_t scale; |
| 1700 | scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); |
| 1701 | const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4); |
| 1702 | const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1); |
| 1703 | const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA; |
| 1704 | uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32; |
| 1705 | grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)]; |
| 1706 | grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f; |
| 1707 | grid32[0] &= 0x0f0f0f0f; |
| 1708 | #pragma unroll |
| 1709 | for (int j = 0; j < 8; ++j) { |
| 1710 | y[j] = d * (q[j] + delta); |
| 1711 | } |
| 1712 | #else |
| 1713 | assert(false); |
| 1714 | #endif |
| 1715 | |
| 1716 | } |
| 1717 | |
| 1718 | template <typename dst_t> |
| 1719 | __dpct_inline__ static void |
| 1720 | dequantize_block_iq4_nl(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1721 | const sycl::nd_item<3> &item_ct1) { |
| 1722 | |
| 1723 | const int64_t i = item_ct1.get_group(2); |
| 1724 | const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL); |
| 1725 | |
| 1726 | const int64_t tid = item_ct1.get_local_id(2); |
| 1727 | const int64_t il = tid/8; // 0...3 |
| 1728 | const int64_t ib = tid%8; // 0...7 |
| 1729 | dst_t * y = yy + i*QK_K + 32*ib + 4*il; |
| 1730 | const uint8_t * q4 = x[ib].qs + 4*il; |
| 1731 | const float d = (float)x[ib].d; |
| 1732 | #pragma unroll |
| 1733 | for (int j = 0; j < 4; ++j) { |
| 1734 | y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf]; |
| 1735 | y[j+16] = d * kvalues_iq4nl[q4[j] >> 4]; |
| 1736 | } |
| 1737 | |
| 1738 | } |
| 1739 | |
| 1740 | |
| 1741 | template <typename dst_t> |
| 1742 | __dpct_inline__ static void |
| 1743 | dequantize_block_iq4_xs(const void *__restrict__ vx, dst_t *__restrict__ yy, |
| 1744 | const sycl::nd_item<3> &item_ct1) { |
| 1745 | const int64_t i = item_ct1.get_group(2); |
| 1746 | const block_iq4_xs * x = (const block_iq4_xs *)vx; |
| 1747 | |
| 1748 | const int64_t tid = item_ct1.get_local_id(2); |
| 1749 | const int64_t il = tid/8; // 0...3 |
| 1750 | const int64_t ib = tid%8; // 0...7 |
| 1751 | dst_t * y = yy + i*QK_K + 32*ib + 4*il; |
| 1752 | const uint8_t * q4 = x[i].qs + 16*ib + 4*il; |
| 1753 | const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32); |
| 1754 | #pragma unroll |
| 1755 | for (int j = 0; j < 4; ++j) { |
| 1756 | y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf]; |
| 1757 | y[j+16] = d * kvalues_iq4nl[q4[j] >> 4]; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | template<typename dst_t> |
| 1762 | static void dequantize_block_mxfp4(const void * __restrict__ vx, dst_t * __restrict__ yy, |
| 1763 | const sycl::nd_item<3> &item_ct1) { |
| 1764 | // auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 1765 | const int64_t i = item_ct1.get_group(2); |
| 1766 | const block_mxfp4 * x = (const block_mxfp4 *) vx + i*(QK_K/QK_MXFP4); |
| 1767 | |
| 1768 | const int64_t tid = item_ct1.get_local_id(2); |
| 1769 | const int64_t il = tid/8; // 0...3 |
| 1770 | const int64_t ib = tid%8; // 0...7 |
| 1771 | dst_t * y = yy + i*QK_K + 32*ib + 4*il; |
| 1772 | const uint8_t * q4 = x[ib].qs + 4*il; |
| 1773 | const float d = ggml_sycl_e8m0_to_fp32(x[ib].e); |
| 1774 | for (int j = 0; j < 4; ++j) { |
| 1775 | y[j+ 0] = d * kvalues_mxfp4[q4[j] & 0xf]*0.5f; |
| 1776 | y[j+16] = d * kvalues_mxfp4[q4[j] >> 4]*0.5f; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | // Reordered MXFP4 ([qs...][e...], see ggml_sycl_reordered::block_q_t<MXFP4>). |
| 1781 | // four qs bytes per item, all inside one 32-element block: two vec<dst_t,4> stores. |
| 1782 | // same LUT lookups and per-block scale; bit-identical output. |
| 1783 | template<typename dst_t> |
| 1784 | static void dequantize_block_mxfp4_reorder_wide(const void * __restrict__ vx, dst_t * __restrict__ yy, int64_t k, |
| 1785 | const sycl::id<1> idx) { |
| 1786 | const int64_t i = (int64_t) idx[0]; |
| 1787 | const int64_t ib = i / 4; |
| 1788 | const int64_t iq = 4 * (i % 4); // first of four qs bytes in the block |
| 1789 | |
| 1790 | const uint8_t * qs = (const uint8_t *) vx + ib * (QK_MXFP4 / 2); |
| 1791 | const float d = ggml_sycl_e8m0_to_fp32(((const uint8_t *) vx)[k / 2 + ib]) * 0.5f; |
| 1792 | dst_t * y = yy + ib * QK_MXFP4 + iq; |
| 1793 | |
| 1794 | sycl::vec<dst_t, 4> lo, hi; |
| 1795 | #pragma unroll |
| 1796 | for (int j = 0; j < 4; ++j) { |
| 1797 | lo[j] = d * kvalues_mxfp4[qs[iq + j] & 0xf]; |
| 1798 | hi[j] = d * kvalues_mxfp4[qs[iq + j] >> 4]; |
| 1799 | } |
| 1800 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y) = lo; |
| 1801 | *reinterpret_cast<sycl::vec<dst_t, 4> *>(y + QK_MXFP4 / 2) = hi; |
| 1802 | } |
| 1803 | |
| 1804 | |
| 1805 | template <typename dst_t> |
| 1806 | static void dequantize_block_nvfp4( |
| 1807 | const void * __restrict__ vx, |
| 1808 | dst_t * __restrict__ yy, |
| 1809 | const int64_t ne) { |
| 1810 | auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); |
| 1811 | const int64_t i = item_ct1.get_group(2); |
| 1812 | const int tid = item_ct1.get_local_id(2); |
| 1813 | |
| 1814 | const int64_t base = i * QK_NVFP4; |
| 1815 | if (base >= ne) { |
| 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | const block_nvfp4 * x = (const block_nvfp4 *) vx; |
| 1820 | const block_nvfp4 & xb = x[i]; |
| 1821 | |
| 1822 | const int sub = tid / (QK_NVFP4_SUB / 2); |
| 1823 | const int j = tid % (QK_NVFP4_SUB / 2); |
| 1824 | |
| 1825 | const float d = ggml_sycl_ue4m3_to_fp32(xb.d[sub]); |
| 1826 | const uint8_t q = xb.qs[sub * (QK_NVFP4_SUB / 2) + j]; |
| 1827 | |
| 1828 | const int64_t y0 = base + sub * QK_NVFP4_SUB + j; |
| 1829 | const int64_t y1 = y0 + QK_NVFP4_SUB / 2; |
| 1830 | |
| 1831 | yy[y0] = ggml_sycl_cast<dst_t>(d * kvalues_mxfp4[q & 0x0F]); |
| 1832 | yy[y1] = ggml_sycl_cast<dst_t>(d * kvalues_mxfp4[q >> 4]); |
| 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | #endif // GGML_SYCL_DEQUANTIZE_HPP |