okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_q3k_swiglu_test.cpp - M2b of the q3k latent A stage: SwiGLU + the q8_1 of H. 2// Two gates, kept apart so a failure says which half is wrong: 3// 1. SwiGLU: the float H the kernel computed against a double silu(g) * u (rel_mean <= 1e-6) 4// 2. the quantizer: the kernel's staged codes and d's against q8_1_store's rule applied on the host to the SAME 5// float H the kernel wrote out (codes byte for byte, d's bit for bit) 6#include "strata/prefill/lat_q3k_swiglu.hpp" 7 8#include <cuda_runtime.h> 9#include <ggml.h> 10 11#include <cmath> 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <random> 16#include <vector> 17 18#define CK (e ) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \ 19 std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0) 20 21int main () { 22constexpr int64_t FF = strata::prefill::lat_q3k_swiglu_ff ; 23constexpr int64_t HQ = strata::prefill::lat_q3k_swiglu_hq_row ; 24constexpr int E = 512 ;// entries (the engine's 4K shape: 512 entries a launch) 25constexpr int NB = (int ) (FF /32 );// 20 q8_1 blocks per entry (n_ff / 32 - the width that bit me in M1) 26 std::printf ("M2b: %d entries x %lld features, hq %lld B an entry (%d blocks)\n" ,E , (long long )FF , (long long )HQ ,NB ); 27 28// gate/up as M2a writes them: real products, entry-major, a spread of magnitudes so the amax varies per block 29 std::mt19937 rng (23 ); 30 std::normal_distribution < float > ng (0.f ,2.0f ),nu (0.f ,1.5f ); 31 std::vector < float > gate ((size_t )E * FF ),up ((size_t )E * FF ); 32for (size_t i = 0 ;i < gate .size ();++ i ) {gate [i ]= ng (rng );up [i ]= nu (rng ); } 33// a few hard blocks: all zeros, one huge value, a negative gate, a near-tie (rounding boundary) 34gate [0 ]= 0.f ;up [0 ]= 0.f ; 35for (int f = 0 ;f < 32 ;++ f ) {gate [(size_t )FF + f ]= 0.f ;up [(size_t )FF + f ]= 0.f ; }// an all-zero block 36gate [(size_t )2 * FF ]= 12.5f ;up [(size_t )2 * FF ]= 3.0f ;// one big value 37for (int f = 0 ;f < 32 ;++ f )gate [(size_t )3 * FF + f ]= -8.0f + 0.01f * f ;// silu of negatives 38 39float * dgate = nullptr ;float * dup = nullptr ;uint8_t * dhq = nullptr ;float * dhout = nullptr ; 40CK (cudaMalloc (& dgate ,gate .size ()* 4 ));CK (cudaMalloc (& dup ,up .size ()* 4 )); 41CK (cudaMalloc (& dhq , (size_t )E * HQ ));CK (cudaMalloc (& dhout ,gate .size ()* 4 )); 42CK (cudaMemcpy (dgate ,gate .data (),gate .size ()* 4 ,cudaMemcpyHostToDevice )); 43CK (cudaMemcpy (dup ,up .data (),up .size ()* 4 ,cudaMemcpyHostToDevice )); 44CK (cudaMemset (dhq ,0xAA , (size_t )E * HQ ));// so an unwritten byte shows 45 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,dhout ,nullptr ); 46CK (cudaDeviceSynchronize ()); 47 std::vector < float > hout (gate .size ()); 48 std::vector < uint8_t > hq ((size_t )E * HQ ); 49CK (cudaMemcpy (hout .data (),dhout ,hout .size ()* 4 ,cudaMemcpyDeviceToHost )); 50CK (cudaMemcpy (hq .data (),dhq ,hq .size (),cudaMemcpyDeviceToHost )); 51 52// ---- gate 1: the SwiGLU floats against double silu(g) * u ------------------------------------------------ 53 { 54double sabs = 0 ,sref = 0 ,num = 0 ,aa = 0 ,bb = 0 ; 55float dmax = 0.f ; 56int bad = 0 ; 57for (size_t i = 0 ;i < hout .size ();++ i ) { 58const double g = gate [i ],u = up [i ]; 59const double ref = (g / (1.0 + std::exp (- g )))* u ; 60const double d = std::fabs ((double )hout [i ]- ref ); 61sabs += d ;sref += std::fabs (ref );dmax = std::fmax (dmax , (float )d ); 62num += (double )hout [i ]* ref ;aa += (double )hout [i ]* hout [i ];bb += ref * ref ; 63if (d > 1e-6 * std::fabs (ref )+ 1e-12 )++ bad ; 64 } 65 std::printf ("swiglu: rel_mean %.3e max|d| %.6e cos %.9f values off by >1e-6 rel: %d\n" , 66sref > 0 ?sabs /sref :0.0 , (double )dmax ,num / (std::sqrt (aa )* std::sqrt (bb )+ 1e-30 ),bad ); 67if (!((sref > 0 ?sabs /sref :1.0 ) <=1e-6 )) { std::printf ("FAIL swiglu\n" );return 1 ; } 68 } 69 70// ---- gate 2: the quantizer against q8_1_store's rule on the host, same floats ---------------------------- 71 { 72int code_bad = 0 ,d_bad = 0 ,blocks = 0 ,zeros = 0 ; 73double dmax_rel = 0 ; 74for (int e = 0 ;e < E ;++ e ) { 75const uint8_t * row = hq .data ()+ (size_t )e * HQ ; 76for (int b = 0 ;b < NB ;++ b ) { 77const float * hv = hout .data ()+ (size_t )e * FF + 32 * b ; 78float am = 0.f ; 79for (int v = 0 ;v < 32 ;++ v )am = std::fmax (am , std::fabs (hv [v ])); 80float d = am /127.0f ;// q8_1_finite: the clamp is > 65504 81if (std::fabs (d )> 65504.0f )d = std::copysign (65504.0f ,d ); 82if (am == 0.f )++ zeros ; 83const uint8_t * ch = row + (size_t ) (b >>1 )* 80 ; 84uint16_t dh ; 85 std::memcpy (& dh ,ch + 64 + 2 * (b & 1 ),2 ); 86const float dmine = ggml_fp16_to_fp32 (dh ); 87dmax_rel = std::max (dmax_rel , std::fabs ((double )dmine - (double )d ) / (std::fabs ((double )d )+ 1e-30 )); 88if (dh != ggml_fp32_to_fp16 (d ))++ d_bad ; 89for (int v = 0 ;v < 32 ;++ v ) { 90const float q = am == 0.f ?0.f :roundf (hv [v ] /d ); 91const int8_t qc = (int8_t ) (q > 127.f ?127.f :q < -127.f ?-127.f :q ); 92const uint8_t got = ch [16 * (2 * (b & 1 )+ (v >>4 ))+ 4 * (v & 3 )+ ((v & 15 ) >>2 )]; 93if ((int8_t )got != qc )++ code_bad ; 94 } 95++ blocks ; 96 } 97 } 98 std::printf ("q8_1: %d blocks (%d all-zero) code mismatches %d d mismatches %d max d rel %.3e\n" ,blocks ,zeros , 99code_bad ,d_bad ,dmax_rel ); 100if (code_bad != 0 || d_bad != 0 ) { std::printf ("FAIL quantizer\n" );return 1 ; } 101 } 102 103// ---- informational: the dequantized H against the float H (the q8_1's own noise) ------------------------ 104 { 105double sabs = 0 ,sref = 0 ; 106for (int e = 0 ;e < E ;++ e ) { 107const uint8_t * row = hq .data ()+ (size_t )e * HQ ; 108for (int b = 0 ;b < NB ;++ b ) { 109const uint8_t * ch = row + (size_t ) (b >>1 )* 80 ; 110uint16_t dh ; 111 std::memcpy (& dh ,ch + 64 + 2 * (b & 1 ),2 ); 112const double d = ggml_fp16_to_fp32 (dh ); 113for (int v = 0 ;v < 32 ;++ v ) { 114const uint8_t c = ch [16 * (2 * (b & 1 )+ (v >>4 ))+ 4 * (v & 3 )+ ((v & 15 ) >>2 )]; 115const double deq = d * (double ) (int8_t )c ; 116const double x = hout [(size_t )e * FF + 32 * b + v ]; 117sabs += std::fabs (deq - x );sref += std::fabs (x ); 118 } 119 } 120 } 121 std::printf ("hq dequantized vs the float H: rel_mean %.3e (q8_1's own resolution)\n" ,sabs /sref ); 122 } 123 124// ---- timing (informational) ------------------------------------------------------------------------------ 125 { 126cudaEvent_t t0 ,t1 ;CK (cudaEventCreate (& t0 ));CK (cudaEventCreate (& t1 )); 127 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 128CK (cudaDeviceSynchronize ()); 129CK (cudaEventRecord (t0 )); 130for (int i = 0 ;i < 100 ;++ i ) strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 131CK (cudaEventRecord (t1 ));CK (cudaEventSynchronize (t1 )); 132float ms = 0 ;CK (cudaEventElapsedTime (& ms ,t0 ,t1 )); 133 std::printf ("M2b swiglu+q8_1: %.3f ms / 100 = %.4f ms %.3f us/entry (%d entries)\n" ,ms ,ms /100.0 , 134ms * 1000.0 /100.0 /E ,E ); 135CK (cudaEventDestroy (t0 ));CK (cudaEventDestroy (t1 )); 136 } 137 138 std::printf ("PASS: M2b (SwiGLU + q8_1 of H)\n" ); 139return 0 ; 140}