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-metal/kernels/dequantize.h # 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-metal / kernels / dequantize.h` 739 lines ยท 28011 bytes Raw Blame History 1 #pragma once 2 3 #include "common.h" 4 5 #define GGML_COMMON_DECL_METAL 6 #define GGML_COMMON_IMPL_METAL 7 #if defined(GGML_METAL_EMBED_LIBRARY) 8 __embed_ggml-common.h__ 9 #else 10 #include "ggml-common.h" 11 #endif 12 13 #define QK_NL 16 // shared by mul_mm and get_rows_q instantiations 14 15 // NOTE: this is not dequantizing - we are simply fitting the template 16 template 17 void dequantize_f32(device const float4x4 * src, short il, thread type4x4 & reg) { 18 reg = (type4x4)(*src); 19 } 20 21 template 22 void dequantize_f32_t4(device const float4 * src, short il, thread type4 & reg) { 23 reg = (type4)(*src); 24 } 25 26 template 27 void dequantize_f16(device const half4x4 * src, short il, thread type4x4 & reg) { 28 reg = (type4x4)(*src); 29 } 30 31 template 32 void dequantize_f16_t4(device const half4 * src, short il, thread type4 & reg) { 33 reg = (type4)(*(src)); 34 } 35 36 #if defined(GGML_METAL_HAS_BF16) 37 template 38 void dequantize_bf16(device const bfloat4x4 * src, short il, thread type4x4 & reg) { 39 reg = (type4x4)(*src); 40 } 41 42 template 43 void dequantize_bf16_t4(device const bfloat4 * src, short il, thread type4 & reg) { 44 reg = (type4)(*(src)); 45 } 46 #endif 47 48 template 49 void dequantize_q1_0(device const block_q1_0 * xb, short il, thread type4x4 & reg) { 50 device const uint8_t * qs = xb->qs; 51 const float d = xb->d; 52 const float neg_d = -d; 53 54 const int byte_offset = il * 2; // il*16 bits = il*2 bytes 55 const uint8_t b0 = qs[byte_offset]; 56 const uint8_t b1 = qs[byte_offset + 1]; 57 58 float4x4 reg_f; 59 60 reg_f[0][0] = select(neg_d, d, bool(b0 & 0x01)); 61 reg_f[0][1] = select(neg_d, d, bool(b0 & 0x02)); 62 reg_f[0][2] = select(neg_d, d, bool(b0 & 0x04)); 63 reg_f[0][3] = select(neg_d, d, bool(b0 & 0x08)); 64 reg_f[1][0] = select(neg_d, d, bool(b0 & 0x10)); 65 reg_f[1][1] = select(neg_d, d, bool(b0 & 0x20)); 66 reg_f[1][2] = select(neg_d, d, bool(b0 & 0x40)); 67 reg_f[1][3] = select(neg_d, d, bool(b0 & 0x80)); 68 69 reg_f[2][0] = select(neg_d, d, bool(b1 & 0x01)); 70 reg_f[2][1] = select(neg_d, d, bool(b1 & 0x02)); 71 reg_f[2][2] = select(neg_d, d, bool(b1 & 0x04)); 72 reg_f[2][3] = select(neg_d, d, bool(b1 & 0x08)); 73 reg_f[3][0] = select(neg_d, d, bool(b1 & 0x10)); 74 reg_f[3][1] = select(neg_d, d, bool(b1 & 0x20)); 75 reg_f[3][2] = select(neg_d, d, bool(b1 & 0x40)); 76 reg_f[3][3] = select(neg_d, d, bool(b1 & 0x80)); 77 78 reg = (type4x4) reg_f; 79 } 80 81 template 82 void dequantize_q1_0_t4(device const block_q1_0 * xb, short il, thread type4 & reg) { 83 const float d = xb->d; 84 const float neg_d = -d; 85 const int base = il * 4; 86 const uint8_t byte = xb->qs[base / 8]; 87 const int s = base % 8; 88 89 float4 reg_f; 90 reg_f[0] = select(neg_d, d, bool((byte >> (s )) & 1)); 91 reg_f[1] = select(neg_d, d, bool((byte >> (s + 1)) & 1)); 92 reg_f[2] = select(neg_d, d, bool((byte >> (s + 2)) & 1)); 93 reg_f[3] = select(neg_d, d, bool((byte >> (s + 3)) & 1)); 94 95 reg = (type4) reg_f; 96 } 97 98 template 99 void dequantize_q2_0(device const block_q2_0 * xb, short il, thread type4x4 & reg) { 100 device const uint8_t * qs = xb->qs; 101 const float d = xb->d; 102 103 const int byte_offset = il * 4; // il*16 elements = il*4 bytes (4 elements per byte) 104 float4x4 reg_f; 105 106 for (int i = 0; i < 4; i++) { 107 const uint8_t b = qs[byte_offset + i]; 108 reg_f[i][0] = ((float)((b >> 0) & 3) - 1.0f) * d; 109 reg_f[i][1] = ((float)((b >> 2) & 3) - 1.0f) * d; 110 reg_f[i][2] = ((float)((b >> 4) & 3) - 1.0f) * d; 111 reg_f[i][3] = ((float)((b >> 6) & 3) - 1.0f) * d; 112 } 113 114 reg = (type4x4) reg_f; 115 } 116 117 template 118 void dequantize_q2_0_t4(device const block_q2_0 * xb, short il, thread type4 & reg) { 119 const float d = xb->d; 120 const uint8_t b = xb->qs[il]; 121 122 float4 reg_f; 123 reg_f[0] = ((float)((b >> 0) & 3) - 1.0f) * d; 124 reg_f[1] = ((float)((b >> 2) & 3) - 1.0f) * d; 125 reg_f[2] = ((float)((b >> 4) & 3) - 1.0f) * d; 126 reg_f[3] = ((float)((b >> 6) & 3) - 1.0f) * d; 127 128 reg = (type4) reg_f; 129 } 130 131 template 132 void dequantize_q4_0(device const block_q4_0 * xb, short il, thread type4x4 & reg) { 133 device const uint16_t * qs = ((device const uint16_t *)xb + 1); 134 const float d1 = il ? (xb->d / 16.h) : xb->d; 135 const float d2 = d1 / 256.f; 136 const float md = -8.h * xb->d; 137 const ushort mask0 = il ? 0x00F0 : 0x000F; 138 const ushort mask1 = mask0 << 8; 139 140 float4x4 reg_f; 141 142 for (int i = 0; i < 8; i++) { 143 reg_f[i/2][2*(i%2) + 0] = d1 * (qs[i] & mask0) + md; 144 reg_f[i/2][2*(i%2) + 1] = d2 * (qs[i] & mask1) + md; 145 } 146 147 reg = (type4x4) reg_f; 148 } 149 150 template 151 void dequantize_q4_0_t4(device const block_q4_0 * xb, short il, thread type4 & reg) { 152 device const uint16_t * qs = ((device const uint16_t *)xb + 1); 153 const float d1 = (il/4) ? (xb->d / 16.h) : xb->d; 154 const float d2 = d1 / 256.f; 155 const float md = -8.h * xb->d; 156 const ushort mask0 = (il/4) ? 0x00F0 : 0x000F; 157 const ushort mask1 = mask0 << 8; 158 159 for (int i = 0; i < 2; i++) { 160 reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + md; 161 reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + md; 162 } 163 } 164 165 166 167 template 168 void dequantize_q4_1(device const block_q4_1 * xb, short il, thread type4x4 & reg) { 169 device const uint16_t * qs = ((device const uint16_t *)xb + 2); 170 const float d1 = il ? (xb->d / 16.h) : xb->d; 171 const float d2 = d1 / 256.f; 172 const float m = xb->m; 173 const ushort mask0 = il ? 0x00F0 : 0x000F; 174 const ushort mask1 = mask0 << 8; 175 176 float4x4 reg_f; 177 178 for (int i = 0; i < 8; i++) { 179 reg_f[i/2][2*(i%2) + 0] = ((qs[i] & mask0) * d1) + m; 180 reg_f[i/2][2*(i%2) + 1] = ((qs[i] & mask1) * d2) + m; 181 } 182 183 reg = (type4x4) reg_f; 184 } 185 186 template 187 void dequantize_q4_1_t4(device const block_q4_1 * xb, short il, thread type4 & reg) { 188 device const uint16_t * qs = ((device const uint16_t *)xb + 2); 189 const float d1 = (il/4) ? (xb->d / 16.h) : xb->d; 190 const float d2 = d1 / 256.f; 191 const float m = xb->m; 192 const ushort mask0 = (il/4) ? 0x00F0 : 0x000F; 193 const ushort mask1 = mask0 << 8; 194 195 for (int i = 0; i < 2; i++) { 196 reg[2*i + 0] = d1 * (qs[2*(il%4) + i] & mask0) + m; 197 reg[2*i + 1] = d2 * (qs[2*(il%4) + i] & mask1) + m; 198 } 199 } 200 201 template 202 void dequantize_q5_0(device const block_q5_0 * xb, short il, thread type4x4 & reg) { 203 device const uint16_t * qs = ((device const uint16_t *)xb + 3); 204 const float d = xb->d; 205 const float md = -16.h * xb->d; 206 const ushort mask = il ? 0x00F0 : 0x000F; 207 208 const uint32_t qh = *((device const uint32_t *)xb->qh); 209 210 const int x_mv = il ? 4 : 0; 211 212 const int gh_mv = il ? 12 : 0; 213 const int gh_bk = il ? 0 : 4; 214 215 float4x4 reg_f; 216 217 for (int i = 0; i < 8; i++) { 218 // extract the 5-th bits for x0 and x1 219 const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10; 220 const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10; 221 222 // combine the 4-bits from qs with the 5th bit 223 const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0); 224 const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1); 225 226 reg_f[i/2][2*(i%2) + 0] = d * x0 + md; 227 reg_f[i/2][2*(i%2) + 1] = d * x1 + md; 228 } 229 230 reg = (type4x4) reg_f; 231 } 232 233 template 234 void dequantize_q5_0_t4(device const block_q5_0 * xb, short il, thread type4 & reg) { 235 device const uint16_t * qs = ((device const uint16_t *)xb + 3); 236 const float d = xb->d; 237 const float md = -16.h * xb->d; 238 const ushort mask = (il/4) ? 0x00F0 : 0x000F; 239 240 const uint32_t qh = *((device const uint32_t *)xb->qh); 241 242 const int x_mv = (il/4) ? 4 : 0; 243 244 const int gh_mv = (il/4) ? 12 : 0; 245 const int gh_bk = (il/4) ? 0 : 4; 246 247 for (int ii = 0; ii < 2; ii++) { 248 int i = 2*(il%4) + ii; 249 250 // extract the 5-th bits for x0 and x1 251 const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10; 252 const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10; 253 254 // combine the 4-bits from qs with the 5th bit 255 const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0); 256 const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1); 257 258 reg[2*ii + 0] = d * x0 + md; 259 reg[2*ii + 1] = d * x1 + md; 260 } 261 } 262 263 template 264 void dequantize_q5_1(device const block_q5_1 * xb, short il, thread type4x4 & reg) { 265 device const uint16_t * qs = ((device const uint16_t *)xb + 4); 266 const float d = xb->d; 267 const float m = xb->m; 268 const ushort mask = il ? 0x00F0 : 0x000F; 269 270 const uint32_t qh = *((device const uint32_t *)xb->qh); 271 272 const int x_mv = il ? 4 : 0; 273 274 const int gh_mv = il ? 12 : 0; 275 const int gh_bk = il ? 0 : 4; 276 277 float4x4 reg_f; 278 279 for (int i = 0; i < 8; i++) { 280 // extract the 5-th bits for x0 and x1 281 const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10; 282 const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10; 283 284 // combine the 4-bits from qs with the 5th bit 285 const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0); 286 const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1); 287 288 reg_f[i/2][2*(i%2) + 0] = d * x0 + m; 289 reg_f[i/2][2*(i%2) + 1] = d * x1 + m; 290 } 291 292 reg = (type4x4) reg_f; 293 } 294 295 template 296 void dequantize_q5_1_t4(device const block_q5_1 * xb, short il, thread type4 & reg) { 297 device const uint16_t * qs = ((device const uint16_t *)xb + 4); 298 const float d = xb->d; 299 const float m = xb->m; 300 const ushort mask = (il/4) ? 0x00F0 : 0x000F; 301 302 const uint32_t qh = *((device const uint32_t *)xb->qh); 303 304 const int x_mv = (il/4) ? 4 : 0; 305 306 const int gh_mv = (il/4) ? 12 : 0; 307 const int gh_bk = (il/4) ? 0 : 4; 308 309 for (int ii = 0; ii < 2; ii++) { 310 int i = 2*(il%4) + ii; 311 312 // extract the 5-th bits for x0 and x1 313 const uint8_t xh_0 = ((qh >> (gh_mv + 2*i )) << gh_bk) & 0x10; 314 const uint8_t xh_1 = ((qh >> (gh_mv + 2*i+1)) << gh_bk) & 0x10; 315 316 // combine the 4-bits from qs with the 5th bit 317 const int32_t x0 = ((((qs[i] ) & mask) >> x_mv) | xh_0); 318 const int32_t x1 = ((((qs[i] >> 8) & mask) >> x_mv) | xh_1); 319 320 reg[2*ii + 0] = d * x0 + m; 321 reg[2*ii + 1] = d * x1 + m; 322 } 323 } 324 325 template 326 void dequantize_q8_0(device const block_q8_0 *xb, short il, thread type4x4 & reg) { 327 device const packed_char4 * qs = (device const packed_char4 *) xb->qs; 328 const float d = xb->d; 329 330 float4x4 reg_f; 331 332 for (int i = 0; i < 4; ++i) { 333 reg_f[i] = float4(qs[4*il + i]) * d; 334 } 335 336 reg = (type4x4) reg_f; 337 } 338 339 template 340 void dequantize_q8_0_t4(device const block_q8_0 *xb, short il, thread type4 & reg) { 341 device const packed_char4 * qs = (device const packed_char4 *) xb->qs; 342 const float d = xb->d; 343 344 reg = (type4) (float4(qs[il]) * d); 345 } 346 347 template 348 void dequantize_mxfp4(device const block_mxfp4 * xb, short il, thread type4x4 & reg) { 349 device const uint8_t * q2 = (device const uint8_t *)xb->qs; 350 351 const float d = e8m0_to_fp32(xb->e); 352 const uint8_t shr = il >= 1 ? 4 : 0; 353 354 float4x4 reg_f; 355 356 for (int i = 0; i < 4; ++i) { 357 reg_f[i][0] = d * kvalues_mxfp4_f[(q2[4*i + 0] >> shr) & 0x0F]; 358 reg_f[i][1] = d * kvalues_mxfp4_f[(q2[4*i + 1] >> shr) & 0x0F]; 359 reg_f[i][2] = d * kvalues_mxfp4_f[(q2[4*i + 2] >> shr) & 0x0F]; 360 reg_f[i][3] = d * kvalues_mxfp4_f[(q2[4*i + 3] >> shr) & 0x0F]; 361 } 362 363 reg = (type4x4) reg_f; 364 } 365 366 template 367 void dequantize_mxfp4_t4(device const block_mxfp4 * xb, short il, thread type4 & reg) { 368 device const uint8_t * q2 = (device const uint8_t *)xb->qs; 369 370 const float d = e8m0_to_fp32(xb->e); 371 const short il4 = il%4; 372 373 const uint8_t shr = il >= 4 ? 4 : 0; 374 375 reg[0] = d * kvalues_mxfp4_f[(q2[4*il4 + 0] >> shr) & 0x0F]; 376 reg[1] = d * kvalues_mxfp4_f[(q2[4*il4 + 1] >> shr) & 0x0F]; 377 reg[2] = d * kvalues_mxfp4_f[(q2[4*il4 + 2] >> shr) & 0x0F]; 378 reg[3] = d * kvalues_mxfp4_f[(q2[4*il4 + 3] >> shr) & 0x0F]; 379 } 380 381 template 382 void dequantize_q2_K(device const block_q2_K *xb, short il, thread type4x4 & reg) { 383 const float d = xb->d; 384 const float min = xb->dmin; 385 device const uint8_t * q = (device const uint8_t *)xb->qs; 386 float dl, ml; 387 uint8_t sc = xb->scales[il]; 388 389 q = q + 32*(il/8) + 16*(il&1); 390 il = (il/2)%4; 391 392 half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h); 393 uchar mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3); 394 dl = d * (sc & 0xF) * coef, ml = min * (sc >> 4); 395 for (int i = 0; i < 16; ++i) { 396 reg[i/4][i%4] = dl * (q[i] & mask) - ml; 397 } 398 } 399 400 template 401 void dequantize_q3_K(device const block_q3_K *xb, short il, thread type4x4 & reg) { 402 const half d_all = xb->d; 403 device const uint8_t * q = (device const uint8_t *)xb->qs; 404 device const uint8_t * h = (device const uint8_t *)xb->hmask; 405 device const int8_t * scales = (device const int8_t *)xb->scales; 406 407 q = q + 32 * (il/8) + 16 * (il&1); 408 h = h + 16 * (il&1); 409 uint8_t m = 1 << (il/2); 410 uint16_t kmask1 = (il/4)>1 ? ((il/4)>2 ? 192 : 48) : \ 411 ((il/4)>0 ? 12 : 3); 412 uint16_t kmask2 = il/8 ? 0xF0 : 0x0F; 413 uint16_t scale_2 = scales[il%8], scale_1 = scales[8 + il%4]; 414 int16_t dl_int = (il/4)&1 ? (scale_2&kmask2) | ((scale_1&kmask1) << 2) 415 : (scale_2&kmask2) | ((scale_1&kmask1) << 4); 416 float dl = il<8 ? d_all * (dl_int - 32.f) : d_all * (dl_int / 16.f - 32.f); 417 const float ml = 4.f * dl; 418 419 il = (il/2) & 3; 420 const half coef = il>1 ? (il>2 ? 1/64.h : 1/16.h) : (il>0 ? 1/4.h : 1.h); 421 const uint8_t mask = il>1 ? (il>2 ? 192 : 48) : (il>0 ? 12 : 3); 422 dl *= coef; 423 424 for (int i = 0; i < 16; ++i) { 425 reg[i/4][i%4] = dl * (q[i] & mask) - (h[i] & m ? 0 : ml); 426 } 427 } 428 429 static inline uchar2 get_scale_min_k4_just2(int j, int k, device const uchar * q) { 430 return j < 4 ? uchar2{uchar(q[j+0+k] & 63), uchar(q[j+4+k] & 63)} 431 : uchar2{uchar((q[j+4+k] & 0xF) | ((q[j-4+k] & 0xc0) >> 2)), uchar((q[j+4+k] >> 4) | ((q[j-0+k] & 0xc0) >> 2))}; 432 } 433 434 template 435 void dequantize_q4_K(device const block_q4_K * xb, short il, thread type4x4 & reg) { 436 device const uchar * q = xb->qs; 437 438 short is = (il/4) * 2; 439 q = q + (il/4) * 32 + 16 * (il&1); 440 il = il & 3; 441 const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales); 442 const float d = il < 2 ? xb->d : xb->d / 16.h; 443 const float min = xb->dmin; 444 const float dl = d * sc[0]; 445 const float ml = min * sc[1]; 446 447 const ushort mask = il < 2 ? 0x0F : 0xF0; 448 for (int i = 0; i < 16; ++i) { 449 reg[i/4][i%4] = dl * (q[i] & mask) - ml; 450 } 451 } 452 453 template 454 void dequantize_q5_K(device const block_q5_K *xb, short il, thread type4x4 & reg) { 455 device const uint8_t * q = xb->qs; 456 device const uint8_t * qh = xb->qh; 457 458 short is = (il/4) * 2; 459 q = q + 32 * (il/4) + 16 * (il&1); 460 qh = qh + 16 * (il&1); 461 uint8_t ul = 1 << (il/2); 462 il = il & 3; 463 const uchar2 sc = get_scale_min_k4_just2(is, il/2, xb->scales); 464 const float d = il < 2 ? xb->d : xb->d / 16.f; 465 const float min = xb->dmin; 466 const float dl = d * sc[0]; 467 const float ml = min * sc[1]; 468 469 const ushort mask = il<2 ? 0x0F : 0xF0; 470 const float qh_val = il<2 ? 16.f : 256.f; 471 for (int i = 0; i < 16; ++i) { 472 reg[i/4][i%4] = dl * ((q[i] & mask) + (qh[i] & ul ? qh_val : 0)) - ml; 473 } 474 } 475 476 template 477 void dequantize_q6_K(device const block_q6_K *xb, short il, thread type4x4 & reg) { 478 const half d_all = xb->d; 479 device const uint16_t * ql = (device const uint16_t *)xb->ql; 480 device const uint16_t * qh = (device const uint16_t *)xb->qh; 481 device const int8_t * scales = (device const int8_t *)xb->scales; 482 483 ql = ql + 32*(il/8) + 16*((il/2)&1) + 8*(il&1); 484 qh = qh + 16*(il/8) + 8*(il&1); 485 float sc = scales[(il%2) + 2 * ((il/2))]; 486 il = (il/2) & 3; 487 488 const uint32_t kmask1 = il>1 ? (il>2 ? 0xC0C0C0C0 : 0x30303030) : (il>0 ? 0x0C0C0C0C : 0x03030303); 489 const uint32_t kmask2 = il>1 ? 0xF0F0F0F0 : 0x0F0F0F0F; 490 const float ml = d_all * sc * 32.f; 491 const float dl0 = d_all * sc; 492 const float dl1 = dl0 / 256.f; 493 const float dl2 = dl0 / (256.f * 256.f); 494 const float dl3 = dl0 / (256.f * 256.f * 256.f); 495 const uint8_t shr_h = il>2 ? 2 : 0; 496 const uint8_t shl_h = il>1 ? 0 : (il>0 ? 2 : 4); 497 const uint8_t shr_l = il>1 ? 4 : 0; 498 for (int i = 0; i < 4; ++i) { 499 const uint32_t low = (ql[2*i] | (uint32_t)(ql[2*i+1] << 16)) & kmask2; 500 const uint32_t high = (qh[2*i] | (uint32_t)(qh[2*i+1] << 16)) & kmask1; 501 const uint32_t q = ((high << shl_h) >> shr_h) | (low >> shr_l); 502 reg[i][0] = dl0 * ((half)(q & 0xFF)) - ml; 503 reg[i][1] = dl1 * ((float)(q & 0xFF00)) - ml; 504 reg[i][2] = dl2 * ((float)(q & 0xFF0000)) - ml; 505 reg[i][3] = dl3 * ((float)(q & 0xFF000000)) - ml; 506 } 507 } 508 509 template 510 void dequantize_iq2_xxs(device const block_iq2_xxs * xb, short il, thread type4x4 & reg) { 511 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 512 const float d = xb->d; 513 const int ib32 = il/2; 514 il = il%2; 515 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 516 // each block of 32 needs 2 uint32_t's for the quants & scale, so 4 uint16_t's. 517 device const uint16_t * q2 = xb->qs + 4*ib32; 518 const uint32_t aux32_g = q2[0] | (q2[1] << 16); 519 const uint32_t aux32_s = q2[2] | (q2[3] << 16); 520 thread const uint8_t * aux8 = (thread const uint8_t *)&aux32_g; 521 const float dl = d * (0.5f + (aux32_s >> 28)) * 0.25f; 522 constant uint8_t * grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+0]); 523 uint8_t signs = ksigns_iq2xs[(aux32_s >> 14*il) & 127]; 524 for (int i = 0; i < 8; ++i) { 525 reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f); 526 } 527 grid = (constant uint8_t *)(iq2xxs_grid + aux8[2*il+1]); 528 signs = ksigns_iq2xs[(aux32_s >> (14*il+7)) & 127]; 529 for (int i = 0; i < 8; ++i) { 530 reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f); 531 } 532 } 533 534 template 535 void dequantize_iq2_xs(device const block_iq2_xs * xb, short il, thread type4x4 & reg) { 536 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 537 const float d = xb->d; 538 const int ib32 = il/2; 539 il = il%2; 540 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 541 device const uint16_t * q2 = xb->qs + 4*ib32; 542 const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f; 543 constant uint8_t * grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+0] & 511)); 544 uint8_t signs = ksigns_iq2xs[q2[2*il+0] >> 9]; 545 for (int i = 0; i < 8; ++i) { 546 reg[i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f); 547 } 548 grid = (constant uint8_t *)(iq2xs_grid + (q2[2*il+1] & 511)); 549 signs = ksigns_iq2xs[q2[2*il+1] >> 9]; 550 for (int i = 0; i < 8; ++i) { 551 reg[2+i/4][i%4] = dl * grid[i] * (signs & kmask_iq2xs[i] ? -1.f : 1.f); 552 } 553 } 554 555 template 556 void dequantize_iq3_xxs(device const block_iq3_xxs * xb, short il, thread type4x4 & reg) { 557 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 558 const float d = xb->d; 559 const int ib32 = il/2; 560 il = il%2; 561 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 562 device const uint8_t * q3 = xb->qs + 8*ib32; 563 device const uint16_t * gas = (device const uint16_t *)(xb->qs + QK_K/4) + 2*ib32; 564 const uint32_t aux32 = gas[0] | (gas[1] << 16); 565 const float dl = d * (0.5f + (aux32 >> 28)) * 0.5f; 566 constant uint8_t * grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+0]); 567 constant uint8_t * grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+1]); 568 uint8_t signs = ksigns_iq2xs[(aux32 >> 14*il) & 127]; 569 for (int i = 0; i < 4; ++i) { 570 reg[0][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f); 571 reg[1][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f); 572 } 573 grid1 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+2]); 574 grid2 = (constant uint8_t *)(iq3xxs_grid + q3[4*il+3]); 575 signs = ksigns_iq2xs[(aux32 >> (14*il+7)) & 127]; 576 for (int i = 0; i < 4; ++i) { 577 reg[2][i] = dl * grid1[i] * (signs & kmask_iq2xs[i+0] ? -1.f : 1.f); 578 reg[3][i] = dl * grid2[i] * (signs & kmask_iq2xs[i+4] ? -1.f : 1.f); 579 } 580 } 581 582 template 583 void dequantize_iq3_s(device const block_iq3_s * xb, short il, thread type4x4 & reg) { 584 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 585 const float d = xb->d; 586 const int ib32 = il/2; 587 il = il%2; 588 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 589 device const uint8_t * qs = xb->qs + 8*ib32; 590 device const uint8_t * signs = xb->signs + 4*ib32 + 2*il; 591 const uint8_t qh = xb->qh[ib32] >> 4*il; 592 const float dl = d * (1 + 2*((xb->scales[ib32/2] >> 4*(ib32%2)) & 0xf)); 593 constant uint8_t * grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+0] | ((qh << 8) & 256))); 594 constant uint8_t * grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+1] | ((qh << 7) & 256))); 595 for (int i = 0; i < 4; ++i) { 596 reg[0][i] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i+0]); 597 reg[1][i] = dl * grid2[i] * select(1, -1, signs[0] & kmask_iq2xs[i+4]); 598 } 599 grid1 = (constant uint8_t *)(iq3s_grid + (qs[4*il+2] | ((qh << 6) & 256))); 600 grid2 = (constant uint8_t *)(iq3s_grid + (qs[4*il+3] | ((qh << 5) & 256))); 601 for (int i = 0; i < 4; ++i) { 602 reg[2][i] = dl * grid1[i] * select(1, -1, signs[1] & kmask_iq2xs[i+0]); 603 reg[3][i] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i+4]); 604 } 605 } 606 607 template 608 void dequantize_iq2_s(device const block_iq2_s * xb, short il, thread type4x4 & reg) { 609 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 610 const float d = xb->d; 611 const int ib32 = il/2; 612 il = il%2; 613 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 614 device const uint8_t * qs = xb->qs + 4*ib32 + 2*il; 615 device const uint8_t * signs = qs + QK_K/8; 616 const uint8_t qh = xb->qh[ib32] >> 4*il; 617 const float dl = d * (0.5f + ((xb->scales[ib32] >> 4*il) & 0xf)) * 0.25f; 618 constant uint8_t * grid1 = (constant uint8_t *)(iq2s_grid + (qs[0] | ((qh << 8) & 0x300))); 619 constant uint8_t * grid2 = (constant uint8_t *)(iq2s_grid + (qs[1] | ((qh << 6) & 0x300))); 620 for (int i = 0; i < 8; ++i) { 621 reg[i/4+0][i%4] = dl * grid1[i] * select(1, -1, signs[0] & kmask_iq2xs[i]); 622 reg[i/4+2][i%4] = dl * grid2[i] * select(1, -1, signs[1] & kmask_iq2xs[i]); 623 } 624 } 625 626 template 627 void dequantize_iq1_s(device const block_iq1_s * xb, short il, thread type4x4 & reg) { 628 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 629 const int ib32 = il/2; 630 il = il%2; 631 const float d = xb->d; 632 device const uint8_t * qs = xb->qs + 4*ib32 + 2*il; 633 device const uint16_t * qh = xb->qh; 634 const float dl = d * (2*((qh[ib32] >> 12) & 7) + 1); 635 const float ml = dl * (qh[ib32] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA); 636 const uint16_t h = qh[ib32] >> 6*il; 637 constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((h << 8) & 0x700))); 638 constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((h << 5) & 0x700))); 639 for (int i = 0; i < 4; ++i) { 640 reg[0][i] = dl * (grid1[i] & 0xf) + ml; 641 reg[1][i] = dl * (grid1[i] >> 4) + ml; 642 reg[2][i] = dl * (grid2[i] & 0xf) + ml; 643 reg[3][i] = dl * (grid2[i] >> 4) + ml; 644 } 645 } 646 647 template 648 void dequantize_iq1_m(device const block_iq1_m * xb, short il, thread type4x4 & reg) { 649 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 650 const int ib32 = il/2; 651 il = il%2; 652 device const uint16_t * sc = (device const uint16_t *)xb->scales; 653 654 iq1m_scale_t scale; 655 scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); 656 const float d = scale.f16; 657 658 device const uint8_t * qs = xb->qs + 4*ib32 + 2*il; 659 device const uint8_t * qh = xb->qh + 2*ib32 + il; 660 661 const float dl = d * (2*((sc[ib32/2] >> (6*(ib32%2)+3*il)) & 7) + 1); 662 const float ml1 = dl * (qh[0] & 0x08 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA); 663 const float ml2 = dl * (qh[0] & 0x80 ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA); 664 constant uint8_t * grid1 = (constant uint8_t *)(iq1s_grid_gpu + (qs[0] | ((qh[0] << 8) & 0x700))); 665 constant uint8_t * grid2 = (constant uint8_t *)(iq1s_grid_gpu + (qs[1] | ((qh[0] << 4) & 0x700))); 666 for (int i = 0; i < 4; ++i) { 667 reg[0][i] = dl * (grid1[i] & 0xf) + ml1; 668 reg[1][i] = dl * (grid1[i] >> 4) + ml1; 669 reg[2][i] = dl * (grid2[i] & 0xf) + ml2; 670 reg[3][i] = dl * (grid2[i] >> 4) + ml2; 671 } 672 } 673 674 template 675 void dequantize_iq4_nl(device const block_iq4_nl * xb, short il, thread type4x4 & reg) { 676 device const uint16_t * q4 = (device const uint16_t *)xb->qs; 677 const float d = xb->d; 678 uint32_t aux32; 679 thread const uint8_t * q8 = (thread const uint8_t *)&aux32; 680 for (int i = 0; i < 4; ++i) { 681 aux32 = ((q4[2*i] | (q4[2*i+1] << 16)) >> 4*il) & 0x0f0f0f0f; 682 reg[i][0] = d * kvalues_iq4nl_f[q8[0]]; 683 reg[i][1] = d * kvalues_iq4nl_f[q8[1]]; 684 reg[i][2] = d * kvalues_iq4nl_f[q8[2]]; 685 reg[i][3] = d * kvalues_iq4nl_f[q8[3]]; 686 } 687 } 688 689 template 690 void dequantize_iq4_nl_t4(device const block_iq4_nl * xb, short il, thread type4 & reg) { 691 device const uint16_t * q4 = (device const uint16_t *)xb->qs; 692 const float d = xb->d; 693 uint32_t aux32; 694 thread const uint8_t * q8 = (thread const uint8_t *)&aux32; 695 aux32 = ((q4[2*(il%4)] | (q4[2*(il%4)+1] << 16)) >> 4*(il/4)) & 0x0f0f0f0f; 696 reg[0] = d * kvalues_iq4nl_f[q8[0]]; 697 reg[1] = d * kvalues_iq4nl_f[q8[1]]; 698 reg[2] = d * kvalues_iq4nl_f[q8[2]]; 699 reg[3] = d * kvalues_iq4nl_f[q8[3]]; 700 } 701 702 template 703 void dequantize_iq4_xs(device const block_iq4_xs * xb, short il, thread type4x4 & reg) { 704 // il is 0...15 for QK_K = 256 => index of block of 32 is il/2 705 const int ib32 = il/2; 706 il = il%2; 707 // il = 0 or 1. il = 0 processes the first 16 quants in a block of 32, il = 1 the second 16 708 device const uint32_t * q4 = (device const uint32_t *)xb->qs + 4*ib32; 709 const int ls = ((xb->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((xb->scales_h >> 2*ib32) & 3) << 4); 710 const float d = (float)xb->d * (ls - 32); 711 uint32_t aux32; 712 thread const uint8_t * q8 = (thread const uint8_t *)&aux32; 713 for (int i = 0; i < 4; ++i) { 714 aux32 = (q4[i] >> 4*il) & 0x0f0f0f0f; 715 reg[i][0] = d * kvalues_iq4nl_f[q8[0]]; 716 reg[i][1] = d * kvalues_iq4nl_f[q8[1]]; 717 reg[i][2] = d * kvalues_iq4nl_f[q8[2]]; 718 reg[i][3] = d * kvalues_iq4nl_f[q8[3]]; 719 } 720 } 721 722 template 723 void dequantize_tq2_0(device const block_tq2_0 * xb, short il, thread type4x4 & reg) { 724 device const uint8_t * qs = xb->qs; 725 const float d = xb->d; 726 727 float4x4 reg_f; 728 729 // 2 bits per element, 4 elements per byte, 128 elements per 32-byte group 730 const short base = il * 16; 731 for (int k = 0; k < 16; k++) { 732 const int i = base + k; 733 const int byte = ((i >> 7) & 1) * 32 + (i & 31); 734 const int l = (i >> 5) & 3; 735 reg_f[k/4][k%4] = d * (float)(((qs[byte] >> (2*l)) & 3) - 1); 736 } 737 738 reg = (type4x4) reg_f; 739 }