okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cpu/agg_bandwidth_bench.cpp 2// Step 5.3 (v2, cold-corrected): the TRUE cold aggregate ceiling of the engine's 15 workers. 3// A cold stream : 15 threads each do ONE pass over a fresh 64 MB slice (960 MB total >> L3) -> raw cold read ceiling 4// B cold matvec : 15 threads each run the U matvec over FRESH groups (60 groups = 292 MB, read once) -> the engine shape, cold 5// Both pinned to the engine's exact worker set: P-cores 2,4,6,8,10,12,14 + E-cores 16..23. 6#define GGML_COMMON_DECL_CPP 7#define GGML_COMMON_IMPL_CPP 8#include "ggml-common.h" 9 10#include <immintrin.h> 11#include <atomic> 12#include <chrono> 13#include <cstdint> 14#include <cstdio> 15#include <cstring> 16#include <random> 17#include <thread> 18#include <vector> 19#ifdef __linux__ 20#include <sched.h> 21#endif 22 23static const int cores [15 ]= {2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 }; 24static void pin_to (int cpu ) { 25#ifdef __linux__ 26cpu_set_t set ; 27CPU_ZERO (& set ); 28CPU_SET (cpu ,& set ); 29sched_setaffinity (0 ,sizeof (set ),& set ); 30#else 31 (void )cpu ; 32#endif 33} 34 35static inline float h2f (uint16_t h ) { 36return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtsi32_si128 ((int )h ))); 37} 38static inline float hsum_float_8 (__m256 x ) { 39__m128 res = _mm256_extractf128_ps (x ,1 ); 40res = _mm_add_ps (res ,_mm256_castps256_ps128 (x )); 41res = _mm_add_ps (res ,_mm_movehl_ps (res ,res )); 42res = _mm_add_ss (res ,_mm_movehdup_ps (res )); 43return _mm_cvtss_f32 (res ); 44} 45template < int NT > 46static void dot_maddubs (const block_q8_0 * x ,int nb ,const void * const * act ,float * res ) { 47__m256 acc [NT ]; 48for (int t = 0 ;t < NT ;++ t )acc [t ]= _mm256_setzero_ps (); 49for (int ib = 0 ;ib < nb ;++ ib ) { 50const float xd = h2f (x [ib ].d ); 51const __m256i qx = _mm256_loadu_si256 ((const __m256i * )x [ib ].qs ); 52const __m256i ax = _mm256_sign_epi8 (qx ,qx ); 53for (int t = 0 ;t < NT ;++ t ) { 54const uint8_t * yb = (const uint8_t * )act [t ]+ (size_t )ib * 36 ; 55const float dy = h2f (* (const uint16_t * ) (yb + 0 )); 56const __m256 d = _mm256_set1_ps (xd * dy ); 57const __m256i qy = _mm256_loadu_si256 ((const __m256i * ) (yb + 4 )); 58const __m256i sy = _mm256_sign_epi8 (qy ,qx ); 59const __m256 q = _mm256_cvtepi32_ps (_mm256_madd_epi16 (_mm256_set1_epi16 (1 ),_mm256_maddubs_epi16 (ax ,sy ))); 60acc [t ]= _mm256_fmadd_ps (q ,d ,acc [t ]); 61 } 62 } 63for (int t = 0 ;t < NT ;++ t )res [t ]= hsum_float_8 (acc [t ]); 64} 65static void rows_maddubs (const block_q8_0 * w ,int R ,int nb ,const void * const * act ,int nt ,float * out ) { 66for (int r = 0 ;r < R ;++ r ) { 67float s [4 ]; 68dot_maddubs < 1 > (w + (size_t )r * nb ,nb ,act ,s ); 69out [r ]= s [0 ]; 70 } 71} 72 73int main () { 74constexpr int R = 2560 ,N = 1792 ,NB = N /32 ,T = 15 ; 75const size_t group_bytes = (size_t )R * NB * 34 ;// 4.87 MB per group 76 std::mt19937 rng (7 ); 77 std::uniform_int_distribution < int > qd (-127 ,127 ); 78const uint16_t scale_half = 0x2000u ; 79 80// ---- A: cold raw stream, 15 threads x 64 MB (960 MB, read once) ---- 81 { 82const size_t per = (size_t )64 * 1024 * 1024 ; 83 std::vector < uint8_t > buf ((size_t )T * per ); 84for (size_t i = 0 ;i < buf .size ();++ i )buf [i ]= (uint8_t )(i & 0xff ); 85 std::vector < uint64_t > sums (T ,0 ); 86 std::atomic < int > go {0 }; 87 std::vector < std::thread > th ; 88for (int t = 0 ;t < T ;++ t ) 89th .emplace_back ([& ,t ]() { 90pin_to (cores [t ]); 91const uint8_t * p = buf .data ()+ (size_t )t * per ; 92uint64_t s = 0 ; 93while (go .load ()== 0 ) {} 94const auto t0 = std::chrono::steady_clock::now (); 95for (size_t j = 0 ;j < per /8 ;++ j )s += ((const uint64_t * )p )[j ]; 96const auto t1 = std::chrono::steady_clock::now (); 97sums [t ]= s ; 98 (void )t1 ; 99 }); 100const auto w0 = std::chrono::steady_clock::now (); 101go .store (1 ); 102for (auto & x :th )x .join (); 103const auto w1 = std::chrono::steady_clock::now (); 104const double wall = std::chrono::duration < double , std::milli > (w1 - w0 ).count (); 105volatile uint64_t sink = 0 ;for (auto s :sums )sink += s ; (void )sink ; 106 std::printf ("A cold raw-stream 15x64MB read-once: wall %.3f ms = %.1f GB/s aggregate\n" ,wall , (double )T * per /wall /1e6 ); 107 } 108 109// ---- B: cold matvec, 15 threads x 4 fresh groups each (60 groups = 292 MB, read once) ---- 110 { 111const int K = 60 ;// 4 groups per thread, disjoint 112 std::vector < block_q8_0 > w ((size_t )K * R * NB ); 113for (auto & b :w ) {b .d = scale_half ;for (int j = 0 ;j < 32 ;++ j )b .qs [j ]= (int8_t )qd (rng ); } 114 std::vector < uint8_t > actb ((size_t )NB * 36 ); 115for (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 ); } 116const void * actp = actb .data (); 117 std::vector < float > out ((size_t )T * R ,0.f ); 118 std::atomic < int > go {0 }; 119 std::vector < std::thread > th ; 120for (int t = 0 ;t < T ;++ t ) 121th .emplace_back ([& ,t ]() { 122pin_to (cores [t ]); 123float * o = out .data ()+ (size_t )t * R ; 124while (go .load ()== 0 ) {} 125const auto t0 = std::chrono::steady_clock::now (); 126for (int g = 0 ;g < 4 ;++ g ) {// thread t reads groups t, t+15, t+30, t+45 (disjoint, cold) 127const block_q8_0 * gw = w .data ()+ (size_t ) (t + g * T )* R * NB ; 128rows_maddubs (gw ,R ,NB ,& actp ,1 ,o ); 129 } 130const auto t1 = std::chrono::steady_clock::now (); 131 (void )t1 ; 132 }); 133const auto w0 = std::chrono::steady_clock::now (); 134go .store (1 ); 135for (auto & x :th )x .join (); 136const auto w1 = std::chrono::steady_clock::now (); 137const double wall = std::chrono::duration < double , std::milli > (w1 - w0 ).count (); 138const double bytes = (double )K * group_bytes ; 139const double flops = (double )K * R * N * 2 ; 140volatile float sink = 0 ;for (int t = 0 ;t < T ;++ t )sink += out [(size_t )t * R ]; (void )sink ; 141 std::printf ("B cold matvec 15 workers x 4 groups read-once: wall %.3f ms = %.1f GB/s aggregate = %.1f GFLOP/s\n" , 142wall ,bytes /wall /1e6 ,flops /wall /1e6 ); 143 } 144return 0 ; 145}