okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/lat_vnni_bench.cpp 2// Step 5.1 microbenchmark: the latent U matvec (Q8_0 weights x Q8_1 acts, H=2560 x ELL=1792) on two paths - 3// the current AVX2 maddubs dot (kq_avx2.cpp lat_q8_0_q8_1_dot) and an AVX-VNNI dpbusd dot (target("avxvnni")), 4// against a double reference. Reports throughput (GFLOP/s) and the output delta (max/mean/p99). 5// No GPU, no model: random Q8_0/Q8_1 blocks of the model's U shape. 6#define GGML_COMMON_DECL_CPP 7#define GGML_COMMON_IMPL_CPP 8#include "ggml-common.h" 9 10#include <immintrin.h> 11#include <algorithm> 12#include <chrono> 13#include <cmath> 14#include <cstdint> 15#include <cstdio> 16#include <cstring> 17#include <random> 18#include <vector> 19 20static inline float h2f (uint16_t h ) { 21return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); 22} 23static inline float hsum_float_8 (__m256 x ) { 24__m128 res = _mm256_extractf128_ps (x ,1 ); 25res = _mm_add_ps (res ,_mm256_castps256_ps128 (x )); 26res = _mm_add_ps (res ,_mm_movehl_ps (res ,res )); 27res = _mm_add_ss (res ,_mm_movehdup_ps (res )); 28return _mm_cvtss_f32 (res ); 29} 30 31// ---- the current maddubs inner dot (kq_avx2.cpp lat_q8_0_q8_1_dot), verbatim ---- 32template < int NT > 33static void dot_maddubs (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 34__m256 acc [NT ]; 35for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 36for (int ib = 0 ;ib < nb ;++ ib ) { 37const float xd = h2f (x [ib ].d ); 38const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 39const __m256i ax = _mm256_sign_epi8 (qx ,qx );// |weight| 40for (int t = 0 ;t < NT ;++ t ) { 41const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ; 42const float dy = h2f (* (const uint16_t * ) (yb + 0 )); 43const __m256 d = _mm256_set1_ps (xd * dy ); 44const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 )); 45const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 46const __m256 q = _mm256_cvtepi32_ps (_mm256_madd_epi16 (_mm256_set1_epi16 (1 ),_mm256_maddubs_epi16 (ax ,sy ))); 47acc [t ]= _mm256_fmadd_ps (q ,d ,acc [t ]); 48 } 49 } 50for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 51} 52 53// ---- the AVX-VNNI dpbusd inner dot (per-block zero-src + cvt + fmadd, like expert.cpp row_dot_multi) ---- 54template < int NT > 55__attribute__((target ("avxvnni" ))) 56static void dot_vnni (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 57__m256 acc [NT ]; 58for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 59for (int ib = 0 ;ib < nb ;++ ib ) { 60const float xd = h2f (x [ib ].d ); 61const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 62const __m256i ax = _mm256_sign_epi8 (qx ,qx );// |weight| (unsigned operand of dpbusd) 63for (int t = 0 ;t < NT ;++ t ) { 64const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ; 65const float dy = h2f (* (const uint16_t * ) (yb + 0 )); 66const __m256 d = _mm256_set1_ps (xd * dy ); 67const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 )); 68const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 69const __m256 q = _mm256_cvtepi32_ps (_mm256_dpbusd_avx_epi32 (_mm256_setzero_si256 (),ax ,sy )); 70acc [t ]= _mm256_fmadd_ps (q ,d ,acc [t ]); 71 } 72 } 73for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 74} 75 76// one full U matvec: R rows x (nb*32) cols, nt tokens; out[t*R + r]. 77static void rows_maddubs (const block_q8_0 * w ,int R ,int nb ,const void * const * act ,int nt ,float * out ) { 78for (int r = 0 ;r < R ;++ r ) { 79const block_q8_0 * wr = w + (size_t )r * nb ; 80float s [4 ]; 81switch (nt ) { 82case 1 :dot_maddubs < 1 > (wr ,nb ,act ,s );out [r ]= s [0 ];break ; 83case 2 :dot_maddubs < 2 > (wr ,nb ,act ,s );out [r ]= s [0 ];out [R + r ]= s [1 ];break ; 84case 3 :dot_maddubs < 3 > (wr ,nb ,act ,s );for (int t = 0 ;t < 3 ;++ t )out [(size_t )t * R + r ]= s [t ];break ; 85default :dot_maddubs < 4 > (wr ,nb ,act ,s );for (int t = 0 ;t < 4 ;++ t )out [(size_t )t * R + r ]= s [t ];break ; 86 } 87 } 88} 89__attribute__((target ("avxvnni" ))) 90static void rows_vnni (const block_q8_0 * w ,int R ,int nb ,const void * const * act ,int nt ,float * out ) { 91for (int r = 0 ;r < R ;++ r ) { 92const block_q8_0 * wr = w + (size_t )r * nb ; 93float s [4 ]; 94switch (nt ) { 95case 1 :dot_vnni < 1 > (wr ,nb ,act ,s );out [r ]= s [0 ];break ; 96case 2 :dot_vnni < 2 > (wr ,nb ,act ,s );out [r ]= s [0 ];out [R + r ]= s [1 ];break ; 97case 3 :dot_vnni < 3 > (wr ,nb ,act ,s );for (int t = 0 ;t < 3 ;++ t )out [(size_t )t * R + r ]= s [t ];break ; 98default :dot_vnni < 4 > (wr ,nb ,act ,s );for (int t = 0 ;t < 4 ;++ t )out [(size_t )t * R + r ]= s [t ];break ; 99 } 100 } 101} 102 103int main () { 104constexpr int R = 2560 ,N = 1792 ,NB = N /32 ;// the U matvec shape H x ELL 105 std::mt19937 rng (7 ); 106 std::uniform_int_distribution < int > qd (-127 ,127 ); 107const uint16_t scale_half = 0x2000u ;// 0.0078125, a valid f16 block scale 108 109 std::vector < block_q8_0 > w ((size_t )R * NB ); 110for (auto & b :w ) { 111b .d = scale_half ; 112for (int j = 0 ;j < 32 ;++ j )b .qs [j ]= (int8_t )qd (rng ); 113 } 114// acts as raw bytes: block_q8_1 = 36 B/block (d +0, s +2 [unused], qs +4) 115 std::vector < uint8_t > actb ((size_t )4 * NB * 36 ); 116for (int t = 0 ;t < 4 ;++ t ) 117for (int ib = 0 ;ib < NB ;++ ib ) { 118uint8_t * p = actb .data ()+ ((size_t )t * NB + ib )* 36 ; 119 std::memcpy (p + 0 ,& scale_half ,2 ); 120* (uint16_t * )(p + 2 )= 0 ; 121for (int j = 0 ;j < 32 ;++ j )p [4 + j ]= (uint8_t )(int8_t )qd (rng ); 122 } 123 124for (int nt : {1 ,2 ,4 }) { 125const void * actp [4 ]; 126for (int t = 0 ;t < nt ;++ t )actp [t ]= actb .data ()+ (size_t )t * NB * 36 ; 127 128// double reference 129 std::vector < double > ref ((size_t )nt * R ,0.0 ); 130for (int t = 0 ;t < nt ;++ t ) 131for (int r = 0 ;r < R ;++ r ) { 132double s = 0 ; 133const block_q8_0 * wr = & w [(size_t )r * NB ]; 134const uint8_t * ar = actb .data ()+ (size_t )t * NB * 36 ; 135for (int ib = 0 ;ib < NB ;++ ib ) { 136int dot = 0 ; 137const int8_t * wq = wr [ib ].qs ; 138const int8_t * aq = (const int8_t * )(ar + (size_t )ib * 36 + 4 ); 139for (int j = 0 ;j < 32 ;++ j )dot += (int )wq [j ]* (int )aq [j ]; 140s += (double )h2f (wr [ib ].d )* (double )h2f (* (const uint16_t * )(ar + (size_t )ib * 36 + 0 ))* (double )dot ; 141 } 142ref [(size_t )t * R + r ]= s ; 143 } 144 145 std::vector < float > om ((size_t )nt * R ),ov ((size_t )nt * R ); 146 std::fill (om .begin (),om .end (),0.f ); 147 std::fill (ov .begin (),ov .end (),0.f ); 148 149rows_maddubs (w .data (),R ,NB ,actp ,nt ,om .data ()); 150rows_vnni (w .data (),R ,NB ,actp ,nt ,ov .data ()); 151 152auto rel = [& ](const std::vector < float >& o ) { 153double num = 0 ,den = 0 ; 154for (size_t i = 0 ;i < o .size ();++ i ) {num += std::fabs (o [i ]- ref [i ]);den += std::fabs (ref [i ]); } 155return num / (den + 1e-30 ); 156 }; 157 std::printf ("nt=%d rel-vs-ref maddubs %.2e vnni %.2e\n" ,nt ,rel (om ),rel (ov )); 158 159 std::vector < double > dd ((size_t )nt * R ); 160for (size_t i = 0 ;i < dd .size ();++ i )dd [i ]= std::fabs ((double )om [i ]- (double )ov [i ]); 161 std::sort (dd .begin (),dd .end ()); 162double dmax = dd .back (),dsum = 0 ; 163for (double v :dd )dsum += v ; 164 std::printf (" delta maddubs-vs-vnni: max %.3e mean %.3e p99 %.3e\n" , 165dmax ,dsum / (double )dd .size (),dd [(size_t ) (0.99 * (dd .size ()- 1 ))]); 166 167const int it = 40 ; 168auto bench = [& ](void (* fn )(const block_q8_0 * ,int ,int ,const void * const * ,int ,float * ),float * o ) { 169double best = 1e30 ; 170for (int i = 0 ;i < it ;++ i ) { 171const auto t0 = std::chrono::steady_clock::now (); 172fn (w .data (),R ,NB ,actp ,nt ,o ); 173const auto t1 = std::chrono::steady_clock::now (); 174best = std::min (best , std::chrono::duration < double , std::milli > (t1 - t0 ).count ()); 175 } 176return best ; 177 }; 178const double tm = bench (rows_maddubs ,om .data ()); 179const double tv = bench (rows_vnni ,ov .data ()); 180const double flops = 2.0 * (double )R * (double )N * (double )nt ; 181 std::printf (" throughput (best of %d): maddubs %.3f ms = %.1f GFLOP/s | vnni %.3f ms = %.1f GFLOP/s | speedup %.3fx\n" , 182it ,tm ,flops /tm /1e6 ,tv ,flops /tv /1e6 ,tm /tv ); 183 } 184return 0 ; 185}