okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c143c403
main
1// src/quant/dot_generic.cpp - the Generic-contract dot products: one weight row against one quantized activation row, plain C++. 2// 3// Derived from ggml-cpu's `ggml_vec_dot_*_generic` (llama.cpp ggml/src/ggml-cpu/quants.c at 3cf03257f219afbe7334045ff7c6a06ac68c627d, 4// Copyright (c) 2023-2026 The ggml authors, MIT; third_party/strata-ggml/LICENSE), ported to C++ and to the layer's signature. These are the 5// layer's scalar REFERENCE: what a build without SIMD gives (ggml's STRATA_ISA_FLOOR=none flavour), and what the self-checks compare against. 6// Float accumulation is sequential in block order, exactly as in ggml's `_generic` code. 7#define STRATA_QUANT_TABLES 8#include "internal.hpp" 9 10#include <cassert> 11 12#include <cstdlib> 13 14#include "registry.hpp" 15#include "strata/quant/dot.hpp" 16 17namespace strata::quant { 18namespace generic { 19 20#define UNUSED (x ) (void) (x) 21 22void dot_q2_0_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 23const int qk = QK2_0 ; 24const int nb = n /qk ; 25 26assert (n %qk == 0 ); 27 28const block_q2_0 * STRATA_RESTRICT x = (const block_q2_0 * )vx ; 29const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 30 31float sumf = 0.0f ; 32 33for (int i = 0 ;i < nb ;i ++ ) { 34const float d0 = fp16_to_fp32 (x [i ].d ); 35 36float sumi = 0.0f ; 37 38// group 64: one Q2_0 block (64 weights) maps to two Q8_0 blocks (2 * 32 = 64) 39for (int k = 0 ;k < 2 ;k ++ ) { 40const block_q8_0 * STRATA_RESTRICT yb = & y [i * 2 + k ]; 41const float d1 = fp16_to_fp32 (yb -> d ); 42int sumi_block = 0 ; 43 44const uint8_t * STRATA_RESTRICT qs = & x [i ].qs [k * 8 ]; 45const int8_t * STRATA_RESTRICT qy = yb -> qs ; 46 47for (int b = 0 ;b < 8 ;++ b ) { 48const uint8_t byte = qs [b ]; 49// Extract 4 two-bit values, map {0,1,2,3} -> {-1,0,1,2} 50sumi_block += ((int )((byte >>0 )& 3 )- 1 )* qy [b * 4 + 0 ]; 51sumi_block += ((int )((byte >>2 )& 3 )- 1 )* qy [b * 4 + 1 ]; 52sumi_block += ((int )((byte >>4 )& 3 )- 1 )* qy [b * 4 + 2 ]; 53sumi_block += ((int )((byte >>6 )& 3 )- 1 )* qy [b * 4 + 3 ]; 54 } 55 56sumi += d1 * sumi_block ; 57 } 58 59sumf += d0 * sumi ; 60 } 61 62* s = sumf ; 63} 64 65void dot_q4_0_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 66const int qk = QK8_0 ; 67const int nb = n /qk ; 68 69assert (n %qk == 0 ); 70 71const block_q4_0 * STRATA_RESTRICT x = (const block_q4_0 * )vx ; 72const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 73 74int ib = 0 ; 75float sumf = 0 ; 76 77for (;ib < nb ;++ ib ) { 78int sumi0 = 0 ; 79int sumi1 = 0 ; 80 81for (int j = 0 ;j < qk /2 ;++ j ) { 82const int v0 = (x [ib ].qs [j ]& 0x0F )- 8 ; 83const int v1 = (x [ib ].qs [j ] >>4 )- 8 ; 84 85sumi0 += (v0 * y [ib ].qs [j ]); 86sumi1 += (v1 * y [ib ].qs [j + qk /2 ]); 87 } 88 89int sumi = sumi0 + sumi1 ; 90sumf += sumi * fp16_to_fp32 (x [ib ].d )* fp16_to_fp32 (y [ib ].d ); 91 } 92 93* s = sumf ; 94} 95 96void dot_q4_1_q8_1_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 97const int qk = QK8_1 ; 98const int nb = n /qk ; 99 100assert (n %qk == 0 ); 101 102const block_q4_1 * STRATA_RESTRICT x = (const block_q4_1 * )vx ; 103const block_q8_1 * STRATA_RESTRICT y = (const block_q8_1 * )vy ; 104 105int ib = 0 ; 106float sumf = 0 ; 107 108for (;ib < nb ;++ ib ) { 109int sumi0 = 0 ; 110int sumi1 = 0 ; 111 112for (int j = 0 ;j < qk /2 ;++ j ) { 113const int v0 = (x [ib ].qs [j ]& 0x0F ); 114const int v1 = (x [ib ].qs [j ] >>4 ); 115 116sumi0 += (v0 * y [ib ].qs [j ]); 117sumi1 += (v1 * y [ib ].qs [j + qk /2 ]); 118 } 119 120int sumi = sumi0 + sumi1 ; 121sumf += (fp16_to_fp32 (x [ib ].d )* fp16_to_fp32 (y [ib ].d ))* sumi + fp16_to_fp32 (x [ib ].m )* fp16_to_fp32 (y [ib ].s ); 122 } 123 124* s = sumf ; 125} 126 127void dot_q5_0_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 128const int qk = QK8_0 ; 129const int nb = n /qk ; 130 131int ib = 0 ; 132float sumf = 0 ; 133 134assert (n %qk == 0 ); 135assert (qk == QK5_0 ); 136 137const block_q5_0 * STRATA_RESTRICT x = (const block_q5_0 * )vx ; 138const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 139 140for (;ib < nb ;++ ib ) { 141uint32_t qh ; 142memcpy (& qh ,x [ib ].qh ,sizeof (qh )); 143 144int sumi0 = 0 ; 145int sumi1 = 0 ; 146 147for (int j = 0 ;j < qk /2 ;++ j ) { 148const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) <<4 ; 149const uint8_t xh_1 = ((qh & (1u << (j + 16 ))) >> (j + 12 )); 150 151const int32_t x0 = (int8_t )(((x [ib ].qs [j ]& 0x0F ) |xh_0 )- 16 ); 152const int32_t x1 = (int8_t )(((x [ib ].qs [j ] >>4 ) |xh_1 )- 16 ); 153 154sumi0 += (x0 * y [ib ].qs [j ]); 155sumi1 += (x1 * y [ib ].qs [j + qk /2 ]); 156 } 157 158int sumi = sumi0 + sumi1 ; 159sumf += (fp16_to_fp32 (x [ib ].d )* fp16_to_fp32 (y [ib ].d ))* sumi ; 160 } 161 162* s = sumf ; 163} 164 165void dot_q5_1_q8_1_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 166const int qk = QK8_1 ; 167const int nb = n /qk ; 168 169int ib = 0 ; 170float sumf = 0 ; 171 172assert (n %qk == 0 ); 173assert (qk == QK5_1 ); 174 175const block_q5_1 * STRATA_RESTRICT x = (const block_q5_1 * )vx ; 176const block_q8_1 * STRATA_RESTRICT y = (const block_q8_1 * )vy ; 177 178for (;ib < nb ;++ ib ) { 179uint32_t qh ; 180memcpy (& qh ,x [ib ].qh ,sizeof (qh )); 181 182int sumi0 = 0 ; 183int sumi1 = 0 ; 184 185for (int j = 0 ;j < qk /2 ;++ j ) { 186const uint8_t xh_0 = ((qh >> (j + 0 )) <<4 )& 0x10 ; 187const uint8_t xh_1 = ((qh >> (j + 12 )) )& 0x10 ; 188 189const int32_t x0 = (x [ib ].qs [j ]& 0xF ) |xh_0 ; 190const int32_t x1 = (x [ib ].qs [j ] >>4 ) |xh_1 ; 191 192sumi0 += (x0 * y [ib ].qs [j ]); 193sumi1 += (x1 * y [ib ].qs [j + qk /2 ]); 194 } 195 196int sumi = sumi0 + sumi1 ; 197sumf += (fp16_to_fp32 (x [ib ].d )* fp16_to_fp32 (y [ib ].d ))* sumi + fp16_to_fp32 (x [ib ].m )* fp16_to_fp32 (y [ib ].s ); 198 } 199 200* s = sumf ; 201} 202 203void dot_q8_0_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 204const int qk = QK8_0 ; 205const int nb = n /qk ; 206 207assert (n %qk == 0 ); 208 209const block_q8_0 * STRATA_RESTRICT x = (const block_q8_0 * )vx ; 210const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 211 212int ib = 0 ; 213float sumf = 0 ; 214 215for (;ib < nb ;++ ib ) { 216int sumi = 0 ; 217 218for (int j = 0 ;j < qk ;j ++ ) { 219sumi += x [ib ].qs [j ]* y [ib ].qs [j ]; 220 } 221 222sumf += sumi * (fp16_to_fp32 (x [ib ].d )* fp16_to_fp32 (y [ib ].d )); 223 } 224 225* s = sumf ; 226} 227 228void dot_iq4_nl_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 229assert (n %QK4_NL == 0 ); 230 static_assert(QK4_NL == QK8_0 ,"QK4_NL and QK8_0 must be the same" ); 231 232const block_iq4_nl * STRATA_RESTRICT x = (const block_iq4_nl * )vx ; 233const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 234 235const int nb = n /QK4_NL ; 236 237int ib = 0 ; 238float sumf = 0 ; 239 240for (;ib < nb ;++ ib ) { 241const float d = fp16_to_fp32 (y [ib ].d )* fp16_to_fp32 (x [ib ].d ); 242int sumi1 = 0 ,sumi2 = 0 ; 243for (int j = 0 ;j < QK4_NL /2 ;++ j ) { 244sumi1 += y [ib ].qs [j + 0 ]* kvalues_iq4nl [x [ib ].qs [j ]& 0xf ]; 245sumi2 += y [ib ].qs [j + QK4_NL /2 ]* kvalues_iq4nl [x [ib ].qs [j ] >>4 ]; 246 } 247sumf += d * (sumi1 + sumi2 ); 248 } 249* s = sumf ; 250} 251 252void dot_mxfp4_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 253assert (n %QK_MXFP4 == 0 ); 254 static_assert(QK_MXFP4 == QK8_0 ,"QK_MXFP4 and QK8_0 must be the same" ); 255 256const block_mxfp4 * STRATA_RESTRICT x = (const block_mxfp4 * )vx ; 257const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 258 259const int nb = n /QK_MXFP4 ; 260 261int ib = 0 ; 262float sumf = 0 ; 263 264for (;ib < nb ;++ ib ) { 265const float d = fp16_to_fp32 (y [ib ].d )* e8m0_to_fp32_half (x [ib ].e ); 266 267int sumi1 = 0 ; 268int sumi2 = 0 ; 269for (int j = 0 ;j < QK_MXFP4 /2 ;++ j ) { 270sumi1 += y [ib ].qs [j + 0 ]* kvalues_mxfp4 [x [ib ].qs [j ]& 0xf ]; 271sumi2 += y [ib ].qs [j + QK_MXFP4 /2 ]* kvalues_mxfp4 [x [ib ].qs [j ] >>4 ]; 272 } 273sumf += d * (sumi1 + sumi2 ); 274 } 275* s = sumf ; 276} 277 278void dot_nvfp4_q8_0_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 279assert (n %QK_NVFP4 == 0 ); 280 281const block_nvfp4 * STRATA_RESTRICT x = (const block_nvfp4 * )vx ; 282const block_q8_0 * STRATA_RESTRICT y = (const block_q8_0 * )vy ; 283 284const int nb = n /QK_NVFP4 ; 285 286float sumf = 0 ; 287 288for (int ib = 0 ;ib < nb ;++ ib ) { 289for (int s_idx = 0 ;s_idx < 4 ;++ s_idx ) { 290const float d = ue4m3_to_fp32 (x [ib ].d [s_idx ]); 291const int q8_block = s_idx /2 ; 292const int q8_off = (s_idx %2 )* QK_NVFP4_SUB ; 293const float dy = fp16_to_fp32 (y [2 * ib + q8_block ].d ); 294 295int sumi_lo = 0 ,sumi_hi = 0 ; 296for (int j = 0 ;j < QK_NVFP4_SUB /2 ;++ j ) { 297const uint8_t qv = x [ib ].qs [s_idx * (QK_NVFP4_SUB /2 )+ j ]; 298sumi_lo += y [2 * ib + q8_block ].qs [q8_off + j + 0 ]* kvalues_mxfp4 [qv & 0xf ]; 299sumi_hi += y [2 * ib + q8_block ].qs [q8_off + j + QK_NVFP4_SUB /2 ]* kvalues_mxfp4 [qv >>4 ]; 300 } 301 302sumf += dy * d * (sumi_lo + sumi_hi ); 303 } 304 } 305* s = sumf ; 306} 307 308void dot_q2_K_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 309 310const block_q2_K * STRATA_RESTRICT x = (const block_q2_K * )vx ; 311const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 312 313const int nb = n /QK_K ; 314 315float sumf = 0 ; 316 317for (int i = 0 ;i < nb ;++ i ) { 318 319const uint8_t * q2 = x [i ].qs ; 320const int8_t * q8 = y [i ].qs ; 321const uint8_t * sc = x [i ].scales ; 322 323int summs = 0 ; 324for (int j = 0 ;j < 16 ;++ j ) { 325summs += y [i ].bsums [j ]* (sc [j ] >>4 ); 326 } 327 328const float dall = y [i ].d * fp16_to_fp32 (x [i ].d ); 329const float dmin = y [i ].d * fp16_to_fp32 (x [i ].dmin ); 330 331int isum = 0 ; 332int is = 0 ; 333int d ; 334for (int k = 0 ;k < QK_K /128 ;++ k ) { 335int shift = 0 ; 336for (int j = 0 ;j < 4 ;++ j ) { 337d = sc [is ++ ]& 0xF ; 338int isuml = 0 ; 339for (int l = 0 ;l < 16 ;++ l )isuml += q8 [l ]* ((q2 [l ] >>shift )& 3 ); 340isum += d * isuml ; 341d = sc [is ++ ]& 0xF ; 342isuml = 0 ; 343for (int l = 16 ;l < 32 ;++ l )isuml += q8 [l ]* ((q2 [l ] >>shift )& 3 ); 344isum += d * isuml ; 345shift += 2 ; 346q8 += 32 ; 347 } 348q2 += 32 ; 349 } 350sumf += dall * isum - dmin * summs ; 351 } 352* s = sumf ; 353} 354 355void dot_q3_K_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 356assert (n %QK_K == 0 ); 357 358const uint32_t kmask1 = 0x03030303 ; 359const uint32_t kmask2 = 0x0f0f0f0f ; 360 361const block_q3_K * STRATA_RESTRICT x = (const block_q3_K * )vx ; 362const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 363 364const int nb = n /QK_K ; 365 366// scalar version 367// This function is written like this so the compiler can manage to vectorize most of it 368// Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the 369// manually vectorized version above. Every other version I tried would run at least 4 times slower. 370// The ideal situation would be if we could just write the code once, and the compiler would 371// automatically produce the best possible set of machine instructions, instead of us having to manually 372// write vectorized versions for AVX, ARM_NEON, etc. 373 374int8_t aux8 [QK_K ]; 375int16_t aux16 [8 ]; 376float sums [8 ]; 377int32_t aux32 [8 ]; 378memset (sums ,0 ,8 * sizeof (float )); 379 380uint32_t auxs [4 ]; 381const int8_t * scales = (const int8_t * )auxs ; 382 383float sumf = 0 ; 384for (int i = 0 ;i < nb ;++ i ) { 385const uint8_t * STRATA_RESTRICT q3 = x [i ].qs ; 386const uint8_t * STRATA_RESTRICT hm = x [i ].hmask ; 387const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 388memset (aux32 ,0 ,8 * sizeof (int32_t )); 389int8_t * STRATA_RESTRICT a = aux8 ; 390uint8_t m = 1 ; 391for (int j = 0 ;j < QK_K ;j += 128 ) { 392for (int l = 0 ;l < 32 ;++ l )a [l ]= q3 [l ]& 3 ; 393for (int l = 0 ;l < 32 ;++ l )a [l ]-= (hm [l ]& m ?0 :4 ); 394a += 32 ;m <<=1 ; 395for (int l = 0 ;l < 32 ;++ l )a [l ]= (q3 [l ] >>2 )& 3 ; 396for (int l = 0 ;l < 32 ;++ l )a [l ]-= (hm [l ]& m ?0 :4 ); 397a += 32 ;m <<=1 ; 398for (int l = 0 ;l < 32 ;++ l )a [l ]= (q3 [l ] >>4 )& 3 ; 399for (int l = 0 ;l < 32 ;++ l )a [l ]-= (hm [l ]& m ?0 :4 ); 400a += 32 ;m <<=1 ; 401for (int l = 0 ;l < 32 ;++ l )a [l ]= (q3 [l ] >>6 )& 3 ; 402for (int l = 0 ;l < 32 ;++ l )a [l ]-= (hm [l ]& m ?0 :4 ); 403a += 32 ;m <<=1 ; 404q3 += 32 ; 405 } 406a = aux8 ; 407 408memcpy (auxs ,x [i ].scales ,12 ); 409uint32_t tmp = auxs [2 ]; 410auxs [2 ]= ((auxs [0 ] >>4 )& kmask2 ) | (((tmp >>4 )& kmask1 ) <<4 ); 411auxs [3 ]= ((auxs [1 ] >>4 )& kmask2 ) | (((tmp >>6 )& kmask1 ) <<4 ); 412auxs [0 ]= (auxs [0 ]& kmask2 ) | (((tmp >>0 )& kmask1 ) <<4 ); 413auxs [1 ]= (auxs [1 ]& kmask2 ) | (((tmp >>2 )& kmask1 ) <<4 ); 414for (int j = 0 ;j < QK_K /16 ;++ j ) { 415for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 416for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= (scales [j ]- 32 )* aux16 [l ]; 417q8 += 8 ;a += 8 ; 418for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 419for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= (scales [j ]- 32 )* aux16 [l ]; 420q8 += 8 ;a += 8 ; 421 } 422const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 423for (int l = 0 ;l < 8 ;++ l )sums [l ]+= d * aux32 [l ]; 424 } 425for (int l = 0 ;l < 8 ;++ l )sumf += sums [l ]; 426* s = sumf ; 427} 428 429void dot_q4_K_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 430assert (n %QK_K == 0 ); 431 432const block_q4_K * STRATA_RESTRICT x = (const block_q4_K * )vx ; 433const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 434 435const int nb = n /QK_K ; 436 437static const uint32_t kmask1 = 0x3f3f3f3f ; 438static const uint32_t kmask2 = 0x0f0f0f0f ; 439static const uint32_t kmask3 = 0x03030303 ; 440 441uint32_t utmp [4 ]; 442 443const uint8_t * scales = (const uint8_t * )& utmp [0 ]; 444const uint8_t * mins = (const uint8_t * )& utmp [2 ]; 445 446int8_t aux8 [QK_K ]; 447int16_t aux16 [8 ]; 448float sums [8 ]; 449int32_t aux32 [8 ]; 450memset (sums ,0 ,8 * sizeof (float )); 451 452float sumf = 0 ; 453for (int i = 0 ;i < nb ;++ i ) { 454const uint8_t * STRATA_RESTRICT q4 = x [i ].qs ; 455const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 456memset (aux32 ,0 ,8 * sizeof (int32_t )); 457int8_t * STRATA_RESTRICT a = aux8 ; 458for (int j = 0 ;j < QK_K /64 ;++ j ) { 459for (int l = 0 ;l < 32 ;++ l )a [l ]= (int8_t )(q4 [l ]& 0xF ); 460a += 32 ; 461for (int l = 0 ;l < 32 ;++ l )a [l ]= (int8_t )(q4 [l ] >>4 ); 462a += 32 ;q4 += 32 ; 463 } 464memcpy (utmp ,x [i ].scales ,12 ); 465utmp [3 ]= ((utmp [2 ] >>4 )& kmask2 ) | (((utmp [1 ] >>6 )& kmask3 ) <<4 ); 466const uint32_t uaux = utmp [1 ]& kmask1 ; 467utmp [1 ]= (utmp [2 ]& kmask2 ) | (((utmp [0 ] >>6 )& kmask3 ) <<4 ); 468utmp [2 ]= uaux ; 469utmp [0 ] &=kmask1 ; 470 471int sumi = 0 ; 472for (int j = 0 ;j < QK_K /16 ;++ j )sumi += y [i ].bsums [j ]* mins [j /2 ]; 473a = aux8 ; 474int is = 0 ; 475for (int j = 0 ;j < QK_K /32 ;++ j ) { 476int32_t scale = scales [is ++ ]; 477for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 478for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 479q8 += 8 ;a += 8 ; 480for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 481for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 482q8 += 8 ;a += 8 ; 483for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 484for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 485q8 += 8 ;a += 8 ; 486for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 487for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 488q8 += 8 ;a += 8 ; 489 } 490const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 491for (int l = 0 ;l < 8 ;++ l )sums [l ]+= d * aux32 [l ]; 492const float dmin = fp16_to_fp32 (x [i ].dmin )* y [i ].d ; 493sumf -= dmin * sumi ; 494 } 495for (int l = 0 ;l < 8 ;++ l )sumf += sums [l ]; 496* s = sumf ; 497} 498 499void dot_q5_K_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 500assert (n %QK_K == 0 ); 501 502const block_q5_K * STRATA_RESTRICT x = (const block_q5_K * )vx ; 503const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 504 505const int nb = n /QK_K ; 506 507static const uint32_t kmask1 = 0x3f3f3f3f ; 508static const uint32_t kmask2 = 0x0f0f0f0f ; 509static const uint32_t kmask3 = 0x03030303 ; 510 511uint32_t utmp [4 ]; 512 513const uint8_t * scales = (const uint8_t * )& utmp [0 ]; 514const uint8_t * mins = (const uint8_t * )& utmp [2 ]; 515 516int8_t aux8 [QK_K ]; 517int16_t aux16 [8 ]; 518float sums [8 ]; 519int32_t aux32 [8 ]; 520memset (sums ,0 ,8 * sizeof (float )); 521 522float sumf = 0 ; 523for (int i = 0 ;i < nb ;++ i ) { 524const uint8_t * STRATA_RESTRICT q4 = x [i ].qs ; 525const uint8_t * STRATA_RESTRICT hm = x [i ].qh ; 526const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 527memset (aux32 ,0 ,8 * sizeof (int32_t )); 528int8_t * STRATA_RESTRICT a = aux8 ; 529uint8_t m = 1 ; 530for (int j = 0 ;j < QK_K /64 ;++ j ) { 531for (int l = 0 ;l < 32 ;++ l )a [l ]= (int8_t )(q4 [l ]& 0xF ); 532for (int l = 0 ;l < 32 ;++ l )a [l ]+= (hm [l ]& m ?16 :0 ); 533a += 32 ;m <<=1 ; 534for (int l = 0 ;l < 32 ;++ l )a [l ]= (int8_t )(q4 [l ] >>4 ); 535for (int l = 0 ;l < 32 ;++ l )a [l ]+= (hm [l ]& m ?16 :0 ); 536a += 32 ;m <<=1 ; 537q4 += 32 ; 538 } 539memcpy (utmp ,x [i ].scales ,12 ); 540utmp [3 ]= ((utmp [2 ] >>4 )& kmask2 ) | (((utmp [1 ] >>6 )& kmask3 ) <<4 ); 541const uint32_t uaux = utmp [1 ]& kmask1 ; 542utmp [1 ]= (utmp [2 ]& kmask2 ) | (((utmp [0 ] >>6 )& kmask3 ) <<4 ); 543utmp [2 ]= uaux ; 544utmp [0 ] &=kmask1 ; 545 546int sumi = 0 ; 547for (int j = 0 ;j < QK_K /16 ;++ j )sumi += y [i ].bsums [j ]* mins [j /2 ]; 548a = aux8 ; 549int is = 0 ; 550for (int j = 0 ;j < QK_K /32 ;++ j ) { 551int32_t scale = scales [is ++ ]; 552for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 553for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 554q8 += 8 ;a += 8 ; 555for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 556for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 557q8 += 8 ;a += 8 ; 558for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 559for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 560q8 += 8 ;a += 8 ; 561for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 562for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 563q8 += 8 ;a += 8 ; 564 } 565const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 566for (int l = 0 ;l < 8 ;++ l )sums [l ]+= d * aux32 [l ]; 567const float dmin = fp16_to_fp32 (x [i ].dmin )* y [i ].d ; 568sumf -= dmin * sumi ; 569 } 570for (int l = 0 ;l < 8 ;++ l )sumf += sums [l ]; 571* s = sumf ; 572} 573 574void dot_q6_K_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 575assert (n %QK_K == 0 ); 576 577const block_q6_K * STRATA_RESTRICT x = (const block_q6_K * )vx ; 578const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 579 580const int nb = n /QK_K ; 581 582int8_t aux8 [QK_K ]; 583int16_t aux16 [8 ]; 584float sums [8 ]; 585int32_t aux32 [8 ]; 586memset (sums ,0 ,8 * sizeof (float )); 587 588float sumf = 0 ; 589for (int i = 0 ;i < nb ;++ i ) { 590const uint8_t * STRATA_RESTRICT q4 = x [i ].ql ; 591const uint8_t * STRATA_RESTRICT qh = x [i ].qh ; 592const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 593memset (aux32 ,0 ,8 * sizeof (int32_t )); 594int8_t * STRATA_RESTRICT a = aux8 ; 595for (int j = 0 ;j < QK_K ;j += 128 ) { 596for (int l = 0 ;l < 32 ;++ l ) { 597a [l + 0 ]= (int8_t )((q4 [l + 0 ]& 0xF ) | (((qh [l ] >>0 )& 3 ) <<4 ))- 32 ; 598a [l + 32 ]= (int8_t )((q4 [l + 32 ]& 0xF ) | (((qh [l ] >>2 )& 3 ) <<4 ))- 32 ; 599a [l + 64 ]= (int8_t )((q4 [l + 0 ] >>4 ) | (((qh [l ] >>4 )& 3 ) <<4 ))- 32 ; 600a [l + 96 ]= (int8_t )((q4 [l + 32 ] >>4 ) | (((qh [l ] >>6 )& 3 ) <<4 ))- 32 ; 601 } 602a += 128 ; 603q4 += 64 ; 604qh += 32 ; 605 } 606a = aux8 ; 607int is = 0 ; 608for (int j = 0 ;j < QK_K /16 ;++ j ) { 609int scale = x [i ].scales [is ++ ]; 610for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 611for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 612q8 += 8 ;a += 8 ; 613for (int l = 0 ;l < 8 ;++ l )aux16 [l ]= q8 [l ]* a [l ]; 614for (int l = 0 ;l < 8 ;++ l )aux32 [l ]+= scale * aux16 [l ]; 615q8 += 8 ;a += 8 ; 616 } 617const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 618for (int l = 0 ;l < 8 ;++ l )sums [l ]+= d * aux32 [l ]; 619 } 620for (int l = 0 ;l < 8 ;++ l )sumf += sums [l ]; 621* s = sumf ; 622} 623 624void dot_iq1_s_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 625assert (n %QK_K == 0 ); 626 627const block_iq1_s * STRATA_RESTRICT x = (const block_iq1_s * )vx ; 628const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 629 630const int nb = n /QK_K ; 631 632float sumf = 0 ; 633for (int i = 0 ;i < nb ;i ++ ) { 634 635const int8_t * q8 = y [i ].qs ; 636const uint8_t * qs = x [i ].qs ; 637const uint16_t * qh = x [i ].qh ; 638 639int sumi = 0 ,sumi1 = 0 ; 640for (int ib = 0 ;ib < QK_K /32 ;++ ib ) { 641const int ls = 2 * ((qh [ib ] >>12 )& 7 )+ 1 ; 642const int delta = qh [ib ]& 0x8000 ?-1 :1 ; 643int lsum = 0 ; 644for (int l = 0 ;l < 4 ;++ l ) { 645const int8_t * grid = (const int8_t * )(iq1s_grid + (qs [l ] | (((qh [ib ] >>3 * l )& 7 ) <<8 ))); 646for (int j = 0 ;j < 8 ;++ j ) { 647lsum += q8 [j ]* grid [j ]; 648 } 649q8 += 8 ; 650 } 651sumi += ls * lsum ; 652sumi1 += ls * delta * (y [i ].bsums [2 * ib + 0 ]+ y [i ].bsums [2 * ib + 1 ]); 653qs += 4 ; 654 } 655 656sumf += fp16_to_fp32 (x [i ].d )* y [i ].d * (sumi + IQ1S_DELTA * sumi1 ); 657 } 658 659* s = sumf ; 660} 661 662void dot_iq1_m_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 663assert (n %QK_K == 0 ); 664 665const block_iq1_m * STRATA_RESTRICT x = (const block_iq1_m * )vx ; 666const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 667 668const int nb = n /QK_K ; 669 670iq1m_scale_t scale ; 671 672int sum1 [2 ],sum2 [2 ],delta [4 ]; 673 674float sumf = 0 ; 675for (int i = 0 ;i < nb ;i ++ ) { 676 677const int8_t * q8 = y [i ].qs ; 678const uint8_t * qs = x [i ].qs ; 679const uint8_t * qh = x [i ].qh ; 680const uint16_t * sc = (const uint16_t * )x [i ].scales ; 681 682scale .u16 = (sc [0 ] >>12 ) | ((sc [1 ] >>8 )& 0x00f0 ) | ((sc [2 ] >>4 )& 0x0f00 ) | (sc [3 ]& 0xf000 ); 683 684int sumi1 = 0 ,sumi2 = 0 ; 685for (int ib = 0 ;ib < QK_K /32 ;++ ib ) { 686delta [0 ]= qh [0 ]& 0x08 ?-1 :1 ; 687delta [1 ]= qh [0 ]& 0x80 ?-1 :1 ; 688delta [2 ]= qh [1 ]& 0x08 ?-1 :1 ; 689delta [3 ]= qh [1 ]& 0x80 ?-1 :1 ; 690sum1 [0 ]= sum1 [1 ]= sum2 [0 ]= sum2 [1 ]= 0 ; 691for (int l = 0 ;l < 4 ;++ l ) { 692const int8_t * grid = (const int8_t * )(iq1s_grid + (qs [l ] | (((uint16_t )qh [l /2 ] << (8 - 4 * (l %2 )))& 0x700 ))); 693int lsum1 = 0 ,lsum2 = 0 ; 694for (int j = 0 ;j < 8 ;++ j ) { 695lsum1 += q8 [j ]* grid [j ]; 696lsum2 += q8 [j ]; 697 } 698q8 += 8 ; 699sum1 [l /2 ]+= lsum1 ; 700sum2 [l /2 ]+= lsum2 * delta [l ]; 701 } 702 703const int ls1 = 2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 0 ))& 0x7 )+ 1 ; 704const int ls2 = 2 * ((sc [ib /2 ] >> (6 * (ib %2 )+ 3 ))& 0x7 )+ 1 ; 705 706sumi1 += sum1 [0 ]* ls1 + sum1 [1 ]* ls2 ; 707sumi2 += sum2 [0 ]* ls1 + sum2 [1 ]* ls2 ; 708qs += 4 ; 709qh += 2 ; 710 } 711 712sumf += fp16_to_fp32 (scale .f16 )* y [i ].d * (sumi1 + IQ1M_DELTA * sumi2 ); 713 } 714 715* s = sumf ; 716} 717 718void dot_iq2_xxs_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 719assert (n %QK_K == 0 ); 720 721const block_iq2_xxs * STRATA_RESTRICT x = (const block_iq2_xxs * )vx ; 722const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 723 724const int nb = n /QK_K ; 725 726uint32_t aux32 [2 ]; 727const uint8_t * aux8 = (const uint8_t * )aux32 ; 728 729float sumf = 0.f ; 730for (int i = 0 ;i < nb ;++ i ) { 731const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 732const uint16_t * STRATA_RESTRICT q2 = x [i ].qs ; 733const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 734int32_t bsum = 0 ; 735for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 736memcpy (aux32 ,q2 ,2 * sizeof (uint32_t )); 737q2 += 4 ; 738const uint32_t ls = 2 * (aux32 [1 ] >>28 )+ 1 ; 739int32_t sumi = 0 ; 740for (int l = 0 ;l < 4 ;++ l ) { 741const uint8_t * grid = (const uint8_t * )(iq2xxs_grid + aux8 [l ]); 742const uint8_t signs = ksigns_iq2xs [(aux32 [1 ] >>7 * l )& 127 ]; 743for (int j = 0 ;j < 8 ;++ j ) { 744sumi += grid [j ]* q8 [j ]* (signs & kmask_iq2xs [j ] ?-1 :1 ); 745 } 746q8 += 8 ; 747 } 748bsum += sumi * ls ; 749 } 750sumf += d * bsum ; 751 } 752* s = 0.125f * sumf ; 753} 754 755void dot_iq2_xs_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 756assert (n %QK_K == 0 ); 757 758const block_iq2_xs * STRATA_RESTRICT x = (const block_iq2_xs * )vx ; 759const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 760 761const int nb = n /QK_K ; 762 763float sumf = 0.f ; 764for (int i = 0 ;i < nb ;++ i ) { 765const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 766const uint16_t * STRATA_RESTRICT q2 = x [i ].qs ; 767const uint8_t * STRATA_RESTRICT sc = x [i ].scales ; 768const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 769int32_t bsum = 0 ; 770for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 771const uint16_t ls1 = 2 * (sc [ib32 ]& 0xf )+ 1 ; 772const uint16_t ls2 = 2 * (sc [ib32 ] >>4 )+ 1 ; 773int32_t sumi = 0 ; 774for (int l = 0 ;l < 2 ;++ l ) { 775const uint8_t * grid = (const uint8_t * )(iq2xs_grid + (q2 [l ]& 511 )); 776const uint8_t signs = ksigns_iq2xs [q2 [l ] >>9 ]; 777for (int j = 0 ;j < 8 ;++ j ) { 778sumi += grid [j ]* q8 [j ]* (signs & kmask_iq2xs [j ] ?-1 :1 ); 779 } 780q8 += 8 ; 781 } 782bsum += sumi * ls1 ; 783sumi = 0 ; 784for (int l = 2 ;l < 4 ;++ l ) { 785const uint8_t * grid = (const uint8_t * )(iq2xs_grid + (q2 [l ]& 511 )); 786const uint8_t signs = ksigns_iq2xs [q2 [l ] >>9 ]; 787for (int j = 0 ;j < 8 ;++ j ) { 788sumi += grid [j ]* q8 [j ]* (signs & kmask_iq2xs [j ] ?-1 :1 ); 789 } 790q8 += 8 ; 791 } 792bsum += sumi * ls2 ; 793q2 += 4 ; 794 } 795sumf += d * bsum ; 796 } 797* s = 0.125f * sumf ; 798} 799 800void dot_iq2_s_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 801assert (n %QK_K == 0 ); 802 803const block_iq2_s * STRATA_RESTRICT x = (const block_iq2_s * )vx ; 804const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 805 806const int nb = n /QK_K ; 807 808float sumf = 0 ; 809for (int i = 0 ;i < nb ;i ++ ) { 810 811const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 812const int8_t * q8 = y [i ].qs ; 813const uint8_t * qs = x [i ].qs ; 814const uint8_t * qh = x [i ].qh ; 815const uint8_t * signs = qs + QK_K /8 ; 816 817int bsum = 0 ; 818for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 819int ls1 = 1 + 2 * (x [i ].scales [ib32 ]& 0xf ); 820int ls2 = 1 + 2 * (x [i ].scales [ib32 ] >>4 ); 821int sumi1 = 0 ,sumi2 = 0 ; 822for (int l = 0 ;l < 2 ;++ l ) { 823const uint8_t * grid = (const uint8_t * )(iq2s_grid + (qs [l ] | (qh [ib32 ] << (8 - 2 * l )& 0x300 ))); 824for (int j = 0 ;j < 8 ;++ j ) { 825sumi1 += q8 [j ]* grid [j ]* (signs [l ]& kmask_iq2xs [j ] ?-1 :1 ); 826 } 827q8 += 8 ; 828 } 829for (int l = 2 ;l < 4 ;++ l ) { 830const uint8_t * grid = (const uint8_t * )(iq2s_grid + (qs [l ] | (qh [ib32 ] << (8 - 2 * l )& 0x300 ))); 831for (int j = 0 ;j < 8 ;++ j ) { 832sumi2 += q8 [j ]* grid [j ]* (signs [l ]& kmask_iq2xs [j ] ?-1 :1 ); 833 } 834q8 += 8 ; 835 } 836bsum += ls1 * sumi1 + ls2 * sumi2 ; 837qs += 4 ; 838signs += 4 ; 839 } 840 841sumf += d * bsum ; 842 } 843 844* s = 0.125f * sumf ; 845} 846 847void dot_iq3_xxs_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 848assert (n %QK_K == 0 ); 849 850const block_iq3_xxs * STRATA_RESTRICT x = (const block_iq3_xxs * )vx ; 851const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 852 853const int nb = n /QK_K ; 854 855uint32_t aux32 ; 856 857float sumf = 0.f ; 858for (int i = 0 ;i < nb ;++ i ) { 859const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 860const uint8_t * STRATA_RESTRICT q3 = x [i ].qs ; 861const uint8_t * STRATA_RESTRICT gas = x [i ].qs + QK_K /4 ; 862const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 863int32_t bsum = 0 ; 864for (int ib32 = 0 ;ib32 < QK_K /32 ;++ ib32 ) { 865memcpy (& aux32 ,gas ,sizeof (uint32_t ));gas += sizeof (uint32_t ); 866const uint32_t ls = 2 * (aux32 >>28 )+ 1 ; 867int32_t sumi = 0 ; 868for (int l = 0 ;l < 4 ;++ l ) { 869const uint8_t * grid1 = (const uint8_t * )(iq3xxs_grid + q3 [2 * l + 0 ]); 870const uint8_t * grid2 = (const uint8_t * )(iq3xxs_grid + q3 [2 * l + 1 ]); 871const uint8_t signs = ksigns_iq2xs [(aux32 >>7 * l )& 127 ]; 872for (int j = 0 ;j < 4 ;++ j ) { 873sumi += grid1 [j ]* q8 [j + 0 ]* (signs & kmask_iq2xs [j + 0 ] ?-1 :1 ); 874sumi += grid2 [j ]* q8 [j + 4 ]* (signs & kmask_iq2xs [j + 4 ] ?-1 :1 ); 875 } 876q8 += 8 ; 877 } 878q3 += 8 ; 879bsum += sumi * ls ; 880 } 881sumf += d * bsum ; 882 } 883* s = 0.25f * sumf ; 884} 885 886void dot_iq3_s_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 887assert (n %QK_K == 0 ); 888 889const block_iq3_s * STRATA_RESTRICT x = (const block_iq3_s * )vx ; 890const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 891 892const int nb = n /QK_K ; 893 894float sumf = 0.f ; 895for (int i = 0 ;i < nb ;++ i ) { 896const float d = fp16_to_fp32 (x [i ].d )* y [i ].d ; 897const uint8_t * STRATA_RESTRICT qs = x [i ].qs ; 898const uint8_t * STRATA_RESTRICT qh = x [i ].qh ; 899const uint8_t * STRATA_RESTRICT signs = x [i ].signs ; 900const int8_t * STRATA_RESTRICT q8 = y [i ].qs ; 901int32_t bsum = 0 ; 902for (int ib32 = 0 ;ib32 < QK_K /32 ;ib32 += 2 ) { 903const uint32_t ls1 = 2 * (x [i ].scales [ib32 /2 ]& 0xf )+ 1 ; 904const uint32_t ls2 = 2 * (x [i ].scales [ib32 /2 ] >>4 )+ 1 ; 905int32_t sumi = 0 ; 906for (int l = 0 ;l < 4 ;++ l ) { 907const uint8_t * grid1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [ib32 + 0 ] << (8 - 2 * l ))& 256 ))); 908const uint8_t * grid2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [ib32 + 0 ] << (7 - 2 * l ))& 256 ))); 909for (int j = 0 ;j < 4 ;++ j ) { 910sumi += grid1 [j ]* q8 [j + 0 ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1 :1 ); 911sumi += grid2 [j ]* q8 [j + 4 ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1 :1 ); 912 } 913q8 += 8 ; 914 } 915qs += 8 ; 916signs += 4 ; 917bsum += sumi * ls1 ; 918sumi = 0 ; 919for (int l = 0 ;l < 4 ;++ l ) { 920const uint8_t * grid1 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 0 ] | ((qh [ib32 + 1 ] << (8 - 2 * l ))& 256 ))); 921const uint8_t * grid2 = (const uint8_t * )(iq3s_grid + (qs [2 * l + 1 ] | ((qh [ib32 + 1 ] << (7 - 2 * l ))& 256 ))); 922for (int j = 0 ;j < 4 ;++ j ) { 923sumi += grid1 [j ]* q8 [j + 0 ]* (signs [l ]& kmask_iq2xs [j + 0 ] ?-1 :1 ); 924sumi += grid2 [j ]* q8 [j + 4 ]* (signs [l ]& kmask_iq2xs [j + 4 ] ?-1 :1 ); 925 } 926q8 += 8 ; 927 } 928qs += 8 ; 929signs += 4 ; 930bsum += sumi * ls2 ; 931 } 932sumf += d * bsum ; 933 } 934* s = sumf ; 935} 936 937void dot_iq4_xs_q8_K_generic (int n ,float * STRATA_RESTRICT s ,const void * STRATA_RESTRICT vx ,const void * STRATA_RESTRICT vy ) { 938assert (n %QK_K == 0 ); 939 940const block_iq4_xs * STRATA_RESTRICT x = (const block_iq4_xs * )vx ; 941const block_q8_K * STRATA_RESTRICT y = (const block_q8_K * )vy ; 942 943const int nb = n /QK_K ; 944 945float sumf = 0 ; 946for (int ibl = 0 ;ibl < nb ;++ ibl ) { 947const float d4d8 = fp16_to_fp32 (x [ibl ].d )* y [ibl ].d ; 948uint16_t h = x [ibl ].scales_h ; 949const uint8_t * qs = x [ibl ].qs ; 950const int8_t * q8 = y [ibl ].qs ; 951for (int ib = 0 ;ib < QK_K /32 ;ib += 2 ) { 952const uint8_t ls1 = (x [ibl ].scales_l [ib /2 ]& 0xf ) | ((h <<4 )& 0x30 ); 953const uint8_t ls2 = (x [ibl ].scales_l [ib /2 ] >>4 ) | ((h <<2 )& 0x30 ); 954h >>=4 ; 955const float d1 = d4d8 * (ls1 - 32 ); 956const float d2 = d4d8 * (ls2 - 32 ); 957int sumi1 = 0 ,sumi2 = 0 ; 958for (int j = 0 ;j < 16 ;++ j ) { 959sumi1 += q8 [j + 0 ]* kvalues_iq4nl [qs [j ]& 0xf ]; 960sumi2 += q8 [j + 16 ]* kvalues_iq4nl [qs [j ] >>4 ]; 961 } 962sumf += d1 * (sumi1 + sumi2 ); 963qs += 16 ; 964q8 += 32 ; 965sumi1 = sumi2 = 0 ; 966for (int j = 0 ;j < 16 ;++ j ) { 967sumi1 += q8 [j + 0 ]* kvalues_iq4nl [qs [j ]& 0xf ]; 968sumi2 += q8 [j + 16 ]* kvalues_iq4nl [qs [j ] >>4 ]; 969 } 970sumf += d2 * (sumi1 + sumi2 ); 971qs += 16 ; 972q8 += 32 ; 973 } 974 } 975* s = sumf ; 976} 977 978}// namespace generic 979 980namespace { 981#define ENTRY (T ,ACT ,NAME ) {generic::dot_##NAME##_generic, Type::T, Type::ACT, Arith::Generic, Isa::Scalar, "generic_" #NAME} 982const DotKernel kGeneric []= { 983ENTRY (Q2_0 ,Q8_0 ,q2_0_q8_0 ),ENTRY (Q4_0 ,Q8_0 ,q4_0_q8_0 ),ENTRY (Q4_1 ,Q8_1 ,q4_1_q8_1 ),ENTRY (Q5_0 ,Q8_0 ,q5_0_q8_0 ), 984ENTRY (Q5_1 ,Q8_1 ,q5_1_q8_1 ),ENTRY (Q8_0 ,Q8_0 ,q8_0_q8_0 ),ENTRY (IQ4_NL ,Q8_0 ,iq4_nl_q8_0 ),ENTRY (MXFP4 ,Q8_0 ,mxfp4_q8_0 ), 985ENTRY (NVFP4 ,Q8_0 ,nvfp4_q8_0 ),ENTRY (Q2_K ,Q8_K ,q2_K_q8_K ),ENTRY (Q3_K ,Q8_K ,q3_K_q8_K ),ENTRY (Q4_K ,Q8_K ,q4_K_q8_K ), 986ENTRY (Q5_K ,Q8_K ,q5_K_q8_K ),ENTRY (Q6_K ,Q8_K ,q6_K_q8_K ),ENTRY (IQ1_S ,Q8_K ,iq1_s_q8_K ),ENTRY (IQ1_M ,Q8_K ,iq1_m_q8_K ), 987ENTRY (IQ2_XXS ,Q8_K ,iq2_xxs_q8_K ),ENTRY (IQ2_XS ,Q8_K ,iq2_xs_q8_K ),ENTRY (IQ2_S ,Q8_K ,iq2_s_q8_K ),ENTRY (IQ3_XXS ,Q8_K ,iq3_xxs_q8_K ), 988ENTRY (IQ3_S ,Q8_K ,iq3_s_q8_K ),ENTRY (IQ4_XS ,Q8_K ,iq4_xs_q8_K ), 989}; 990#undef ENTRY 991}// namespace 992 993DotTable generic_dot_table ()noexcept {return {kGeneric , (int ) (sizeof kGeneric /sizeof kGeneric [0 ])}; } 994 995void dot_ref (Type w ,int n ,float * s ,const void * wrow ,const void * act ) { 996for (const DotKernel & k :kGeneric ) 997if (k .weight == w ) { 998k .fn (n ,s ,wrow ,act ); 999return ; 1000 } 1001 std::abort (); 1002} 1003 1004}// namespace strata::quant