okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0918cba0a
main
1// src/kernels/cpu/router_dot_parity.cpp - the router lookahead's BF16 dot on each rung this CPU has: the AVX2 kernel 2// (kq_avx2.cpp) and the AVX1 one for older CPUs (kq_avx1.cpp), against a double-precision reference, plus the time 3// of one layer's router. No GPU, no model: random BF16 rows of the model's router shape [512 x 2560], 1..8 tokens. 4// 5// router_dot_parity (STRATA_FORCE_ISA=avx leaves out the AVX2 kernel, as on an AVX-only CPU) 6#include "strata/kernels/cpu/expert_layout.hpp" 7#include "strata/kernels/cpu/kq_avx1.hpp" 8#include "strata/kernels/cpu/kq_avx2.hpp" 9 10#include <algorithm> 11#include <chrono> 12#include <cmath> 13#include <cstdint> 14#include <cstdio> 15#include <cstring> 16#include <random> 17#include <vector> 18 19namespace cpu= strata::kernels::cpu; 20 21int main () { 22constexpr int R = 512 ,C = 2560 ,MAXT = 8 ; 23 std::mt19937 rng (7 ); 24 std::normal_distribution < float > nd (0.f ,0.05f ); 25 std::vector < uint16_t > w ((size_t )R * C ); 26for (auto & v :w ) { 27const float f = nd (rng ); 28uint32_t b ; 29 std::memcpy (& b ,& f ,4 ); 30v = (uint16_t ) (b >>16 ); 31 } 32 std::vector < float > x ((size_t )MAXT * C ); 33for (auto & v :x )v = nd (rng )* 20.f ; 34 std::vector < double > ref ((size_t )MAXT * R ); 35for (int t = 0 ;t < MAXT ;++ t ) 36for (int r = 0 ;r < R ;++ r ) { 37double s = 0 ; 38for (int c = 0 ;c < C ;++ c ) { 39const uint32_t b = (uint32_t )w [(size_t )r * C + c ] <<16 ; 40float f ; 41 std::memcpy (& f ,& b ,4 ); 42s += (double )f * x [(size_t )t * C + c ]; 43 } 44ref [(size_t )t * R + r ]= s ; 45 } 46int failures = 0 ; 47auto run = [& ](const char * tag ,void (* fn )(const uint16_t * ,int ,int ,const float * ,int ,float * )) { 48 std::vector < float > out ((size_t )MAXT * R ); 49for (int nt = 1 ;nt <=MAXT ;++ nt ) { 50 std::fill (out .begin (),out .end (),0.f ); 51fn (w .data (),R ,C ,x .data (),nt ,out .data ()); 52double num = 0 ,den = 0 ; 53for (int i = 0 ;i < nt * R ;++ i ) {num += std::fabs (out [(size_t )i ]- ref [(size_t )i ]);den += std::fabs (ref [(size_t )i ]); } 54const double rel = num / (den + 1e-30 ); 55if (nt == 1 || nt == MAXT ) std::printf (" %-5s nt=%d rel %.2e vs the double reference\n" ,tag ,nt ,rel ); 56if (!(rel < 1e-5 )) { std::printf (" %-5s nt=%d MISMATCH\n" ,tag ,nt );++ failures ; } 57 } 58const int it = 20 ; 59const auto t0 = std::chrono::steady_clock::now (); 60for (int i = 0 ;i < it ;++ i )fn (w .data (),R ,C ,x .data (),4 ,out .data ()); 61const double ms = std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count () /it ; 62 std::printf (" %-5s one layer's router, 4 tokens: %.3f ms\n" ,tag ,ms ); 63 }; 64 std::printf ("router_dot_parity: %s (avx2 %d, avx %d)\n" , cpu::cpu_name ().c_str (), (int ) cpu::cpu_avx2_ok (), 65 (int ) cpu::cpu_avx1_ok ()); 66if (cpu::cpu_avx2_ok ())run ("avx2" , cpu::bf16_rows_dot_multi ); 67if (cpu::cpu_avx1_ok ())run ("avx" , cpu::bf16_rows_dot_multi_avx1 ); 68 std::printf ("router_dot_parity: %d failures\n" ,failures ); 69return failures == 0 ?0 :1 ; 70}