char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
cb8f4fa3f
char/aliceai-t5-moe
1#include "ggml.h" 2#include "gguf.h" 3 4#include <clocale> 5#include <cstdio> 6#include <string> 7#include <sstream> 8#include <vector> 9 10#undef MIN 11#undef MAX 12#define MIN (a ,b ) ((a) < (b) ? (a) : (b)) 13#define MAX (a ,b ) ((a) > (b) ? (a) : (b)) 14 15template < typename T > 16static std::string to_string (const T & val ) { 17 std::stringstream ss ; 18ss <<val ; 19return ss .str (); 20} 21 22static bool gguf_ex_write (const std::string & fname ) { 23struct gguf_context * ctx = gguf_init_empty (); 24 25gguf_set_val_u8 (ctx ,"some.parameter.uint8" ,0x12 ); 26gguf_set_val_i8 (ctx ,"some.parameter.int8" ,-0x13 ); 27gguf_set_val_u16 (ctx ,"some.parameter.uint16" ,0x1234 ); 28gguf_set_val_i16 (ctx ,"some.parameter.int16" ,-0x1235 ); 29gguf_set_val_u32 (ctx ,"some.parameter.uint32" ,0x12345678 ); 30gguf_set_val_i32 (ctx ,"some.parameter.int32" ,-0x12345679 ); 31gguf_set_val_f32 (ctx ,"some.parameter.float32" ,0.123456789f ); 32gguf_set_val_u64 (ctx ,"some.parameter.uint64" ,0x123456789abcdef0ull ); 33gguf_set_val_i64 (ctx ,"some.parameter.int64" ,-0x123456789abcdef1ll ); 34gguf_set_val_f64 (ctx ,"some.parameter.float64" ,0.1234567890123456789 ); 35gguf_set_val_bool (ctx ,"some.parameter.bool" , true); 36gguf_set_val_str (ctx ,"some.parameter.string" ,"hello world" ); 37 38gguf_set_arr_data (ctx ,"some.parameter.arr.i16" ,GGUF_TYPE_INT16 , std::vector < int16_t > {1 ,2 ,3 ,4 , }.data (),4 ); 39gguf_set_arr_data (ctx ,"some.parameter.arr.f32" ,GGUF_TYPE_FLOAT32 , std::vector < float > {3.145f ,2.718f ,1.414f , }.data (),3 ); 40gguf_set_arr_str (ctx ,"some.parameter.arr.str" , std::vector < const char *> {"hello" ,"world" ,"!" }.data (),3 ); 41 42struct ggml_init_params params = { 43/*.mem_size =*/ 128ull * 1024ull * 1024ull , 44/*.mem_buffer =*/ NULL , 45/*.no_alloc =*/ false, 46 }; 47 48struct ggml_context * ctx_data = ggml_init (params ); 49 50const int n_tensors = 10 ; 51 52// tensor infos 53for (int i = 0 ;i < n_tensors ;++ i ) { 54const std::string name = "tensor_" + to_string (i ); 55 56int64_t ne [GGML_MAX_DIMS ]= {1 }; 57int32_t n_dims = rand () %GGML_MAX_DIMS + 1 ; 58 59for (int j = 0 ;j < n_dims ;++ j ) { 60ne [j ]= rand () %10 + 1 ; 61 } 62 63struct ggml_tensor * cur = ggml_new_tensor (ctx_data ,GGML_TYPE_F32 ,n_dims ,ne ); 64ggml_set_name (cur ,name .c_str ()); 65 66 { 67float * data = (float * )cur -> data ; 68for (int j = 0 ;j < ggml_nelements (cur );++ j ) { 69data [j ]= 100 + i ; 70 } 71 } 72 73gguf_add_tensor (ctx ,cur ); 74 } 75 76gguf_write_to_file (ctx ,fname .c_str (), false); 77 78printf ("%s: wrote file '%s;\n" ,__func__ ,fname .c_str ()); 79 80ggml_free (ctx_data ); 81gguf_free (ctx ); 82 83return true; 84} 85 86// just read tensor info 87static bool gguf_ex_read_0 (const std::string & fname ) { 88struct gguf_init_params params = { 89/*.no_alloc = */ false, 90/*.ctx = */ NULL , 91 }; 92 93struct gguf_context * ctx = gguf_init_from_file (fname .c_str (),params ); 94 95if (!ctx ) { 96fprintf (stderr ,"%s: failed to load '%s'\n" ,__func__ ,fname .c_str ()); 97return false; 98 } 99 100printf ("%s: version: %d\n" ,__func__ ,gguf_get_version (ctx )); 101printf ("%s: alignment: %zu\n" ,__func__ ,gguf_get_alignment (ctx )); 102printf ("%s: data offset: %zu\n" ,__func__ ,gguf_get_data_offset (ctx )); 103 104// kv 105 { 106const int n_kv = gguf_get_n_kv (ctx ); 107 108printf ("%s: n_kv: %d\n" ,__func__ ,n_kv ); 109 110for (int i = 0 ;i < n_kv ;++ i ) { 111const char * key = gguf_get_key (ctx ,i ); 112 113printf ("%s: kv[%d]: key = %s\n" ,__func__ ,i ,key ); 114 } 115 } 116 117// find kv string 118 { 119const char * findkey = "some.parameter.string" ; 120 121const int keyidx = gguf_find_key (ctx ,findkey ); 122if (keyidx == -1 ) { 123printf ("%s: find key: %s not found.\n" ,__func__ ,findkey ); 124 }else { 125const char * key_value = gguf_get_val_str (ctx ,keyidx ); 126printf ("%s: find key: %s found, kv[%d] value = %s\n" ,__func__ ,findkey ,keyidx ,key_value ); 127 } 128 } 129 130// tensor info 131 { 132const int n_tensors = gguf_get_n_tensors (ctx ); 133 134printf ("%s: n_tensors: %d\n" ,__func__ ,n_tensors ); 135 136for (int i = 0 ;i < n_tensors ;++ i ) { 137const char * name = gguf_get_tensor_name (ctx ,i ); 138const size_t size = gguf_get_tensor_size (ctx ,i ); 139const size_t offset = gguf_get_tensor_offset (ctx ,i ); 140 141printf ("%s: tensor[%d]: name = %s, size = %zu, offset = %zu\n" ,__func__ ,i ,name ,size ,offset ); 142 } 143 } 144 145gguf_free (ctx ); 146 147return true; 148} 149 150// read and create ggml_context containing the tensors and their data 151static bool gguf_ex_read_1 (const std::string & fname ,bool check_data ) { 152struct ggml_context * ctx_data = NULL ; 153 154struct gguf_init_params params = { 155/*.no_alloc = */ false, 156/*.ctx = */ & ctx_data , 157 }; 158 159struct gguf_context * ctx = gguf_init_from_file (fname .c_str (),params ); 160 161printf ("%s: version: %d\n" ,__func__ ,gguf_get_version (ctx )); 162printf ("%s: alignment: %zu\n" ,__func__ ,gguf_get_alignment (ctx )); 163printf ("%s: data offset: %zu\n" ,__func__ ,gguf_get_data_offset (ctx )); 164 165// kv 166 { 167const int n_kv = gguf_get_n_kv (ctx ); 168 169printf ("%s: n_kv: %d\n" ,__func__ ,n_kv ); 170 171for (int i = 0 ;i < n_kv ;++ i ) { 172const char * key = gguf_get_key (ctx ,i ); 173 174printf ("%s: kv[%d]: key = %s\n" ,__func__ ,i ,key ); 175 } 176 } 177 178// tensor info 179 { 180const int n_tensors = gguf_get_n_tensors (ctx ); 181 182printf ("%s: n_tensors: %d\n" ,__func__ ,n_tensors ); 183 184for (int i = 0 ;i < n_tensors ;++ i ) { 185const char * name = gguf_get_tensor_name (ctx ,i ); 186const size_t size = gguf_get_tensor_size (ctx ,i ); 187const size_t offset = gguf_get_tensor_offset (ctx ,i ); 188const auto type = gguf_get_tensor_type (ctx ,i ); 189 190const char * type_name = ggml_type_name (type ); 191const size_t type_size = ggml_type_size (type ); 192const size_t n_elements = size /type_size ; 193 194printf ("%s: tensor[%d]: name = %s, size = %zu, offset = %zu, type = %s, n_elts = %zu\n" ,__func__ ,i ,name ,size ,offset ,type_name ,n_elements ); 195 } 196 } 197 198// data 199 { 200const int n_tensors = gguf_get_n_tensors (ctx ); 201 202for (int i = 0 ;i < n_tensors ;++ i ) { 203printf ("%s: reading tensor %d data\n" ,__func__ ,i ); 204 205const char * name = gguf_get_tensor_name (ctx ,i ); 206 207struct ggml_tensor * cur = ggml_get_tensor (ctx_data ,name ); 208 209printf ("%s: tensor[%d]: n_dims = %d, ne = (%d, %d, %d, %d), name = %s, data = %p\n" , 210__func__ ,i ,ggml_n_dims (cur ),int (cur -> ne [0 ]),int (cur -> ne [1 ]),int (cur -> ne [2 ]),int (cur -> ne [3 ]),cur -> name ,cur -> data ); 211 212// print first 10 elements 213const float * data = (const float * )cur -> data ; 214 215printf ("%s data[:10] : " ,name ); 216for (int j = 0 ;j < MIN (10 ,ggml_nelements (cur ));++ j ) { 217printf ("%f " ,data [j ]); 218 } 219printf ("\n\n" ); 220 221// check data 222if (check_data ) { 223const float * data = (const float * )cur -> data ; 224for (int j = 0 ;j < ggml_nelements (cur );++ j ) { 225if (data [j ]!= 100 + i ) { 226fprintf (stderr ,"%s: tensor[%d], data[%d]: found %f, expected %f\n" ,__func__ ,i ,j ,data [j ],float (100 + i )); 227gguf_free (ctx ); 228return false; 229 } 230 } 231 } 232 } 233 } 234 235printf ("%s: ctx_data size: %zu\n" ,__func__ ,ggml_get_mem_size (ctx_data )); 236 237ggml_free (ctx_data ); 238gguf_free (ctx ); 239 240return true; 241} 242 243int main (int argc ,char ** argv ) { 244 std::setlocale (LC_NUMERIC ,"C" ); 245 246if (argc < 3 ) { 247printf ("usage: %s data.gguf r|w [n]\n" ,argv [0 ]); 248printf ("r: read data.gguf file\n" ); 249printf ("w: write data.gguf file\n" ); 250printf ("n: no check of tensor data\n" ); 251return -1 ; 252 } 253bool check_data = true; 254if (argc == 4 ) { 255check_data = false; 256 } 257 258srand (123456 ); 259 260const std::string fname (argv [1 ]); 261const std::string mode (argv [2 ]); 262 263GGML_ASSERT ((mode == "r" || mode == "w" )&& "mode must be r or w" ); 264 265if (mode == "w" ) { 266GGML_ASSERT (gguf_ex_write (fname )&& "failed to write gguf file" ); 267 }else if (mode == "r" ) { 268GGML_ASSERT (gguf_ex_read_0 (fname )&& "failed to read gguf file" ); 269GGML_ASSERT (gguf_ex_read_1 (fname ,check_data )&& "failed to read gguf file" ); 270 } 271 272return 0 ; 273}