okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/cpu/tq_avx2.cpp - the trellis expert rows (TQ2_T / TQK6 / TQK7) on AVX2 + FMA (tq_cpu.hpp). 2// Compiled for AVX2 only and reached behind cpu_avx2_ok(), so nothing here can fault on an older CPU. 3// 4// Per 128-weight block: the 32 states in scalar code (tq_states.hpp: one 32-bit read, a shift and a mask each, used 5// as table indices straight away), four 8-byte state-table rows per YMM register as scalar loads, then per token 6// VPMADDWD against the unpacked int16 activations and a VPHADDD tree that leaves the four exact sub-block sums 7// S[k] in one XMM, so the float part is the scalar kernel's, lane for lane. The weights are decoded once per block 8// for all the tokens of the group. 9// 10// Measured on Zen 5 (cycles per block, one token, AVX2): ~48 with this scheme; computing the states with byte 11// shuffles instead (VPSHUFB / VPSRLVD per 8 states) and moving them to GPRs or through memory was slower (60-80), 12// and ggml-cpu's AVX2 vec_dot for these types (agentionai llama.cpp, the same state table) takes about 2.3x as long. 13#include "strata/kernels/cpu/tq_cpu.hpp" 14 15#include "tq_states.hpp" 16 17#include <immintrin.h> 18 19namespace strata::kernels::cpu::tq_detail { 20namespace { 21 22// four state-table rows (8 bytes each) into one YMM: a load and three broadcast loads blended in. Broadcast loads 23// and VPBLENDD avoid the shuffle port that VPINSRQ / VINSERTI128 need (the two measure the same on Zen 5). 24inline __m256i rows4 (const long long * tab ,uint32_t s0 ,uint32_t s1 ,uint32_t s2 ,uint32_t s3 ) { 25__m256i v = _mm256_castsi128_si256 (_mm_loadl_epi64 ((const __m128i * ) (tab + s0 ))); 26v = _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s1 ]),0x0C ); 27v = _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s2 ]),0x30 ); 28return _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s3 ]),0xC0 ); 29} 30 31template < int KB > 32void rows_t (const int16_t * table ,const uint8_t * w ,size_t row_bytes ,int n ,const TqAct * const * a ,int nt ,float * const * out ,int r0 , 33int r1 ) { 34const int nb = n /128 ; 35constexpr size_t bsz = 2 + 4 * KB ; 36const long long * tab = (const long long * )table ; 37for (int r = r0 ;r < r1 ;++ r ) { 38const uint8_t * x = w + (size_t )r * row_bytes ; 39__m128 acc [kTqMaxTokens ]; 40for (int t = 0 ;t < nt ;++ t )acc [t ]= _mm_setzero_ps (); 41for (int i = 0 ;i < nb ;++ i ) { 42const uint8_t * blk = x + (size_t )i * bsz ; 43const uint8_t * qs = blk + 2 ; 44const uint64_t wr = tq_wrap < KB > (qs ); 45__m256i wv [8 ];// wv[m] = weights 16m .. 16m+15 (states 4m .. 4m+3) 46#if defined(__GNUC__ ) 47#pragma GCC unroll 8 48#endif 49for (int m = 0 ;m < 8 ;++ m ) 50wv [m ]= rows4 (tab ,tq_state < KB > (qs ,wr ,4 * m ),tq_state < KB > (qs ,wr ,4 * m + 1 ), 51tq_state < KB > (qs ,wr ,4 * m + 2 ),tq_state < KB > (qs ,wr ,4 * m + 3 )); 52const __m128 dx = _mm_set1_ps (tq_half_to_float (tq_ld16 (blk ))); 53for (int t = 0 ;t < nt ;++ t ) { 54const __m256i * q = (const __m256i * ) (a [t ]-> q + 128 * i ); 55const __m256i p0 = _mm256_add_epi32 (_mm256_madd_epi16 (wv [0 ],_mm256_load_si256 (q + 0 )), 56_mm256_madd_epi16 (wv [1 ],_mm256_load_si256 (q + 1 ))); 57const __m256i p1 = _mm256_add_epi32 (_mm256_madd_epi16 (wv [2 ],_mm256_load_si256 (q + 2 )), 58_mm256_madd_epi16 (wv [3 ],_mm256_load_si256 (q + 3 ))); 59const __m256i p2 = _mm256_add_epi32 (_mm256_madd_epi16 (wv [4 ],_mm256_load_si256 (q + 4 )), 60_mm256_madd_epi16 (wv [5 ],_mm256_load_si256 (q + 5 ))); 61const __m256i p3 = _mm256_add_epi32 (_mm256_madd_epi16 (wv [6 ],_mm256_load_si256 (q + 6 )), 62_mm256_madd_epi16 (wv [7 ],_mm256_load_si256 (q + 7 ))); 63// per 128-bit lane [p0, p1, p2, p3] partial sums, then the two lanes added: S[0..3], exact 64const __m256i h = _mm256_hadd_epi32 (_mm256_hadd_epi32 (p0 ,p1 ),_mm256_hadd_epi32 (p2 ,p3 )); 65const __m128i s = _mm_add_epi32 (_mm256_castsi256_si128 (h ),_mm256_extracti128_si256 (h ,1 )); 66const __m128 sc = _mm_mul_ps (dx ,_mm_loadu_ps (a [t ]-> d + 4 * i )); 67acc [t ]= _mm_fmadd_ps (_mm_cvtepi32_ps (s ),sc ,acc [t ]); 68 } 69 } 70for (int t = 0 ;t < nt ;++ t ) { 71 alignas(16 )float v [4 ]; 72_mm_store_ps (v ,acc [t ]); 73out [t ][r ]= ((v [0 ]+ v [1 ])+ (v [2 ]+ v [3 ]))* (1.0f /8192.0f ); 74 } 75 } 76} 77 78// TQP (147-149): 64 pair-steps per 128-value block. A step's two weights are the FIRST pair of its state 79// table row, so 8 states make one YMM of 16 int16 weights, and a 32-value group (16 steps) is two YMM of 80// weights against two YMM of activations: VPMADDWD, one exact int32 sum per group, then the four group sums 81// go into the same per-token __m128 the other kernels use, so the float part is the scalar kernel's. 82inline __m256i pairs8 (const int16_t * tab ,uint32_t s0 ,uint32_t s1 ,uint32_t s2 ,uint32_t s3 ,uint32_t s4 , 83uint32_t s5 ,uint32_t s6 ,uint32_t s7 ) { 84const __m128i a0 = _mm_unpacklo_epi32 (_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s0 )), 85_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s1 ))); 86const __m128i a1 = _mm_unpacklo_epi32 (_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s2 )), 87_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s3 ))); 88const __m128i a2 = _mm_unpacklo_epi32 (_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s4 )), 89_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s5 ))); 90const __m128i a3 = _mm_unpacklo_epi32 (_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s6 )), 91_mm_loadl_epi64 ((const __m128i * ) (tab + 4 * s7 ))); 92return _mm256_set_m128i (_mm_unpacklo_epi64 (a2 ,a3 ),_mm_unpacklo_epi64 (a0 ,a1 )); 93} 94 95template < int KB > 96void rows_tqp_t (const uint8_t * w ,size_t row_bytes ,int n ,const TqAct * const * a ,int nt ,float * const * out ,int r0 , 97int r1 ) { 98const int nb = n /128 ; 99constexpr size_t bsz = 2 + 8 * KB ; 100const int16_t * tab = tq_state_table (); 101for (int r = r0 ;r < r1 ;++ r ) { 102const uint8_t * x = w + (size_t )r * row_bytes ; 103__m128 acc [kTqMaxTokens ]; 104for (int t = 0 ;t < nt ;++ t )acc [t ]= _mm_setzero_ps (); 105for (int i = 0 ;i < nb ;++ i ) { 106const uint8_t * blk = x + (size_t )i * bsz ; 107uint32_t st [64 ]; 108tq_states_tqp < KB > (blk + 2 ,st ); 109__m256i wv [8 ];// wv[2g], wv[2g+1]: the weights of the 32-value group g 110#pragma GCC unroll 4 111for (int g = 0 ;g < 4 ;++ g ) { 112const uint32_t * s = st + 16 * g ; 113wv [2 * g ]= pairs8 (tab ,s [0 ],s [1 ],s [2 ],s [3 ],s [4 ],s [5 ],s [6 ],s [7 ]); 114wv [2 * g + 1 ]= pairs8 (tab ,s [8 ],s [9 ],s [10 ],s [11 ],s [12 ],s [13 ],s [14 ],s [15 ]); 115 } 116const __m128 dx = _mm_set1_ps (tq_half_to_float (tq_ld16 (blk ))); 117for (int t = 0 ;t < nt ;++ t ) { 118const __m256i * q = (const __m256i * ) (a [t ]-> q + 128 * i ); 119int32_t sum [4 ]; 120#pragma GCC unroll 4 121for (int g = 0 ;g < 4 ;++ g ) { 122const __m256i p = _mm256_add_epi32 (_mm256_madd_epi16 (wv [2 * g ],_mm256_load_si256 (q + 2 * g )), 123_mm256_madd_epi16 (wv [2 * g + 1 ],_mm256_load_si256 (q + 2 * g + 1 ))); 124__m128i s = _mm_add_epi32 (_mm256_castsi256_si128 (p ),_mm256_extracti128_si256 (p ,1 )); 125s = _mm_hadd_epi32 (s ,s ); 126s = _mm_hadd_epi32 (s ,s ); 127sum [g ]= _mm_cvtsi128_si32 (s ); 128 } 129const __m128 sc = _mm_mul_ps (dx ,_mm_loadu_ps (a [t ]-> d + 4 * i )); 130const __m128i s4 = _mm_set_epi32 (sum [3 ],sum [2 ],sum [1 ],sum [0 ]); 131acc [t ]= _mm_fmadd_ps (_mm_cvtepi32_ps (s4 ),sc ,acc [t ]); 132 } 133 } 134for (int t = 0 ;t < nt ;++ t ) { 135 alignas(16 )float v [4 ]; 136_mm_store_ps (v ,acc [t ]); 137out [t ][r ]= ((v [0 ]+ v [1 ])+ (v [2 ]+ v [3 ]))* (1.0f /8192.0f ); 138 } 139 } 140} 141 142}// namespace 143 144void tq_rows_avx2_tqp (int kb ,const uint8_t * w ,size_t row_bytes ,int n ,const TqAct * const * a ,int nt , 145float * const * out ,int r0 ,int r1 ) { 146if (kb == 5 )rows_tqp_t < 5 > (w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 147else if (kb == 6 )rows_tqp_t < 6 > (w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 148else rows_tqp_t < 7 > (w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 149} 150 151void tq_rows_avx2 (int kb ,const int16_t * table ,const uint8_t * w ,size_t row_bytes ,int n ,const TqAct * const * a ,int nt , 152float * const * out ,int r0 ,int r1 ) { 153if (kb == 8 )rows_t < 8 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 154else if (kb == 6 )rows_t < 6 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 155else if (kb == 5 )rows_t < 5 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 );// experimental TQK5X 156else if (kb == 4 )rows_t < 4 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 );// experimental TQK4X 157else rows_t < 7 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 158} 159 160}// namespace strata::kernels::cpu::tq_detail