okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28ce98bf1
main
1// src/kernels/cpu/tq_avx512.cpp - the trellis expert rows (TQ2_T / TQK6 / TQK7) on AVX-512 F/BW/VL/VNNI 2// (tq_cpu.hpp). Reached behind cpu_avx512_ok() only. 3// 4// Per 128-weight block: the AVX2 kernel's decode (scalar states, scalar 8-byte state-table loads, one ZMM of 32 5// int16 weights per Q8_0 sub-block); per token four VPDPWSSD against the unpacked int16 activations, an unpack/add 6// tree to the four exact sub-block sums S[k] in one XMM, and the scalar kernel's float steps lane for lane. The 7// weights are decoded once per block for all the tokens of the group. 8// 9// Measured on Zen 5 (cycles per block): the decode bounds it, so one token is no faster than AVX2 (~48); each extra 10// token of a group costs ~10% less than on AVX2. ggml-cpu's AVX-512 decode for these types - all states from two 11// VPERMB and the table rows by VPGATHERQQ - was 20-30% slower here: Zen gathers are slow, and so are Intel ones 12// under the Gather Data Sampling microcode mitigation. 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 ZMM of 16 int32 partial sums -> [S0, S1, S2, S3] (exact integer adds) 23inline __m128i reduce4 (__m512i p0 ,__m512i p1 ,__m512i p2 ,__m512i p3 ) { 24const __m512i a = _mm512_add_epi32 (_mm512_unpacklo_epi32 (p0 ,p1 ),_mm512_unpackhi_epi32 (p0 ,p1 )); 25const __m512i b = _mm512_add_epi32 (_mm512_unpacklo_epi32 (p2 ,p3 ),_mm512_unpackhi_epi32 (p2 ,p3 )); 26const __m512i c = _mm512_add_epi32 (_mm512_unpacklo_epi64 (a ,b ),_mm512_unpackhi_epi64 (a ,b )); 27// c: per 128-bit lane [p0, p1, p2, p3] partial sums; add the four lanes 28const __m256i d = _mm256_add_epi32 (_mm512_castsi512_si256 (c ),_mm512_extracti64x4_epi64 (c ,1 )); 29return _mm_add_epi32 (_mm256_castsi256_si128 (d ),_mm256_extracti128_si256 (d ,1 )); 30} 31 32// four state-table rows (8 bytes each) into one YMM: scalar loads, broadcast and blended in; two make a ZMM 33inline __m256i rows4 (const long long * tab ,uint32_t s0 ,uint32_t s1 ,uint32_t s2 ,uint32_t s3 ) { 34__m256i v = _mm256_castsi128_si256 (_mm_loadl_epi64 ((const __m128i * ) (tab + s0 ))); 35v = _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s1 ]),0x0C ); 36v = _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s2 ]),0x30 ); 37return _mm256_blend_epi32 (v ,_mm256_set1_epi64x (tab [s3 ]),0xC0 ); 38} 39 40template < int KB > 41inline __m512i rows8 (const long long * tab ,const uint8_t * qs ,uint64_t wr ,int t0 ) { 42const __m256i lo = rows4 (tab ,tq_state < KB > (qs ,wr ,t0 ),tq_state < KB > (qs ,wr ,t0 + 1 ),tq_state < KB > (qs ,wr ,t0 + 2 ), 43tq_state < KB > (qs ,wr ,t0 + 3 )); 44const __m256i hi = rows4 (tab ,tq_state < KB > (qs ,wr ,t0 + 4 ),tq_state < KB > (qs ,wr ,t0 + 5 ), 45tq_state < KB > (qs ,wr ,t0 + 6 ),tq_state < KB > (qs ,wr ,t0 + 7 )); 46return _mm512_inserti64x4 (_mm512_castsi256_si512 (lo ),hi ,1 ); 47} 48 49template < int KB > 50void 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 , 51int r1 ) { 52const int nb = n /128 ; 53constexpr size_t bsz = 2 + 4 * KB ; 54const long long * tab = (const long long * )table ; 55for (int r = r0 ;r < r1 ;++ r ) { 56const uint8_t * x = w + (size_t )r * row_bytes ; 57__m128 acc [kTqMaxTokens ]; 58for (int t = 0 ;t < nt ;++ t )acc [t ]= _mm_setzero_ps (); 59for (int i = 0 ;i < nb ;++ i ) { 60const uint8_t * blk = x + (size_t )i * bsz ; 61const uint8_t * qs = blk + 2 ; 62const uint64_t wr = tq_wrap < KB > (qs ); 63// states 8k .. 8k+7 -> weights 32k .. 32k+31 = Q8_0 sub-block k 64const __m512i w0 = rows8 < KB > (tab ,qs ,wr ,0 ),w1 = rows8 < KB > (tab ,qs ,wr ,8 ); 65const __m512i w2 = rows8 < KB > (tab ,qs ,wr ,16 ),w3 = rows8 < KB > (tab ,qs ,wr ,24 ); 66const __m128 dx = _mm_set1_ps (tq_half_to_float (tq_ld16 (blk ))); 67for (int t = 0 ;t < nt ;++ t ) { 68const __m512i * q = (const __m512i * ) (a [t ]-> q + 128 * i ); 69const __m512i z = _mm512_setzero_si512 (); 70const __m128i s = reduce4 (_mm512_dpwssd_epi32 (z ,w0 ,_mm512_load_si512 (q + 0 )), 71_mm512_dpwssd_epi32 (z ,w1 ,_mm512_load_si512 (q + 1 )), 72_mm512_dpwssd_epi32 (z ,w2 ,_mm512_load_si512 (q + 2 )), 73_mm512_dpwssd_epi32 (z ,w3 ,_mm512_load_si512 (q + 3 ))); 74const __m128 sc = _mm_mul_ps (dx ,_mm_loadu_ps (a [t ]-> d + 4 * i )); 75acc [t ]= _mm_fmadd_ps (_mm_cvtepi32_ps (s ),sc ,acc [t ]); 76 } 77 } 78for (int t = 0 ;t < nt ;++ t ) { 79 alignas(16 )float v [4 ]; 80_mm_store_ps (v ,acc [t ]); 81out [t ][r ]= ((v [0 ]+ v [1 ])+ (v [2 ]+ v [3 ]))* (1.0f /8192.0f ); 82 } 83 } 84} 85 86}// namespace 87 88void tq_rows_avx512 (int kb ,const int16_t * table ,const uint8_t * w ,size_t row_bytes ,int n ,const TqAct * const * a ,int nt , 89float * const * out ,int r0 ,int r1 ) { 90if (kb == 8 )rows_t < 8 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 91else if (kb == 6 )rows_t < 6 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 92else if (kb == 5 )rows_t < 5 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 );// experimental TQK5X 93else if (kb == 4 )rows_t < 4 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 );// experimental TQK4X 94else rows_t < 7 > (table ,w ,row_bytes ,n ,a ,nt ,out ,r0 ,r1 ); 95} 96 97}// namespace strata::kernels::cpu::tq_detail