okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1a2649206
main
1// metal/src/ref_quant.cpp - see ref_quant.h. The i-quant loops are transcribed from ggml-quants.c 2// (dequantize_row_iq2_xxs / iq2_xs / iq2_s / iq3_xxs / iq3_s / iq4_nl / iq4_xs) at 3cf03257; the K-quants and the 3// 32-blocks from Strata's include/strata/artifact/dequant.hpp (themselves transcribed from the same commit). 4#include "ref_quant.h" 5 6#include <algorithm> 7#include <cmath> 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11 12#define GGML_COMMON_DECL_CPP 13#define GGML_COMMON_IMPL_CPP 14#include "ggml-common.h" 15#include "ggml-common-tq.h" // the trellis block structs and the codebook tq_lut_f16 (Gyro) 16#include "../../src/kernels/cpu/tq_states.hpp" // the CPU trellis state extraction (the reference decoder) 17 18namespace ref { 19 20const char * type_name (int t ) { 21switch (t ) { 22case F32 :return "F32" ;case F16 :return "F16" ;case BF16 :return "BF16" ; 23case Q4_0 :return "Q4_0" ;case Q5_0 :return "Q5_0" ;case Q5_1 :return "Q5_1" ;case Q8_0 :return "Q8_0" ; 24case Q3_K :return "Q3_K" ;case Q4_K :return "Q4_K" ;case Q5_K :return "Q5_K" ;case Q6_K :return "Q6_K" ; 25case IQ2_XXS :return "IQ2_XXS" ;case IQ2_XS :return "IQ2_XS" ;case IQ2_S :return "IQ2_S" ; 26case IQ3_XXS :return "IQ3_XXS" ;case IQ3_S :return "IQ3_S" ;case IQ4_NL :return "IQ4_NL" ; 27case IQ4_XS :return "IQ4_XS" ;case Q2_0 :return "Q2_0" ;case IQ1_S :return "IQ1_S" ;case IQ1_M :return "IQ1_M" ; 28case TQ2_T :return "TQ2_T" ;case TQK6 :return "TQK6" ;case TQK7 :return "TQK7" ; 29 } 30return "?" ; 31} 32 33int block_elems (int t ) { 34switch (t ) { 35case F32 :case F16 :case BF16 :return 1 ; 36case Q4_0 :case Q5_0 :case Q5_1 :case Q8_0 :case IQ4_NL :return 32 ; 37case Q2_0 :return 64 ; 38case TQ2_T :case TQK6 :case TQK7 :return 128 ; 39default :return 256 ; 40 } 41} 42 43int block_bytes (int t ) { 44switch (t ) { 45case F32 :return 4 ;case F16 :case BF16 :return 2 ; 46case Q4_0 :return 18 ;case Q5_0 :return 22 ;case Q5_1 :return 24 ;case Q8_0 :return 34 ;case IQ4_NL :return 18 ;case Q2_0 :return 18 ; 47case Q3_K :return 110 ;case Q4_K :return 144 ;case Q5_K :return 176 ;case Q6_K :return 210 ; 48case IQ2_XXS :return 66 ;case IQ2_XS :return 74 ;case IQ2_S :return 82 ;case IQ3_XXS :return 98 ; 49case IQ3_S :return 110 ;case IQ4_XS :return 136 ;case IQ1_S :return 50 ;case IQ1_M :return 56 ; 50case TQ2_T :return 34 ;case TQK6 :return 26 ;case TQK7 :return 30 ; 51 } 52 std::fprintf (stderr ,"ref: unknown type %d\n" ,t ); 53 std::exit (2 ); 54} 55 56float f16_to_f32 (uint16_t h ) { 57const uint32_t sign = (uint32_t )(h >>15 )& 1u ; 58uint32_t exp = (h >>10 )& 0x1Fu ,man = h & 0x3FFu ,f ; 59if (exp == 0 ) { 60if (man == 0 ) 61f = sign <<31 ; 62else { 63exp = 127 - 15 + 1 ; 64while (!(man & 0x400u )) {man <<=1 ;-- exp ; } 65man &=0x3FFu ; 66f = (sign <<31 ) | (exp <<23 ) | (man <<13 ); 67 } 68 }else if (exp == 31 ) 69f = (sign <<31 ) |0x7F800000u | (man <<13 ); 70else 71f = (sign <<31 ) | ((exp - 15 + 127 ) <<23 ) | (man <<13 ); 72float out ; 73 std::memcpy (& out ,& f ,4 ); 74return out ; 75} 76 77uint16_t f32_to_f16 (float f ) { 78_Float16 h = (_Float16 )f ; 79uint16_t r ; 80 std::memcpy (& r ,& h ,2 ); 81return r ; 82} 83 84float bf16_to_f32 (uint16_t h ) { 85const uint32_t f = (uint32_t )h <<16 ; 86float out ; 87 std::memcpy (& out ,& f ,4 ); 88return out ; 89} 90 91uint16_t f32_to_bf16 (float f ) { 92uint32_t u ; 93 std::memcpy (& u ,& f ,4 ); 94u += 0x7FFFu + ((u >>16 )& 1u ); 95return (uint16_t )(u >>16 ); 96} 97 98static inline uint16_t rd16 (const uint8_t * p ) {return (uint16_t )(p [0 ] | (p [1 ] <<8 )); } 99 100static void get_scale_min_k4 (int j ,const uint8_t * q ,uint8_t & d ,uint8_t & m ) { 101if (j < 4 ) { 102d = q [j ]& 63 ; 103m = q [j + 4 ]& 63 ; 104 }else { 105d = (q [j + 4 ]& 0x0F ) | ((q [j - 4 ] >>6 ) <<4 ); 106m = (q [j + 4 ] >>4 ) | ((q [j - 0 ] >>6 ) <<4 ); 107 } 108} 109 110// The trellis types (ggml 144 TQ2_T, 145 TQK6, 146 TQK7): 128 weights = 32 steps of 4; step t's state comes from 111// the CPU engine's own extraction (src/kernels/cpu/tq_states.hpp), x = state * 0x9e3779b1, the two codebook points 112// lut[x >> 21] and lut[(x >> 10) & 2047] (fp16 pairs) give weights 4t..4t+3, times the fp16 scale. 113static void deq_tq (int kb ,const uint8_t * b ,float * y ) { 114uint32_t st [32 ]; 115if (kb == 8 ) strata::kernels::cpu::tq_detail::tq_states < 8 > (b + 2 ,st ); 116else if (kb == 6 ) strata::kernels::cpu::tq_detail::tq_states < 6 > (b + 2 ,st ); 117else strata::kernels::cpu::tq_detail::tq_states < 7 > (b + 2 ,st ); 118const float d = f16_to_f32 (rd16 (b )); 119for (int t = 0 ;t < 32 ;++ t ) { 120const uint32_t x = st [t ]* 0x9e3779b1u ; 121const uint32_t pa = x >>21 ,pb = (x >>10 )& 2047u ; 122y [4 * t + 0 ]= f16_to_f32 (tq_lut_f16 [2 * pa ])* d ; 123y [4 * t + 1 ]= f16_to_f32 (tq_lut_f16 [2 * pa + 1 ])* d ; 124y [4 * t + 2 ]= f16_to_f32 (tq_lut_f16 [2 * pb ])* d ; 125y [4 * t + 3 ]= f16_to_f32 (tq_lut_f16 [2 * pb + 1 ])* d ; 126 } 127} 128 129static void deq_block (int t ,const uint8_t * b ,float * y ) { 130switch (t ) { 131case TQ2_T :deq_tq (8 ,b ,y );return ; 132case TQK6 :deq_tq (6 ,b ,y );return ; 133case TQK7 :deq_tq (7 ,b ,y );return ; 134case Q2_0 : { 135const float d = f16_to_f32 (rd16 (b )); 136for (int j = 0 ;j < 64 ;++ j )y [j ]= (float )(((b [2 + j /4 ] >> ((j %4 )* 2 ))& 3 )- 1 )* d ; 137return ; 138 } 139case Q8_0 : { 140const float d = f16_to_f32 (rd16 (b )); 141for (int j = 0 ;j < 32 ;++ j )y [j ]= (float )(int8_t )b [2 + j ]* d ; 142return ; 143 } 144case Q4_0 : { 145const float d = f16_to_f32 (rd16 (b )); 146for (int j = 0 ;j < 16 ;++ j ) { 147y [j ]= (float )((b [2 + j ]& 15 )- 8 )* d ; 148y [j + 16 ]= (float )((b [2 + j ] >>4 )- 8 )* d ; 149 } 150return ; 151 } 152case Q5_0 : { 153const float d = f16_to_f32 (rd16 (b )); 154uint32_t h ; 155 std::memcpy (& h ,b + 2 ,4 ); 156const uint8_t * qs = b + 6 ; 157for (int j = 0 ;j < 16 ;++ j ) { 158const int x0 = (qs [j ]& 15 ) | (((h >>j )& 1 ) <<4 ),x1 = (qs [j ] >>4 ) | (((h >> (j + 16 ))& 1 ) <<4 ); 159y [j ]= (float )(x0 - 16 )* d ; 160y [j + 16 ]= (float )(x1 - 16 )* d ; 161 } 162return ; 163 } 164case Q5_1 : {// ggml dequantize_row_q5_1: y = x * d + m 165const float d = f16_to_f32 (rd16 (b )),m = f16_to_f32 (rd16 (b + 2 )); 166uint32_t h ; 167 std::memcpy (& h ,b + 4 ,4 ); 168const uint8_t * qs = b + 8 ; 169for (int j = 0 ;j < 16 ;++ j ) { 170const int x0 = (qs [j ]& 15 ) | (((h >>j )& 1 ) <<4 ),x1 = (qs [j ] >>4 ) | (((h >> (j + 16 ))& 1 ) <<4 ); 171y [j ]= (float )x0 * d + m ; 172y [j + 16 ]= (float )x1 * d + m ; 173 } 174return ; 175 } 176case IQ4_NL : { 177const float d = f16_to_f32 (rd16 (b )); 178for (int j = 0 ;j < 16 ;++ j ) { 179y [j ]= d * kvalues_iq4nl [b [2 + j ]& 15 ]; 180y [j + 16 ]= d * kvalues_iq4nl [b [2 + j ] >>4 ]; 181 } 182return ; 183 } 184case IQ4_XS : { 185const float d = f16_to_f32 (rd16 (b )); 186const uint16_t sh = rd16 (b + 2 ); 187const uint8_t * sl = b + 4 ; 188const uint8_t * qs = b + 8 ; 189for (int ib = 0 ;ib < 8 ;++ ib ) { 190const int ls = ((sl [ib /2 ] >> (4 * (ib %2 )))& 15 ) | (((sh >> (2 * ib ))& 3 ) <<4 ); 191const float dl = d * (ls - 32 ); 192for (int j = 0 ;j < 16 ;++ j ) { 193y [j ]= dl * kvalues_iq4nl [qs [j ]& 15 ]; 194y [j + 16 ]= dl * kvalues_iq4nl [qs [j ] >>4 ]; 195 } 196y += 32 ; 197qs += 16 ; 198 } 199return ; 200 } 201case Q6_K : { 202const uint8_t * ql = b ; 203const uint8_t * qh = b + 128 ; 204const int8_t * sc = (const int8_t * )(b + 192 ); 205const float d = f16_to_f32 (rd16 (b + 208 )); 206for (int n = 0 ;n < 256 ;n += 128 ) { 207for (int l = 0 ;l < 32 ;++ l ) { 208const int is = l /16 ; 209const int q1 = (int )((ql [l ]& 15 ) | (((qh [l ] >>0 )& 3 ) <<4 ))- 32 ; 210const int q2 = (int )((ql [l + 32 ]& 15 ) | (((qh [l ] >>2 )& 3 ) <<4 ))- 32 ; 211const int q3 = (int )((ql [l ] >>4 ) | (((qh [l ] >>4 )& 3 ) <<4 ))- 32 ; 212const int q4 = (int )((ql [l + 32 ] >>4 ) | (((qh [l ] >>6 )& 3 ) <<4 ))- 32 ; 213y [l ]= d * sc [is ]* q1 ; 214y [l + 32 ]= d * sc [is + 2 ]* q2 ; 215y [l + 64 ]= d * sc [is + 4 ]* q3 ; 216y [l + 96 ]= d * sc [is + 6 ]* q4 ; 217 } 218y += 128 ; 219ql += 64 ; 220qh += 32 ; 221sc += 8 ; 222 } 223return ; 224 } 225case Q4_K : { 226const float d = f16_to_f32 (rd16 (b )),mn = f16_to_f32 (rd16 (b + 2 )); 227const uint8_t * sc = b + 4 ; 228const uint8_t * q = b + 16 ; 229int is = 0 ; 230for (int j = 0 ;j < 256 ;j += 64 ) { 231uint8_t s ,m ; 232get_scale_min_k4 (is ,sc ,s ,m ); 233const float d1 = d * s ,m1 = mn * m ; 234get_scale_min_k4 (is + 1 ,sc ,s ,m ); 235const float d2 = d * s ,m2 = mn * m ; 236for (int l = 0 ;l < 32 ;++ l )* y ++ = d1 * (q [l ]& 15 )- m1 ; 237for (int l = 0 ;l < 32 ;++ l )* y ++ = d2 * (q [l ] >>4 )- m2 ; 238q += 32 ; 239is += 2 ; 240 } 241return ; 242 } 243case Q5_K : { 244const float d = f16_to_f32 (rd16 (b )),mn = f16_to_f32 (rd16 (b + 2 )); 245const uint8_t * sc = b + 4 ; 246const uint8_t * qh = b + 16 ; 247const uint8_t * ql = b + 48 ; 248int is = 0 ; 249uint8_t u1 = 1 ,u2 = 2 ; 250for (int j = 0 ;j < 256 ;j += 64 ) { 251uint8_t s ,m ; 252get_scale_min_k4 (is ,sc ,s ,m ); 253const float d1 = d * s ,m1 = mn * m ; 254get_scale_min_k4 (is + 1 ,sc ,s ,m ); 255const float d2 = d * s ,m2 = mn * m ; 256for (int l = 0 ;l < 32 ;++ l )* y ++ = d1 * ((ql [l ]& 15 )+ ((qh [l ]& u1 ) ?16 :0 ))- m1 ; 257for (int l = 0 ;l < 32 ;++ l )* y ++ = d2 * ((ql [l ] >>4 )+ ((qh [l ]& u2 ) ?16 :0 ))- m2 ; 258ql += 32 ; 259is += 2 ; 260u1 <<=2 ; 261u2 <<=2 ; 262 } 263return ; 264 } 265case Q3_K : { 266const uint8_t * hm = b ; 267const uint8_t * q = b + 32 ; 268const float d_all = f16_to_f32 (rd16 (b + 108 )); 269const uint32_t kmask1 = 0x03030303u ,kmask2 = 0x0f0f0f0fu ; 270uint32_t aux [4 ]= {0 ,0 ,0 ,0 }; 271 std::memcpy (aux ,b + 96 ,12 ); 272const uint32_t tmp = aux [2 ]; 273aux [2 ]= ((aux [0 ] >>4 )& kmask2 ) | (((tmp >>4 )& kmask1 ) <<4 ); 274aux [3 ]= ((aux [1 ] >>4 )& kmask2 ) | (((tmp >>6 )& kmask1 ) <<4 ); 275aux [0 ]= (aux [0 ]& kmask2 ) | (((tmp >>0 )& kmask1 ) <<4 ); 276aux [1 ]= (aux [1 ]& kmask2 ) | (((tmp >>2 )& kmask1 ) <<4 ); 277const int8_t * scales = (const int8_t * )aux ; 278int is = 0 ; 279uint8_t m = 1 ; 280for (int n = 0 ;n < 256 ;n += 128 ) { 281int shift = 0 ; 282for (int j = 0 ;j < 4 ;++ j ) { 283float dl = d_all * (scales [is ++ ]- 32 ); 284for (int l = 0 ;l < 16 ;++ l )* y ++ = dl * ((int8_t )((q [l ] >>shift )& 3 )- ((hm [l ]& m ) ?0 :4 )); 285dl = d_all * (scales [is ++ ]- 32 ); 286for (int l = 0 ;l < 16 ;++ l ) 287* y ++ = dl * ((int8_t )((q [l + 16 ] >>shift )& 3 )- ((hm [l + 16 ]& m ) ?0 :4 )); 288shift += 2 ; 289m <<=1 ; 290 } 291q += 32 ; 292 } 293return ; 294 } 295case IQ1_S : {// ggml dequantize_row_iq1_s: dl = d (2 s + 1), y = dl (grid + delta) 296const float d = f16_to_f32 (rd16 (b )); 297const uint8_t * qs = b + 2 ; 298for (int ib = 0 ;ib < 8 ;++ ib ) { 299const uint16_t qh = rd16 (b + 34 + 2 * ib ); 300const float dl = d * (float )(2 * ((qh >>12 )& 7 )+ 1 ); 301const float delta = qh & 0x8000 ?- IQ1S_DELTA :IQ1S_DELTA ; 302for (int l = 0 ;l < 4 ;++ l ) { 303const int8_t * grid = (const int8_t * )(iq1s_grid + (qs [l ] | (((qh >>3 * l )& 7 ) <<8 ))); 304for (int j = 0 ;j < 8 ;++ j )y [j ]= dl * (grid [j ]+ delta ); 305y += 8 ; 306 } 307qs += 4 ; 308 } 309return ; 310 } 311case IQ1_M : {// ggml dequantize_row_iq1_m: the f16 scale in the top nibbles of the four u16 scales 312const uint8_t * qs = b ; 313const uint8_t * qh = b + 32 ; 314const uint16_t * sc = (const uint16_t * )(b + 48 ); 315uint16_t s16 = (uint16_t )((sc [0 ] >>12 ) | ((sc [1 ] >>8 )& 0x00f0 ) | ((sc [2 ] >>4 )& 0x0f00 ) | (sc [3 ]& 0xf000 )); 316const float d = f16_to_f32 (s16 ); 317for (int ib = 0 ;ib < 8 ;++ ib ) { 318const float dl1 = d * (float )(2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 0 ))& 7 )+ 1 ); 319const float dl2 = d * (float )(2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 3 ))& 7 )+ 1 ); 320uint16_t idx [4 ]; 321idx [0 ]= qs [0 ] | ((qh [0 ] <<8 )& 0x700 ); 322idx [1 ]= qs [1 ] | ((qh [0 ] <<4 )& 0x700 ); 323idx [2 ]= qs [2 ] | ((qh [1 ] <<8 )& 0x700 ); 324idx [3 ]= qs [3 ] | ((qh [1 ] <<4 )& 0x700 ); 325const float delta [4 ]= {qh [0 ]& 0x08 ?- IQ1S_DELTA :IQ1S_DELTA ,qh [0 ]& 0x80 ?- IQ1S_DELTA :IQ1S_DELTA , 326qh [1 ]& 0x08 ?- IQ1S_DELTA :IQ1S_DELTA ,qh [1 ]& 0x80 ?- IQ1S_DELTA :IQ1S_DELTA }; 327for (int l = 0 ;l < 4 ;++ l ) { 328const int8_t * grid = (const int8_t * )(iq1s_grid + idx [l ]); 329for (int j = 0 ;j < 8 ;++ j )y [j ]= (l < 2 ?dl1 :dl2 )* (grid [j ]+ delta [l ]); 330y += 8 ; 331 } 332qs += 4 ; 333qh += 2 ; 334 } 335return ; 336 } 337case IQ2_XXS : { 338const float d = f16_to_f32 (rd16 (b )); 339const uint8_t * qs = b + 2 ; 340for (int ib32 = 0 ;ib32 < 8 ;++ ib32 ) { 341uint32_t aux32 [2 ]; 342 std::memcpy (aux32 ,qs + 8 * ib32 ,8 ); 343const uint8_t * aux8 = (const uint8_t * )aux32 ; 344const float db = d * (0.5f + (aux32 [1 ] >>28 ))* 0.25f ; 345for (int l = 0 ;l < 4 ;++ l ) { 346const uint8_t * grid = (const uint8_t * )(iq2xxs_grid + aux8 [l ]); 347const uint8_t signs = ksigns_iq2xs [(aux32 [1 ] >>7 * l )& 127 ]; 348for (int j = 0 ;j < 8 ;++ j )y [j ]= db * grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 349y += 8 ; 350 } 351 } 352return ; 353 } 354case IQ2_XS : { 355const float d = f16_to_f32 (rd16 (b )); 356const uint8_t * q8 = b + 2 ; 357const uint8_t * scales = b + 66 ; 358for (int ib32 = 0 ;ib32 < 8 ;++ ib32 ) { 359const float db0 = d * (0.5f + (scales [ib32 ]& 15 ))* 0.25f ; 360const float db1 = d * (0.5f + (scales [ib32 ] >>4 ))* 0.25f ; 361for (int l = 0 ;l < 4 ;++ l ) { 362const uint16_t q = rd16 (q8 + 2 * (4 * ib32 + l )); 363const uint8_t * grid = (const uint8_t * )(iq2xs_grid + (q & 511 )); 364const uint8_t signs = ksigns_iq2xs [q >>9 ]; 365const float db = l < 2 ?db0 :db1 ; 366for (int j = 0 ;j < 8 ;++ j )y [j ]= db * grid [j ]* (signs & kmask_iq2xs [j ] ?-1.f :1.f ); 367y += 8 ; 368 } 369 } 370return ; 371 } 372case IQ2_S : { 373const float d = f16_to_f32 (rd16 (b )); 374const uint8_t * qs = b + 2 ; 375const uint8_t * qh = b + 66 ; 376const uint8_t * scales = b + 74 ; 377const uint8_t * signs = qs + 32 ; 378for (int ib32 = 0 ;ib32 < 8 ;++ ib32 ) { 379const float db0 = d * (0.5f + (scales [ib32 ]& 15 ))* 0.25f ; 380const float db1 = d * (0.5f + (scales [ib32 ] >>4 ))* 0.25f ; 381for (int l = 0 ;l < 4 ;++ l ) { 382const float dl = l < 2 ?db0 :db1 ; 383const uint8_t * grid = (const uint8_t * )(iq2s_grid + (qs [l ] | ((qh [ib32 ] << (8 - 2 * l ))& 0x300 ))); 384for (int j = 0 ;j < 8 ;++ j )y [j ]= dl * grid [j ]* (signs [l ]& kmask_iq2xs [j ] ?-1.f :1.f ); 385y += 8 ; 386 } 387qs += 4 ; 388signs += 4 ; 389 } 390return ; 391 } 392case IQ3_XXS : { 393const float d = f16_to_f32 (rd16 (b )); 394const uint8_t * qs = b + 2 ; 395const uint8_t * sas = b + 2 + 64 ; 396for (int ib32 = 0 ;ib32 < 8 ;++ ib32 ) { 397uint32_t aux32 ; 398 std::memcpy (& aux32 ,sas + 4 * ib32 ,4 ); 399const float db = d * (0.5f + (aux32 >>28 ))* 0.5f ; 400for (int l = 0 ;l < 4 ;++ l ) { 401const uint8_t signs = ksigns_iq2xs [(aux32 >>7 * l )& 127 ]; 402const uint8_t * g1 = (const uint8_t * )(iq3xxs_grid + qs [2 * l + 0 ]); 403const uint8_t * g2 = (const uint8_t * )(iq3xxs_grid + qs [2 * l + 1 ]); 404for (int j = 0 ;j < 4 ;++ j ) { 405y [j + 0 ]= db * g1 [j ]* (signs & kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 406y [j + 4 ]= db * g2 [j ]* (signs & kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 407 } 408y += 8 ; 409 } 410qs += 8 ; 411 } 412return ; 413 } 414case IQ3_S : { 415const float d = f16_to_f32 (rd16 (b )); 416const uint8_t * qs = b + 2 ; 417const uint8_t * qh = b + 66 ; 418const uint8_t * signs = b + 74 ; 419const uint8_t * scales = b + 106 ; 420for (int ib32 = 0 ;ib32 < 8 ;ib32 += 2 ) { 421const float db1 = d * (1 + 2 * (scales [ib32 /2 ]& 15 )); 422const float db2 = d * (1 + 2 * (scales [ib32 /2 ] >>4 )); 423for (int l = 0 ;l < 4 ;++ l ) { 424const uint8_t * g1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [0 ] << (8 - 2 * l ))& 256 ))); 425const uint8_t * g2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [0 ] << (7 - 2 * l ))& 256 ))); 426for (int j = 0 ;j < 4 ;++ j ) { 427y [j + 0 ]= db1 * g1 [j ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 428y [j + 4 ]= db1 * g2 [j ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 429 } 430y += 8 ; 431 } 432qs += 8 ; 433signs += 4 ; 434for (int l = 0 ;l < 4 ;++ l ) { 435const uint8_t * g1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [1 ] << (8 - 2 * l ))& 256 ))); 436const uint8_t * g2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [1 ] << (7 - 2 * l ))& 256 ))); 437for (int j = 0 ;j < 4 ;++ j ) { 438y [j + 0 ]= db2 * g1 [j ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1.f :1.f ); 439y [j + 4 ]= db2 * g2 [j ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1.f :1.f ); 440 } 441y += 8 ; 442 } 443qh += 2 ; 444qs += 8 ; 445signs += 4 ; 446 } 447return ; 448 } 449 } 450 std::fprintf (stderr ,"ref: dequant of %s not implemented\n" ,type_name (t )); 451 std::exit (2 ); 452} 453 454void dequant (int t ,const void * src ,float * dst ,size_t n ) { 455const uint8_t * s = (const uint8_t * )src ; 456if (t == F32 ) { std::memcpy (dst ,s ,n * 4 );return ; } 457if (t == F16 ) {for (size_t i = 0 ;i < n ;++ i )dst [i ]= f16_to_f32 (rd16 (s + 2 * i ));return ; } 458if (t == BF16 ) {for (size_t i = 0 ;i < n ;++ i )dst [i ]= bf16_to_f32 (rd16 (s + 2 * i ));return ; } 459const int be = block_elems (t ),bb = block_bytes (t ); 460for (size_t i = 0 ;i < n /be ;++ i )deq_block (t ,s + i * bb ,dst + i * be ); 461} 462 463void quantize_q8_0 (const float * x ,size_t n ,uint8_t * out ) { 464for (size_t b = 0 ;b < n /32 ;++ b ) { 465float amax = 0.0f ; 466for (int j = 0 ;j < 32 ;++ j )amax = std::max (amax , std::fabs (x [b * 32 + j ])); 467const float d = amax /127.0f ,id = d != 0.0f ?1.0f /d :0.0f ; 468const uint16_t h = f32_to_f16 (d ); 469uint8_t * o = out + b * 34 ; 470o [0 ]= (uint8_t )h ; 471o [1 ]= (uint8_t )(h >>8 ); 472for (int j = 0 ;j < 32 ;++ j )o [2 + j ]= (uint8_t )(int8_t )std::roundf (x [b * 32 + j ]* id ); 473 } 474} 475 476static void put16 (uint8_t * p ,uint16_t v ) {p [0 ]= (uint8_t )v ;p [1 ]= (uint8_t )(v >>8 ); } 477 478void random_blocks (int t ,uint8_t * dst ,int rows ,int cols ,uint64_t seed ,float scale ) { 479Rng r (seed ); 480const size_t n = (size_t )rows * cols ; 481if (t == F32 || t == F16 || t == BF16 ) { 482for (size_t i = 0 ;i < n ;++ i ) { 483const float v = r .sym ()* scale * 2.0f ; 484if (t == F32 ) std::memcpy (dst + 4 * i ,& v ,4 ); 485else put16 (dst + 2 * i ,t == F16 ?f32_to_f16 (v ) :f32_to_bf16 (v )); 486 } 487return ; 488 } 489const int be = block_elems (t ),bb = block_bytes (t ); 490const size_t nb = n /be ; 491for (size_t i = 0 ;i < nb ;++ i ) { 492uint8_t * b = dst + i * bb ; 493for (int k = 0 ;k < bb ;++ k )b [k ]= (uint8_t )r .next (); 494// the scale fields: magnitude so that |w| ~ scale. Typical code magnitudes differ per type. 495const float u = 0.5f + r .uni (); 496switch (t ) { 497case Q2_0 :put16 (b ,f32_to_f16 (scale * u ));break ; 498case Q8_0 :put16 (b ,f32_to_f16 (scale * u /64 ));break ; 499case Q4_0 :case IQ4_NL :put16 (b ,f32_to_f16 (scale * u / (t == Q4_0 ?4 :64 )));break ; 500case Q5_0 :put16 (b ,f32_to_f16 (scale * u /8 ));break ; 501case Q5_1 :put16 (b ,f32_to_f16 (scale * u /16 ));put16 (b + 2 ,f32_to_f16 (- scale * u ));break ; 502case IQ4_XS :put16 (b ,f32_to_f16 (scale * u /64 /16 ));break ; 503case Q4_K :case Q5_K :put16 (b ,f32_to_f16 (scale * u /32 /8 ));put16 (b + 2 ,f32_to_f16 (scale * u /32 /2 ));break ; 504case Q6_K :put16 (b + 208 ,f32_to_f16 (scale * u /32 /64 ));break ; 505case Q3_K :put16 (b + 108 ,f32_to_f16 (scale * u /32 /2 ));break ; 506case IQ2_XXS :case IQ2_XS :case IQ2_S :put16 (b ,f32_to_f16 (scale * u /4 ));break ; 507case IQ1_S :put16 (b ,f32_to_f16 (scale * u /2 ));break ; 508case IQ1_M : {uint16_t h = f32_to_f16 (scale * u /2 );for (int j = 0 ;j < 4 ;++ j ) {uint16_t w = (uint16_t )(b [48 + 2 * j ] | (b [49 + 2 * j ] <<8 ));w = (uint16_t )((w & 0x0fff ) | (((h >> (4 * j ))& 0xf ) <<12 ));b [48 + 2 * j ]= (uint8_t )w ;b [49 + 2 * j ]= (uint8_t )(w >>8 ); }break ; } 509case IQ3_XXS :put16 (b ,f32_to_f16 (scale * u /8 ));break ; 510case IQ3_S :put16 (b ,f32_to_f16 (scale * u /64 ));break ; 511case TQ2_T :case TQK6 :case TQK7 :put16 (b ,f32_to_f16 (scale * u /1.5f ));break ;// codebook |point| ~ 1 512default :break ; 513 } 514 } 515} 516 517}// namespace ref