okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/artifact/dequant.cpp - the `strata-dequant` CLI (dequantize a GGUF tensor and dump F32). 2// The dequantizers themselves are header-only in include/strata/artifact/dequant.hpp. 3#include "strata/artifact/dequant.hpp" 4 5#ifdef STRATA_DEQUANT_SELFTEST 6int main (int argc ,char ** argv ) { 7if (argc < 2 ) { 8 std::printf ("usage: dequant <file.gguf>\n" ); 9return 2 ; 10 } 11try { 12 strata::GgufFile g (argv [1 ]); 13int checked = 0 ,bad = 0 ; 14for (const auto & t :g .tensors ()) { 15if (t .type != 42 )continue ;// Q2_0 only for now 16if (checked >=8 )break ; 17const uint8_t * base = g .tensor_data (t ); 18const uint64_t nblocks = t .elements () /64 ; 19for (uint64_t b = 0 ;b < nblocks && b < 64 ;++ b ) { 20const uint8_t * blk = base + b * 18 ; 21float out [64 ]; 22 strata::dequantize_q2_0 (blk ,out ); 23const float d = strata::fp16_to_fp32 (strata::read_u16 (blk )); 24// STRUCTURAL INVARIANT: every value must be exactly one of {-d, 0, d, 2d}. 25for (int j = 0 ;j < 64 ;++ j ) { 26const float v = out [j ]; 27const bool ok = (v == - d )|| (v == 0.0f )|| (v == d )|| (v == 2.0f * d )|| (d == 0.0f ); 28if (!ok ) { 29++ bad ; 30if (bad < 4 ) 31 std::printf (" VIOLATION %s block %llu elem %d: v=%g d=%g\n" ,t .name .c_str (), 32 (unsigned long long )b ,j ,v ,d ); 33 } 34 } 35 } 36if (checked == 0 ) { 37// A folded checksum over the FIRST 64 blocks, for cross-checking against the Python 38// implementation on identical bytes. A single implementation being self-consistent 39// proves little - two disagreeing is what finds bugs (round 67). 40double sum = 0 ,sumabs = 0 ; 41const uint8_t * base = g .tensor_data (t ); 42for (uint64_t b = 0 ;b < nblocks && b < 64 ;++ b ) { 43float out [64 ]; 44 strata::dequantize_q2_0 (base + b * 18 ,out ); 45for (int j = 0 ;j < 64 ;++ j ) { 46sum += out [j ]; 47sumabs += std::fabs (out [j ]); 48 } 49 } 50 std::printf (" CHECKSUM %s sum=%.10g sumabs=%.10g\n" ,t .name .c_str (),sum ,sumabs ); 51 } 52++ checked ; 53 } 54// Q5_0 cross-check on the same 64-block window gguf-py is given, using the first Q5_0 tensor. 55for (const auto & t :g .tensors ()) { 56if (t .type != 6 && t .type != 20 && t .type != 14 && t .type != 12 && t .type != 13 && t .type != 11 && 57t .type != 23 ) 58continue ; 59const uint8_t * base = g .tensor_data (t ); 60const int bsz = 61 (t .type == 20 ) 62 ?18 63 : (t .type == 14 64 ?210 65 : (t .type == 12 66 ?144 67 : (t .type == 13 ?176 : (t .type == 11 ?110 : (t .type == 23 ?136 :22 ))))); 68const int nel = 69 (t .type == 14 || t .type == 12 || t .type == 13 || t .type == 11 || t .type == 23 ) ?256 :32 ; 70const uint64_t nb = t .elements () / (uint64_t )nel ; 71double sum = 0 ,sumabs = 0 ; 72for (uint64_t b = 0 ;b < nb && b < 64 ;++ b ) { 73float out [256 ]; 74if (t .type == 20 ) 75 strata::dequantize_iq4_nl (base + b * bsz ,out ); 76else if (t .type == 14 ) 77 strata::dequantize_q6_K (base + b * bsz ,out ); 78else if (t .type == 12 ) 79 strata::dequantize_q4_K (base + b * bsz ,out ); 80else if (t .type == 13 ) 81 strata::dequantize_q5_K (base + b * bsz ,out ); 82else if (t .type == 11 ) 83 strata::dequantize_q3_K (base + b * bsz ,out ); 84else if (t .type == 23 ) 85 strata::dequantize_iq4_xs (base + b * bsz ,out ); 86else 87 strata::dequantize_q5_0 (base + b * bsz ,out ); 88for (int j = 0 ;j < nel ;++ j ) { 89sum += out [j ]; 90sumabs += std::fabs (out [j ]); 91 } 92 } 93 std::printf (" CHECKSUM %s %s sum=%.10g sumabs=%.10g\n" ,t .type_name (),t .name .c_str (),sum , 94sumabs ); 95float f8 [256 ]; 96if (t .type == 20 ) 97 strata::dequantize_iq4_nl (base ,f8 ); 98else if (t .type == 14 ) 99 strata::dequantize_q6_K (base ,f8 ); 100else if (t .type == 12 ) 101 strata::dequantize_q4_K (base ,f8 ); 102else if (t .type == 13 ) 103 strata::dequantize_q5_K (base ,f8 ); 104else if (t .type == 11 ) 105 strata::dequantize_q3_K (base ,f8 ); 106else if (t .type == 23 ) 107 strata::dequantize_iq4_xs (base ,f8 ); 108else 109 strata::dequantize_q5_0 (base ,f8 ); 110 std::printf (" REF first8:" ); 111for (int j = 0 ;j < 8 ;++ j ) std::printf (" %.6g" ,f8 [j ]); 112 std::printf ("\n" ); 113if (t .type == 23 )break ;// emit Q5_0 first, then IQ4_NL 114 } 115 std::printf ("%s\n Q2_0 tensors spot-checked: %d\n symbolic invariant violations: %d -> %s\n" , 116argv [1 ],checked ,bad ,bad ?"FAIL" :"PASS" ); 117return bad ?1 :0 ; 118 }catch (const std::exception & e ) { 119 std::printf ("ERROR: %s\n" ,e .what ()); 120return 1 ; 121 } 122} 123#endif