okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). 2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. 3// src/ds4/ds4_iq_cpu.cpp - a GGUF's low-bit experts on the CPU lane (see ds4_gguf_cpu.hpp): the GLM CPU lane's native 4// experts (src/kernels/cpu/native_expert.cpp), whose arithmetic is ggml-cpu's - and for IQ3_XXS / IQ3_S (Maya-S's 5// down, Maya-M's gate / up / down) rows of our own, the same numbers bit for bit, faster. 6#include "ds4_gguf_cpu.hpp" 7// Strata port: the low-bit (IQ) experts have no CPU lane here (they need Maya's native_expert split-base rows); the stub 8// branch below answers for them. MXFP4, the source format, has its own CPU rows (ds4_gguf_cpu.cpp). 9#undef STRATA_NATIVE_EXPERTS 10#if defined(STRATA_NATIVE_EXPERTS ) 11#include "strata/kernels/cpu/native_expert.hpp" // (only with STRATA_NATIVE_EXPERTS) 12 13#define GGML_COMMON_DECL_CPP 14#define GGML_COMMON_IMPL_CPP 15#include "ggml-common.h" 16#include "ggml-cpu.h" 17#include "ggml.h" 18#endif 19 20#include <algorithm> 21#include <chrono> 22#include <cmath> 23#include <cstdio> 24#include <cstdlib> 25#include <cstring> 26#include <limits> 27#include <mutex> 28#include <string> 29#include <vector> 30 31#if defined(__x86_64__ )|| defined(_M_X64 ) 32#define IQ_X86 1 33#include <immintrin.h> 34#if defined(_MSC_VER ) 35#include <intrin.h> 36#else 37#include <cpuid.h> 38#endif 39#endif 40#if defined(IQ_X86 )&& (defined(__GNUC__ )|| defined(__clang__ )) 41#define IQ_AVX2 __attribute__((target("avx2,fma"))) 42#else 43#define IQ_AVX2 44#endif 45 46namespace strata::ds4::gguf { 47 48#if defined(STRATA_NATIVE_EXPERTS ) 49namespace kc= strata::dsv41k::cpu; 50namespace { 51kc::NativeFmt native (const IqCpu & f ) {// (the fields the rows read; no lookups) 52 kc::NativeFmt n ; 53n .gu_type = f .tg ; 54n .d_type = f .td ; 55n .gu_act = f .gu_act ; 56n .d_act = f .d_act ; 57n .n_embd = f .n_embd ; 58n .n_ff = f .n_ff ; 59n .gu_row = f .gu_row ; 60n .d_row = f .d_row ; 61n .up_off = f .gu_row * (size_t )f .n_ff ; 62n .down_off = 2 * n .up_off ; 63n .bytes = n .down_off + f .d_row * (size_t )f .n_embd ; 64n .act_bytes = f .act_bytes ; 65n .h_bytes = f .h_bytes ; 66return n ; 67} 68 69// ---- what this CPU runs: AVX2 + FMA (the OS saving the YMM state), and AVX-512 F / BW + VBMI with BMI2 (the ZMM state) 70struct IqCpuIsa { 71bool avx2 = false,w512 = false; 72}; 73IqCpuIsa iq_isa () { 74IqCpuIsa c ; 75#if defined(IQ_X86 ) 76unsigned r [4 ]= {0 ,0 ,0 ,0 }; 77auto cpuid = [& ](unsigned leaf ,unsigned sub ) { 78#if defined(_MSC_VER ) 79int x [4 ]; 80__cpuidex (x , (int )leaf , (int )sub ); 81for (int i = 0 ;i < 4 ;++ i )r [i ]= (unsigned )x [i ]; 82#else 83__cpuid_count (leaf ,sub ,r [0 ],r [1 ],r [2 ],r [3 ]); 84#endif 85 }; 86cpuid (0 ,0 ); 87if (r [0 ]< 7 )return c ; 88cpuid (1 ,0 ); 89if (!((r [2 ] >>12 )& 1u )|| !((r [2 ] >>27 )& 1u )|| !((r [2 ] >>28 )& 1u ))return c ;// FMA, OSXSAVE, AVX 90#if defined(_MSC_VER ) 91const unsigned long long xcr0 = _xgetbv (0 ); 92#else 93unsigned lo = 0 ,hi = 0 ; 94 __asm__volatile ("xgetbv" :"=a" (lo ),"=d" (hi ) :"c" (0 )); 95const unsigned long long xcr0 = ((unsigned long long )hi <<32 ) |lo ; 96#endif 97if ((xcr0 & 0x6 )!= 0x6 )return c ; 98cpuid (7 ,0 ); 99c .avx2 = (r [1 ] >>5 )& 1u ; 100c .w512 = c .avx2 && (xcr0 & 0xE6 )== 0xE6 && ((r [1 ] >>16 )& 1u )&& ((r [1 ] >>30 )& 1u )&& ((r [2 ] >>1 )& 1u )&& 101 ((r [1 ] >>8 )& 1u );// AVX-512 F, BW, VBMI; BMI2 102#endif 103return c ; 104} 105const IqCpuIsa & isa () { 106static const IqCpuIsa c = iq_isa (); 107return c ; 108} 109 110// ---- IQ3_XXS / IQ3_S rows of our own. ggml-cpu's AVX2 dots build each 32-value chunk's grid magnitudes from eight 111// scalar table loads and inserts, and redo it for every token; here the grid comes in with one vpgatherdd from the 112// same table, the signs are built in registers (the GLM rows' sgn7_vec / sgn_vec), and every token of a call applies 113// the decoded chunk. The arithmetic is ggml's to the bit: the same exact int32 lanes per block (lane l: the chunks' 114// grid entries l, each times its 2 s + 1), then for each block fma(d_x d_y, lanes, acc) and hsum_float_8 (IQ3_XXS: 115// times 0.25) - checked against ggml-cpu's own dot once per type (iq3_check), else ggml's rows are used. 116inline float h2f (uint16_t h ) {// F16 -> F32, exactly 117const uint32_t s = (uint32_t ) (h & 0x8000u ) <<16 ,e = (h >>10 )& 0x1fu ,m = h & 0x3ffu ; 118uint32_t b ; 119if (e == 0 ) { 120const float f = (float )m * 5.9604644775390625e-8f ;// m 2^-24 (exact) 121 std::memcpy (& b ,& f ,4 ); 122b |=s ; 123 }else if (e == 31 ) { 124b = s |0x7f800000u | (m <<13 ); 125 }else { 126b = s | ((e + 112u ) <<23 ) | (m <<13 ); 127 } 128float f ; 129 std::memcpy (& f ,& b ,4 ); 130return f ; 131} 132inline uint32_t u32 (const uint8_t * p ) { 133uint32_t v ; 134 std::memcpy (& v ,p ,4 ); 135return v ; 136} 137 138#if defined(IQ_X86 ) 139IQ_AVX2 inline float hsum_ggml (__m256 x ) {// ggml's hsum_float_8, the same additions in the same order 140__m128 r = _mm256_extractf128_ps (x ,1 ); 141r = _mm_add_ps (r ,_mm256_castps256_ps128 (x )); 142r = _mm_add_ps (r ,_mm_movehl_ps (r ,r )); 143r = _mm_add_ss (r ,_mm_movehdup_ps (r )); 144return _mm_cvtss_f32 (r ); 145} 146// IQ2_S / IQ3_S: 32 explicit sign bits -> 32 bytes of -1 / +1 (ggml's bit selector) 147IQ_AVX2 inline __m256i sgn32 (uint32_t m ) { 148const __m256i sh = _mm256_setr_epi8 (0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,// 1492 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ); 150const __m256i sel = _mm256_set1_epi64x ((long long )0x8040201008040201ull ); 151const __m256i b = _mm256_and_si256 (_mm256_shuffle_epi8 (_mm256_set1_epi32 ((int )m ),sh ),sel ); 152return _mm256_or_si256 (_mm256_cmpeq_epi8 (b ,sel ),_mm256_set1_epi8 (1 )); 153} 154// table entries into a vector without the shuffle port (ggml's _mm256_set_epi64x / _epi32 of table loads ends up in 155// inserts, one shuffle each; here each entry is a broadcast load and the lanes are put together by dword blends) 156IQ_AVX2 inline __m256i bq (const void * p ) {return _mm256_castpd_si256 (_mm256_broadcast_sd ((const double * )p )); } 157IQ_AVX2 inline __m256i bd (const void * p ) {return _mm256_castps_si256 (_mm256_broadcast_ss ((const float * )p )); } 158IQ_AVX2 inline __m256i tab4 (const uint64_t * t ,unsigned a ,unsigned b ,unsigned c ,unsigned d ) {// qwords t[a] t[b] t[c] t[d] 159return _mm256_blend_epi32 (_mm256_blend_epi32 (bq (t + a ),bq (t + b ),0x0C ),_mm256_blend_epi32 (bq (t + c ),bq (t + d ),0xC0 ), 1600xF0 ); 161} 162IQ_AVX2 inline __m256i tab8 (const uint32_t * t ,const unsigned * i ) {// dwords t[i[0]] .. t[i[7]] 163const __m256i a = _mm256_blend_epi32 (bd (t + i [0 ]),bd (t + i [1 ]),0x02 ),b = _mm256_blend_epi32 (bd (t + i [2 ]),bd (t + i [3 ]),0x08 ); 164const __m256i c = _mm256_blend_epi32 (bd (t + i [4 ]),bd (t + i [5 ]),0x20 ),e = _mm256_blend_epi32 (bd (t + i [6 ]),bd (t + i [7 ]),0x80 ); 165return _mm256_blend_epi32 (_mm256_blend_epi32 (a ,b ,0x0C ),_mm256_blend_epi32 (c ,e ,0xC0 ),0xF0 ); 166} 167// the 8 sign bytes (-1 / +1) of each 7-bit sign index with its parity bit: ggml's keven_signs_q2xs, as +-1 bytes 168alignas(64 )uint64_t g_ks64 [128 ]; 169void build_ks64 () { 170for (int i = 0 ;i < 128 ;++ i ) { 171int par = 0 ; 172for (int b = 0 ;b < 7 ;++ b )par ^= (i >>b )& 1 ; 173const int s = i | (par <<7 ); 174uint64_t v = 0 ; 175for (int j = 0 ;j < 8 ;++ j )v |= (uint64_t ) (((s >>j )& 1 ) ?0xFFu :0x01u ) << (8 * j ); 176g_ks64 [i ]= v ; 177 } 178} 179IQ_AVX2 inline __m256i sgn7t (uint32_t w ) {// a chunk's four 7-bit sign indices (bits 0..27 of w) 180return tab4 (g_ks64 ,w & 127u , (w >>7 )& 127u , (w >>14 )& 127u , (w >>21 )& 127u ); 181} 182IQ_AVX2 inline __m256i sc16 (int a ,int b ) {// int16 lanes 0-7 a (values 0-15), 8-15 b (values 16-31) 183return _mm256_inserti128_si256 (_mm256_castsi128_si256 (_mm_set1_epi16 ((short )a )),_mm_set1_epi16 ((short )b ),1 ); 184} 185bool g_gather18 = false,g_gather21 = false;// the IQ3_XXS / IQ3_S grids by vpgatherdd (own(): if faster here) 186// IQ2_XXS's grid entry with its signs applied, for every grid index g and sign index i, at g << 7 | i (256 KB, built on 187// first use): one lookup instead of two; the products are then (q8 + 128) x signed grid less 128 x the grid's pair sums 188// (vpmaddubsw takes u8 x s8), the same integers as ggml's |grid| x signed q8 189alignas(64 )uint64_t g_ct2x [256 * 128 ]; 190bool g_ct16 = false;// (own(): if faster here) 191void build_ct2x () { 192for (int g = 0 ;g < 256 ;++ g ) 193for (int i = 0 ;i < 128 ;++ i ) { 194const uint64_t sg = g_ks64 [i ];// (+-1 bytes) 195uint64_t v = 0 ; 196for (int j = 0 ;j < 8 ;++ j ) { 197const int gb = (int ) ((iq2xxs_grid [g ] >> (8 * j ))& 255u ); 198const int sb = ((sg >> (8 * j ))& 255u )== 0xFFu ?- gb :gb ; 199v |= (uint64_t ) (uint8_t ) (int8_t )sb << (8 * j ); 200 } 201g_ct2x [(size_t )g <<7 | (size_t )i ]= v ; 202 } 203} 204 205// rows [r0, r1) (out[t][r - r0]) of NT tokens; TY 16 = IQ2_XXS, 22 = IQ2_S, 18 = IQ3_XXS, 21 = IQ3_S. A pair of 206// 32-value chunks a step: grid magnitudes g1 / g2, sign vectors s1 / s2 (-1 / +1), scales c1 / c2 - decoded once - then 207// every token: q8 times the signs, vpmaddubsw against the grid, vpmaddwd by the scales, into ggml's 8 int32 lanes 208template < int TY ,int NT > 209IQ_AVX2 void iq_rows_avx2 (const uint8_t * w ,size_t rb ,int nb ,const block_q8_K * const * y ,float * const * out ,int r0 , 210int r1 ) { 211const __m256i sub = _mm256_setr_epi32 (0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ); 212const bool gat = TY == 18 ?g_gather18 :TY == 21 ?g_gather21 : false; 213const bool ct = TY == 16 && g_ct16 ; 214const __m256i k80 = _mm256_set1_epi8 ((char )0x80 ); 215constexpr float K = TY == 18 ?0.25f :TY == 21 ?1.0f :0.125f ; 216constexpr size_t BB = TY == 16 ?sizeof (block_iq2_xxs ) :TY == 22 ?sizeof (block_iq2_s ) : 217TY == 18 ?sizeof (block_iq3_xxs ) :sizeof (block_iq3_s ); 218for (int r = r0 ;r < r1 ;++ r ) { 219const uint8_t * row = w + (size_t )r * rb ; 220__m256 accf [NT ]; 221for (int t = 0 ;t < NT ;++ t )accf [t ]= _mm256_setzero_ps (); 222for (int i = 0 ;i < nb ;++ i ) { 223const uint8_t * blk = row + (size_t )i * BB ; 224__m256i sumi [NT ]; 225for (int t = 0 ;t < NT ;++ t )sumi [t ]= _mm256_setzero_si256 (); 226uint16_t dh ; 227 std::memcpy (& dh ,blk ,2 ); 228for (int ib = 0 ;ib < QK_K /32 ;ib += 2 ) { 229__m256i g1 ,g2 ,s1 ,s2 ,c1 ,c2 ; 230if (TY == 16 ) {// IQ2_XXS: per chunk a word of 4 grid indices, a word of 4 sign indices + the scale 231const uint8_t * q = blk + 2 + 8 * ib ; 232const uint32_t a0 = u32 (q ),a1 = u32 (q + 4 ),b0 = u32 (q + 8 ),b1 = u32 (q + 12 ); 233if (ct ) {// (g1 / g2 the signed grids; s1 / s2 their 128 x pair sums) 234g1 = tab4 (g_ct2x , (a0 & 255u ) <<7 | (a1 & 127u ), ((a0 >>8 )& 255u ) <<7 | ((a1 >>7 )& 127u ), 235 ((a0 >>16 )& 255u ) <<7 | ((a1 >>14 )& 127u ), (a0 >>24 ) <<7 | ((a1 >>21 )& 127u )); 236g2 = tab4 (g_ct2x , (b0 & 255u ) <<7 | (b1 & 127u ), ((b0 >>8 )& 255u ) <<7 | ((b1 >>7 )& 127u ), 237 ((b0 >>16 )& 255u ) <<7 | ((b1 >>14 )& 127u ), (b0 >>24 ) <<7 | ((b1 >>21 )& 127u )); 238s1 = _mm256_maddubs_epi16 (k80 ,g1 ); 239s2 = _mm256_maddubs_epi16 (k80 ,g2 ); 240 }else { 241g1 = tab4 (iq2xxs_grid ,a0 & 255u , (a0 >>8 )& 255u , (a0 >>16 )& 255u ,a0 >>24 ); 242g2 = tab4 (iq2xxs_grid ,b0 & 255u , (b0 >>8 )& 255u , (b0 >>16 )& 255u ,b0 >>24 ); 243s1 = sgn7t (a1 ); 244s2 = sgn7t (b1 ); 245 } 246c1 = _mm256_set1_epi16 ((short ) (2 * (a1 >>28 )+ 1 )); 247c2 = _mm256_set1_epi16 ((short ) (2 * (b1 >>28 )+ 1 )); 248 }else if (TY == 22 ) {// IQ2_S: 10-bit grid indices (qs | 2 bits of qh), explicit signs, 2 scales a chunk 249const uint8_t * qs = blk + 2 + 4 * ib ; 250const uint8_t * qh = blk + 2 + QK_K /4 ; 251const uint8_t * sg = blk + 2 + QK_K /8 + 4 * ib ; 252const uint8_t * scl = blk + 2 + QK_K /4 + QK_K /32 ; 253const unsigned h0 = qh [ib ],h1 = qh [ib + 1 ]; 254g1 = tab4 (iq2s_grid ,qs [0 ] | ((h0 <<8 )& 0x300u ),qs [1 ] | ((h0 <<6 )& 0x300u ),qs [2 ] | ((h0 <<4 )& 0x300u ), 255qs [3 ] | ((h0 <<2 )& 0x300u )); 256g2 = tab4 (iq2s_grid ,qs [4 ] | ((h1 <<8 )& 0x300u ),qs [5 ] | ((h1 <<6 )& 0x300u ),qs [6 ] | ((h1 <<4 )& 0x300u ), 257qs [7 ] | ((h1 <<2 )& 0x300u )); 258s1 = sgn32 (u32 (sg )); 259s2 = sgn32 (u32 (sg + 4 )); 260c1 = sc16 (2 * (scl [ib ]& 0xf )+ 1 ,2 * (scl [ib ] >>4 )+ 1 ); 261c2 = sc16 (2 * (scl [ib + 1 ]& 0xf )+ 1 ,2 * (scl [ib + 1 ] >>4 )+ 1 ); 262 }else if (TY == 18 ) {// IQ3_XXS: 8 grid indices a chunk, then the chunks' sign / scale words 263const uint8_t * q3 = blk + 2 + 8 * ib ; 264const uint8_t * gas = blk + 2 + QK_K /4 + 4 * ib ; 265if (gat ) { 266g1 = _mm256_i32gather_epi32 ((const int * )iq3xxs_grid ,_mm256_cvtepu8_epi32 (_mm_loadl_epi64 ((const __m128i * )q3 )),4 ); 267g2 = _mm256_i32gather_epi32 ((const int * )iq3xxs_grid , 268_mm256_cvtepu8_epi32 (_mm_loadl_epi64 ((const __m128i * ) (q3 + 8 ))),4 ); 269 }else { 270unsigned i1 [8 ],i2 [8 ]; 271for (int k = 0 ;k < 8 ;++ k ) { 272i1 [k ]= q3 [k ]; 273i2 [k ]= q3 [8 + k ]; 274 } 275g1 = tab8 (iq3xxs_grid ,i1 ); 276g2 = tab8 (iq3xxs_grid ,i2 ); 277 } 278const uint32_t a0 = u32 (gas ),a1 = u32 (gas + 4 ); 279s1 = sgn7t (a0 ); 280s2 = sgn7t (a1 ); 281c1 = _mm256_set1_epi16 ((short ) (2 * (a0 >>28 )+ 1 )); 282c2 = _mm256_set1_epi16 ((short ) (2 * (a1 >>28 )+ 1 )); 283 }else {// IQ3_S: 9-bit grid indices (qs | a bit of qh), explicit signs 284const uint8_t * qs = blk + 2 + 8 * ib ; 285const uint8_t * qh = blk + 2 + QK_K /4 ; 286const uint8_t * sg = blk + 2 + QK_K /4 + QK_K /32 + 4 * ib ; 287const int sc = blk [2 + QK_K /4 + QK_K /32 + QK_K /8 + ib /2 ]; 288const unsigned h0 = qh [ib ],h1 = qh [ib + 1 ]; 289if (gat ) { 290const __m256i hb1 = _mm256_slli_epi32 (_mm256_and_si256 (_mm256_srlv_epi32 (_mm256_set1_epi32 ((int )h0 ),sub ),_mm256_set1_epi32 (1 )),8 ); 291const __m256i hb2 = _mm256_slli_epi32 (_mm256_and_si256 (_mm256_srlv_epi32 (_mm256_set1_epi32 ((int )h1 ),sub ),_mm256_set1_epi32 (1 )),8 ); 292g1 = _mm256_i32gather_epi32 ((const int * )iq3s_grid , 293_mm256_or_si256 (_mm256_cvtepu8_epi32 (_mm_loadl_epi64 ((const __m128i * )qs )),hb1 ),4 ); 294g2 = _mm256_i32gather_epi32 ((const int * )iq3s_grid , 295_mm256_or_si256 (_mm256_cvtepu8_epi32 (_mm_loadl_epi64 ((const __m128i * ) (qs + 8 ))),hb2 ),4 ); 296 }else { 297unsigned i1 [8 ],i2 [8 ]; 298for (int k = 0 ;k < 8 ;++ k ) { 299i1 [k ]= qs [k ] | (((h0 >>k )& 1u ) <<8 ); 300i2 [k ]= qs [8 + k ] | (((h1 >>k )& 1u ) <<8 ); 301 } 302g1 = tab8 (iq3s_grid ,i1 ); 303g2 = tab8 (iq3s_grid ,i2 ); 304 } 305s1 = sgn32 (u32 (sg )); 306s2 = sgn32 (u32 (sg + 4 )); 307c1 = _mm256_set1_epi16 ((short ) (2 * (sc & 0xf )+ 1 )); 308c2 = _mm256_set1_epi16 ((short ) (2 * (sc >>4 )+ 1 )); 309 } 310if (ct ) { 311for (int t = 0 ;t < NT ;++ t ) { 312const int8_t * q8 = y [t ][i ].qs + 32 * ib ; 313const __m256i u1 = _mm256_xor_si256 (_mm256_loadu_si256 ((const __m256i * )q8 ),k80 );// (q8 + 128) 314const __m256i u2 = _mm256_xor_si256 (_mm256_loadu_si256 ((const __m256i * ) (q8 + 32 )),k80 ); 315const __m256i d1 = _mm256_sub_epi16 (_mm256_maddubs_epi16 (u1 ,g1 ),s1 ); 316const __m256i d2 = _mm256_sub_epi16 (_mm256_maddubs_epi16 (u2 ,g2 ),s2 ); 317sumi [t ]= _mm256_add_epi32 (sumi [t ],_mm256_add_epi32 (_mm256_madd_epi16 (d1 ,c1 ),_mm256_madd_epi16 (d2 ,c2 ))); 318 } 319continue ; 320 } 321for (int t = 0 ;t < NT ;++ t ) { 322const int8_t * q8 = y [t ][i ].qs + 32 * ib ; 323const __m256i d1 = _mm256_maddubs_epi16 (g1 ,_mm256_sign_epi8 (_mm256_loadu_si256 ((const __m256i * )q8 ),s1 )); 324const __m256i d2 = _mm256_maddubs_epi16 (g2 ,_mm256_sign_epi8 (_mm256_loadu_si256 ((const __m256i * ) (q8 + 32 )),s2 )); 325sumi [t ]= _mm256_add_epi32 (sumi [t ],_mm256_add_epi32 (_mm256_madd_epi16 (d1 ,c1 ),_mm256_madd_epi16 (d2 ,c2 ))); 326 } 327 } 328const float dx = h2f (dh ); 329for (int t = 0 ;t < NT ;++ t ) 330accf [t ]= _mm256_fmadd_ps (_mm256_set1_ps (dx * y [t ][i ].d ),_mm256_cvtepi32_ps (sumi [t ]),accf [t ]); 331 } 332for (int t = 0 ;t < NT ;++ t )out [t ][r - r0 ]= K * hsum_ggml (accf [t ]); 333 } 334} 335 336// ---- the same rows in 512-bit lanes (AVX-512 BW + VBMI, BMI2: Ice Lake and later Intel, Zen 4 / Zen 5), the GLM 337// lane's one-token IQ2_XXS scheme (src/kernels/cpu/iq_avx512_one.cpp) for 3-bit grids: every grid byte is one of 8 338// levels, so an entry is four 3-bit codes (a u16) and the grid's codes fit in registers - IQ3_XXS's 256 in 8 zmm, 339// IQ3_S's 512 in 16; a block's 64 indices are looked up 32 at a time with two-register word permutes and blends, each 340// 64 values' 16 codes spread to their bytes by a word permute, a multishift, an and and a byte shuffle; IQ3_XXS's signs 341// come from pdep and a parity fold, IQ3_S's are a mask register's bits. Two chunks a register, folded to ggml's 8 342// lanes, then ggml's float steps: the same numbers to the bit (own_check). 343#if defined(__GNUC__ )|| defined(__clang__ ) 344#define IQ_512 __attribute__((target("avx512f,avx512bw,avx512vbmi,bmi2,avx2,fma"))) 345#else 346#define IQ_512 347#endif 348alignas(64 )uint16_t g_code3x [256 ],g_code3s [512 ]; 349bool g_codes_ok = false; 350 351IQ_512 inline uint32_t signs4 (uint32_t w ) {// ksigns_iq2xs for the four 7-bit fields of w: i | parity(i) << 7 352const uint64_t s = _pdep_u64 (w & 0x0FFFFFFFu ,0x7F7F7F7Full ); 353uint64_t p = s ^ ((s >>4 )& 0x0F0F0F0Full ); 354p ^= (p >>2 )& 0x03030303ull ; 355p ^= (p >>1 )& 0x01010101ull ; 356return (uint32_t ) (s | ((p & 0x01010101ull ) <<7 )); 357} 358// each grid entry as four 3-bit level codes; false if a byte is not one of the 8 levels (a grid this file does not know) 359bool build_codes3 () { 360static const int lx [8 ]= {4 ,12 ,20 ,28 ,36 ,44 ,52 ,62 },ls [8 ]= {1 ,3 ,5 ,7 ,9 ,11 ,13 ,15 }; 361auto code = [](uint32_t e ,const int * lv )-> int { 362int c = 0 ; 363for (int k = 0 ;k < 4 ;++ k ) { 364const int b = (int ) ((e >> (8 * k ))& 255u ); 365int l = -1 ; 366for (int j = 0 ;j < 8 ;++ j ) 367if (lv [j ]== b )l = j ; 368if (l < 0 )return -1 ; 369c |=l << (3 * k ); 370 } 371return c ; 372 }; 373for (int i = 0 ;i < 256 ;++ i ) { 374const int c = code (iq3xxs_grid [i ],lx ); 375if (c < 0 )return false; 376g_code3x [i ]= (uint16_t )c ; 377 } 378for (int i = 0 ;i < 512 ;++ i ) { 379const int c = code (iq3s_grid [i ],ls ); 380if (c < 0 )return false; 381g_code3s [i ]= (uint16_t )c ; 382 } 383return true; 384} 385IQ_512 inline __m256i fold (__m512i a ) {// a block's 16 int32 lanes -> ggml's 8 (lane k + lane k + 8), exact 386return _mm256_add_epi32 (_mm512_castsi512_si256 (a ),_mm512_extracti64x4_epi64 (a ,1 )); 387} 388IQ_512 inline __m512i scales4 (int s0 ,int s1 ,int s2 ,int s3 ) {// int16 lanes 0-7 s0, 8-15 s1, 16-23 s2, 24-31 s3 389const __m512i idx = _mm512_set_epi16 (3 ,3 ,3 ,3 ,3 ,3 ,3 ,3 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,// 3900 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ); 391const uint64_t packed = (uint64_t ) (uint16_t )s0 | ((uint64_t ) (uint16_t )s1 <<16 ) | 392 ((uint64_t ) (uint16_t )s2 <<32 ) | ((uint64_t ) (uint16_t )s3 <<48 ); 393return _mm512_permutexvar_epi16 (idx ,_mm512_set1_epi64 ((long long )packed )); 394} 395// the codes of 32 word indices: 64 entries a register pair, NP pairs, blended by the index's bits 6, 7 (and 8) 396template < int NP > 397IQ_512 inline __m512i lookup (const __m512i * T ,__m512i idx ) { 398__m512i r [NP ]; 399for (int j = 0 ;j < NP ;++ j )r [j ]= _mm512_permutex2var_epi16 (T [2 * j ],idx ,T [2 * j + 1 ]); 400const __mmask32 m6 = _mm512_test_epi16_mask (idx ,_mm512_set1_epi16 (64 )); 401const __mmask32 m7 = _mm512_test_epi16_mask (idx ,_mm512_set1_epi16 (128 )); 402__m512i a = _mm512_mask_blend_epi16 (m7 ,_mm512_mask_blend_epi16 (m6 ,r [0 ],r [1 ]),_mm512_mask_blend_epi16 (m6 ,r [2 ],r [3 ])); 403if (NP == 8 ) { 404const __mmask32 m8 = _mm512_test_epi16_mask (idx ,_mm512_set1_epi16 (256 )); 405const __m512i b = _mm512_mask_blend_epi16 (m7 ,_mm512_mask_blend_epi16 (m6 ,r [NP > 4 ?4 :0 ],r [NP > 5 ?5 :0 ]), 406_mm512_mask_blend_epi16 (m6 ,r [NP > 6 ?6 :0 ],r [NP > 7 ?7 :0 ])); 407a = _mm512_mask_blend_epi16 (m8 ,a ,b ); 408 } 409return a ; 410} 411 412template < int TY ,int NT > 413IQ_512 void iq3_rows_512 (const uint8_t * w ,size_t rb ,int nb ,const block_q8_K * const * y ,float * const * out ,int r0 , 414int r1 ) { 415constexpr int NTAB = TY == 18 ?8 :16 ; 416constexpr size_t BB = TY == 18 ?sizeof (block_iq3_xxs ) :sizeof (block_iq3_s ); 417__m512i T [NTAB ]; 418for (int k = 0 ;k < NTAB ;++ k ) 419T [k ]= _mm512_load_si512 ((const void * ) ((TY == 18 ?g_code3x :g_code3s )+ 32 * k )); 420// code -> grid byte (IQ3_XXS 4 .. 62, IQ3_S 1 .. 15) in every 128-bit lane; byte b of an output qword <- 3 bits at 421// [0, 3, 6, 9] (its first entry's code, word 0) or [32, 35, 38, 41] (the second's, word 2) 422const __m512i lut = TY == 18 ?_mm512_set4_epi32 (0 ,0 ,0x3E342C24 ,0x1C140C04 ) :_mm512_set4_epi32 (0 ,0 ,0x0F0D0B09 ,0x07050301 ); 423const __m512i ctl = _mm512_set1_epi64 (0x2926232009060300LL ); 424const __m512i seven = _mm512_set1_epi8 (7 ),zero = _mm512_setzero_si512 (); 425__m512i pidx [2 ];// a 64-value group h of 32 codes: output qword m <- entries 16 h + 2 m (word 0), + 1 (word 2) 426for (int h = 0 ;h < 2 ;++ h ) { 427 alignas(64 )uint16_t p [32 ]; 428for (int m = 0 ;m < 8 ;++ m ) { 429p [4 * m ]= p [4 * m + 1 ]= (uint16_t ) (16 * h + 2 * m ); 430p [4 * m + 2 ]= p [4 * m + 3 ]= (uint16_t ) (16 * h + 2 * m + 1 ); 431 } 432pidx [h ]= _mm512_load_si512 ((const void * )p ); 433 } 434for (int r = r0 ;r < r1 ;++ r ) { 435const uint8_t * row = w + (size_t )r * rb ; 436__m256 accf [NT ]; 437for (int t = 0 ;t < NT ;++ t )accf [t ]= _mm256_setzero_ps (); 438for (int i = 0 ;i < nb ;++ i ) { 439const uint8_t * blk = row + (size_t )i * BB ; 440__m512i codes [2 ]; 441if (TY == 18 ) { 442const __m512i q = _mm512_loadu_si512 ((const void * ) (blk + 2 )); 443codes [0 ]= lookup < 4 > (T ,_mm512_cvtepu8_epi16 (_mm512_castsi512_si256 (q ))); 444codes [1 ]= lookup < 4 > (T ,_mm512_cvtepu8_epi16 (_mm512_extracti64x4_epi64 (q ,1 ))); 445 }else { 446const uint8_t * qh = blk + 2 + QK_K /4 ; 447const __m512i hi = _mm512_set1_epi16 (256 ); 448const __m512i ia = _mm512_cvtepu8_epi16 (_mm256_loadu_si256 ((const __m256i * ) (blk + 2 ))); 449const __m512i ib = _mm512_cvtepu8_epi16 (_mm256_loadu_si256 ((const __m256i * ) (blk + 34 ))); 450// (index k of chunk c: qs[8 c + k] | bit k of qh[c] << 8 - the 32 bits of qh[0..3] are chunks 0..3's) 451codes [0 ]= lookup < 8 > (T ,_mm512_or_si512 (ia ,_mm512_maskz_mov_epi16 ((__mmask32 )u32 (qh ),hi ))); 452codes [1 ]= lookup < 8 > (T ,_mm512_or_si512 (ib ,_mm512_maskz_mov_epi16 ((__mmask32 )u32 (qh + 4 ),hi ))); 453 } 454__m512i acc [NT ]; 455for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm512_setzero_si512 (); 456for (int p = 0 ;p < 4 ;++ p ) { 457const __m512i rep = _mm512_permutexvar_epi16 (pidx [p & 1 ],codes [p >>1 ]); 458const __m512i g = _mm512_shuffle_epi8 (lut ,_mm512_and_si512 (_mm512_multishift_epi64_epi8 (ctl ,rep ),seven )); 459uint64_t sm ; 460int sa ,sb ; 461if (TY == 18 ) { 462const uint8_t * sw = blk + 2 + QK_K /4 + 8 * p ; 463const uint32_t wa = u32 (sw ),wb = u32 (sw + 4 ); 464sm = (uint64_t )signs4 (wa ) | ((uint64_t )signs4 (wb ) <<32 ); 465sa = 2 * (int ) (wa >>28 )+ 1 ; 466sb = 2 * (int ) (wb >>28 )+ 1 ; 467 }else { 468 std::memcpy (& sm ,blk + 2 + QK_K /4 + QK_K /32 + 8 * p ,8 ); 469const int sc = blk [2 + QK_K /4 + QK_K /32 + QK_K /8 + p ]; 470sa = 2 * (sc & 0xf )+ 1 ; 471sb = 2 * (sc >>4 )+ 1 ; 472 } 473const __mmask64 m = _cvtu64_mask64 (sm ); 474const __m512i S = scales4 (sa ,sa ,sb ,sb ); 475for (int t = 0 ;t < NT ;++ t ) { 476const __m512i yv = _mm512_loadu_si512 ((const void * ) (y [t ][i ].qs + 64 * p )); 477const __m512i ys = _mm512_mask_sub_epi8 (yv ,m ,zero ,yv ); 478acc [t ]= _mm512_add_epi32 (acc [t ],_mm512_madd_epi16 (_mm512_maddubs_epi16 (g ,ys ),S )); 479 } 480 } 481uint16_t dh ; 482 std::memcpy (& dh ,blk ,2 ); 483const float dx = h2f (dh ); 484for (int t = 0 ;t < NT ;++ t ) 485accf [t ]= _mm256_fmadd_ps (_mm256_set1_ps (dx * y [t ][i ].d ),_mm256_cvtepi32_ps (fold (acc [t ])),accf [t ]); 486 } 487for (int t = 0 ;t < NT ;++ t )out [t ][r - r0 ]= (TY == 18 ?0.25f :1.0f )* hsum_ggml (accf [t ]); 488 } 489} 490 491template < int TY > 492void iq_rows_n (const uint8_t * w ,size_t rb ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 ,int r1 ) { 493static const bool w512 = [] { 494const char * v = std::getenv ("DS4_IQ_AVX2" );// (=1: the AVX2 rows on an AVX-512 CPU, to compare) 495return (TY == 18 || TY == 21 )&& g_codes_ok && isa ().w512 && !(v != nullptr && std::atoi (v )!= 0 ); 496 }(); 497const int nb = n /QK_K ; 498for (int t0 = 0 ;t0 < nt ;t0 += 4 ) { 499const block_q8_K * y [4 ]; 500float * o [4 ]; 501const int c = nt - t0 < 4 ?nt - t0 :4 ; 502for (int t = 0 ;t < c ;++ t ) { 503y [t ]= (const block_q8_K * )act [t0 + t ]; 504o [t ]= out [t0 + t ]; 505 } 506if constexpr (TY == 18 || TY == 21 ) { 507if (w512 ) { 508switch (c ) { 509case 1 :iq3_rows_512 < TY ,1 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 510case 2 :iq3_rows_512 < TY ,2 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 511case 3 :iq3_rows_512 < TY ,3 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 512default :iq3_rows_512 < TY ,4 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 513 } 514continue ; 515 } 516 } 517switch (c ) { 518case 1 :iq_rows_avx2 < TY ,1 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 519case 2 :iq_rows_avx2 < TY ,2 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 520case 3 :iq_rows_avx2 < TY ,3 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 521default :iq_rows_avx2 < TY ,4 > (w ,rb ,nb ,y ,o ,r0 ,r1 );break ; 522 } 523 } 524} 525#endif // IQ_X86 526 527// out[t][r - r0] for the rows of `type` (an own type only) 528void own_rows (int type ,const uint8_t * w ,size_t rb ,int n ,const void * const * act ,int nt ,float * const * out ,int r0 , 529int r1 ) { 530#if defined(IQ_X86 ) 531if (type == GGML_TYPE_IQ2_XXS )return iq_rows_n < 16 > (w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 532if (type == GGML_TYPE_IQ2_S )return iq_rows_n < 22 > (w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 533if (type == GGML_TYPE_IQ3_XXS )return iq_rows_n < 18 > (w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 534if (type == GGML_TYPE_IQ3_S )return iq_rows_n < 21 > (w ,rb ,n ,act ,nt ,out ,r0 ,r1 ); 535#endif 536 (void )type , (void )w , (void )rb , (void )n , (void )act , (void )nt , (void )out , (void )r0 , (void )r1 ; 537} 538 539// our rows against ggml-cpu's dot, bit for bit: random blocks of the type (its F16 scale a small normal value), Q8_K 540// activations from ggml's quantizer, 1, 2, 3, 5 and 9 tokens, 13 rows of 8 blocks 541bool own_check (int type ) { 542ggml_cpu_init (); 543const ggml_type t = (ggml_type )type ; 544const int n = 2048 ,rows = 13 ; 545const size_t rb = ggml_row_size (t ,n ),bb = ggml_type_size (t ),ab = ggml_row_size (GGML_TYPE_Q8_K ,n ); 546uint64_t st = 0x9E3779B97F4A7C15ull ; 547auto next = [& st ] { 548st ^=st <<13 ; 549st ^=st >>7 ; 550st ^=st <<17 ; 551return st ; 552 }; 553 std::vector < uint8_t > w (rb * rows ); 554for (auto & b :w )b = (uint8_t )next (); 555for (size_t o = 0 ;o < w .size ();o += bb ) { 556const ggml_fp16_t d = ggml_fp32_to_fp16 (0.002f + 0.001f * (float ) (next () %16 )); 557 std::memcpy (& w [o ],& d ,sizeof d ); 558 } 559const auto * q8 = ggml_get_type_traits_cpu (GGML_TYPE_Q8_K ); 560const auto dot = ggml_get_type_traits_cpu (t )-> vec_dot ; 561for (int nt : {1 ,2 ,3 ,5 ,9 }) { 562 std::vector < uint8_t > a (ab * (size_t )nt ); 563 std::vector < float > x ((size_t )n ),got ((size_t )nt * rows ),ref ((size_t )nt * rows ); 564 std::vector < const void *> ap ((size_t )nt ); 565 std::vector < float *> op ((size_t )nt ); 566for (int k = 0 ;k < nt ;++ k ) { 567for (auto & v :x )v = ((float ) (next () %2001 )- 1000.0f )* 1e-3f ; 568q8 -> from_float (x .data (),a .data ()+ ab * (size_t )k ,n ); 569ap [(size_t )k ]= a .data ()+ ab * (size_t )k ; 570op [(size_t )k ]= got .data ()+ (size_t )k * rows ; 571 } 572own_rows (type ,w .data (),rb ,n ,ap .data (),nt ,op .data (),0 ,rows ); 573for (int k = 0 ;k < nt ;++ k ) 574for (int r = 0 ;r < rows ;++ r )dot (n ,& ref [(size_t )k * rows + r ],0 ,w .data ()+ (size_t )r * rb ,0 ,ap [(size_t )k ],0 ,1 ); 575if (std::memcmp (got .data (),ref .data (),got .size ()* sizeof (float ))!= 0 )return false; 576 } 577return true; 578} 579 580// the time (us, best of 5 rounds) of 32 rows of 16 blocks of the type against one token: ours with the IQ3 grids as 581// broadcasts + blends (variant 1) or by vpgatherdd (2), or the native lane's rows (0: the GLM lane's / ggml-cpu's) 582double time_variant (int type ,int variant ) { 583const ggml_type t = (ggml_type )type ; 584const int n = 4096 ,rows = 32 ; 585const size_t rb = ggml_row_size (t ,n ),bb = ggml_type_size (t ),ab = ggml_row_size (GGML_TYPE_Q8_K ,n ); 586 std::vector < uint8_t > w (rb * rows ),a (ab ); 587uint32_t s = 12345u ; 588for (auto & b :w )b = (uint8_t ) ((s = s * 1664525u + 1013904223u ) >>24 ); 589for (size_t o = 0 ;o < w .size ();o += bb ) { 590const ggml_fp16_t d = ggml_fp32_to_fp16 (0.003f ); 591 std::memcpy (& w [o ],& d ,sizeof d ); 592 } 593 std::vector < float > x ((size_t )n ),y ((size_t )rows ); 594for (auto & v :x )v = (float ) ((int ) ((s = s * 1664525u + 1013904223u ) >>16 )- 32768 ) /32768.0f ; 595ggml_get_type_traits_cpu (GGML_TYPE_Q8_K )-> from_float (x .data (),a .data (),n ); 596const void * ap [1 ]= {a .data ()}; 597float * yp [1 ]= {y .data ()}; 598 kc::NativeFmt nf ;// (the native rows timed as a gate / up pair of the same rows: 2 x rows dots, as ours below) 599 std::string err ; 600if (variant == 0 && !kc::native_fmt (type ,type ,n ,QK_K ,nf ,err ))return 1e30 ; 601bool * gflag = type == GGML_TYPE_IQ3_XXS ?& g_gather18 :type == GGML_TYPE_IQ3_S ?& g_gather21 : 602type == GGML_TYPE_IQ2_XXS ?& g_ct16 :nullptr ; 603const bool keep = gflag != nullptr && * gflag ; 604if (gflag != nullptr )* gflag = variant >=2 ; 605double best = 1e30 ; 606for (int round = 0 ;round < 6 ;++ round ) { 607const auto t0 = std::chrono::steady_clock::now (); 608for (int rep = 0 ;rep < 4 ;++ rep ) { 609if (variant == 0 ) { 610 kc::native_gu_rows_split (nf ,w .data (),w .data (),ap ,1 ,yp ,0 ,rows , std::numeric_limits < float > ::infinity ()); 611 }else { 612own_rows (type ,w .data (),rb ,n ,ap ,1 ,yp ,0 ,rows ); 613own_rows (type ,w .data (),rb ,n ,ap ,1 ,yp ,0 ,rows ); 614 } 615 } 616const double us = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count (); 617if (round > 0 )best = std::min (best ,us ); 618 } 619if (gflag != nullptr )* gflag = keep ; 620return best ; 621} 622 623// our rows for this type, here: IQ3_XXS / IQ3_S on AVX2 or AVX-512 VBMI, IQ2_XXS / IQ2_S on AVX2 - checked once 624// against ggml-cpu's dot (each variant), then the fastest of ours and the native lane's measured on this CPU (they 625// give the same bits, so the choice changes no answer). DS4_IQ_OWN=0: the native lane's rows; =1: ours without 626// measuring (DS4_IQ_GATHER=1: the IQ3 grids by vpgatherdd); DS4_IQ_AVX2=1: our AVX2 rows on an AVX-512 CPU too. 627bool own (int type ) { 628#if defined(IQ_X86 ) 629int k ; 630switch (type ) { 631case GGML_TYPE_IQ3_XXS :k = 0 ;break ; 632case GGML_TYPE_IQ3_S :k = 1 ;break ; 633case GGML_TYPE_IQ2_XXS :k = 2 ;break ; 634case GGML_TYPE_IQ2_S :k = 3 ;break ; 635default :return false; 636 } 637static int mode = -1 ;// DS4_IQ_OWN: -1 measured, 0 never, 1 always 638static bool force_avx2 = false,force_gather = false; 639static const bool allowed = [] { 640const char * v = std::getenv ("DS4_IQ_OWN" ); 641mode = v == nullptr ?-1 : std::atoi (v )!= 0 ?1 :0 ; 642const char * a2 = std::getenv ("DS4_IQ_AVX2" ); 643force_avx2 = a2 != nullptr && std::atoi (a2 )!= 0 ; 644const char * gt = std::getenv ("DS4_IQ_GATHER" ); 645force_gather = gt != nullptr && std::atoi (gt )!= 0 ; 646g_codes_ok = build_codes3 ();// (the 512-bit rows' tables; without them those rows are not used) 647build_ks64 (); 648build_ct2x (); 649return mode != 0 && isa ().avx2 ; 650 }(); 651// (IQ2 on an AVX-512 CPU: the GLM lane's AVX-512 rows serve them, unless DS4_IQ_AVX2) 652if (!allowed || (k >=2 && isa ().w512 && !force_avx2 ))return false; 653static std::once_flag once [4 ]; 654static bool ok [4 ]; 655 std::call_once (once [k ], [& ] { 656bool * gflag = k == 0 ?& g_gather18 :k == 1 ?& g_gather21 :k == 2 ?& g_ct16 :nullptr ;// (the second form) 657bool good = own_check (type );// (the blend form; the 512-bit rows where they serve) 658bool good_g = false; 659if (gflag != nullptr ) { 660* gflag = true; 661good_g = own_check (type ); 662* gflag = false; 663 } 664if (mode == 1 ) { 665if (gflag != nullptr )* gflag = force_gather && good_g ; 666ok [k ]= good || (force_gather && good_g ); 667return ; 668 } 669// the variants timed in turns, the best of each kept (other work on the machine - the load's read-ahead, the 670// prompt - then hits them alike), and ours taken only when clearly faster (10 %): the choice changes no answer, 671// a wrong one costs time (Uranus' Golden Cove: one-shot timings 30-80 % apart run to run, the choice flipping) 672double tn = 1e30 ,tb = 1e30 ,tg = 1e30 ; 673for (int it = 0 ;it < 5 ;++ it ) { 674tn = std::min (tn ,time_variant (type ,0 )); 675if (good )tb = std::min (tb ,time_variant (type ,1 )); 676if (good_g )tg = std::min (tg ,time_variant (type ,2 )); 677 } 678if (gflag != nullptr )* gflag = tg < tb ; 679ok [k ]= std::min (tb ,tg )< 0.9 * tn ; 680if (std::getenv ("DS4_IQ_VERBOSE" )!= nullptr ) 681 std::fprintf (stderr ,"ds4: CPU lane %s rows: native %.0f us, ours %.0f us%s -> %s\n" ,ggml_type_name ((ggml_type )type ), 682tn ,tb ,gflag != nullptr ? (std::string (" (second form: gather / one table " )+ std::to_string ((int )tg )+ " us)" ).c_str () :"" , 683ok [k ] ? (gflag != nullptr && * gflag ?"ours (second form)" :"ours" ) :"native" ); 684 }); 685return ok [k ]; 686#else 687 (void )type ; 688return false; 689#endif 690} 691}// namespace 692 693bool iq_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ) { 694 kc::NativeFmt n ; 695if (!kc::native_fmt (tg ,td ,n_embd ,n_ff ,n ,err ))return false; 696f .tg = tg ; 697f .td = td ; 698f .n_embd = n_embd ; 699f .n_ff = n_ff ; 700f .gu_act = n .gu_act ; 701f .d_act = n .d_act ; 702f .gu_row = n .gu_row ; 703f .d_row = n .d_row ; 704f .act_bytes = n .act_bytes ; 705f .h_bytes = n .h_bytes ; 706return true; 707} 708 709void iq_cpu_quant (int act ,const float * x ,int k ,void * dst ) { 710 kc::NativeFmt n ; 711n .gu_act = act ; 712n .n_embd = k ; 713 kc::native_quant_act (n ,x ,dst ); 714} 715 716void iq_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt , 717float * const * ff ,int r0 ,int r1 ,float limit ) { 718if (r1 > r0 && own (f .tg )) {// (native_gu_rows_split's epilogue, on the same values) 719const int nr = r1 - r0 ; 720 thread_local std::vector < float > buf ; 721 thread_local std::vector < float *> go ,uo ; 722buf .resize ((size_t )2 * nt * nr ); 723go .resize ((size_t )nt ); 724uo .resize ((size_t )nt ); 725for (int t = 0 ;t < nt ;++ t ) { 726go [(size_t )t ]= buf .data ()+ (size_t )t * nr ; 727uo [(size_t )t ]= buf .data ()+ (size_t ) (nt + t )* nr ; 728 } 729own_rows (f .tg ,gate ,f .gu_row ,f .n_embd ,act ,nt ,go .data (),r0 ,r1 ); 730own_rows (f .tg ,up ,f .gu_row ,f .n_embd ,act ,nt ,uo .data (),r0 ,r1 ); 731for (int t = 0 ;t < nt ;++ t ) 732for (int r = 0 ;r < nr ;++ r ) { 733float g = go [(size_t )t ][r ],u = uo [(size_t )t ][r ]; 734if (limit > 0.0f ) { 735g = std::fmin (g ,limit ); 736u = std::fmin (std::fmax (u ,- limit ),limit ); 737 } 738ff [t ][r0 + r ]= (g / (1.f + std::exp (- g )))* u ; 739 } 740return ; 741 } 742 kc::native_gu_rows_split (native (f ),gate ,up ,act ,nt ,ff ,r0 ,r1 , 743limit > 0.0f ?limit : std::numeric_limits < float > ::infinity ()); 744} 745 746void iq_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ) { 747if (r1 > r0 && own (f .td )) { 748 thread_local std::vector < float *> o ; 749o .resize ((size_t )nt ); 750for (int t = 0 ;t < nt ;++ t )o [(size_t )t ]= out [t ]+ r0 ; 751own_rows (f .td ,down ,f .d_row ,f .n_ff ,hq ,nt ,o .data (),r0 ,r1 ); 752return ; 753 } 754 kc::native_down_rows_split (native (f ),down ,hq ,nt ,out ,r0 ,r1 ); 755} 756 757bool iq_cpu_own_rows (int type ) {return own (type ); } 758#else 759// (no ggml-cpu lane for the low-bit types here; the trellis types have their own: ds4_tq_cpu.cpp) 760bool iq_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ) { 761if (tq_cpu_type (tg )&& tq_cpu_type (td ))return tq_cpu_fmt (tg ,td ,n_embd ,n_ff ,f ,err ); 762if (gg_cpu_type (tg )&& gg_cpu_type (td ))return gg_cpu_fmt (tg ,td ,n_embd ,n_ff ,f ,err ); 763err = "this build has no ggml-cpu (STRATA_NATIVE_EXPERTS=OFF): the low-bit experts CPU lane is not in it" ; 764return false; 765} 766void iq_cpu_quant (int act ,const float * x ,int k ,void * dst ) { 767if (act == tq_cpu_act ())tq_cpu_quant (x ,k ,dst ); 768else gg_cpu_quant (act ,x ,k ,dst ); 769} 770void iq_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt ,float * const * ff , 771int r0 ,int r1 ,float limit ) { 772if (tq_cpu_type (f .tg ))tq_cpu_gate_up (f ,gate ,up ,act ,nt ,ff ,r0 ,r1 ,limit ); 773else gg_cpu_gate_up (f ,gate ,up ,act ,nt ,ff ,r0 ,r1 ,limit ); 774} 775void iq_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ) { 776if (tq_cpu_type (f .td ))tq_cpu_down (f ,down ,hq ,nt ,out ,r0 ,r1 ); 777else gg_cpu_down (f ,down ,hq ,nt ,out ,r0 ,r1 ); 778} 779bool iq_cpu_own_rows (int ) {return false; } 780#endif 781 782}// namespace strata::ds4::gguf