okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
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#define DPCT_PROFILING_ENABLED 9#include <sycl/sycl.hpp> 10#include <dpct/dpct.hpp> 11#include "strata/artifact/dequant.hpp" 12#include "strata/artifact/gguf_reader.hpp" 13#include "strata/kernels/dequant_bf16.hpp" 14 15#include <cmath> 16#include <cstdio> 17#include <cstring> 18#include <map> 19#include <string> 20#include <vector> 21 22namespace { 23 24bool cpu_block (int type ,const uint8_t * b ,float * out ) { 25using namespace strata ; 26switch (type ) { 27case 2 :dequantize_q4_0 (b ,out );return true; 28case 6 :dequantize_q5_0 (b ,out );return true; 29case 7 :dequantize_q5_1 (b ,out );return true; 30case 8 :dequantize_q8_0 (b ,out );return true; 31case 11 :dequantize_q3_K (b ,out );return true; 32case 12 :dequantize_q4_K (b ,out );return true; 33case 13 :dequantize_q5_K (b ,out );return true; 34case 14 :dequantize_q6_K (b ,out );return true; 35case 20 :dequantize_iq4_nl (b ,out );return true; 36case 23 :dequantize_iq4_xs (b ,out );return true; 37case 42 :dequantize_q2_0 (b ,out );return true; 38default :return false; 39 } 40} 41 42float bf16_to_f32 (uint16_t h ) { 43uint32_t u = (uint32_t )h <<16 ; 44float f ; 45 std::memcpy (& f ,& u ,4 ); 46return f ; 47} 48 49}// namespace 50 51int main (int argc ,char ** argv ) { 52if (argc < 2 ) { 53 std::fprintf (stderr ,"usage: dequant_bf16_test SHARD.gguf [SHARD2.gguf]\n" ); 54return 2 ; 55 } 56 std::map < int ,bool > done ; 57int fails = 0 ,checked = 0 ; 58for (int a = 1 ;a < argc ;++ a ) { 59 strata::GgufFile gguf (argv [a ]); 60for (const auto & t :gguf .tensors ()) { 61const int type = (int )t .type ; 62if (done .count (type )|| !strata::kernels::dequant_bf16_supported (type )|| t .shape .size ()!= 2 )continue ; 63int be = 0 ,bb = 0 ; 64if (!strata::block_geometry (t .type ,be ,bb ))continue ; 65const int64_t cols = (int64_t )t .shape [0 ]; 66const int64_t rows = (std::min < int64_t > )(4 , (int64_t )t .shape [1 ]); 67const int64_t row_bytes = cols /be * bb ; 68const uint8_t * host = (const uint8_t * )gguf .tensor_data (t ); 69void * d_blocks = nullptr ; 70float * d_f = nullptr ; 71uint16_t * d_h = nullptr ; 72d_blocks = (void * )sycl::malloc_device ((size_t )(rows * row_bytes ), 73 dpct::get_in_order_queue ()); 74d_f = (float * )sycl::malloc_device ((size_t )(rows * cols )* 4 , 75 dpct::get_in_order_queue ()); 76d_h = (uint16_t * )sycl::malloc_device ((size_t )(rows * cols )* 2 , 77 dpct::get_in_order_queue ()); 78 dpct::get_in_order_queue () 79 .memcpy (d_blocks ,host , (size_t )(rows * row_bytes )) 80 .wait (); 81 strata::kernels::dequant_f32 (type ,d_blocks ,0 ,rows ,cols ,d_f ,nullptr ); 82 strata::kernels::dequant_bf16 (type ,d_blocks ,0 ,rows ,cols ,d_h ,nullptr ); 83 std::vector < float > gf ((size_t ) (rows * cols )); 84 std::vector < uint16_t > gh ((size_t ) (rows * cols )); 85 dpct::get_in_order_queue ().memcpy (gf .data (),d_f ,gf .size ()* 4 ).wait (); 86 dpct::get_in_order_queue () 87 .memcpy (gh .data (),d_h ,gh .size ()* 2 ) 88 .wait (); 89 sycl::free (d_blocks , dpct::get_in_order_queue ()); 90 sycl::free (d_f , dpct::get_in_order_queue ()); 91 sycl::free (d_h , dpct::get_in_order_queue ()); 92 std::vector < float > ref ((size_t ) (rows * cols )); 93for (int64_t r = 0 ;r < rows ;++ r ) 94for (int64_t b = 0 ;b < cols /be ;++ b ) 95cpu_block (type ,host + r * row_bytes + b * bb ,ref .data ()+ r * cols + b * be ); 96int64_t bad_f = 0 ,bad_h = 0 ; 97double worst = 0 ; 98for (size_t i = 0 ;i < ref .size ();++ i ) { 99const double e = std::fabs ((double )gf [i ]- ref [i ]); 100const double tol = 1e-6 * std::fabs ((double )ref [i ])+ 1e-30 ; 101if (e > tol )++ bad_f ; 102worst = (std::max )(worst ,e / (std::fabs ((double )ref [i ])+ 1e-30 )); 103const double eh = std::fabs ((double )bf16_to_f32 (gh [i ])- ref [i ]); 104if (eh > std::ldexp (std::fabs ((double )ref [i ]),-8 )+ 1e-30 )++ bad_h ; 105 } 106++ checked ; 107done [type ]= true; 108const bool ok = bad_f == 0 && bad_h == 0 ; 109if (!ok )++ fails ; 110 std::printf ("type %2d %-40s %lld x %lld: f32 %s (%lld off, worst rel %.2e), bf16 %s (%lld off)\n" ,type , 111t .name .c_str (), (long long )rows , (long long )cols ,bad_f ?"FAIL" :"ok" , (long long )bad_f , 112worst ,bad_h ?"FAIL" :"ok" , (long long )bad_h ); 113 } 114 } 115 std::printf ("dequant_bf16_test: %d types checked, %s\n" ,checked ,fails ?"FAILED" :"OK" ); 116return fails ?1 :0 ; 117}