okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8a66da38a
main
1// tools/vision/gguf_meta.h - open a model GGUF for its vocabulary only, whatever its weight tensors are. 2// 3// The picture reader needs the text model for one thing: its tokenizer (and the header values that go with it). llama.cpp's 4// loader reads the whole tensor directory first and refuses a file with a tensor type it does not know - and a Gyro 5// quantization (TQK6 / TQK7, ids 145 and 146) and other Strata-private types are exactly that - so even a vocab-only 6// load failed with "cannot open the text model". strata_open_text_model() therefore copies the key/value header of the 7// GGUF (everything before the tensor directory) into a small temporary GGUF without tensors, and loads THAT vocab-only. 8// The weights are never read, mapped or type-checked, and the split keys are dropped so the loader does not go looking 9// for the other shards. If the header cannot be read (a type this reader does not know), it falls back to loading the 10// file itself, as before. 11#pragma once 12 13#include "gguf.h" 14#include "llama.h" 15 16#include <cstdint> 17#include <cstdio> 18#include <cstdlib> 19#include <cstring> 20#include <string> 21#include <vector> 22 23#if defined(_WIN32 ) 24#include <process.h> 25#define STRATA_GETPID _getpid 26#else 27#include <unistd.h> 28#define STRATA_GETPID getpid 29#endif 30 31namespace strata_gguf { 32 33struct Reader { 34FILE * f = nullptr; 35std ::string err ; 36bool take (void * dst ,size_t n ) { 37if (std ::fread (dst ,1 ,n ,f )!= n ) {err = "the GGUF header is cut short" ;return false ; } 38return true ; 39 } 40template < class T > bool num (T & v ) {return take (& v ,sizeof v ); } 41bool str (std ::string & s ,uint64_t cap = (1ull <<26 )) { 42uint64_t n = 0 ; 43if (!num (n ))return false; 44if (n > cap ) {err = "a string in the GGUF header is too long" ;return false; } 45s .resize ((size_t )n ); 46return n == 0 || take (& s [0 ], (size_t )n ); 47 } 48}; 49 50inline size_t scalar_size (uint32_t t ) { 51switch (t ) { 52case GGUF_TYPE_UINT8 :case GGUF_TYPE_INT8 :case GGUF_TYPE_BOOL :return 1 ; 53case GGUF_TYPE_UINT16 :case GGUF_TYPE_INT16 :return 2 ; 54case GGUF_TYPE_UINT32 :case GGUF_TYPE_INT32 :case GGUF_TYPE_FLOAT32 :return 4 ; 55case GGUF_TYPE_UINT64 :case GGUF_TYPE_INT64 :case GGUF_TYPE_FLOAT64 :return 8 ; 56default :return 0 ; 57 } 58} 59 60inline bool dropped_key (const std ::string & k ) { 61return k == "split.no" || k == "split.count" || k == "split.tensors.count" ; 62} 63 64// Copy the key/value header of `src` into `dst` as a GGUF with no tensors. False (and `err`) when the header holds 65// something this reader does not handle. 66inline bool copy_meta (const std ::string & src ,const std ::string & dst ,std ::string & err ) { 67Reader r ; 68r .f = std ::fopen (src .c_str (),"rb" ); 69if (!r .f ) {err = "cannot open " + src ;return false; } 70struct Closer {FILE * f ; ~Closer () {if (f )std ::fclose (f ); } }closer {r .f }; 71char magic [4 ]; 72uint32_t version = 0 ; 73uint64_t n_tensors = 0 ,n_kv = 0 ; 74if (!r .take (magic ,4 )|| std ::memcmp (magic ,"GGUF" ,4 )!= 0 ) {err = r .err .empty () ?"not a GGUF file" :r .err ;return false; } 75if (!r .num (version )|| !r .num (n_tensors )|| !r .num (n_kv )) {err = r .err ;return false; } 76if (version < 2 || version > 3 ) {err = "GGUF version " + std ::to_string (version )+ " is not handled" ;return false; } 77if (n_kv > 100000 ) {err = "too many keys in the GGUF header" ;return false; } 78gguf_context * out = gguf_init_empty (); 79if (!out ) {err = "out of memory" ;return false; } 80struct Freer {gguf_context * g ; ~Freer () {gguf_free (g ); } }freer {out }; 81for (uint64_t i = 0 ;i < n_kv ;++ i ) { 82std ::string key ; 83uint32_t type = 0 ; 84if (!r .str (key ,1 <<16 )|| !r .num (type )) {err = r .err ;return false; } 85const bool keep = !dropped_key (key ); 86if (type == GGUF_TYPE_STRING ) { 87std ::string v ; 88if (!r .str (v )) {err = r .err ;return false; } 89if (keep )gguf_set_val_str (out ,key .c_str (),v .c_str ()); 90 }else if (type == GGUF_TYPE_ARRAY ) { 91uint32_t et = 0 ; 92uint64_t n = 0 ; 93if (!r .num (et )|| !r .num (n )) {err = r .err ;return false; } 94if (n > 50'000'000ull ) {err = "an array in the GGUF header is too long" ;return false; } 95if (et == GGUF_TYPE_STRING ) { 96std ::vector < std ::string > items ((size_t )n ); 97for (auto& s :items )if (!r .str (s )) {err = r .err ;return false ; } 98if (keep ) { 99std ::vector < const char *> ptrs ; 100ptrs .reserve (items .size ()); 101for (const auto & s :items )ptrs .push_back (s .c_str ()); 102gguf_set_arr_str (out ,key .c_str (),ptrs .data (),ptrs .size ()); 103 } 104 }else if (scalar_size (et )) { 105std ::vector < uint8_t > raw ((size_t ) (n * scalar_size (et ))); 106if (!raw .empty ()&& !r .take (raw .data (),raw .size ())) {err = r .err ;return false; } 107if (keep )gguf_set_arr_data (out ,key .c_str (), (gguf_type )et ,raw .data (), (size_t )n ); 108 }else {err = "an array of arrays in the GGUF header is not handled" ;return false; } 109 }else if (scalar_size (type )) { 110uint64_t raw = 0 ; 111if (!r .take (& raw ,scalar_size (type ))) {err = r .err ;return false; }// little endian, as the file 112if (!keep )continue ; 113switch (type ) { 114case GGUF_TYPE_UINT8 :gguf_set_val_u8 (out ,key .c_str (), (uint8_t )raw );break ; 115case GGUF_TYPE_INT8 :gguf_set_val_i8 (out ,key .c_str (), (int8_t )raw );break ; 116case GGUF_TYPE_UINT16 :gguf_set_val_u16 (out ,key .c_str (), (uint16_t )raw );break ; 117case GGUF_TYPE_INT16 :gguf_set_val_i16 (out ,key .c_str (), (int16_t )raw );break ; 118case GGUF_TYPE_UINT32 :gguf_set_val_u32 (out ,key .c_str (), (uint32_t )raw );break ; 119case GGUF_TYPE_INT32 :gguf_set_val_i32 (out ,key .c_str (), (int32_t )raw );break ; 120case GGUF_TYPE_FLOAT32 : {float v ;std ::memcpy (& v ,& raw ,4 );gguf_set_val_f32 (out ,key .c_str (),v );break ; } 121case GGUF_TYPE_BOOL :gguf_set_val_bool (out ,key .c_str (),raw != 0 );break ; 122case GGUF_TYPE_UINT64 :gguf_set_val_u64 (out ,key .c_str (),raw );break ; 123case GGUF_TYPE_INT64 :gguf_set_val_i64 (out ,key .c_str (), (int64_t )raw );break ; 124case GGUF_TYPE_FLOAT64 : {double v ;std ::memcpy (& v ,& raw ,8 );gguf_set_val_f64 (out ,key .c_str (),v );break ; } 125default :break ; 126 } 127 }else {err = "a value type in the GGUF header is not handled" ;return false; } 128 } 129if (!gguf_write_to_file (out ,dst .c_str (), true)) {err = "cannot write " + dst ;return false; } 130return true; 131} 132 133}// namespace strata_gguf 134 135// The text model for the picture reader: vocabulary and header only. `how` says which way it was opened. 136inline llama_model * strata_open_text_model (const std ::string & model ,std ::string * how = nullptr ) { 137llama_model_params mp = llama_model_default_params (); 138mp .vocab_only = true; 139const char * tmpdir = std ::getenv ("TMPDIR" ); 140if (!tmpdir || !* tmpdir )tmpdir = std ::getenv ("TEMP" ); 141if (!tmpdir || !* tmpdir )tmpdir = std ::getenv ("TMP" ); 142if (!tmpdir || !* tmpdir )tmpdir = "/tmp" ; 143const std ::string meta = std ::string (tmpdir )+ "/strata-vision-meta-" + std ::to_string ((long )STRATA_GETPID ())+ ".gguf" ; 144std ::string err ; 145llama_model * m = nullptr ; 146if (strata_gguf ::copy_meta (model ,meta ,err )) { 147m = llama_model_load_from_file (meta .c_str (),mp ); 148if (how )* how = m ?"header only" :"header copy was not accepted" ; 149 }else if (how )* how = "header copy failed: " + err ; 150std ::remove (meta .c_str ()); 151if (!m ) {// the file as it is (a type of header this reader does not handle, or an llama.cpp that wants more) 152m = llama_model_load_from_file (model .c_str (),mp ); 153if (how )* how += m ?", loaded the file itself" :", the file itself did not load either" ; 154 } 155return m ; 156}