okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/kq_scalar.cpp - scalar fallbacks for the edge0 latent CPU lane on non-x86 hosts 2// (Windows ARM64, DGX Spark). The AVX-2 versions live in kq_avx2.cpp and are chosen at run time by 3// cpu_avx2_ok(); on a host without them these run instead - same numbers, one row/token at a time. 4// The row math mirrors pool.cpp's static lat_*_row functions (the reference for the AVX2 kernels). 5#include "strata/kernels/cpu/kq_avx2.hpp" 6#include "strata/kernels/f16_bits.hpp" 7 8#include <cmath> 9 10namespace strata::kernels::cpu { 11 12void lat_quant_q8_1_qs_avx2 (const float * x ,const float * d_ptr ,int8_t * q ) { 13// The AVX2 version's scalar contract (probe_qs.cpp): divide by d (not 1/d), round 14// ties-away-from-zero like roundf, clamp to [-127, 127]. 15const float d = * d_ptr ; 16for (int j = 0 ;j < 32 ;++ j ) { 17const float v = x [j ] /d ; 18int r = (int ) std::lroundf (v );// roundf: half away from zero 19if (r > 127 )r = 127 ; 20if (r < -127 )r = -127 ; 21q [j ]= (int8_t )r ; 22 } 23} 24 25void lat_q8_0_q8_1_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt , 26float * const * out ,int r0 ,int r1 ) { 27const int nb = n /32 ; 28for (int r = r0 ;r < r1 ;++ r ) { 29const uint8_t * row = w + (size_t )r * row_bytes ; 30for (int t = 0 ;t < nt ;++ t ) { 31const uint8_t * xq = (const uint8_t * )act [t ]; 32float acc = 0.0f ; 33for (int b = 0 ;b < nb ;++ b ) { 34const uint8_t * wb = row + (size_t )b * 34 ; 35const uint8_t * xb = xq + (size_t )b * 36 ; 36const float dw = f32_from_f16 ((uint16_t )(wb [0 ] | (wb [1 ] <<8 ))); 37const float dx = f32_from_f16 ((uint16_t )(xb [0 ] | (xb [1 ] <<8 ))); 38const int8_t * ws = (const int8_t * )(wb + 2 ); 39const int8_t * xs = (const int8_t * )(xb + 4 ); 40int s = 0 ; 41for (int j = 0 ;j < 32 ;++ j )s += (int )xs [j ]* (int )ws [j ]; 42acc += dw * dx * (float )s ; 43 } 44out [t ][r ]= acc ; 45 } 46 } 47} 48 49void lat_q4_0_q8_1_rows (const uint8_t * w ,size_t row_bytes ,int n ,const void * const * act ,int nt , 50float * const * out ,int r0 ,int r1 ) { 51const int nb = n /32 ; 52for (int r = r0 ;r < r1 ;++ r ) { 53const uint8_t * row = w + (size_t )r * row_bytes ; 54for (int t = 0 ;t < nt ;++ t ) { 55const uint8_t * xq = (const uint8_t * )act [t ]; 56float acc = 0.0f ; 57for (int b = 0 ;b < nb ;++ b ) { 58const uint8_t * wb = row + (size_t )b * 18 ; 59const uint8_t * xb = xq + (size_t )b * 36 ; 60const float dw = f32_from_f16 ((uint16_t )(wb [0 ] | (wb [1 ] <<8 ))); 61const float dx = f32_from_f16 ((uint16_t )(xb [0 ] | (xb [1 ] <<8 ))); 62const int8_t * xs = (const int8_t * )(xb + 4 ); 63int s = 0 ; 64for (int j = 0 ;j < 16 ;++ j ) { 65const int byte_ = wb [2 + j ]; 66s += (int )xs [j ]* ((int )(byte_ & 0xF )- 8 ); 67s += (int )xs [j + 16 ]* ((int )(byte_ >>4 )- 8 ); 68 } 69acc += dw * dx * (float )s ; 70 } 71out [t ][r ]= acc ; 72 } 73 } 74} 75 76}// namespace strata::kernels::cpu