okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
1// Writes what the loader makes of tensors of a GGUF (src/tensor_convert.hpp): for each tensor named on the command line, the 2// converted tensor's values as float32 to OUTDIR/<name>.f32 plus a line "name type_after nbytes" on stdout. tests/split/convert_check.py 3// compares those with gguf-py's own dequantization of the same tensors in the file. CPU only. 4// bin/convert_dump MODEL.gguf OUTDIR name [name ...] (a name of "all" dumps every tensor the loader converted) 5#include <cstdio> 6#include <cstdlib> 7#include <string> 8#include <vector> 9 10#include "../src/gguf.hpp" 11#include "../src/tensor_convert.hpp" 12 13int main (int argc ,char ** argv ) { 14if (argc < 4 ) { 15 std::fprintf (stderr ,"usage: convert_dump MODEL.gguf OUTDIR name [name ...]\n" ); 16return 2 ; 17 } 18try { 19 fnx::Gguf g (argv [1 ]); 20 std::printf ("converted %d\n" ,g .converted ()); 21 std::vector < std::string > names ; 22for (int i = 3 ;i < argc ;++ i ) { 23if (std::string (argv [i ])== "all" ) { 24for (const auto & kv :g .tensors ()) 25if (fnx::conv::wanted_type (kv .first )!= 0xFFFFFFFFu )names .push_back (kv .first ); 26 }else { 27names .push_back (argv [i ]); 28 } 29 } 30for (const std::string & n :names ) { 31const fnx::Tensor & t = g .t (n ); 32size_t cnt = 1 ; 33for (int d = 0 ;d < 4 ;++ d )cnt *= (size_t )t .ne [d ]; 34 std::vector < float > f (cnt ); 35 fnx::conv::dequantize (t .type ,t .data ,cnt ,f .data ()); 36const std::string path = std::string (argv [2 ])+ "/" + n + ".f32" ; 37FILE * fp = std::fopen (path .c_str (),"wb" ); 38if (!fp )throw std::runtime_error ("cannot write " + path ); 39 std::fwrite (f .data (),4 ,cnt ,fp ); 40 std::fclose (fp ); 41 std::printf ("%s %u %zu\n" ,n .c_str (),t .type ,t .nbytes ); 42 } 43 }catch (const std::exception & e ) { 44 std::fprintf (stderr ,"convert_dump: %s\n" ,e .what ()); 45return 1 ; 46 } 47return 0 ; 48}