okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/lat_bandwidth_bench.cpp 2// Step 5.2: is the U matvec memory-bound? Measures the maddubs kernel's throughput in two regimes: 3// hot = one group (H x ELL Q8_0 = ~4.87 MB), re-read every iteration (fits L3) -> compute-bound ceiling 4// cold = K groups (> 4x L3), each read once and cycled (streamed from RAM) -> the arena's streaming rate 5// Reports GFLOP/s and the weight stream GB/s for both, so (a) bandwidth vs (b) dilution is decided. 6#define GGML_COMMON_DECL_CPP 7#define GGML_COMMON_IMPL_CPP 8#include "ggml-common.h" 9 10#include <immintrin.h> 11#include <chrono> 12#include <cstdint> 13#include <cstdio> 14#include <cstring> 15#include <random> 16#include <vector> 17 18static inline float h2f (uint16_t h ) { 19return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); 20} 21static inline float hsum_float_8 (__m256 x ) { 22__m128 res = _mm256_extractf128_ps (x ,1 ); 23res = _mm_add_ps (res ,_mm256_castps256_ps128 (x )); 24res = _mm_add_ps (res ,_mm_movehl_ps (res ,res )); 25res = _mm_add_ss (res ,_mm_movehdup_ps (res )); 26return _mm_cvtss_f32 (res ); 27} 28template < int NT > 29static void dot_maddubs (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 30__m256 acc [NT ]; 31for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 32for (int ib = 0 ;ib < nb ;++ ib ) { 33const float xd = h2f (x [ib ].d ); 34const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 35const __m256i ax = _mm256_sign_epi8 (qx ,qx ); 36for (int t = 0 ;t < NT ;++ t ) { 37const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ; 38const float dy = h2f (* (const uint16_t * ) (yb + 0 )); 39const __m256 d = _mm256_set1_ps (xd * dy ); 40const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 )); 41const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 42const __m256 q = _mm256_cvtepi32_ps (_mm256_madd_epi16 (_mm256_set1_epi16 (1 ),_mm256_maddubs_epi16 (ax ,sy ))); 43acc [t ]= _mm256_fmadd_ps (q ,d ,acc [t ]); 44 } 45 } 46for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 47} 48static void rows_maddubs (const block_q8_0 * w ,int R ,int nb ,const void * const * act ,int nt ,float * out ) { 49for (int r = 0 ;r < R ;++ r ) { 50float s [4 ]; 51switch (nt ) { 52case 1 :dot_maddubs < 1 > (w + (size_t )r * nb ,nb ,act ,s );out [r ]= s [0 ];break ; 53case 2 :dot_maddubs < 2 > (w + (size_t )r * nb ,nb ,act ,s );out [r ]= s [0 ];out [R + r ]= s [1 ];break ; 54default :dot_maddubs < 4 > (w + (size_t )r * nb ,nb ,act ,s );for (int t = 0 ;t < 4 ;++ t )out [(size_t )t * R + r ]= s [t ];break ; 55 } 56 } 57} 58 59int main () { 60constexpr int R = 2560 ,N = 1792 ,NB = N /32 ; 61const size_t group_bytes = (size_t )R * NB * 34 ;// one group's Q8_0 weights 62 std::mt19937 rng (7 ); 63 std::uniform_int_distribution < int > qd (-127 ,127 ); 64const uint16_t scale_half = 0x2000u ; 65 66const int K = 32 ;// cold: 32 groups = 32 * 4.87 MB ~ 156 MB > 4x L3 (36 MB) 67 std::vector < block_q8_0 > w ((size_t )K * R * NB ); 68for (auto & b :w ) {b .d = scale_half ;for (int j = 0 ;j < 32 ;++ j )b .qs [j ]= (int8_t )qd (rng ); } 69 std::vector < uint8_t > actb ((size_t )4 * NB * 36 ); 70for (size_t p = 0 ;p < actb .size ();p += 36 ) { std::memcpy (actb .data ()+ p ,& scale_half ,2 );for (int j = 0 ;j < 32 ;++ j )actb [p + 4 + j ]= (uint8_t )(int8_t )qd (rng ); } 71const void * actp [4 ]; 72for (int t = 0 ;t < 4 ;++ t )actp [t ]= actb .data ()+ (size_t )t * NB * 36 ; 73 74 std::vector < float > out ((size_t )4 * R ); 75 std::fill (out .begin (),out .end (),0.f ); 76volatile float sink = 0.f ; 77 78for (int nt : {1 ,4 }) { 79const double flops = 2.0 * (double )R * (double )N * (double )nt ; 80const int it = 40 ; 81 82// hot: group 0 re-read every iteration 83 { 84double best = 1e30 ; 85for (int i = 0 ;i < it ;++ i ) { 86const auto t0 = std::chrono::steady_clock::now (); 87rows_maddubs (w .data (),R ,NB ,actp ,nt ,out .data ()); 88const auto t1 = std::chrono::steady_clock::now (); 89best = std::min (best , std::chrono::duration < double , std::milli > (t1 - t0 ).count ()); 90 } 91 std::printf ("nt=%d HOT (1 group, L3-resident): %.3f ms = %.1f GFLOP/s (weight stream %.1f GB/s)\n" , 92nt ,best ,flops /best /1e6 , (double )group_bytes /best /1e6 ); 93sink += out [0 ]; 94 } 95 96// cold: cycle K groups, each read once per pass 97 { 98double best = 1e30 ; 99for (int i = 0 ;i < it ;++ i ) { 100const int g = i %K ; 101const block_q8_0 * gw = w .data ()+ (size_t )g * R * NB ; 102const auto t0 = std::chrono::steady_clock::now (); 103rows_maddubs (gw ,R ,NB ,actp ,nt ,out .data ()); 104const auto t1 = std::chrono::steady_clock::now (); 105best = std::min (best , std::chrono::duration < double , std::milli > (t1 - t0 ).count ()); 106 } 107 std::printf ("nt=%d COLD (%d groups cycled, streamed): %.3f ms = %.1f GFLOP/s (weight stream %.1f GB/s)\n" , 108nt ,K ,best ,flops /best /1e6 , (double )group_bytes /best /1e6 ); 109sink += out [0 ]; 110 } 111 } 112 (void )sink ; 113return 0 ; 114}