okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0fff115df
main
1// src/kernels/dequant_bf16_test.cpp - plan v0.3 P5: the device dequantizers against the artifact's validated CPU 2// dequantizers (`strata/artifact/dequant.hpp`), on one real tensor of every type the model uses. 3// 4// dequant_bf16_test SHARD1.gguf [SHARD2.gguf] 5// 6// For each type: the first 4 rows of the first tensor of that type, FP32 path compared with a relative tolerance of 7// 1e-6 (the products are the reference's, reordered at most), BF16 path within half a BF16 ulp of the CPU value. 8#include "strata/artifact/dequant.hpp" 9#include "strata/artifact/gguf_reader.hpp" 10#include "strata/kernels/dequant_bf16.hpp" 11 12#include <cuda_runtime.h> 13 14#include <cmath> 15#include <cstdio> 16#include <cstring> 17#include <map> 18#include <string> 19#include <vector> 20 21namespace { 22 23bool cpu_block (int type ,const uint8_t * b ,float * out ) { 24using namespace strata ; 25switch (type ) { 26case 2 :dequantize_q4_0 (b ,out );return true; 27case 6 :dequantize_q5_0 (b ,out );return true; 28case 7 :dequantize_q5_1 (b ,out );return true; 29case 8 :dequantize_q8_0 (b ,out );return true; 30case 11 :dequantize_q3_K (b ,out );return true; 31case 12 :dequantize_q4_K (b ,out );return true; 32case 13 :dequantize_q5_K (b ,out );return true; 33case 14 :dequantize_q6_K (b ,out );return true; 34case 20 :dequantize_iq4_nl (b ,out );return true; 35case 23 :dequantize_iq4_xs (b ,out );return true; 36case 42 :dequantize_q2_0 (b ,out );return true; 37default :return false; 38 } 39} 40 41float bf16_to_f32 (uint16_t h ) { 42uint32_t u = (uint32_t )h <<16 ; 43float f ; 44 std::memcpy (& f ,& u ,4 ); 45return f ; 46} 47 48}// namespace 49 50int main (int argc ,char ** argv ) { 51if (argc < 2 ) { 52 std::fprintf (stderr ,"usage: dequant_bf16_test SHARD.gguf [SHARD2.gguf]\n" ); 53return 2 ; 54 } 55 std::map < int ,bool > done ; 56int fails = 0 ,checked = 0 ; 57for (int a = 1 ;a < argc ;++ a ) { 58 strata::GgufFile gguf (argv [a ]); 59for (const auto & t :gguf .tensors ()) { 60const int type = (int )t .type ; 61if (done .count (type )|| !strata::kernels::dequant_bf16_supported (type )|| t .shape .size ()!= 2 )continue ; 62int be = 0 ,bb = 0 ; 63if (!strata::block_geometry (t .type ,be ,bb ))continue ; 64const int64_t cols = (int64_t )t .shape [0 ]; 65const int64_t rows = (std::min < int64_t > )(4 , (int64_t )t .shape [1 ]); 66const int64_t row_bytes = cols /be * bb ; 67const uint8_t * host = (const uint8_t * )gguf .tensor_data (t ); 68void * d_blocks = nullptr ; 69float * d_f = nullptr ; 70uint16_t * d_h = nullptr ; 71cudaMalloc (& d_blocks , (size_t ) (rows * row_bytes )); 72cudaMalloc (& d_f , (size_t ) (rows * cols )* 4 ); 73cudaMalloc (& d_h , (size_t ) (rows * cols )* 2 ); 74cudaMemcpy (d_blocks ,host , (size_t ) (rows * row_bytes ),cudaMemcpyHostToDevice ); 75 strata::kernels::dequant_f32 (type ,d_blocks ,0 ,rows ,cols ,d_f ,nullptr ); 76 strata::kernels::dequant_bf16 (type ,d_blocks ,0 ,rows ,cols ,d_h ,nullptr ); 77 std::vector < float > gf ((size_t ) (rows * cols )); 78 std::vector < uint16_t > gh ((size_t ) (rows * cols )); 79cudaMemcpy (gf .data (),d_f ,gf .size ()* 4 ,cudaMemcpyDeviceToHost ); 80cudaMemcpy (gh .data (),d_h ,gh .size ()* 2 ,cudaMemcpyDeviceToHost ); 81cudaFree (d_blocks ); 82cudaFree (d_f ); 83cudaFree (d_h ); 84 std::vector < float > ref ((size_t ) (rows * cols )); 85for (int64_t r = 0 ;r < rows ;++ r ) 86for (int64_t b = 0 ;b < cols /be ;++ b ) 87cpu_block (type ,host + r * row_bytes + b * bb ,ref .data ()+ r * cols + b * be ); 88int64_t bad_f = 0 ,bad_h = 0 ; 89double worst = 0 ; 90for (size_t i = 0 ;i < ref .size ();++ i ) { 91const double e = std::fabs ((double )gf [i ]- ref [i ]); 92const double tol = 1e-6 * std::fabs ((double )ref [i ])+ 1e-30 ; 93if (e > tol )++ bad_f ; 94worst = (std::max )(worst ,e / (std::fabs ((double )ref [i ])+ 1e-30 )); 95const double eh = std::fabs ((double )bf16_to_f32 (gh [i ])- ref [i ]); 96if (eh > std::ldexp (std::fabs ((double )ref [i ]),-8 )+ 1e-30 )++ bad_h ; 97 } 98++ checked ; 99done [type ]= true; 100const bool ok = bad_f == 0 && bad_h == 0 ; 101if (!ok )++ fails ; 102 std::printf ("type %2d %-40s %lld x %lld: f32 %s (%lld off, worst rel %.2e), bf16 %s (%lld off)\n" ,type , 103t .name .c_str (), (long long )rows , (long long )cols ,bad_f ?"FAIL" :"ok" , (long long )bad_f , 104worst ,bad_h ?"FAIL" :"ok" , (long long )bad_h ); 105 } 106 } 107 std::printf ("dequant_bf16_test: %d types checked, %s\n" ,checked ,fails ?"FAILED" :"OK" ); 108return fails ?1 :0 ; 109}