okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c143c403
main
1// src/quant/dequant.cpp - block -> float row dequantizers. 2// 3// Derived from ggml's `dequantize_row_*` (llama.cpp ggml/src/ggml-quants.c at 3cf03257f219afbe7334045ff7c6a06ac68c627d, Copyright (c) 2023-2026 The ggml 4// authors, MIT; third_party/strata-ggml/LICENSE). Scalar and exact: the same float operations as ggml's, so a requantizer built on them (--mtp-q4) gives 5// the same bytes. fp16 -> fp32 conversions are exact whatever the instruction set. 6#define STRATA_QUANT_TABLES 7#include "internal.hpp" 8 9#include <cassert> 10 11#include "strata/quant/dequant.hpp" 12 13namespace strata::quant { 14namespace { 15 16static inline void get_scale_min_k4 (int j ,const uint8_t * STRATA_RESTRICT q ,uint8_t * STRATA_RESTRICT d ,uint8_t * STRATA_RESTRICT m ) { 17if (j < 4 ) { 18* d = q [j ]& 63 ;* m = q [j + 4 ]& 63 ; 19 }else { 20* d = (q [j + 4 ]& 0xF ) | ((q [j - 4 ] >>6 ) <<4 ); 21* m = (q [j + 4 ] >>4 ) | ((q [j - 0 ] >>6 ) <<4 ); 22 } 23} 24 25void dequant_q2_0 (const block_q2_0 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 26static const int qk = QK2_0 ; 27 28assert (k %qk == 0 ); 29 30const int nb = k /qk ; 31 32for (int i = 0 ;i < nb ;i ++ ) { 33const float d = fp16_to_fp32 (x [i ].d ); 34 35for (int j = 0 ;j < qk ;++ j ) { 36const int byte_index = j /4 ; 37const int bit_offset = (j %4 )* 2 ; 38const uint8_t q = (x [i ].qs [byte_index ] >>bit_offset )& 0x03 ; 39// 00=-1, 01=0, 10=+1, 11=+2 40y [i * qk + j ]= ((int )q - 1 )* d ; 41 } 42 } 43} 44 45void dequant_q4_0 (const block_q4_0 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 46static const int qk = QK4_0 ; 47 48assert (k %qk == 0 ); 49 50const int nb = k /qk ; 51 52for (int i = 0 ;i < nb ;i ++ ) { 53const float d = fp16_to_fp32 (x [i ].d ); 54 55for (int j = 0 ;j < qk /2 ;++ j ) { 56const int x0 = (x [i ].qs [j ]& 0x0F )- 8 ; 57const int x1 = (x [i ].qs [j ] >>4 )- 8 ; 58 59y [i * qk + j + 0 ]= x0 * d ; 60y [i * qk + j + qk /2 ]= x1 * d ; 61 } 62 } 63} 64 65void dequant_q4_1 (const block_q4_1 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 66static const int qk = QK4_1 ; 67 68assert (k %qk == 0 ); 69 70const int nb = k /qk ; 71 72for (int i = 0 ;i < nb ;i ++ ) { 73const float d = fp16_to_fp32 (x [i ].d ); 74const float m = fp16_to_fp32 (x [i ].m ); 75 76for (int j = 0 ;j < qk /2 ;++ j ) { 77const int x0 = (x [i ].qs [j ]& 0x0F ); 78const int x1 = (x [i ].qs [j ] >>4 ); 79 80y [i * qk + j + 0 ]= x0 * d + m ; 81y [i * qk + j + qk /2 ]= x1 * d + m ; 82 } 83 } 84} 85 86void dequant_q5_0 (const block_q5_0 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 87static const int qk = QK5_0 ; 88 89assert (k %qk == 0 ); 90 91const int nb = k /qk ; 92 93for (int i = 0 ;i < nb ;i ++ ) { 94const float d = fp16_to_fp32 (x [i ].d ); 95 96uint32_t qh ; 97memcpy (& qh ,x [i ].qh ,sizeof (qh )); 98 99for (int j = 0 ;j < qk /2 ;++ j ) { 100const uint8_t xh_0 = ((qh >> (j + 0 )) <<4 )& 0x10 ; 101const uint8_t xh_1 = ((qh >> (j + 12 )) )& 0x10 ; 102 103const int32_t x0 = ((x [i ].qs [j ]& 0x0F ) |xh_0 )- 16 ; 104const int32_t x1 = ((x [i ].qs [j ] >>4 ) |xh_1 )- 16 ; 105 106y [i * qk + j + 0 ]= x0 * d ; 107y [i * qk + j + qk /2 ]= x1 * d ; 108 } 109 } 110} 111 112void dequant_q5_1 (const block_q5_1 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 113static const int qk = QK5_1 ; 114 115assert (k %qk == 0 ); 116 117const int nb = k /qk ; 118 119for (int i = 0 ;i < nb ;i ++ ) { 120const float d = fp16_to_fp32 (x [i ].d ); 121const float m = fp16_to_fp32 (x [i ].m ); 122 123uint32_t qh ; 124memcpy (& qh ,x [i ].qh ,sizeof (qh )); 125 126for (int j = 0 ;j < qk /2 ;++ j ) { 127const uint8_t xh_0 = ((qh >> (j + 0 )) <<4 )& 0x10 ; 128const uint8_t xh_1 = ((qh >> (j + 12 )) )& 0x10 ; 129 130const int x0 = (x [i ].qs [j ]& 0x0F ) |xh_0 ; 131const int x1 = (x [i ].qs [j ] >>4 ) |xh_1 ; 132 133y [i * qk + j + 0 ]= x0 * d + m ; 134y [i * qk + j + qk /2 ]= x1 * d + m ; 135 } 136 } 137} 138 139void dequant_q8_0 (const block_q8_0 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 140static const int qk = QK8_0 ; 141 142assert (k %qk == 0 ); 143 144const int nb = k /qk ; 145 146for (int i = 0 ;i < nb ;i ++ ) { 147const float d = fp16_to_fp32 (x [i ].d ); 148 149for (int j = 0 ;j < qk ;++ j ) { 150y [i * qk + j ]= x [i ].qs [j ]* d ; 151 } 152 } 153} 154 155void dequant_mxfp4 (const block_mxfp4 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 156static const int qk = QK_MXFP4 ; 157 158assert (k %qk == 0 ); 159 160const int nb = k /qk ; 161 162for (int i = 0 ;i < nb ;i ++ ) { 163const float d = e8m0_to_fp32_half (x [i ].e ); 164 165for (int j = 0 ;j < qk /2 ;++ j ) { 166const int8_t x0 = kvalues_mxfp4 [x [i ].qs [j ]& 0x0F ]; 167const int8_t x1 = kvalues_mxfp4 [x [i ].qs [j ] >>4 ]; 168 169y [i * qk + j + 0 ]= x0 * d ; 170y [i * qk + j + qk /2 ]= x1 * d ; 171 } 172 } 173} 174 175void dequant_nvfp4 (const block_nvfp4 * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 176static const int qk = QK_NVFP4 ; 177static const int qk_sub = QK_NVFP4_SUB ; 178static const int n_sub = QK_NVFP4 /QK_NVFP4_SUB ; 179 180assert (k %qk == 0 ); 181 182const int nb = k /qk ; 183 184for (int i = 0 ;i < nb ;i ++ ) { 185for (int s = 0 ;s < n_sub ;s ++ ) { 186const float d = ue4m3_to_fp32 (x [i ].d [s ]); 187float * yb = y + i * qk + s * qk_sub ; 188 189for (int j = 0 ;j < qk_sub /2 ;++ j ) { 190const int8_t v0 = kvalues_mxfp4 [x [i ].qs [s * (qk_sub /2 )+ j ]& 0x0F ]; 191const int8_t v1 = kvalues_mxfp4 [x [i ].qs [s * (qk_sub /2 )+ j ] >>4 ]; 192 193yb [j + 0 ]= v0 * d ; 194yb [j + qk_sub /2 ]= v1 * d ; 195 } 196 } 197 } 198} 199 200void dequant_q2_K (const block_q2_K * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 201assert (k %QK_K == 0 ); 202const int nb = k /QK_K ; 203 204for (int i = 0 ;i < nb ;i ++ ) { 205 206const float d = fp16_to_fp32 (x [i ].d ); 207const float min = fp16_to_fp32 (x [i ].dmin ); 208 209const uint8_t * q = x [i ].qs ; 210 211int is = 0 ; 212float dl ,ml ; 213for (int n = 0 ;n < QK_K ;n += 128 ) { 214int shift = 0 ; 215for (int j = 0 ;j < 4 ;++ j ) { 216 217uint8_t sc = x [i ].scales [is ++ ]; 218dl = d * (sc & 0xF );ml = min * (sc >>4 ); 219for (int l = 0 ;l < 16 ;++ l )* y ++ = dl * ((int8_t )((q [l ] >>shift )& 3 ))- ml ; 220 221sc = x [i ].scales [is ++ ]; 222dl = d * (sc & 0xF );ml = min * (sc >>4 ); 223for (int l = 0 ;l < 16 ;++ l )* y ++ = dl * ((int8_t )((q [l + 16 ] >>shift )& 3 ))- ml ; 224 225shift += 2 ; 226 } 227q += 32 ; 228 } 229 } 230} 231 232void dequant_q3_K (const block_q3_K * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 233assert (k %QK_K == 0 ); 234const int nb = k /QK_K ; 235 236const uint32_t kmask1 = 0x03030303 ; 237const uint32_t kmask2 = 0x0f0f0f0f ; 238 239uint32_t aux [4 ]; 240const int8_t * scales = (const int8_t * )aux ; 241 242for (int i = 0 ;i < nb ;i ++ ) { 243 244const float d_all = fp16_to_fp32 (x [i ].d ); 245 246const uint8_t * STRATA_RESTRICT q = x [i ].qs ; 247const uint8_t * STRATA_RESTRICT hm = x [i ].hmask ; 248uint8_t m = 1 ; 249 250memcpy (aux ,x [i ].scales ,12 ); 251uint32_t tmp = aux [2 ]; 252aux [2 ]= ((aux [0 ] >>4 )& kmask2 ) | (((tmp >>4 )& kmask1 ) <<4 ); 253aux [3 ]= ((aux [1 ] >>4 )& kmask2 ) | (((tmp >>6 )& kmask1 ) <<4 ); 254aux [0 ]= (aux [0 ]& kmask2 ) | (((tmp >>0 )& kmask1 ) <<4 ); 255aux [1 ]= (aux [1 ]& kmask2 ) | (((tmp >>2 )& kmask1 ) <<4 ); 256 257int is = 0 ; 258float dl ; 259for (int n = 0 ;n < QK_K ;n += 128 ) { 260int shift = 0 ; 261for (int j = 0 ;j < 4 ;++ j ) { 262 263dl = d_all * (scales [is ++ ]- 32 ); 264for (int l = 0 ;l < 16 ;++ l ) { 265* y ++ = dl * ((int8_t )((q [l + 0 ] >>shift )& 3 )- ((hm [l + 0 ]& m ) ?0 :4 )); 266 } 267 268dl = d_all * (scales [is ++ ]- 32 ); 269for (int l = 0 ;l < 16 ;++ l ) { 270* y ++ = dl * ((int8_t )((q [l + 16 ] >>shift )& 3 )- ((hm [l + 16 ]& m ) ?0 :4 )); 271 } 272 273shift += 2 ; 274m <<=1 ; 275 } 276q += 32 ; 277 } 278 279 } 280} 281 282void dequant_q4_K (const block_q4_K * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 283assert (k %QK_K == 0 ); 284const int nb = k /QK_K ; 285 286for (int i = 0 ;i < nb ;i ++ ) { 287const uint8_t * q = x [i ].qs ; 288 289const float d = fp16_to_fp32 (x [i ].d ); 290const float min = fp16_to_fp32 (x [i ].dmin ); 291 292int is = 0 ; 293uint8_t sc ,m ; 294for (int j = 0 ;j < QK_K ;j += 64 ) { 295get_scale_min_k4 (is + 0 ,x [i ].scales ,& sc ,& m ); 296const float d1 = d * sc ;const float m1 = min * m ; 297get_scale_min_k4 (is + 1 ,x [i ].scales ,& sc ,& m ); 298const float d2 = d * sc ;const float m2 = min * m ; 299for (int l = 0 ;l < 32 ;++ l )* y ++ = d1 * (q [l ]& 0xF )- m1 ; 300for (int l = 0 ;l < 32 ;++ l )* y ++ = d2 * (q [l ] >>4 )- m2 ; 301q += 32 ;is += 2 ; 302 } 303 } 304} 305 306void dequant_q5_K (const block_q5_K * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 307assert (k %QK_K == 0 ); 308const int64_t nb = k /QK_K ; 309 310for (int i = 0 ;i < nb ;i ++ ) { 311const uint8_t * ql = x [i ].qs ; 312const uint8_t * qh = x [i ].qh ; 313 314const float d = fp16_to_fp32 (x [i ].d ); 315const float min = fp16_to_fp32 (x [i ].dmin ); 316 317int is = 0 ; 318uint8_t sc ,m ; 319uint8_t u1 = 1 ,u2 = 2 ; 320for (int j = 0 ;j < QK_K ;j += 64 ) { 321get_scale_min_k4 (is + 0 ,x [i ].scales ,& sc ,& m ); 322const float d1 = d * sc ;const float m1 = min * m ; 323get_scale_min_k4 (is + 1 ,x [i ].scales ,& sc ,& m ); 324const float d2 = d * sc ;const float m2 = min * m ; 325for (int l = 0 ;l < 32 ;++ l )* y ++ = d1 * ((ql [l ]& 0xF )+ (qh [l ]& u1 ?16 :0 ))- m1 ; 326for (int l = 0 ;l < 32 ;++ l )* y ++ = d2 * ((ql [l ] >>4 )+ (qh [l ]& u2 ?16 :0 ))- m2 ; 327ql += 32 ;is += 2 ; 328u1 <<=2 ;u2 <<=2 ; 329 } 330 } 331} 332 333void dequant_q6_K (const block_q6_K * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 334assert (k %QK_K == 0 ); 335const int64_t nb = k /QK_K ; 336 337for (int i = 0 ;i < nb ;i ++ ) { 338const float d = fp16_to_fp32 (x [i ].d ); 339 340const uint8_t * STRATA_RESTRICT ql = x [i ].ql ; 341const uint8_t * STRATA_RESTRICT qh = x [i ].qh ; 342const int8_t * STRATA_RESTRICT sc = x [i ].scales ; 343 344for (int n = 0 ;n < QK_K ;n += 128 ) { 345for (int l = 0 ;l < 32 ;++ l ) { 346int is = l /16 ; 347const int8_t q1 = (int8_t )((ql [l + 0 ]& 0xF ) | (((qh [l ] >>0 )& 3 ) <<4 ))- 32 ; 348const int8_t q2 = (int8_t )((ql [l + 32 ]& 0xF ) | (((qh [l ] >>2 )& 3 ) <<4 ))- 32 ; 349const int8_t q3 = (int8_t )((ql [l + 0 ] >>4 ) | (((qh [l ] >>4 )& 3 ) <<4 ))- 32 ; 350const int8_t q4 = (int8_t )((ql [l + 32 ] >>4 ) | (((qh [l ] >>6 )& 3 ) <<4 ))- 32 ; 351y [l + 0 ]= d * sc [is + 0 ]* q1 ; 352y [l + 32 ]= d * sc [is + 2 ]* q2 ; 353y [l + 64 ]= d * sc [is + 4 ]* q3 ; 354y [l + 96 ]= d * sc [is + 6 ]* q4 ; 355 } 356y += 128 ; 357ql += 64 ; 358qh += 32 ; 359sc += 8 ; 360 } 361 } 362} 363 364void dequant_iq2_xxs (const block_iq2_xxs * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 365assert (k %QK_K == 0 ); 366const int64_t nb = k /QK_K ; 367 368uint32_t aux32 [2 ]; 369const uint8_t * aux8 = (const uint8_t * )aux32 ; 370 371for (int i = 0 ;i < nb ;i ++ ) { 372 373const float d = fp16_to_fp32 (x [i ].d ); 374 375for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 376memcpy (aux32 ,x [i ].qs + 4 * ib32 ,2 * sizeof (uint32_t )); 377const float db = d * (0.5f + (aux32 [1 ] >>28 ))* 0.25f ; 378for (int l = 0 ;l < 4 ;++ l ) { 379const uint8_t * grid = (const uint8_t * )(iq2xxs_grid + aux8 [l ]); 380const uint8_t signs = ksigns_iq2xs [(aux32 [1 ] >>7 * l )& 127 ]; 381for (int j = 0 ;j < 8 ;++ j ) { 382y [j ]= db * grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 383 } 384y += 8 ; 385 } 386 } 387 } 388} 389 390void dequant_iq2_xs (const block_iq2_xs * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 391assert (k %QK_K == 0 ); 392const int64_t nb = k /QK_K ; 393 394float db [2 ]; 395 396for (int i = 0 ;i < nb ;i ++ ) { 397 398const float d = fp16_to_fp32 (x [i ].d ); 399 400for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 401db [0 ]= d * (0.5f + (x [i ].scales [ib32 ]& 0xf ))* 0.25f ; 402db [1 ]= d * (0.5f + (x [i ].scales [ib32 ] >>4 ))* 0.25f ; 403for (int l = 0 ;l < 4 ;++ l ) { 404const uint8_t * grid = (const uint8_t * )(iq2xs_grid + (x [i ].qs [4 * ib32 + l ]& 511 )); 405const uint8_t signs = ksigns_iq2xs [x [i ].qs [4 * ib32 + l ] >>9 ]; 406for (int j = 0 ;j < 8 ;++ j ) { 407y [j ]= db [l /2 ]* grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 408 } 409y += 8 ; 410 } 411 } 412 } 413} 414 415void dequant_iq2_s (const block_iq2_s * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 416assert (k %QK_K == 0 ); 417const int64_t nb = k /QK_K ; 418 419float db [2 ]; 420 421for (int i = 0 ;i < nb ;i ++ ) { 422 423const float d = fp16_to_fp32 (x [i ].d ); 424const uint8_t * qs = x [i ].qs ; 425const uint8_t * qh = x [i ].qh ; 426const uint8_t * signs = qs + QK_K /8 ; 427 428for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 429db [0 ]= d * (0.5f + (x [i ].scales [ib32 ]& 0xf ))* 0.25f ; 430db [1 ]= d * (0.5f + (x [i ].scales [ib32 ] >>4 ))* 0.25f ; 431for (int l = 0 ;l < 4 ;++ l ) { 432const float dl = db [l /2 ]; 433const uint8_t * grid = (const uint8_t * )(iq2s_grid + (qs [l ] | (qh [ib32 ] << (8 - 2 * l )& 0x300 ))); 434for (int j = 0 ;j < 8 ;++ j ) { 435y [j ]= dl * grid [j ]* (signs [l ]& kmask_iq2xs [j ] ?-1.f :1.f ); 436 } 437y += 8 ; 438 } 439qs += 4 ; 440signs += 4 ; 441 } 442 } 443} 444 445void dequant_iq3_xxs (const block_iq3_xxs * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 446assert (k %QK_K == 0 ); 447const int64_t nb = k /QK_K ; 448 449uint32_t aux32 ; 450 451for (int i = 0 ;i < nb ;i ++ ) { 452 453const float d = fp16_to_fp32 (x [i ].d ); 454const uint8_t * qs = x [i ].qs ; 455const uint8_t * scales_and_signs = qs + QK_K /4 ; 456 457for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 458memcpy (& aux32 ,scales_and_signs + 4 * ib32 ,sizeof (uint32_t )); 459const float db = d * (0.5f + (aux32 >>28 ))* 0.5f ; 460for (int l = 0 ;l < 4 ;++ l ) { 461const uint8_t signs = ksigns_iq2xs [(aux32 >>7 * l )& 127 ]; 462const uint8_t * grid1 = (const uint8_t * )(iq3xxs_grid + qs [2 * l + 0 ]); 463const uint8_t * grid2 = (const uint8_t * )(iq3xxs_grid + qs [2 * l + 1 ]); 464for (int j = 0 ;j < 4 ;++ j ) { 465y [j + 0 ]= db * grid1 [j ]* (signs & kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 466y [j + 4 ]= db * grid2 [j ]* (signs & kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 467 } 468y += 8 ; 469 } 470qs += 8 ; 471 } 472 } 473} 474 475void dequant_iq3_s (const block_iq3_s * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 476assert (k %QK_K == 0 ); 477const int64_t nb = k /QK_K ; 478 479for (int i = 0 ;i < nb ;i ++ ) { 480 481const float d = fp16_to_fp32 (x [i ].d ); 482const uint8_t * qs = x [i ].qs ; 483const uint8_t * qh = x [i ].qh ; 484const uint8_t * signs = x [i ].signs ; 485 486for (int ib32 = 0 ;ib32 < QK_K /32 ;ib32 += 2 ) { 487const float db1 = d * (1 + 2 * (x [i ].scales [ib32 /2 ]& 0xf )); 488const float db2 = d * (1 + 2 * (x [i ].scales [ib32 /2 ] >>4 )); 489for (int l = 0 ;l < 4 ;++ l ) { 490const uint8_t * grid1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [0 ] << (8 - 2 * l ))& 256 ))); 491const uint8_t * grid2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [0 ] << (7 - 2 * l ))& 256 ))); 492for (int j = 0 ;j < 4 ;++ j ) { 493y [j + 0 ]= db1 * grid1 [j ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 494y [j + 4 ]= db1 * grid2 [j ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 495 } 496y += 8 ; 497 } 498qs += 8 ; 499signs += 4 ; 500for (int l = 0 ;l < 4 ;++ l ) { 501const uint8_t * grid1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [1 ] << (8 - 2 * l ))& 256 ))); 502const uint8_t * grid2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [1 ] << (7 - 2 * l ))& 256 ))); 503for (int j = 0 ;j < 4 ;++ j ) { 504y [j + 0 ]= db2 * grid1 [j ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 505y [j + 4 ]= db2 * grid2 [j ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 506 } 507y += 8 ; 508 } 509qh += 2 ; 510qs += 8 ; 511signs += 4 ; 512 } 513 } 514} 515 516void dequant_iq1_s (const block_iq1_s * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 517assert (k %QK_K == 0 ); 518const int64_t nb = k /QK_K ; 519 520for (int i = 0 ;i < nb ;i ++ ) { 521 522const float d = fp16_to_fp32 (x [i ].d ); 523const uint8_t * qs = x [i ].qs ; 524const uint16_t * qh = x [i ].qh ; 525 526for (int ib = 0 ;ib < QK_K /32 ;++ ib ) { 527const float dl = d * (2 * ((qh [ib ] >>12 )& 7 )+ 1 ); 528const float delta = qh [ib ]& 0x8000 ?- IQ1S_DELTA :IQ1S_DELTA ; 529for (int l = 0 ;l < 4 ;++ l ) { 530const int8_t * grid = (const int8_t * )(iq1s_grid + (qs [l ] | (((qh [ib ] >>3 * l )& 7 ) <<8 ))); 531for (int j = 0 ;j < 8 ;++ j ) { 532y [j ]= dl * (grid [j ]+ delta ); 533 } 534y += 8 ; 535 } 536qs += 4 ; 537 } 538 } 539} 540 541void dequant_iq1_m (const block_iq1_m * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 542assert (k %QK_K == 0 ); 543const int64_t nb = k /QK_K ; 544 545float delta [4 ]; 546uint16_t idx [4 ]; 547 548iq1m_scale_t scale ; 549 550for (int i = 0 ;i < nb ;i ++ ) { 551 552const uint16_t * sc = (const uint16_t * )x [i ].scales ; 553scale .u16 = (sc [0 ] >>12 ) | ((sc [1 ] >>8 )& 0x00f0 ) | ((sc [2 ] >>4 )& 0x0f00 ) | (sc [3 ]& 0xf000 ); 554const float d = fp16_to_fp32 (scale .f16 ); 555 556const uint8_t * qs = x [i ].qs ; 557const uint8_t * qh = x [i ].qh ; 558 559for (int ib = 0 ;ib < QK_K /32 ;++ ib ) { 560const float dl1 = d * (2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 0 ))& 0x7 )+ 1 ); 561const float dl2 = d * (2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 3 ))& 0x7 )+ 1 ); 562 563idx [0 ]= qs [0 ] | ((qh [0 ] <<8 )& 0x700 ); 564idx [1 ]= qs [1 ] | ((qh [0 ] <<4 )& 0x700 ); 565idx [2 ]= qs [2 ] | ((qh [1 ] <<8 )& 0x700 ); 566idx [3 ]= qs [3 ] | ((qh [1 ] <<4 )& 0x700 ); 567delta [0 ]= qh [0 ]& 0x08 ?- IQ1S_DELTA :IQ1S_DELTA ; 568delta [1 ]= qh [0 ]& 0x80 ?- IQ1S_DELTA :IQ1S_DELTA ; 569delta [2 ]= qh [1 ]& 0x08 ?- IQ1S_DELTA :IQ1S_DELTA ; 570delta [3 ]= qh [1 ]& 0x80 ?- IQ1S_DELTA :IQ1S_DELTA ; 571for (int l = 0 ;l < 2 ;++ l ) { 572const int8_t * grid = (const int8_t * )(iq1s_grid + idx [l ]); 573for (int j = 0 ;j < 8 ;++ j ) { 574y [j ]= dl1 * (grid [j ]+ delta [l ]); 575 } 576y += 8 ; 577 } 578for (int l = 2 ;l < 4 ;++ l ) { 579const int8_t * grid = (const int8_t * )(iq1s_grid + idx [l ]); 580for (int j = 0 ;j < 8 ;++ j ) { 581y [j ]= dl2 * (grid [j ]+ delta [l ]); 582 } 583y += 8 ; 584 } 585qs += 4 ; 586qh += 2 ; 587 } 588 } 589} 590 591void dequant_iq4_nl (const block_iq4_nl * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 592assert (k %QK4_NL == 0 ); 593const int64_t nb = k /QK4_NL ; 594 595for (int i = 0 ;i < nb ;i ++ ) { 596 597const uint8_t * qs = x [i ].qs ; 598 599const float d = fp16_to_fp32 (x [i ].d ); 600for (int j = 0 ;j < QK4_NL /2 ;++ j ) { 601y [j + 0 ]= d * kvalues_iq4nl [qs [j ]& 0xf ]; 602y [j + QK4_NL /2 ]= d * kvalues_iq4nl [qs [j ] >>4 ]; 603 } 604y += QK4_NL ; 605qs += QK4_NL /2 ; 606 } 607} 608 609void dequant_iq4_xs (const block_iq4_xs * STRATA_RESTRICT x ,float * STRATA_RESTRICT y ,int64_t k ) { 610assert (k %QK_K == 0 ); 611const int64_t nb = k /QK_K ; 612 613for (int i = 0 ;i < nb ;i ++ ) { 614 615const uint8_t * qs = x [i ].qs ; 616 617const float d = fp16_to_fp32 (x [i ].d ); 618 619for (int ib = 0 ;ib < QK_K /32 ;++ ib ) { 620const int ls = ((x [i ].scales_l [ib /2 ] >>4 * (ib %2 ))& 0xf ) | (((x [i ].scales_h >>2 * ib )& 3 ) <<4 ); 621const float dl = d * (ls - 32 ); 622for (int j = 0 ;j < 16 ;++ j ) { 623y [j + 0 ]= dl * kvalues_iq4nl [qs [j ]& 0xf ]; 624y [j + 16 ]= dl * kvalues_iq4nl [qs [j ] >>4 ]; 625 } 626y += 32 ; 627qs += 16 ; 628 } 629 } 630} 631 632}// namespace 633 634bool can_dequant (Type t )noexcept { 635switch (t ) { 636case Type ::Q2_0 : 637case Type ::Q4_0 : 638case Type ::Q4_1 : 639case Type ::Q5_0 : 640case Type ::Q5_1 : 641case Type ::Q8_0 : 642case Type ::MXFP4 : 643case Type ::NVFP4 : 644case Type ::Q2_K : 645case Type ::Q3_K : 646case Type ::Q4_K : 647case Type ::Q5_K : 648case Type ::Q6_K : 649case Type ::IQ2_XXS : 650case Type ::IQ2_XS : 651case Type ::IQ2_S : 652case Type ::IQ3_XXS : 653case Type ::IQ3_S : 654case Type ::IQ1_S : 655case Type ::IQ1_M : 656case Type ::IQ4_NL : 657case Type ::IQ4_XS : 658return true; 659default :return false; 660 } 661} 662 663bool dequant_row (Type t ,const void * src ,float * dst ,int64_t n ) { 664switch (t ) { 665case Type ::Q2_0 :dequant_q2_0 ((const block_q2_0 * )src ,dst ,n );return true; 666case Type ::Q4_0 :dequant_q4_0 ((const block_q4_0 * )src ,dst ,n );return true; 667case Type ::Q4_1 :dequant_q4_1 ((const block_q4_1 * )src ,dst ,n );return true; 668case Type ::Q5_0 :dequant_q5_0 ((const block_q5_0 * )src ,dst ,n );return true; 669case Type ::Q5_1 :dequant_q5_1 ((const block_q5_1 * )src ,dst ,n );return true; 670case Type ::Q8_0 :dequant_q8_0 ((const block_q8_0 * )src ,dst ,n );return true; 671case Type ::MXFP4 :dequant_mxfp4 ((const block_mxfp4 * )src ,dst ,n );return true; 672case Type ::NVFP4 :dequant_nvfp4 ((const block_nvfp4 * )src ,dst ,n );return true; 673case Type ::Q2_K :dequant_q2_K ((const block_q2_K * )src ,dst ,n );return true; 674case Type ::Q3_K :dequant_q3_K ((const block_q3_K * )src ,dst ,n );return true; 675case Type ::Q4_K :dequant_q4_K ((const block_q4_K * )src ,dst ,n );return true; 676case Type ::Q5_K :dequant_q5_K ((const block_q5_K * )src ,dst ,n );return true; 677case Type ::Q6_K :dequant_q6_K ((const block_q6_K * )src ,dst ,n );return true; 678case Type ::IQ2_XXS :dequant_iq2_xxs ((const block_iq2_xxs * )src ,dst ,n );return true; 679case Type ::IQ2_XS :dequant_iq2_xs ((const block_iq2_xs * )src ,dst ,n );return true; 680case Type ::IQ2_S :dequant_iq2_s ((const block_iq2_s * )src ,dst ,n );return true; 681case Type ::IQ3_XXS :dequant_iq3_xxs ((const block_iq3_xxs * )src ,dst ,n );return true; 682case Type ::IQ3_S :dequant_iq3_s ((const block_iq3_s * )src ,dst ,n );return true; 683case Type ::IQ1_S :dequant_iq1_s ((const block_iq1_s * )src ,dst ,n );return true; 684case Type ::IQ1_M :dequant_iq1_m ((const block_iq1_m * )src ,dst ,n );return true; 685case Type ::IQ4_NL :dequant_iq4_nl ((const block_iq4_nl * )src ,dst ,n );return true; 686case Type ::IQ4_XS :dequant_iq4_xs ((const block_iq4_xs * )src ,dst ,n );return true; 687default :return false; 688 } 689} 690 691}// namespace strata::quant