okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/kernels/bf16_gemv_parity.cpp - P2.S5's test for the BF16 GEMV. 2// 3// THREE THINGS ARE CHECKED, and the third is the one the engine's correctness rests on: 4// 5// 1. both kernels against a host reference that reads the SAME bf16 bits - so what is under test is the 6// reduction, not the conversion, and the tolerance is set by summation order and nothing else; 7// 2. the naive and the split kernel agree with each other, because the naive one is the reference the 8// split one exists to be checked against and a disagreement there means one of them is wrong; 9// 3. **THE ACTIVATION CONTRACT IS OBSERVABLE.** A BF16 weight sees a BF16 activation; feeding it fp16 or 10// f32 instead is a 2^-9 = 1.95e-03 relative change, which is above any 1e-3 tolerance. An fp16 11// activation is the rival reading, computed and required to differ, because it is what the engine did 12// before `docs/activation-contract.md` settled the question and it produces perfectly plausible output. 13#include "strata/kernels/bf16_bits.hpp" 14#include "strata/kernels/bf16_gemv.hpp" 15#include "strata/kernels/f16_bits.hpp" 16 17#include <cuda_runtime.h> 18 19#include <cmath> 20#include <cstdio> 21#include <cstdlib> 22#include <random> 23#include <string> 24#include <vector> 25 26namespace { 27 28using strata::kernels::bf16_from_f32 ; 29using strata::kernels::f32_from_bf16 ; 30 31void check (cudaError_t e ,const char * what ) { 32if (e != cudaSuccess ) { 33 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 34 std::exit (1 ); 35 } 36} 37 38double rel_l1 (const std::vector < float >& a ,const std::vector < float >& b ) { 39double d = 0 ,m = 0 ; 40for (size_t i = 0 ;i < a .size ();++ i ) { 41d += std::fabs ((double )a [i ]- (double )b [i ]); 42m += std::fabs ((double )a [i ]); 43 } 44return d / (m > 1e-30 ?m :1e-30 ); 45} 46 47/// The host reference, in DOUBLE, over the same bf16 bits. 48void reference (const std::vector < uint16_t >& x ,const std::vector < uint16_t >& w ,long long n_in ,long long n_out , 49 std::vector < float >& y ) { 50y .assign ((size_t )n_out ,0.0f ); 51for (long long o = 0 ;o < n_out ;++ o ) { 52double acc = 0 ; 53for (long long i = 0 ;i < n_in ;++ i ) 54acc += (double )f32_from_bf16 (x [(size_t )i ])* (double )f32_from_bf16 (w [(size_t ) (o * n_in + i )]); 55y [(size_t )o ]= (float )acc ; 56 } 57} 58 59}// namespace 60 61int main (int argc ,char ** argv ) { 62bool selftest = false; 63for (int i = 1 ;i < argc ;++ i ) { 64if (std::string (argv [i ])== "--selftest" )selftest = true; 65else { std::fprintf (stderr ,"usage: bf16_gemv_parity [--selftest]\n" );return 2 ; } 66 } 67int bad = 0 ; 68 69// THE REAL SHAPES, because a kernel that only works on a square toy is not a kernel this engine can use. 70// `ssm_alpha` is [2560, 48] - a wide reduction and 48 outputs - and `indexer.k_proj` is [2560, 128]. 71struct Shape {long long n_in ,n_out ;const char * what ; }; 72const Shape shapes []= { 73 {2560 ,48 ,"ssm_alpha.weight [2560, 48]" }, 74 {2560 ,128 ,"indexer.k_proj [2560, 128]" }, 75 {2560 ,512 ,"indexer.q_proj [2560, 512]" }, 76 {2560 ,2560 ,"ple_value [2560, 2560]" }, 77 }; 78 79for (const Shape & s :shapes ) { 80 std::mt19937 rng (4242 ); 81 std::normal_distribution < float > g (0.0f ,1.0f ); 82 std::vector < float > fx ((size_t )s .n_in ),fw ((size_t ) (s .n_in * s .n_out )); 83for (auto & v :fx )v = g (rng ); 84// a realistic weight scale: these are projections, not embeddings 85for (auto & v :fw )v = g (rng )* 0.05f ; 86 87// THE ACTIVATION IS ROUNDED TO BF16 FIRST, which is the contract, and the WEIGHT is already bf16 in 88// the pack - the loaded bytes ARE the high 16 bits of an f32. 89 std::vector < uint16_t > x ((size_t )s .n_in ),w ((size_t ) (s .n_in * s .n_out )); 90for (size_t i = 0 ;i < fx .size ();++ i )x [i ]= bf16_from_f32 (fx [i ]); 91for (size_t i = 0 ;i < fw .size ();++ i )w [i ]= bf16_from_f32 (fw [i ]); 92 93 std::vector < float > want ; 94reference (x ,w ,s .n_in ,s .n_out ,want ); 95 96uint16_t * d_x = nullptr ,* d_w = nullptr ; 97float * d_y = nullptr ; 98check (cudaMalloc (& d_x ,x .size ()* 2 ),"x" ); 99check (cudaMalloc (& d_w ,w .size ()* 2 ),"w" ); 100check (cudaMalloc (& d_y , (size_t )s .n_out * 4 ),"y" ); 101check (cudaMemcpy (d_x ,x .data (),x .size ()* 2 ,cudaMemcpyHostToDevice ),"cx" ); 102check (cudaMemcpy (d_w ,w .data (),w .size ()* 2 ,cudaMemcpyHostToDevice ),"cw" ); 103 104 std::vector < float > naive ((size_t )s .n_out ),warp ((size_t )s .n_out ),split ((size_t )s .n_out ); 105 strata::kernels::bf16_gemv (d_x ,d_w ,d_y ,s .n_in ,s .n_out ,nullptr ); 106check (cudaMemcpy (naive .data (),d_y ,naive .size ()* 4 ,cudaMemcpyDeviceToHost ),"cy1" ); 107 strata::kernels::bf16_gemv_split (d_x ,d_w ,d_y ,s .n_in ,s .n_out ,32 ,nullptr ); 108check (cudaMemcpy (warp .data (),d_y ,warp .size ()* 4 ,cudaMemcpyDeviceToHost ),"cy2" ); 109 strata::kernels::bf16_gemv_split (d_x ,d_w ,d_y ,s .n_in ,s .n_out ,256 ,nullptr ); 110check (cudaMemcpy (split .data (),d_y ,split .size ()* 4 ,cudaMemcpyDeviceToHost ),"cy3" ); 111 112const double rn = rel_l1 (want ,naive ),rw = rel_l1 (want ,warp ),rs = rel_l1 (want ,split ); 113 std::printf (" %-34s naive %.2e warp %.2e split256 %.2e\n" ,s .what ,rn ,rw ,rs ); 114// The products are EXACT - both sides are bf16-valued, so f32 represents each product exactly - and 115// the only difference is the order the 2560 terms are added. A tight bound is therefore meaningful. 116if (!(rn <=1e-5 && rw <=1e-5 && rs <=1e-5 )) { std::printf (" *** over 1e-5 ***\n" );++ bad ; } 117 118// ---- 3. THE ACTIVATION CONTRACT. The rival: an fp16 activation instead of bf16, which is what the 119// engine did before the contract was settled. It must be observable, or check 1 above would pass 120// against either and the contract would be untested. 121 { 122 std::vector < float > fp16_as_f32 ((size_t )s .n_in ); 123 std::vector < uint16_t > x16 ((size_t )s .n_in ); 124for (size_t i = 0 ;i < fx .size ();++ i ) { 125x16 [i ]= strata::kernels::f16_from_f32 (fx [i ]); 126fp16_as_f32 [i ]= strata::kernels::f32_from_f16 (x16 [i ]); 127 } 128// Round the fp16 value to bf16 - i.e. give the kernel a bf16 activation whose VALUES came through 129// fp16. That is the whole difference between the two contracts at this call site. 130for (size_t i = 0 ;i < fp16_as_f32 .size ();++ i )x [i ]= bf16_from_f32 (fp16_as_f32 [i ]); 131check (cudaMemcpy (d_x ,x .data (),x .size ()* 2 ,cudaMemcpyHostToDevice ),"cx2" ); 132 std::vector < float > rival ((size_t )s .n_out ); 133 strata::kernels::bf16_gemv (d_x ,d_w ,d_y ,s .n_in ,s .n_out ,nullptr ); 134check (cudaMemcpy (rival .data (),d_y ,rival .size ()* 4 ,cudaMemcpyDeviceToHost ),"cy4" ); 135const double r = rel_l1 (want ,rival ); 136const bool visible = r > 1e-4 ; 137 std::printf (" %-30s %s (%.4f%% apart)\n" ,"bf16 vs fp16 activation" , 138visible ?"yes" :"*** NO ***" ,r * 100 ); 139if (!visible )++ bad ; 140 } 141cudaFree (d_x );cudaFree (d_w );cudaFree (d_y ); 142 } 143 144 std::printf ("\nbf16_gemv: %d failures\n" ,bad ); 145if (bad )return 1 ; 146if (selftest ) std::printf ("bf16_gemv_parity OK\n" ); 147return 0 ; 148}