okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8a66da38a
main
1// tools/vision/gguf_meta_test.cpp - strata-vision opens a GGUF whose weight tensors have types llama.cpp does not know. 2// 3// Writes a small GGUF by hand (a tokenizer, two tensors of the Strata-private types TQK6 = 145 and TQK7 = 146, and split 4// keys that point at shards which are not there), then checks that 5// - llama.cpp itself cannot load it (the reason the picture reader used to say "cannot open the text model"), 6// - strata_open_text_model() can, and sees the same vocabulary. 7// Exit code 0 = all checks passed. 8#include "gguf_meta.h" 9 10#include <cstdint> 11#include <cstdio> 12#include <string> 13#include <vector> 14 15namespace { 16 17struct Out { 18 std::vector < uint8_t > b ; 19void raw (const void * p ,size_t n ) {const auto * c = (const uint8_t * )p ;b .insert (b .end (),c ,c + n ); } 20template < class T > void num (T v ) {raw (& v ,sizeof v ); } 21void str (const std::string & s ) {num < uint64_t > (s .size ());raw (s .data (),s .size ()); } 22void kv_str (const std::string & k ,const std::string & v ) {str (k );num < uint32_t > (GGUF_TYPE_STRING );str (v ); } 23void kv_u32 (const std::string & k ,uint32_t v ) {str (k );num < uint32_t > (GGUF_TYPE_UINT32 );num < uint32_t > (v ); } 24void kv_strs (const std::string & k ,const std::vector < std::string >& v ) { 25str (k );num < uint32_t > (GGUF_TYPE_ARRAY );num < uint32_t > (GGUF_TYPE_STRING );num < uint64_t > (v .size ()); 26for (const auto & s :v )str (s ); 27 } 28void kv_i32s (const std::string & k ,const std::vector < int32_t >& v ) { 29str (k );num < uint32_t > (GGUF_TYPE_ARRAY );num < uint32_t > (GGUF_TYPE_INT32 );num < uint64_t > (v .size ()); 30raw (v .data (),v .size ()* 4 ); 31 } 32}; 33 34int failures = 0 ; 35void check (bool ok ,const char * what ) { 36 std::printf ("%s %s\n" ,ok ?"ok " :"FAIL" ,what ); 37if (!ok )++ failures ; 38} 39 40bool write_fixture (const std::string & path ,size_t & n_vocab ,bool split ) { 41 std::vector < std::string > tokens ; 42for (int i = 0 ;i < 256 ;++ i )tokens .push_back (std::string ("t" )+ std::to_string (i )); 43tokens .push_back ("<|endoftext|>" ); 44n_vocab = tokens .size (); 45 std::vector < int32_t > types (tokens .size (),1 ); 46types .back ()= 3 ; 47Out o ; 48o .raw ("GGUF" ,4 ); 49o .num < uint32_t > (3 ); 50o .num < uint64_t > (2 );// two tensors 51const uint64_t n_kv = split ?9 :6 ; 52o .num < uint64_t > (n_kv ); 53o .kv_str ("general.architecture" ,"llama" ); 54o .kv_str ("tokenizer.ggml.model" ,"gpt2" ); 55o .kv_str ("tokenizer.ggml.pre" ,"default" ); 56o .kv_strs ("tokenizer.ggml.tokens" ,tokens ); 57o .kv_i32s ("tokenizer.ggml.token_type" ,types ); 58o .kv_strs ("tokenizer.ggml.merges" , {"t1 t2" }); 59if (split ) { 60o .kv_u32 ("split.no" ,0 ); 61o .kv_u32 ("split.count" ,2 ); 62o .kv_u32 ("split.tensors.count" ,4 ); 63 } 64// two tensors of types llama.cpp's ggml does not have (145 = TQK6, 146 = TQK7), 128 elements each 65for (int i = 0 ;i < 2 ;++ i ) { 66o .str (i == 0 ?"blk.0.ffn_gate_exps.weight" :"blk.0.ffn_up_exps.weight" ); 67o .num < uint32_t > (1 ); 68o .num < uint64_t > (128 ); 69o .num < uint32_t > (145 + i ); 70o .num < uint64_t > (i * 64 ); 71 } 72while (o .b .size () %32 )o .b .push_back (0 ); 73o .b .resize (o .b .size ()+ 128 ,0 ); 74FILE * f = std::fopen (path .c_str (),"wb" ); 75if (!f )return false; 76const bool ok = std::fwrite (o .b .data (),1 ,o .b .size (),f )== o .b .size (); 77 std::fclose (f ); 78return ok ; 79} 80 81}// namespace 82 83int main (int argc ,char ** argv ) { 84const std::string dir = argc > 1 ?argv [1 ] :"." ; 85llama_backend_init (); 86for (int split = 0 ;split < 2 ;++ split ) { 87const std::string label = split ?" (a first shard whose other shards are missing)" :"" ; 88const std::string path = dir + "/strata-vision-gguf-test-" + std::to_string (split )+ ".gguf" ; 89size_t n_vocab = 0 ; 90check (write_fixture (path ,n_vocab ,split == 1 ), ("wrote a GGUF with tensor types 145 and 146" + label ).c_str ()); 91llama_model_params mp = llama_model_default_params (); 92mp .vocab_only = true; 93llama_model * plain = llama_model_load_from_file (path .c_str (),mp ); 94 std::printf ("note llama.cpp itself %s this file\n" ,plain ?"accepts" :"refuses" ); 95if (plain )llama_model_free (plain ); 96 std::string how ; 97llama_model * m = strata_open_text_model (path ,& how ); 98check (m != nullptr , ("strata_open_text_model opens it (" + how + ")" + label ).c_str ()); 99if (m ) { 100check ((size_t )llama_vocab_n_tokens (llama_model_get_vocab (m ))== n_vocab ,"and sees the whole vocabulary" ); 101llama_model_free (m ); 102 } 103check (how .find ("header only" )== 0 ,"by reading the header, not the weights" ); 104 std::remove (path .c_str ()); 105 } 106check (strata_open_text_model (dir + "/does-not-exist.gguf" )== nullptr ,"a file that is not there gives no model" ); 107llama_backend_free (); 108 std::printf ("%s\n" ,failures ?"GGUF-META FAIL" :"GGUF-META PASS" ); 109return failures ?1 :0 ; 110}