okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/kq_avx2.cpp - Unsloth UD-Q4_K_XL's expert rows (Q4_K gate/up, Q5_1 and Q8_0 down) for several 2// tokens at once, BIT-EXACT against ggml-cpu's own dot products. 3// 4// ggml-cpu is built for AVX2 here (STRATA_PORTABLE: GGML_AVX2=ON, GGML_AVX512=OFF), so its vec_dot for these formats 5// is the __AVX2__ branch of ggml-cpu/arch/x86/quants.c: ggml_vec_dot_q4_K_q8_K, ggml_vec_dot_q5_1_q8_1, 6// ggml_vec_dot_q8_0_q8_0. The functions below are those branches with the loop over tokens moved inside the loop 7// over blocks: everything that depends only on the weights (the 4-bit unpacking, the 6-bit scales and mins, the 8// fifth bits, the absolute values of the Q8_0 weights, the fp16 scales) is done once per block, and every token then 9// runs exactly ggml's integer chain and ggml's float operations in ggml's order - so each token's result is ggml's 10// to the bit (native_expert_parity checks it), whatever the number of tokens. Unlike the i-quant kernels (#152) 11// the group size therefore never changes an answer. 12// 13// The gain is the weight-side work and the weight bytes, read once per verify window instead of once per token. 14#include "strata/kernels/cpu/kq_avx2.hpp" 15 16#define GGML_COMMON_DECL_CPP 17#define GGML_COMMON_IMPL_CPP 18#include "ggml-common.h" 19 20#include <immintrin.h> 21 22#include <cmath> 23#include <cstring> 24 25namespace strata::kernels::cpu { 26 27void lat_quant_q8_1_qs_avx2 (const float * x ,const float * d_ptr ,int8_t * q ) { 28// Probe-validated (probe_qs.cpp, 100k random blocks bit-identical): divide by d (NOT 1/d mul - 29// the ulp difference moves exact .5 ties), round ties-away-from-zero like roundf (floor(x+0.5) 30// for x>=0, ceil(x-0.5) = -floor(-x+0.5) for x<0 - vroundps has no away mode), clamp, then 31// narrow int32->int16->int8 with the per-lane packs semantics (probe3/probe5). 32const __m256 dv = _mm256_set1_ps (* d_ptr ); 33const __m256 lim = _mm256_set1_ps (127.0f ); 34const __m256 nlim = _mm256_sub_ps (_mm256_setzero_ps (),lim ); 35const __m256 half = _mm256_set1_ps (0.5f ); 36const __m256 zero = _mm256_setzero_ps (); 37for (int j = 0 ;j < 32 ;j += 8 ) { 38const __m256 v = _mm256_div_ps (_mm256_loadu_ps (x + j ),dv ); 39const __m256 fa = _mm256_floor_ps (_mm256_add_ps (v ,half ));// floor(x+0.5) 40const __m256 tneg = _mm256_sub_ps (zero ,_mm256_sub_ps (v ,half ));// -x+0.5 41const __m256 neg = _mm256_sub_ps (zero ,_mm256_floor_ps (tneg ));// ceil(x-0.5) 42const __m256 mask = _mm256_cmp_ps (v ,zero ,_CMP_GE_OQ ); 43const __m256 rf = _mm256_blendv_ps (neg ,fa ,mask ); 44const __m256 cl = _mm256_min_ps (_mm256_max_ps (rf ,nlim ),lim ); 45const __m256i r = _mm256_cvtps_epi32 (cl );// 8 x int32 in [-127, 127] 46const __m256i w16 = _mm256_packs_epi32 (r ,r );// per-lane int16: lo=[r0..r3,dup] 47const __m128i lo16 = _mm256_castsi256_si128 (w16 ); 48const __m128i hi16 = _mm256_extracti128_si256 (w16 ,1 );// hi=[r4..r7,dup] 49const __m128i m16 = _mm_unpacklo_epi64 (lo16 ,hi16 );// [r0..r3, r4..r7] (int16) 50const __m128i w8 = _mm_packs_epi16 (m16 ,m16 );// low 64: r0..r7 as int8 51_mm_storel_epi64 ((__m128i * ) (q + j ),w8 );// q[j..j+7] 52 } 53} 54 55namespace { 56 57constexpr int MAXT = 8 ;// the verify window's tokens per group (kVerifyMaxT) 58 59inline float h2f (ggml_half h ) { 60uint16_t u ; 61 std::memcpy (& u ,& h ,2 ); 62return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )u ))); 63} 64// the fp16 pair {d, dmin} / {d, m} / {d, s} a block starts with (a union in ggml-common.h's C++ declarations) 65inline float half_at (const void * block ,int i ) { 66ggml_half h ; 67 std::memcpy (& h , (const uint8_t * )block + 2 * i ,2 ); 68return h2f (h ); 69} 70 71// ---- ggml's helpers (ggml-cpu/arch/x86/quants.c, the AVX2 / non-VNNI forms), verbatim 72inline float hsum_float_8 (const __m256 x ) { 73__m128 res = _mm256_extractf128_ps (x ,1 ); 74res = _mm_add_ps (res ,_mm256_castps256_ps128 (x )); 75res = _mm_add_ps (res ,_mm_movehl_ps (res ,res )); 76res = _mm_add_ss (res ,_mm_movehdup_ps (res )); 77return _mm_cvtss_f32 (res ); 78} 79inline __m256i bytes_from_bits_32 (const uint8_t * x ) { 80uint32_t x32 ; 81 std::memcpy (& x32 ,x ,sizeof (uint32_t )); 82const __m256i shuf_mask = _mm256_set_epi64x (0x0303030303030303 ,0x0202020202020202 ,0x0101010101010101 , 830x0000000000000000 ); 84__m256i bytes = _mm256_shuffle_epi8 (_mm256_set1_epi32 ((int )x32 ),shuf_mask ); 85const __m256i bit_mask = _mm256_set1_epi64x (0x7fbfdfeff7fbfdfe ); 86bytes = _mm256_or_si256 (bytes ,bit_mask ); 87return _mm256_cmpeq_epi8 (bytes ,_mm256_set1_epi64x (-1 )); 88} 89inline __m256i bytes_from_nibbles_32 (const uint8_t * rsi ) { 90const __m128i tmp = _mm_loadu_si128 ((const __m128i * )rsi ); 91const __m256i bytes = _mm256_insertf128_si256 (_mm256_castsi128_si256 (tmp ),_mm_srli_epi16 (tmp ,4 ),1 ); 92const __m256i lowMask = _mm256_set1_epi8 (0xF ); 93return _mm256_and_si256 (lowMask ,bytes ); 94} 95inline __m256 sum_i16_pairs_float (const __m256i x ) { 96const __m256i ones = _mm256_set1_epi16 (1 ); 97return _mm256_cvtepi32_ps (_mm256_madd_epi16 (ones ,x )); 98} 99inline __m256 mul_sum_us8_pairs_float (const __m256i ax ,const __m256i sy ) { 100return sum_i16_pairs_float (_mm256_maddubs_epi16 (ax ,sy )); 101} 102alignas(32 )const uint8_t k_shuffle [256 ]= { 1030 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,1 , 1042 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 ,2 ,3 , 1054 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 ,4 ,5 , 1066 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 ,6 ,7 , 1078 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 ,8 ,9 , 10810 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 ,10 ,11 , 10912 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 ,12 ,13 , 11014 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 ,14 ,15 }; 111inline __m256i get_scale_shuffle_k4 (int i ) {return _mm256_loadu_si256 ((const __m256i * )k_shuffle + i ); } 112 113// ---- Q4_K x Q8_K (ggml_vec_dot_q4_K_q8_K, __AVX2__) 114void q4k_dot (const block_q4_K * x ,int nb ,const void * const * act ,int nt ,float * res ) { 115static const uint32_t kmask1 = 0x3f3f3f3f ,kmask2 = 0x0f0f0f0f ,kmask3 = 0x03030303 ; 116const __m256i m4 = _mm256_set1_epi8 (0xF ); 117__m256 acc [MAXT ]; 118__m128 acc_m [MAXT ]; 119for (int t = 0 ;t < nt ;++ t ) {acc [t ]= _mm256_setzero_ps ();acc_m [t ]= _mm_setzero_ps (); } 120for (int i = 0 ;i < nb ;++ i ) { 121const float xd = half_at (& x [i ],0 ),xdmin = half_at (& x [i ],1 ); 122uint32_t utmp [4 ]; 123 std::memcpy (utmp ,x [i ].scales ,12 ); 124utmp [3 ]= ((utmp [2 ] >>4 )& kmask2 ) | (((utmp [1 ] >>6 )& kmask3 ) <<4 ); 125const uint32_t uaux = utmp [1 ]& kmask1 ; 126utmp [1 ]= (utmp [2 ]& kmask2 ) | (((utmp [0 ] >>6 )& kmask3 ) <<4 ); 127utmp [2 ]= uaux ; 128utmp [0 ] &=kmask1 ; 129const __m256i mins_and_scales = _mm256_cvtepu8_epi16 (_mm_set_epi32 ((int )utmp [3 ], (int )utmp [2 ], 130 (int )utmp [1 ], (int )utmp [0 ])); 131const __m128i mins = _mm256_extracti128_si256 (mins_and_scales ,1 ); 132const __m128i sc128 = _mm256_extracti128_si256 (mins_and_scales ,0 ); 133const __m256i scales = _mm256_insertf128_si256 (_mm256_castsi128_si256 (sc128 ),sc128 ,1 ); 134__m256i scale_l [4 ],scale_h [4 ],q4l [4 ],q4h [4 ]; 135const uint8_t * q4 = x [i ].qs ; 136for (int j = 0 ;j < QK_K /64 ;++ j ) { 137scale_l [j ]= _mm256_shuffle_epi8 (scales ,get_scale_shuffle_k4 (2 * j + 0 )); 138scale_h [j ]= _mm256_shuffle_epi8 (scales ,get_scale_shuffle_k4 (2 * j + 1 )); 139const __m256i q4bits = _mm256_loadu_si256 ((const __m256i * )q4 ); 140q4 += 32 ; 141q4l [j ]= _mm256_and_si256 (q4bits ,m4 ); 142q4h [j ]= _mm256_and_si256 (_mm256_srli_epi16 (q4bits ,4 ),m4 ); 143 } 144for (int t = 0 ;t < nt ;++ t ) { 145const block_q8_K * y = (const block_q8_K * )act [t ]+ i ; 146const float d = y -> d * xd ; 147const float dmin = - y -> d * xdmin ; 148const int8_t * q8 = y -> qs ; 149const __m256i q8sums = _mm256_loadu_si256 ((const __m256i * )y -> bsums ); 150const __m128i q8s = _mm_hadd_epi16 (_mm256_extracti128_si256 (q8sums ,0 ),_mm256_extracti128_si256 (q8sums ,1 )); 151const __m128i prod = _mm_madd_epi16 (mins ,q8s ); 152acc_m [t ]= _mm_fmadd_ps (_mm_set1_ps (dmin ),_mm_cvtepi32_ps (prod ),acc_m [t ]); 153__m256i sumi = _mm256_setzero_si256 (); 154for (int j = 0 ;j < QK_K /64 ;++ j ) { 155const __m256i q8l = _mm256_loadu_si256 ((const __m256i * )q8 ); 156q8 += 32 ; 157__m256i p16l = _mm256_maddubs_epi16 (q4l [j ],q8l ); 158p16l = _mm256_madd_epi16 (scale_l [j ],p16l ); 159const __m256i q8h = _mm256_loadu_si256 ((const __m256i * )q8 ); 160q8 += 32 ; 161__m256i p16h = _mm256_maddubs_epi16 (q4h [j ],q8h ); 162p16h = _mm256_madd_epi16 (scale_h [j ],p16h ); 163sumi = _mm256_add_epi32 (sumi ,_mm256_add_epi32 (p16l ,p16h )); 164 } 165acc [t ]= _mm256_fmadd_ps (_mm256_set1_ps (d ),_mm256_cvtepi32_ps (sumi ),acc [t ]); 166 } 167 } 168for (int t = 0 ;t < nt ;++ t ) { 169__m128 m = acc_m [t ]; 170m = _mm_add_ps (m ,_mm_movehl_ps (m ,m )); 171m = _mm_add_ss (m ,_mm_movehdup_ps (m )); 172res [t ]= hsum_float_8 (acc [t ])+ _mm_cvtss_f32 (m ); 173 } 174} 175 176// ---- Q5_1 x Q8_1 (ggml_vec_dot_q5_1_q8_1, __AVX2__) 177template < int NT > 178void q5_1_dot (const block_q5_1 * x ,int nb ,const void * const * act ,float * res ) { 179__m256 acc [NT ]; 180float summs [NT ]; 181for (int t = 0 ;t < NT ;++ t ) {acc [t ]= _mm256_setzero_ps ();summs [t ]= 0.0f ; } 182for (int ib = 0 ;ib < nb ;++ ib ) { 183const __m256 dx = _mm256_set1_ps (half_at (& x [ib ],0 )); 184const float xm = half_at (& x [ib ],1 ); 185__m256i qx = bytes_from_nibbles_32 (x [ib ].qs ); 186__m256i bxhi = bytes_from_bits_32 (x [ib ].qh ); 187bxhi = _mm256_and_si256 (bxhi ,_mm256_set1_epi8 (0x10 )); 188qx = _mm256_or_si256 (qx ,bxhi ); 189for (int t = 0 ;t < NT ;++ t ) { 190const block_q8_1 * y = (const block_q8_1 * )act [t ]+ ib ; 191summs [t ]+= xm * half_at (y ,1 ); 192const __m256 dy = _mm256_set1_ps (half_at (y ,0 )); 193const __m256i qy = _mm256_loadu_si256 ((const __m256i * )y -> qs ); 194const __m256 q = mul_sum_us8_pairs_float (qx ,qy ); 195acc [t ]= _mm256_fmadd_ps (q ,_mm256_mul_ps (dx ,dy ),acc [t ]); 196 } 197 } 198for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ])+ summs [t ]; 199} 200 201// ---- Q8_0 x Q8_0 (ggml_vec_dot_q8_0_q8_0, __AVX2__; mul_sum_i8_pairs_float without AVX-VNNI-INT8) 202template < int NT > 203void q8_0_dot (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 204__m256 acc [NT ]; 205for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 206for (int ib = 0 ;ib < nb ;++ ib ) { 207const float xd = h2f (x [ib ].d ); 208const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 209const __m256i ax = _mm256_sign_epi8 (qx ,qx ); 210for (int t = 0 ;t < NT ;++ t ) { 211const block_q8_0 * y = (const block_q8_0 * )act [t ]+ ib ; 212const __m256 d = _mm256_set1_ps (xd * h2f (y -> d )); 213const __m256i qy = _mm256_loadu_si256 ((const __m256i * )y -> qs ); 214const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 215const __m256 q = mul_sum_us8_pairs_float (ax ,sy ); 216acc [t ]= _mm256_fmadd_ps (d ,q ,acc [t ]); 217 } 218 } 219for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 220} 221 222void q5_1_dot_nt (const block_q5_1 * x ,int nb ,const void * const * act ,int nt ,float * res ) { 223for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 224const int k = nt - t0 < 4 ?nt - t0 :4 ; 225switch (k ) { 226case 1 :q5_1_dot < 1 > (x ,nb ,act + t0 ,res + t0 );break ; 227case 2 :q5_1_dot < 2 > (x ,nb ,act + t0 ,res + t0 );break ; 228case 3 :q5_1_dot < 3 > (x ,nb ,act + t0 ,res + t0 );break ; 229default :q5_1_dot < 4 > (x ,nb ,act + t0 ,res + t0 );break ; 230 } 231 } 232} 233 234void q8_0_dot_nt (const block_q8_0 * x ,int nb ,const void * const * act ,int nt ,float * res ) { 235for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 236const int k = nt - t0 < 4 ?nt - t0 :4 ; 237switch (k ) { 238case 1 :q8_0_dot < 1 > (x ,nb ,act + t0 ,res + t0 );break ; 239case 2 :q8_0_dot < 2 > (x ,nb ,act + t0 ,res + t0 );break ; 240case 3 :q8_0_dot < 3 > (x ,nb ,act + t0 ,res + t0 );break ; 241default :q8_0_dot < 4 > (x ,nb ,act + t0 ,res + t0 );break ; 242 } 243 } 244} 245 246// ---- Q8_0 x Q8_1 (edge0 latent MoE's U stage; the weight row is Q8_0 blocks, the activation Q8_1's). 247// The Q8_1 block's d sits at +0 (f16) and its qs at +4; the sum word at +2 is not read. NT tokens share 248// one loaded weight row (the scalar mode-7 kernel re-read it per token). 249template < int NT > 250void lat_q8_0_q8_1_dot (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 251__m256 acc [NT ]; 252for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 253for (int ib = 0 ;ib < nb ;++ ib ) { 254const float xd = h2f (x [ib ].d ); 255const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 256const __m256i ax = _mm256_sign_epi8 (qx ,qx ); 257for (int t = 0 ;t < NT ;++ t ) { 258const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ;// block_q8_1's stride 259const float dy = h2f (* (const uint16_t * ) (yb + 0 )); 260const __m256 d = _mm256_set1_ps (xd * dy ); 261const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 )); 262const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 263const __m256 q = mul_sum_us8_pairs_float (ax ,sy ); 264acc [t ]= _mm256_fmadd_ps (d ,q ,acc [t ]); 265 } 266 } 267for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 268} 269 270void dot_rows (int type ,const uint8_t * row ,int n ,const void * const * act ,int nt ,float * res ) { 271switch (type ) { 272case 12 :q4k_dot ((const block_q4_K * )row ,n /QK_K ,act ,nt ,res );break ; 273case 7 :q5_1_dot_nt ((const block_q5_1 * )row ,n /QK5_1 ,act ,nt ,res );break ; 274case 8 :q8_0_dot_nt ((const block_q8_0 * )row ,n /QK8_0 ,act ,nt ,res );break ; 275default :break ; 276 } 277} 278 279}// namespace 280 281void bf16_rows_dot_multi (const uint16_t * w ,int rows ,int cols ,const float * x ,int nt ,float * out ) { 282for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 283const int m = nt - t0 < 4 ?nt - t0 :4 ; 284const float * x0 = x + (size_t )t0 * cols ; 285const float * x1 = x0 + cols ; 286const float * x2 = x1 + cols ; 287const float * x3 = x2 + cols ; 288for (int r = 0 ;r < rows ;++ r ) { 289const uint16_t * wr = w + (size_t )r * (size_t )cols ; 290__m256 a0 = _mm256_setzero_ps (),a1 = _mm256_setzero_ps (),a2 = _mm256_setzero_ps (), 291a3 = _mm256_setzero_ps (); 292for (int c = 0 ;c + 8 <=cols ;c += 8 ) { 293const __m256 wf = _mm256_castsi256_ps ( 294_mm256_slli_epi32 (_mm256_cvtepu16_epi32 (_mm_loadu_si128 ((const __m128i * ) (wr + c ))),16 )); 295a0 = _mm256_fmadd_ps (wf ,_mm256_loadu_ps (x0 + c ),a0 ); 296if (m > 1 )a1 = _mm256_fmadd_ps (wf ,_mm256_loadu_ps (x1 + c ),a1 ); 297if (m > 2 )a2 = _mm256_fmadd_ps (wf ,_mm256_loadu_ps (x2 + c ),a2 ); 298if (m > 3 )a3 = _mm256_fmadd_ps (wf ,_mm256_loadu_ps (x3 + c ),a3 ); 299 } 300out [(size_t )t0 * rows + r ]= hsum_float_8 (a0 ); 301if (m > 1 )out [(size_t ) (t0 + 1 )* rows + r ]= hsum_float_8 (a1 ); 302if (m > 2 )out [(size_t ) (t0 + 2 )* rows + r ]= hsum_float_8 (a2 ); 303if (m > 3 )out [(size_t ) (t0 + 3 )* rows + r ]= hsum_float_8 (a3 ); 304 } 305 } 306} 307 308void bf16_rows_dot (const uint16_t * w ,int rows ,int cols ,const float * x ,float * out ) { 309for (int r = 0 ;r < rows ;++ r ) { 310const uint16_t * wr = w + (size_t )r * (size_t )cols ; 311__m256 acc = _mm256_setzero_ps (); 312int c = 0 ; 313for (;c + 8 <=cols ;c += 8 ) { 314const __m256i h = _mm256_cvtepu16_epi32 (_mm_loadu_si128 ((const __m128i * ) (wr + c ))); 315acc = _mm256_fmadd_ps (_mm256_castsi256_ps (_mm256_slli_epi32 (h ,16 )),_mm256_loadu_ps (x + c ),acc ); 316 } 317float s = hsum_float_8 (acc ); 318for (;c < cols ;++ c ) { 319const uint32_t b = (uint32_t )wr [c ] <<16 ; 320float f ; 321 std::memcpy (& f ,& b ,4 ); 322s += f * x [c ]; 323 } 324out [r ]= s ; 325 } 326} 327 328void kq256_gu_rows (int type ,const uint8_t * blob ,size_t gu_row ,size_t up_off ,int n ,const void * const * act ,int nt , 329float * const * ff ,int r0 ,int r1 ) { 330for (int t0 = 0 ;t0 < nt ;t0 += MAXT ) { 331const int m = nt - t0 < MAXT ?nt - t0 :MAXT ; 332float g [MAXT ],u [MAXT ]; 333for (int r = r0 ;r < r1 ;++ r ) { 334dot_rows (type ,blob + (size_t )r * gu_row ,n ,act + t0 ,m ,g ); 335dot_rows (type ,blob + up_off + (size_t )r * gu_row ,n ,act + t0 ,m ,u ); 336// native_gu_rows' own SwiGLU expression: the same bits as the per-token ggml path 337for (int t = 0 ;t < m ;++ t )ff [t0 + t ][r ]= (g [t ] / (1.f + std::exp (- g [t ])))* u [t ]; 338 } 339 } 340} 341 342void kq256_rows (int type ,const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt ,float * const * out , 343int r0 ,int r1 ) { 344for (int t0 = 0 ;t0 < nt ;t0 += MAXT ) { 345const int m = nt - t0 < MAXT ?nt - t0 :MAXT ; 346float s [MAXT ]; 347for (int r = r0 ;r < r1 ;++ r ) { 348dot_rows (type ,w + (size_t )r * row_bytes ,n ,act + t0 ,m ,s ); 349for (int t = 0 ;t < m ;++ t )out [t0 + t ][r ]= s [t ]; 350 } 351 } 352} 353 354// ---- Q4_0 x Q8_1 (Q4DU: edge0 latent MoE's U stage, the Q4_0 pack). The weight block is 355// {f16 d; uint8 nib[16]} - element j in byte j's low nibble, j+16 in its high one, value 356// (code - 8) * d. The same maddubs sign trick as lat_q8_0_q8_1_dot above (u8 x s8 with the 357// weight's signs folded into the q8_1 codes), the same float order (acc += (dx*dw) * sumi per 358// block, FMA chain), sumi the whole 32-value integer dot of one block. 359template < int NT > 360void lat_q4_0_q8_1_dot (const uint8_t * x ,int nb ,const void * const * act ,float * res ) { 361__m256 acc [NT ]; 362for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 363const __m256i mask = _mm256_set1_epi8 (0xF ); 364const __m256i ones = _mm256_set1_epi16 (1 ); 365for (int ib = 0 ;ib < nb ;++ ib ) { 366const uint8_t * wb = x + (size_t )ib * 18 ; 367const float xd = h2f (* (const ggml_half * )wb ); 368// 16 nibble bytes -> 32 PACKED code bytes (maddubs runs on bytes): the file's own 369// bytes_from_nibbles_32 (ggml's) leaves byte j's low nibble at position j and its high 370// nibble at position j+16 - exactly block_q4_0's element order. Centred: code - 8. 371const __m256i q4bytes = _mm256_and_si256 (bytes_from_nibbles_32 (wb + 2 ),mask ); 372const __m256i w = _mm256_sub_epi8 (q4bytes ,_mm256_set1_epi8 (8 ));// 32 centred codes, packed 373const __m256i aw = _mm256_sign_epi8 (w ,w );// |w|, the u8 side of maddubs 374for (int t = 0 ;t < NT ;++ t ) { 375const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ;// block_q8_1's stride 376const float dy = h2f (* (const ggml_half * ) (yb + 0 )); 377const __m256 d = _mm256_set1_ps (xd * dy ); 378const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 ));// 32 q8_1 codes, packed 379// signed x signed byte dot: maddubs(aw, sign(qy, w)) = |w| * (qy flipped where w < 0) 380// = w * qy, the q8_0_dot trick verbatim (a zero code contributes 0 either way). 381const __m256i sq = _mm256_sign_epi8 (qy ,w ); 382__m256i p16 = _mm256_maddubs_epi16 (aw ,sq ); 383__m256i si = _mm256_madd_epi16 (ones ,p16 ); 384const __m256 q = _mm256_cvtepi32_ps (si ); 385acc [t ]= _mm256_fmadd_ps (d ,q ,acc [t ]); 386 } 387 } 388for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 389} 390 391void lat_q4_0_q8_1_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt , 392float * const * out ,int r0 ,int r1 ) { 393// Q4DU: the Q4_0 form of lat_q8_0_q8_1_rows (mode 7's Q4 arm), rows [r0, r1) 394const int nb = n /32 ; 395for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 396const int m = nt - t0 < 4 ?nt - t0 :4 ; 397for (int r = r0 ;r < r1 ;++ r ) { 398const uint8_t * row = w + (size_t )r * row_bytes ; 399float s [4 ]; 400switch (m ) { 401case 1 :lat_q4_0_q8_1_dot < 1 > (row ,nb ,act + t0 ,s );break ; 402case 2 :lat_q4_0_q8_1_dot < 2 > (row ,nb ,act + t0 ,s );break ; 403case 3 :lat_q4_0_q8_1_dot < 3 > (row ,nb ,act + t0 ,s );break ; 404default :lat_q4_0_q8_1_dot < 4 > (row ,nb ,act + t0 ,s );break ; 405 } 406for (int t = 0 ;t < m ;++ t )out [t0 + t ][r ]= s [t ]; 407 } 408 } 409} 410 411void lat_q8_0_q8_1_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt , 412float * const * out ,int r0 ,int r1 ) { 413// edge0 latent MoE's U stage: Q8_0 weight rows x Q8_1 activations (the pool's mode 7), rows [r0, r1) 414const int nb = n /32 ; 415for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 416const int m = nt - t0 < 4 ?nt - t0 :4 ; 417for (int r = r0 ;r < r1 ;++ r ) { 418const block_q8_0 * row = (const block_q8_0 * ) (w + (size_t )r * row_bytes ); 419float s [4 ]; 420switch (m ) { 421case 1 :lat_q8_0_q8_1_dot < 1 > (row ,nb ,act + t0 ,s );break ; 422case 2 :lat_q8_0_q8_1_dot < 2 > (row ,nb ,act + t0 ,s );break ; 423case 3 :lat_q8_0_q8_1_dot < 3 > (row ,nb ,act + t0 ,s );break ; 424default :lat_q8_0_q8_1_dot < 4 > (row ,nb ,act + t0 ,s );break ; 425 } 426for (int t = 0 ;t < m ;++ t )out [t0 + t ][r ]= s [t ]; 427 } 428 } 429} 430 431}// namespace strata::kernels::cpu