okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
66db34dc4
main
1// src/kernels/nvfp4_expert_gpu_parity.cpp - the decode path's GPU experts (native_expert_grouped) on real NVFP4 2// blobs with their global scales, against a double-precision expert. 3// 4// nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...] 5// 6// Eight tokens of N(0,1) activations through one expert: q8_1 input, gate/up with s_gate / s_up, SwiGLU, q8_1 of the 7// hidden, down with s_down on its output. Only the two activation roundings separate it from the reference (~1%). 8#include "strata/kernels/iq_kernels.hpp" 9 10#include "ggml.h" 11 12#include <cuda_runtime.h> 13 14#include <cmath> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <fstream> 19#include <random> 20#include <vector> 21 22namespace k= strata::kernels; 23 24namespace { 25void ck (cudaError_t e ,const char * what ) { 26if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); std::exit (1 ); } 27} 28}// namespace 29 30int main (int argc ,char ** argv ) { 31if (argc < 2 ) { std::fprintf (stderr ,"usage: nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...]\n" );return 2 ; } 32constexpr int64_t N = 2560 ,FF = 640 ; 33constexpr int T = 8 ; 34const k::NativeExpertLayout L = k::native_expert_layout (40 ,40 ,N ,FF ); 35const size_t layer_bytes = 512 * L .bytes ;// L.bytes counts the 16-byte tail 36 std::vector < int > picks ; 37for (int i = 2 ;i + 1 < argc ;i += 2 ) {picks .push_back (std::atoi (argv [i ]));picks .push_back (std::atoi (argv [i + 1 ])); } 38if (picks .empty ())picks = {0 ,0 ,0 ,7 ,12 ,100 ,24 ,300 ,40 ,5 ,47 ,511 }; 39 std::ifstream f (argv [1 ], std::ios::binary ); 40 std::mt19937 rng (9 ); 41 std::normal_distribution < float > nd (0.f ,1.f ); 42const ggml_type_traits * tt = ggml_get_type_traits (GGML_TYPE_NVFP4 ); 43double worst = 0 ; 44for (size_t pi = 0 ;pi < picks .size ();pi += 2 ) { 45const int layer = picks [pi ],e = picks [pi + 1 ]; 46 std::vector < uint8_t > blob (L .bytes ); 47f .seekg ((std::streamoff ) (layer * layer_bytes + (size_t )e * L .bytes )); 48if (!f .read ((char * )blob .data (), (std::streamsize )blob .size ())) { std::fprintf (stderr ,"cannot read\n" );return 2 ; } 49float tail [4 ]; 50 std::memcpy (tail ,blob .data ()+ L .tail_off ,sizeof tail ); 51 std::vector < float > wg ((size_t )FF * N ),wu ((size_t )FF * N ),wd ((size_t )N * FF ),x ((size_t )T * N ); 52tt -> to_float (blob .data (),wg .data (),FF * N ); 53tt -> to_float (blob .data ()+ L .up_off ,wu .data (),FF * N ); 54tt -> to_float (blob .data ()+ L .down_off ,wd .data (),N * FF ); 55for (float & v :x )v = nd (rng ); 56 std::vector < double > ref ((size_t )T * N ); 57for (int t = 0 ;t < T ;++ t ) { 58 std::vector < double > h (FF ); 59for (int64_t r = 0 ;r < FF ;++ r ) { 60double g = 0 ,u = 0 ; 61for (int64_t c = 0 ;c < N ;++ c ) { 62g += (double )wg [(size_t )r * N + c ]* x [(size_t )t * N + c ]; 63u += (double )wu [(size_t )r * N + c ]* x [(size_t )t * N + c ]; 64 } 65g *=tail [0 ]; 66u *=tail [1 ]; 67h [r ]= g / (1 + std::exp (- g ))* u ; 68 } 69for (int64_t r = 0 ;r < N ;++ r ) { 70double s = 0 ; 71for (int64_t c = 0 ;c < FF ;++ c )s += (double )wd [(size_t )r * FF + c ]* h [c ]; 72ref [(size_t )t * N + r ]= s * tail [2 ]; 73 } 74 } 75uint8_t * db ;float * dx ,* dout ;void * dxq ,* scr ;unsigned long long * dptr ;int32_t * dstart ,* dng ,* ddst ,* dtok ; 76ck (cudaMalloc (& db ,blob .size ()),"blob" ); 77ck (cudaMemcpy (db ,blob .data (),blob .size (),cudaMemcpyHostToDevice ),"blob" ); 78ck (cudaMalloc (& dx ,x .size ()* 4 ),"x" ); 79ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 80ck (cudaMalloc (& dxq , (size_t )T * N /32 * 36 ),"xq" ); 81ck (cudaMalloc (& dout , (size_t )T * N * 4 ),"out" ); 82ck (cudaMalloc (& scr , k::native_expert_scratch_bytes (T ,FF )),"scratch" ); 83const unsigned long long ptr = (unsigned long long )db ; 84const int32_t start [2 ]= {0 ,T },ng = 1 ,ids [T ]= {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 }; 85ck (cudaMalloc (& dptr ,8 ),"p" );ck (cudaMemcpy (dptr ,& ptr ,8 ,cudaMemcpyHostToDevice ),"p" ); 86ck (cudaMalloc (& dstart ,8 ),"s" );ck (cudaMemcpy (dstart ,start ,8 ,cudaMemcpyHostToDevice ),"s" ); 87ck (cudaMalloc (& dng ,4 ),"n" );ck (cudaMemcpy (dng ,& ng ,4 ,cudaMemcpyHostToDevice ),"n" ); 88ck (cudaMalloc (& ddst ,4 * T ),"d" );ck (cudaMemcpy (ddst ,ids ,4 * T ,cudaMemcpyHostToDevice ),"d" ); 89ck (cudaMalloc (& dtok ,4 * T ),"t" );ck (cudaMemcpy (dtok ,ids ,4 * T ,cudaMemcpyHostToDevice ),"t" ); 90 k::quantize_q8_1_rows (dx ,T ,N ,dxq ,nullptr ); 91 k::native_expert_grouped (L ,dptr ,dstart ,dng ,ddst ,dtok ,1 ,T ,dxq ,scr ,dout ,nullptr ); 92ck (cudaDeviceSynchronize (),"run" ); 93 std::vector < float > got ((size_t )T * N ); 94ck (cudaMemcpy (got .data (),dout ,got .size ()* 4 ,cudaMemcpyDeviceToHost ),"out" ); 95for (void * p : {(void * )db , (void * )dx ,dxq , (void * )dout ,scr , (void * )dptr , (void * )dstart , (void * )dng , 96 (void * )ddst , (void * )dtok }) 97cudaFree (p ); 98double num = 0 ,den = 0 ; 99for (size_t i = 0 ;i < got .size ();++ i ) {num += (got [i ]- ref [i ])* (got [i ]- ref [i ]);den += ref [i ]* ref [i ]; } 100const double rel = std::sqrt (num /den ); 101worst = std::fmax (worst ,rel ); 102 std::printf ("layer %2d expert %3d: s_gate %.3g s_up %.3g s_down %.3g -> expert output rel. error %.3f%%\n" ,layer , 103e ,tail [0 ],tail [1 ],tail [2 ],100 * rel ); 104 } 105 std::printf ("RESULT: %s (worst %.3f%%)\n" ,worst < 0.03 ?"ok" :"TOO FAR" ,100 * worst ); 106return worst < 0.03 ?0 :1 ; 107}