char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
ca86fb222
char/aliceai-t5-moe
1#include "ggml.h" 2#include "ggml-backend.h" 3#include "ggml-impl.h" 4#include "gguf.h" 5 6#include <cinttypes> 7#include <cstddef> 8#include <cstdint> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <cerrno> 13#include <map> 14#include <new> 15#include <stdexcept> 16#include <string> 17#include <vector> 18 19#define GGUF_MAX_STRING_LENGTH (1024*1024*1024) 20#define GGUF_MAX_ARRAY_ELEMENTS (1024*1024*1024) 21 22#ifdef _WIN32 23# define gguf_ftell _ftelli64 24# define gguf_fseek _fseeki64 25#else 26# define gguf_ftell ftello 27# define gguf_fseek fseeko 28#endif 29 30template < typename T > 31struct type_to_gguf_type ; 32 33template <> 34struct type_to_gguf_type < uint8_t > { 35static constexpr enum gguf_type value = GGUF_TYPE_UINT8 ; 36}; 37 38template <> 39struct type_to_gguf_type < int8_t > { 40static constexpr enum gguf_type value = GGUF_TYPE_INT8 ; 41}; 42 43template <> 44struct type_to_gguf_type < uint16_t > { 45static constexpr enum gguf_type value = GGUF_TYPE_UINT16 ; 46}; 47 48template <> 49struct type_to_gguf_type < int16_t > { 50static constexpr enum gguf_type value = GGUF_TYPE_INT16 ; 51}; 52 53template <> 54struct type_to_gguf_type < uint32_t > { 55static constexpr enum gguf_type value = GGUF_TYPE_UINT32 ; 56}; 57 58template <> 59struct type_to_gguf_type < int32_t > { 60static constexpr enum gguf_type value = GGUF_TYPE_INT32 ; 61}; 62 63template <> 64struct type_to_gguf_type < float > { 65static constexpr enum gguf_type value = GGUF_TYPE_FLOAT32 ; 66}; 67 68template <> 69struct type_to_gguf_type < bool > { 70static constexpr enum gguf_type value = GGUF_TYPE_BOOL ; 71}; 72 73template <> 74struct type_to_gguf_type < std::string > { 75static constexpr enum gguf_type value = GGUF_TYPE_STRING ; 76}; 77 78template <> 79struct type_to_gguf_type < uint64_t > { 80static constexpr enum gguf_type value = GGUF_TYPE_UINT64 ; 81}; 82 83template <> 84struct type_to_gguf_type < int64_t > { 85static constexpr enum gguf_type value = GGUF_TYPE_INT64 ; 86}; 87 88template <> 89struct type_to_gguf_type < double > { 90static constexpr enum gguf_type value = GGUF_TYPE_FLOAT64 ; 91}; 92 93static const std::map < gguf_type ,size_t > GGUF_TYPE_SIZE = { 94 {GGUF_TYPE_UINT8 ,sizeof (uint8_t )}, 95 {GGUF_TYPE_INT8 ,sizeof (int8_t )}, 96 {GGUF_TYPE_UINT16 ,sizeof (uint16_t )}, 97 {GGUF_TYPE_INT16 ,sizeof (int16_t )}, 98 {GGUF_TYPE_UINT32 ,sizeof (uint32_t )}, 99 {GGUF_TYPE_INT32 ,sizeof (int32_t )}, 100 {GGUF_TYPE_FLOAT32 ,sizeof (float )}, 101 {GGUF_TYPE_BOOL ,sizeof (int8_t )}, 102 {GGUF_TYPE_STRING ,0 },// undefined 103 {GGUF_TYPE_ARRAY ,0 },// undefined 104 {GGUF_TYPE_UINT64 ,sizeof (uint64_t )}, 105 {GGUF_TYPE_INT64 ,sizeof (int64_t )}, 106 {GGUF_TYPE_FLOAT64 ,sizeof (double )}, 107}; 108static_assert(GGUF_TYPE_COUNT == 13 ,"GGUF_TYPE_COUNT != 13" ); 109 110static const std::map < gguf_type ,const char *> GGUF_TYPE_NAME = { 111 {GGUF_TYPE_UINT8 ,"u8" }, 112 {GGUF_TYPE_INT8 ,"i8" }, 113 {GGUF_TYPE_UINT16 ,"u16" }, 114 {GGUF_TYPE_INT16 ,"i16" }, 115 {GGUF_TYPE_UINT32 ,"u32" }, 116 {GGUF_TYPE_INT32 ,"i32" }, 117 {GGUF_TYPE_FLOAT32 ,"f32" }, 118 {GGUF_TYPE_BOOL ,"bool" }, 119 {GGUF_TYPE_STRING ,"str" }, 120 {GGUF_TYPE_ARRAY ,"arr" }, 121 {GGUF_TYPE_UINT64 ,"u64" }, 122 {GGUF_TYPE_INT64 ,"i64" }, 123 {GGUF_TYPE_FLOAT64 ,"f64" }, 124}; 125static_assert(GGUF_TYPE_COUNT == 13 ,"GGUF_TYPE_COUNT != 13" ); 126 127size_t gguf_type_size (enum gguf_type type ) { 128auto it = GGUF_TYPE_SIZE .find (type ); 129return it == GGUF_TYPE_SIZE .end () ?0 :it -> second ; 130} 131 132struct gguf_kv { 133 std::string key ; 134 135bool is_array ; 136enum gguf_type type ; 137 138 std::vector < int8_t > data ; 139 std::vector < std::string > data_string ; 140 141template < typename T > 142gguf_kv (const std::string & key ,const T value ) 143 :key (key ),is_array (false),type (type_to_gguf_type < T > ::value ) { 144GGML_ASSERT (!key .empty ()); 145data .resize (sizeof (T )); 146memcpy (data .data (),& value ,sizeof (T )); 147 } 148 149template < typename T > 150gguf_kv (const std::string & key ,const std::vector < T > & value ) 151 :key (key ),is_array (true),type (type_to_gguf_type < T > ::value ) { 152GGML_ASSERT (!key .empty ()); 153data .resize (value .size ()* sizeof (T )); 154for (size_t i = 0 ;i < value .size ();++ i ) { 155const T tmp = value [i ]; 156memcpy (data .data ()+ i * sizeof (T ),& tmp ,sizeof (T )); 157 } 158 } 159 160gguf_kv (const std::string & key ,const std::string & value ) 161 :key (key ),is_array (false),type (GGUF_TYPE_STRING ) { 162GGML_ASSERT (!key .empty ()); 163data_string .push_back (value ); 164 } 165 166gguf_kv (const std::string & key ,const std::vector < std::string > & value ) 167 :key (key ),is_array (true),type (GGUF_TYPE_STRING ) { 168GGML_ASSERT (!key .empty ()); 169data_string = value ; 170 } 171 172const std::string & get_key ()const { 173return key ; 174 } 175 176const enum gguf_type & get_type ()const { 177return type ; 178 } 179 180size_t get_ne ()const { 181if (type == GGUF_TYPE_STRING ) { 182const size_t ne = data_string .size (); 183GGML_ASSERT (is_array || ne == 1 ); 184return ne ; 185 } 186const size_t type_size = gguf_type_size (type ); 187GGML_ASSERT (data .size () %type_size == 0 ); 188const size_t ne = data .size () /type_size ; 189GGML_ASSERT (is_array || ne == 1 ); 190return ne ; 191 } 192 193template < typename T > 194const T & get_val (const size_t i = 0 )const { 195GGML_ASSERT (type_to_gguf_type < T > ::value == type ); 196if constexpr (std::is_same < T , std::string > ::value ) { 197GGML_ASSERT (data_string .size () >=i + 1 ); 198return data_string [i ]; 199 } 200const size_t type_size = gguf_type_size (type ); 201GGML_ASSERT (data .size () %type_size == 0 ); 202GGML_ASSERT (data .size () >= (i + 1 )* type_size ); 203return reinterpret_cast < const T *> (data .data ())[i ]; 204 } 205 206void cast (const enum gguf_type new_type ) { 207const size_t new_type_size = gguf_type_size (new_type ); 208GGML_ASSERT (data .size () %new_type_size == 0 ); 209type = new_type ; 210 } 211}; 212 213struct gguf_tensor_info { 214struct ggml_tensor t ;// for holding the equivalent info 215uint64_t offset ;// offset from start of `data`, must be a multiple of `ALIGNMENT` 216}; 217 218struct gguf_context { 219uint32_t version = GGUF_VERSION ; 220 221 std::vector < struct gguf_kv > kv ; 222 std::vector < struct gguf_tensor_info > info ; 223 224size_t alignment = GGUF_DEFAULT_ALIGNMENT ; 225size_t offset = 0 ;// offset of `data` from beginning of file 226size_t size = 0 ;// size of `data` in bytes 227 228void * data = nullptr ; 229}; 230 231struct gguf_reader { 232gguf_reader ( 233gguf_reader_callback_t callback , 234void * userdata , 235size_t max_chunk_read , 236uint64_t data_offset = 0 , 237uint64_t nbytes_remain = 0 ) 238 :callback (callback ), 239userdata (userdata ), 240max_chunk_read (max_chunk_read ), 241data_offset (data_offset ), 242nbytes_remain (nbytes_remain ) { 243GGML_ASSERT (max_chunk_read > 0 ); 244 } 245 246// helper for remaining bytes in a file 247static uint64_t file_remain (FILE * file ) { 248const int64_t cur = gguf_ftell (file ); 249if (cur < 0 ) { 250return 0 ; 251 } 252if (gguf_fseek (file ,0 ,SEEK_END )!= 0 ) { 253gguf_fseek (file ,cur ,SEEK_SET ); 254 255return 0 ; 256 } 257const int64_t end = gguf_ftell (file ); 258if (end < 0 ) { 259gguf_fseek (file ,cur ,SEEK_SET ); 260 261return 0 ; 262 } 263gguf_fseek (file ,cur ,SEEK_SET ); 264return static_cast < uint64_t > (end - cur ); 265 } 266 267template < typename T > 268bool read (T & dst )const { 269const size_t size = sizeof (dst ); 270if (size > nbytes_remain ) { 271return false; 272 } 273return read_raw (& dst ,size )== size ; 274 } 275 276template < typename T > 277bool read (std::vector < T > & dst ,const size_t n )const { 278if (n > GGUF_MAX_ARRAY_ELEMENTS ) { 279return false; 280 } 281if constexpr (std::is_same < T , std::string > ::value ) { 282// strings are prefixed with their length, so we need to account for that 283if (n > SIZE_MAX /sizeof (uint64_t )) { 284return false; 285 } 286if (nbytes_remain < n * sizeof (uint64_t )) { 287return false; 288 } 289 }else { 290if (n > SIZE_MAX /sizeof (T )) { 291return false; 292 } 293if (nbytes_remain < n * sizeof (T )) { 294return false; 295 } 296 } 297dst .resize (n ); 298for (size_t i = 0 ;i < dst .size ();++ i ) { 299if constexpr (std::is_same < T ,bool > ::value ) { 300bool tmp ; 301if (!read (tmp )) { 302return false; 303 } 304dst [i ]= tmp ; 305 }else { 306if (!read (dst [i ])) { 307return false; 308 } 309 } 310 } 311return true; 312 } 313 314bool read (bool & dst )const { 315int8_t tmp = -1 ; 316if (!read (tmp )) { 317return false; 318 } 319dst = tmp != 0 ; 320return true; 321 } 322 323bool read (enum ggml_type & dst )const { 324int32_t tmp = -1 ; 325if (!read (tmp )) { 326return false; 327 } 328dst = ggml_type (tmp ); 329return true; 330 } 331 332bool read (enum gguf_type & dst )const { 333int32_t tmp = -1 ; 334if (!read (tmp )) { 335return false; 336 } 337dst = gguf_type (tmp ); 338return true; 339 } 340 341bool read (std::string & dst )const { 342uint64_t size = 0 ; 343if (!read (size )) { 344return false; 345 } 346if (size > GGUF_MAX_STRING_LENGTH ) { 347GGML_LOG_ERROR ("%s: string length %" PRIu64 " exceeds maximum %" PRIu64 "\n" ,__func__ ,size , (uint64_t )GGUF_MAX_STRING_LENGTH ); 348return false; 349 } 350if (size > nbytes_remain ) { 351GGML_LOG_ERROR ("%s: string length %" PRIu64 " exceeds remaining file size %" PRIu64 " bytes\n" ,__func__ ,size ,nbytes_remain ); 352return false; 353 } 354dst .resize (static_cast < size_t > (size )); 355return read_raw (dst .data (),static_cast < size_t > (size ))== size ; 356 } 357 358bool read (void * dst ,const size_t size )const { 359if (size > nbytes_remain ) { 360return false; 361 } 362return read_raw (dst ,size )== size ; 363 } 364 365uint64_t tell ()const { 366return data_offset ; 367 } 368 369bool seek (uint64_t absolute_offset )const { 370const uint64_t end_offset = uint64_t (data_offset )+ nbytes_remain ; 371if (absolute_offset > end_offset ) { 372return false; 373 } 374 375data_offset = absolute_offset ; 376nbytes_remain = end_offset - absolute_offset ; 377 378return true; 379 } 380 381private : 382size_t read_raw (void * dst ,size_t size )const { 383if (callback == nullptr || size == 0 ) { 384return 0 ; 385 } 386 387uint8_t * data = static_cast < uint8_t *> (dst ); 388size_t total_nread = 0 ; 389bool reached_eof = false; 390 391while (total_nread < size ) { 392const size_t chunk_size = std::min (max_chunk_read ,size - total_nread ); 393if (data_offset + total_nread < data_offset ) { 394break ; 395 } 396const size_t nread = callback (userdata ,static_cast < void *> (data + total_nread ),data_offset + total_nread ,chunk_size ); 397total_nread += nread ; 398if (nread != chunk_size ) { 399reached_eof = true; 400break ; 401 } 402 } 403 404data_offset += total_nread ; 405GGML_ASSERT (total_nread <=nbytes_remain ); 406nbytes_remain -= total_nread ; 407 408if (reached_eof ) { 409nbytes_remain = 0 ; 410 } 411 412return total_nread ; 413 } 414 415gguf_reader_callback_t callback = nullptr ; 416void * userdata = nullptr ; 417size_t max_chunk_read = 0 ; 418mutable uint64_t data_offset = 0 ; 419mutable uint64_t nbytes_remain = 0 ; 420}; 421 422struct gguf_context * gguf_init_empty (void ) { 423return new gguf_context ; 424} 425 426template < typename T > 427bool gguf_read_emplace_helper (const struct gguf_reader & gr , std::vector < struct gguf_kv > & kv ,const std::string & key ,const bool is_array ,const size_t n ) { 428if (is_array ) { 429 std::vector < T > value ; 430try { 431if (!gr .read (value ,n )) { 432return false; 433 } 434 }catch (std::length_error & ) { 435GGML_LOG_ERROR ("%s: encountered length_error while reading value for key '%s'\n" ,__func__ ,key .c_str ()); 436return false; 437 }catch (std::bad_alloc & ) { 438GGML_LOG_ERROR ("%s: encountered bad_alloc error while reading value for key '%s'\n" ,__func__ ,key .c_str ()); 439return false; 440 } 441kv .emplace_back (key ,value ); 442 }else { 443T value ; 444if (!gr .read (value )) { 445return false; 446 } 447kv .emplace_back (key ,value ); 448 } 449return true; 450} 451 452static struct gguf_context * gguf_init_from_reader (const struct gguf_reader & gr ,struct gguf_init_params params ) { 453struct gguf_context * ctx = new gguf_context ; 454 455bool ok = true; 456 457// file magic 458 { 459 std::vector < char > magic ; 460ok = ok && gr .read (magic ,4 ); 461 462if (!ok ) { 463GGML_LOG_ERROR ("%s: failed to read magic\n" ,__func__ ); 464gguf_free (ctx ); 465return nullptr ; 466 } 467 468for (uint32_t i = 0 ;i < magic .size ();i ++ ) { 469if (magic [i ]!= GGUF_MAGIC [i ]) { 470char c0 = isprint (magic [0 ]) ?magic [0 ] :'?' ; 471char c1 = isprint (magic [1 ]) ?magic [1 ] :'?' ; 472char c2 = isprint (magic [2 ]) ?magic [2 ] :'?' ; 473char c3 = isprint (magic [3 ]) ?magic [3 ] :'?' ; 474GGML_LOG_ERROR ("%s: invalid magic characters: '%c%c%c%c', expected 'GGUF'\n" ,__func__ ,c0 ,c1 ,c2 ,c3 ); 475gguf_free (ctx ); 476return nullptr ; 477 } 478 } 479 } 480 481// header 482int64_t n_kv = 0 ; 483int64_t n_tensors = 0 ; 484 485if (ok && gr .read (ctx -> version )) { 486if (ok && ctx -> version == 0 ) { 487GGML_LOG_ERROR ("%s: bad GGUF version: %" PRIu32 "\n" ,__func__ ,ctx -> version ); 488ok = false; 489 } 490 491/* 492* bit layout is different when reading non-native endian models. 493* assuming that the GGUF version is 3, the non-native endian model 494* would read it as 0x30000000. we can use the AND operation against 495* the last 4 hexadecimal digits to check if the model is the same 496* endianness as the host system. 497*/ 498if (ok && (ctx -> version & 0x0000FFFF )== 0x00000000 ) { 499GGML_LOG_ERROR ("%s: failed to load model: this GGUF file version %" PRIu32 " is extremely large, is there a mismatch between the host and model endianness?\n" ,__func__ ,ctx -> version ); 500ok = false; 501 } 502 503if (ok && ctx -> version == 1 ) { 504GGML_LOG_ERROR ("%s: GGUFv1 is no longer supported, please use a more up-to-date version\n" ,__func__ ); 505ok = false; 506 } 507if (ok && ctx -> version > GGUF_VERSION ) { 508GGML_LOG_ERROR ("%s: this GGUF file is version %" PRIu32 " but this software only supports up to version %d\n" , 509__func__ ,ctx -> version ,GGUF_VERSION ); 510ok = false; 511 } 512 }else { 513ok = false; 514 } 515 516if (ok && gr .read (n_tensors )) { 517 static_assert(sizeof (size_t ) <=8 && sizeof (gguf_tensor_info ) >=2 ,"int64_t insufficient for indexing" ); 518if (n_tensors < 0 || n_tensors > int64_t (SIZE_MAX /sizeof (gguf_tensor_info ))) { 519GGML_LOG_ERROR ("%s: number of tensors is %" PRIi64 " but must be in [0, %zu]\n" , 520__func__ ,n_tensors ,SIZE_MAX /sizeof (gguf_tensor_info )); 521ok = false; 522 } 523 }else { 524ok = false; 525 } 526 527if (ok && gr .read (n_kv )) { 528 static_assert(sizeof (size_t ) <=8 && sizeof (gguf_tensor_info ) >=2 ,"int64_t insufficient for indexing" ); 529if (n_kv < 0 || n_kv > int64_t (SIZE_MAX /sizeof (gguf_kv ))) { 530GGML_LOG_ERROR ("%s: number of key value pairs is %" PRIi64 " but must be in [0, %zu]\n" , 531__func__ ,n_kv ,SIZE_MAX /sizeof (gguf_kv )); 532ok = false; 533 } 534 }else { 535ok = false; 536 } 537 538if (!ok ) { 539GGML_LOG_ERROR ("%s: failed to read header\n" ,__func__ ); 540gguf_free (ctx ); 541return nullptr ; 542 } 543 544// KV pairs 545 { 546for (int64_t i = 0 ;ok && i < n_kv ;++ i ) { 547 std::string key ; 548gguf_type type = gguf_type (-1 ); 549bool is_array = false; 550uint64_t n = 1 ; 551 552try { 553ok = ok && gr .read (key ); 554 }catch (std::length_error & ) { 555GGML_LOG_ERROR ("%s: encountered length_error while reading key %" PRIi64 "\n" ,__func__ ,i ); 556ok = false; 557 }catch (std::bad_alloc & ) { 558GGML_LOG_ERROR ("%s: encountered bad_alloc error while reading key %" PRIi64 "\n" ,__func__ ,i ); 559ok = false; 560 } 561if (ok && key .empty ()) { 562GGML_LOG_ERROR ("%s: key %" PRIi64 " is empty\n" ,__func__ ,i ); 563ok = false; 564 } 565for (size_t j = 0 ;ok && j < ctx -> kv .size ();++ j ) { 566if (key == ctx -> kv [j ].key ) { 567GGML_LOG_ERROR ("%s: duplicate key '%s' for tensors %zu and %" PRIi64 " \n" ,__func__ ,key .c_str (),j ,i ); 568ok = false; 569 } 570 } 571if (!ok ) { 572break ; 573 } 574 575ok = ok && gr .read (type ); 576if (type == GGUF_TYPE_ARRAY ) { 577is_array = true; 578ok = ok && gr .read (type ); 579ok = ok && gr .read (n ); 580 } 581if (!ok ) { 582break ; 583 } 584 585switch (type ) { 586case GGUF_TYPE_UINT8 :ok = ok && gguf_read_emplace_helper < uint8_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 587case GGUF_TYPE_INT8 :ok = ok && gguf_read_emplace_helper < int8_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 588case GGUF_TYPE_UINT16 :ok = ok && gguf_read_emplace_helper < uint16_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 589case GGUF_TYPE_INT16 :ok = ok && gguf_read_emplace_helper < int16_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 590case GGUF_TYPE_UINT32 :ok = ok && gguf_read_emplace_helper < uint32_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 591case GGUF_TYPE_INT32 :ok = ok && gguf_read_emplace_helper < int32_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 592case GGUF_TYPE_FLOAT32 :ok = ok && gguf_read_emplace_helper < float > (gr ,ctx -> kv ,key ,is_array ,n );break ; 593case GGUF_TYPE_BOOL :ok = ok && gguf_read_emplace_helper < bool > (gr ,ctx -> kv ,key ,is_array ,n );break ; 594case GGUF_TYPE_STRING :ok = ok && gguf_read_emplace_helper < std::string > (gr ,ctx -> kv ,key ,is_array ,n );break ; 595case GGUF_TYPE_UINT64 :ok = ok && gguf_read_emplace_helper < uint64_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 596case GGUF_TYPE_INT64 :ok = ok && gguf_read_emplace_helper < int64_t > (gr ,ctx -> kv ,key ,is_array ,n );break ; 597case GGUF_TYPE_FLOAT64 :ok = ok && gguf_read_emplace_helper < double > (gr ,ctx -> kv ,key ,is_array ,n );break ; 598case GGUF_TYPE_ARRAY : 599default : 600 { 601GGML_LOG_ERROR ("%s: key '%s' has invalid GGUF type %d\n" ,__func__ ,key .c_str (),type ); 602ok = false; 603 }break ; 604 } 605 } 606 607if (!ok ) { 608GGML_LOG_ERROR ("%s: failed to read key-value pairs\n" ,__func__ ); 609gguf_free (ctx ); 610return nullptr ; 611 } 612GGML_ASSERT (int64_t (ctx -> kv .size ())== n_kv ); 613 614const int alignment_idx = gguf_find_key (ctx ,GGUF_KEY_GENERAL_ALIGNMENT ); 615if (alignment_idx != -1 && gguf_get_kv_type (ctx ,alignment_idx )!= GGUF_TYPE_UINT32 ) { 616GGML_LOG_ERROR ("%s: key '%s' must be of type %s but is %s\n" , 617__func__ ,GGUF_KEY_GENERAL_ALIGNMENT ,gguf_type_name (GGUF_TYPE_UINT32 ), 618gguf_type_name (gguf_get_kv_type (ctx ,alignment_idx ))); 619gguf_free (ctx ); 620return nullptr ; 621 } 622ctx -> alignment = alignment_idx == -1 ?GGUF_DEFAULT_ALIGNMENT :gguf_get_val_u32 (ctx ,alignment_idx ); 623 624if (ctx -> alignment == 0 || (ctx -> alignment & (ctx -> alignment - 1 ))!= 0 ) { 625GGML_LOG_ERROR ("%s: alignment %zu is not a power of 2\n" ,__func__ ,ctx -> alignment ); 626gguf_free (ctx ); 627return nullptr ; 628 } 629 } 630 631// read the tensor info 632for (int64_t i = 0 ;ok && i < n_tensors ;++ i ) { 633struct gguf_tensor_info info ; 634 635// tensor name 636 { 637 std::string name ; 638try { 639ok = ok && gr .read (name ); 640 }catch (std::length_error & ) { 641GGML_LOG_ERROR ("%s: encountered length_error while reading tensor name %" PRIi64 "\n" ,__func__ ,i ); 642ok = false; 643 }catch (std::bad_alloc & ) { 644GGML_LOG_ERROR ("%s: encountered bad_alloc error while reading tensor name %" PRIi64 "\n" ,__func__ ,i ); 645ok = false; 646 } 647if (name .length () >=GGML_MAX_NAME ) { 648GGML_LOG_ERROR ("%s: tensor name %" PRIi64 " is too long: %zu >= %d\n" ,__func__ ,i ,name .length (),GGML_MAX_NAME ); 649ok = false; 650break ; 651 } 652ggml_set_name (& info .t ,name .c_str ()); 653 654// make sure there are no duplicate tensor names 655for (int64_t j = 0 ;ok && j < i ;++ j ) { 656if (strcmp (info .t .name ,ctx -> info [j ].t .name )== 0 ) { 657GGML_LOG_ERROR ("%s: duplicate tensor name '%s' for tensors %" PRIi64 " and %" PRIi64 "\n" ,__func__ ,info .t .name ,j ,i ); 658ok = false; 659break ; 660 } 661 } 662 } 663if (!ok ) { 664break ; 665 } 666 667// tensor shape 668 { 669uint32_t n_dims = 0 ; 670ok = ok && gr .read (n_dims ); 671if (n_dims > GGML_MAX_DIMS ) { 672GGML_LOG_ERROR ("%s: tensor '%s' has invalid number of dimensions: %" PRIu32 " > %" PRIu32 "\n" , 673__func__ ,info .t .name ,n_dims ,GGML_MAX_DIMS ); 674ok = false; 675break ; 676 } 677for (uint32_t j = 0 ;ok && j < GGML_MAX_DIMS ;++ j ) { 678info .t .ne [j ]= 1 ; 679if (j < n_dims ) { 680ok = ok && gr .read (info .t .ne [j ]); 681 } 682 683// check that all ne are non-negative 684if (info .t .ne [j ]< 0 ) { 685GGML_LOG_ERROR ("%s: tensor '%s' dimension %" PRIu32 " has invalid number of elements: %" PRIi64 " < 0\n" , 686__func__ ,info .t .name ,j ,info .t .ne [j ]); 687ok = false; 688break ; 689 } 690 } 691 692// check that the total number of elements is representable 693// (a zero-element tensor is trivially representable; the guard also avoids a division by zero below) 694if (ok && ggml_nelements (& info .t )> 0 && 695 ((INT64_MAX /info .t .ne [1 ] <=info .t .ne [0 ])|| 696 (INT64_MAX /info .t .ne [2 ] <=info .t .ne [0 ]* info .t .ne [1 ])|| 697 (INT64_MAX /info .t .ne [3 ] <=info .t .ne [0 ]* info .t .ne [1 ]* info .t .ne [2 ]))) { 698 699GGML_LOG_ERROR ("%s: total number of elements in tensor '%s' with shape " 700"(%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") is >= %" PRIi64 "\n" , 701__func__ ,info .t .name ,info .t .ne [0 ],info .t .ne [1 ],info .t .ne [2 ],info .t .ne [3 ],INT64_MAX ); 702ok = false; 703break ; 704 } 705 } 706if (!ok ) { 707break ; 708 } 709 710// tensor type 711 { 712ok = ok && gr .read (info .t .type ); 713 714// check that tensor type is within defined range 715if (info .t .type < 0 || info .t .type >=GGML_TYPE_COUNT ) { 716GGML_LOG_ERROR ("%s: tensor '%s' has invalid ggml type %d. should be in [0, %d)\n" , 717__func__ ,info .t .name ,info .t .type ,GGML_TYPE_COUNT ); 718ok = false; 719break ; 720 } 721const size_t type_size = ggml_type_size (info .t .type ); 722const int64_t blck_size = ggml_blck_size (info .t .type ); 723 724// check that row size is divisible by block size 725if (blck_size == 0 || info .t .ne [0 ] %blck_size != 0 ) { 726GGML_LOG_ERROR ("%s: tensor '%s' of type %d (%s) has %" PRId64 " elements per row, " 727"not a multiple of block size (%" PRId64 ")\n" , 728__func__ ,info .t .name , (int )info .t .type ,ggml_type_name (info .t .type ),info .t .ne [0 ],blck_size ); 729ok = false; 730break ; 731 } 732 733// check that the size of the tensor in bytes is representable 734if (ok && uint64_t (ggml_nelements (& info .t )/ggml_blck_size (info .t .type ))> SIZE_MAX /ggml_type_size (info .t .type )) { 735GGML_LOG_ERROR ("%s: tensor '%s' with shape (%" PRIi64 ", %" PRIi64 ", %" PRIi64 ", %" PRIi64 ") has a size in bytes > %zu\n" , 736__func__ ,info .t .name ,info .t .ne [0 ],info .t .ne [1 ],info .t .ne [2 ],info .t .ne [3 ],SIZE_MAX ); 737ok = false; 738break ; 739 } 740 741// calculate byte offsets given the tensor shape and type 742info .t .nb [0 ]= type_size ; 743info .t .nb [1 ]= info .t .nb [0 ]* (info .t .ne [0 ]/blck_size ); 744for (int j = 2 ;j < GGML_MAX_DIMS ;++ j ) { 745info .t .nb [j ]= info .t .nb [j - 1 ]* info .t .ne [j - 1 ]; 746 } 747 } 748if (!ok ) { 749break ; 750 } 751 752// tensor data offset within buffer 753ok = ok && gr .read (info .offset ); 754 755ctx -> info .push_back (info ); 756 } 757 758if (!ok ) { 759GGML_LOG_ERROR ("%s: failed to read tensor info\n" ,__func__ ); 760gguf_free (ctx ); 761return nullptr ; 762 } 763GGML_ASSERT (int64_t (ctx -> info .size ())== n_tensors ); 764 765// we require the data section to be aligned, so take into account any padding 766if (n_tensors > 0 && !gr .seek (GGML_PAD (gr .tell (),ctx -> alignment ))) { 767GGML_LOG_ERROR ("%s: failed to seek to beginning of data section\n" ,__func__ ); 768gguf_free (ctx ); 769return nullptr ; 770 } 771 772// store the current file offset - this is where the data section starts 773ctx -> offset = gr .tell (); 774 775// compute the total size of the data section, taking into account the alignment 776 { 777ctx -> size = 0 ; 778for (size_t i = 0 ;i < ctx -> info .size ();++ i ) { 779const gguf_tensor_info & ti = ctx -> info [i ]; 780if (ti .offset != ctx -> size ) { 781GGML_LOG_ERROR ("%s: tensor '%s' has offset %" PRIu64 ", expected %zu\n" , 782__func__ ,ti .t .name ,ti .offset ,ctx -> size ); 783GGML_LOG_ERROR ("%s: failed to read tensor data\n" ,__func__ ); 784gguf_free (ctx ); 785return nullptr ; 786 } 787size_t padded_size = GGML_PAD (ggml_nbytes (& ti .t ),ctx -> alignment ); 788if (SIZE_MAX - ctx -> size < padded_size ) { 789GGML_LOG_ERROR ("%s: tensor '%s' size overflow, cannot accumulate size %zu + %zu\n" , 790__func__ ,ti .t .name ,ctx -> size ,padded_size ); 791gguf_free (ctx ); 792return nullptr ; 793 } 794ctx -> size += padded_size ; 795 } 796 } 797 798// load the tensor data only if requested 799if (params .ctx != nullptr ) { 800// if the provided gguf_context is no_alloc, then we create "empty" tensors and do not read the binary blob 801// otherwise, we load the binary blob into the created ggml_context as well, and point the "data" members of 802// the ggml_tensor structs to the appropriate locations in the binary blob 803 804// compute the exact size needed for the new ggml_context 805size_t mem_size = 0 ; 806if (params .no_alloc ) { 807if (n_tensors != 0 && SIZE_MAX /n_tensors < ggml_tensor_overhead ()) { 808GGML_LOG_ERROR ("%s: memory size overflow while allocating ggml context\n" ,__func__ ); 809gguf_free (ctx ); 810return nullptr ; 811 } 812 813const size_t overhead = n_tensors * ggml_tensor_overhead (); 814 815mem_size = overhead ; 816 }else { 817if ((n_tensors + 1 )!= 0 && SIZE_MAX / (n_tensors + 1 )< ggml_tensor_overhead ()) { 818GGML_LOG_ERROR ("%s: memory size overflow while allocating ggml context\n" ,__func__ ); 819gguf_free (ctx ); 820return nullptr ; 821 } 822 823const size_t overhead = (n_tensors + 1 )* ggml_tensor_overhead (); 824 825if (SIZE_MAX - overhead < ctx -> size ) { 826GGML_LOG_ERROR ("%s: memory size overflow while allocating ggml context\n" ,__func__ ); 827gguf_free (ctx ); 828return nullptr ; 829 } 830 831mem_size = overhead + ctx -> size ; 832 } 833 834struct ggml_init_params pdata = { 835/*mem_size =*/ mem_size , 836/*mem_buffer =*/ nullptr , 837/*no_alloc =*/ params .no_alloc , 838 }; 839 840* params .ctx = ggml_init (pdata ); 841if (* params .ctx == nullptr ) { 842GGML_LOG_ERROR ("%s: failed to initialize ggml context for storing tensors\n" ,__func__ ); 843gguf_free (ctx ); 844return nullptr ; 845 } 846 847struct ggml_context * ctx_data = * params .ctx ; 848 849struct ggml_tensor * data = nullptr ; 850 851if (!params .no_alloc ) { 852data = ggml_new_tensor_1d (ctx_data ,GGML_TYPE_I8 ,ctx -> size ); 853 854ok = ok && data != nullptr ; 855 856if (ok ) { 857ggml_set_name (data ,"GGUF tensor data binary blob" ); 858 } 859 860// read the binary blob with the tensor data 861ok = ok && gr .read (data -> data ,ctx -> size ); 862 863if (!ok ) { 864GGML_LOG_ERROR ("%s: failed to read tensor data binary blob\n" ,__func__ ); 865ggml_free (ctx_data ); 866* params .ctx = nullptr ; 867gguf_free (ctx ); 868return nullptr ; 869 } 870 871ctx -> data = data -> data ; 872 } 873 874ggml_set_no_alloc (ctx_data , true); 875 876// create the tensors 877for (size_t i = 0 ;i < ctx -> info .size ();++ i ) { 878const struct gguf_tensor_info & info = ctx -> info [i ]; 879 880struct ggml_tensor * cur = ggml_new_tensor (ctx_data ,info .t .type ,GGML_MAX_DIMS ,info .t .ne ); 881 882ok = ok && cur != nullptr ; 883 884if (!ok ) { 885break ; 886 } 887 888ggml_set_name (cur ,info .t .name ); 889 890// point the data member to the appropriate location in the binary blob using the tensor info 891if (!params .no_alloc ) { 892cur -> data = (char * )data -> data + info .offset ; 893 } 894 } 895 896if (!ok ) { 897GGML_LOG_ERROR ("%s: failed to create tensors\n" ,__func__ ); 898ggml_free (ctx_data ); 899* params .ctx = nullptr ; 900gguf_free (ctx ); 901return nullptr ; 902 } 903 904ggml_set_no_alloc (ctx_data ,params .no_alloc ); 905 } 906 907return ctx ; 908} 909 910struct gguf_context * gguf_init_from_callback (gguf_reader_callback_t callback ,void * userdata ,size_t max_chunk_read ,uint64_t max_expected_size ,struct gguf_init_params params ) { 911if (callback == nullptr ) { 912return nullptr ; 913 } 914 915const struct gguf_reader gr (callback ,userdata ,max_chunk_read == 0 ?SIZE_MAX :max_chunk_read ,0 ,max_expected_size ); 916return gguf_init_from_reader (gr ,params ); 917} 918 919struct gguf_file_reader { 920FILE * file ; 921uint64_t offset ; 922}; 923 924static size_t gguf_file_reader_callback (void * userdata ,void * output ,uint64_t offset ,size_t len ) { 925GGML_ASSERT (len > 0 ); 926 927gguf_file_reader & reader = * static_cast < gguf_file_reader *> (userdata ); 928 929if (reader .offset != offset ) { 930if (offset > INT64_MAX || gguf_fseek (reader .file ,static_cast < int64_t > (offset ),SEEK_SET )!= 0 ) { 931return 0 ; 932 } 933 934reader .offset = offset ; 935 } 936 937const size_t nread = fread (static_cast < uint8_t *> (output ),1 ,len ,reader .file ); 938reader .offset += nread ; 939return nread ; 940} 941 942struct gguf_context * gguf_init_from_file_ptr (FILE * file ,struct gguf_init_params params ) { 943if (!file ) { 944return nullptr ; 945 } 946 947const int64_t cur = gguf_ftell (file ); 948if (cur < 0 ) { 949return nullptr ; 950 } 951 952gguf_file_reader reader = { 953/*.file = */ file , 954/*.offset = */ static_cast < uint64_t > (cur ), 955 }; 956const struct gguf_reader gr (gguf_file_reader_callback ,& reader ,SIZE_MAX ,reader .offset , gguf_reader::file_remain (file )); 957return gguf_init_from_reader (gr ,params ); 958} 959 960struct gguf_buffer_reader { 961const uint8_t * data ; 962size_t size ; 963}; 964 965static size_t gguf_buffer_reader_callback (void * userdata ,void * output ,uint64_t offset ,size_t len ) { 966GGML_ASSERT (len > 0 ); 967 968const gguf_buffer_reader & reader = * static_cast < gguf_buffer_reader *> (userdata ); 969 970if (offset > reader .size || len > reader .size - offset ) { 971return 0 ; 972 } 973 974const size_t data_offset = static_cast < size_t > (offset ); 975const size_t nread = std::min (len ,reader .size - data_offset ); 976memcpy (static_cast < uint8_t *> (output ),reader .data + data_offset ,nread ); 977return nread ; 978} 979 980struct gguf_context * gguf_init_from_buffer (const void * data ,size_t size ,struct gguf_init_params params ) { 981if (data == nullptr || size == 0 ) { 982return nullptr ; 983 } 984 985gguf_buffer_reader reader = { 986/*.data = */ static_cast < const uint8_t *> (data ), 987/*.size = */ size , 988 }; 989const struct gguf_reader gr (gguf_buffer_reader_callback ,& reader ,SIZE_MAX ,0 ,size ); 990return gguf_init_from_reader (gr ,params ); 991} 992 993struct gguf_context * gguf_init_from_file (const char * fname ,struct gguf_init_params params ) { 994FILE * file = ggml_fopen (fname ,"rb" ); 995 996if (!file ) { 997GGML_LOG_ERROR ("%s: failed to open GGUF file '%s' (%s)\n" ,__func__ ,fname ,strerror (errno )); 998return nullptr ; 999 } 1000 1001struct gguf_context * result = gguf_init_from_file_ptr (file ,params ); 1002fclose (file ); 1003return result ; 1004} 1005 1006void gguf_free (struct gguf_context * ctx ) { 1007if (ctx == nullptr ) { 1008return ; 1009 } 1010delete ctx ; 1011} 1012 1013const char * gguf_type_name (enum gguf_type type ) { 1014auto it = GGUF_TYPE_NAME .find (type ); 1015return it == GGUF_TYPE_NAME .end () ?nullptr :it -> second ; 1016} 1017 1018uint32_t gguf_get_version (const struct gguf_context * ctx ) { 1019return ctx -> version ; 1020} 1021 1022size_t gguf_get_alignment (const struct gguf_context * ctx ) { 1023return ctx -> alignment ; 1024} 1025 1026size_t gguf_get_data_offset (const struct gguf_context * ctx ) { 1027return ctx -> offset ; 1028} 1029 1030int64_t gguf_get_n_kv (const struct gguf_context * ctx ) { 1031return ctx -> kv .size (); 1032} 1033 1034int64_t gguf_find_key (const struct gguf_context * ctx ,const char * key ) { 1035// return -1 if key not found 1036int64_t keyfound = -1 ; 1037 1038const int64_t n_kv = gguf_get_n_kv (ctx ); 1039 1040for (int64_t i = 0 ;i < n_kv ;++ i ) { 1041if (strcmp (key ,gguf_get_key (ctx ,i ))== 0 ) { 1042keyfound = i ; 1043break ; 1044 } 1045 } 1046 1047return keyfound ; 1048} 1049 1050const char * gguf_get_key (const struct gguf_context * ctx ,int64_t key_id ) { 1051GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1052return ctx -> kv [key_id ].get_key ().c_str (); 1053} 1054 1055enum gguf_type gguf_get_kv_type (const struct gguf_context * ctx ,int64_t key_id ) { 1056GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1057return ctx -> kv [key_id ].is_array ?GGUF_TYPE_ARRAY :ctx -> kv [key_id ].get_type (); 1058} 1059 1060enum gguf_type gguf_get_arr_type (const struct gguf_context * ctx ,int64_t key_id ) { 1061GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1062GGML_ASSERT (ctx -> kv [key_id ].is_array ); 1063return ctx -> kv [key_id ].get_type (); 1064} 1065 1066const void * gguf_get_arr_data (const struct gguf_context * ctx ,int64_t key_id ) { 1067GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1068GGML_ASSERT (ctx -> kv [key_id ].get_type ()!= GGUF_TYPE_STRING ); 1069return ctx -> kv [key_id ].data .data (); 1070} 1071 1072const char * gguf_get_arr_str (const struct gguf_context * ctx ,int64_t key_id ,size_t i ) { 1073GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1074GGML_ASSERT (ctx -> kv [key_id ].get_type ()== GGUF_TYPE_STRING ); 1075return ctx -> kv [key_id ].data_string [i ].c_str (); 1076} 1077 1078size_t gguf_get_arr_n (const struct gguf_context * ctx ,int64_t key_id ) { 1079GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1080 1081if (ctx -> kv [key_id ].type == GGUF_TYPE_STRING ) { 1082return ctx -> kv [key_id ].data_string .size (); 1083 } 1084 1085const size_t type_size = gguf_type_size (ctx -> kv [key_id ].type ); 1086GGML_ASSERT (ctx -> kv [key_id ].data .size () %type_size == 0 ); 1087return ctx -> kv [key_id ].data .size () /type_size ; 1088} 1089 1090uint8_t gguf_get_val_u8 (const struct gguf_context * ctx ,int64_t key_id ) { 1091GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1092GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1093return ctx -> kv [key_id ].get_val < uint8_t > (); 1094} 1095 1096int8_t gguf_get_val_i8 (const struct gguf_context * ctx ,int64_t key_id ) { 1097GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1098GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1099return ctx -> kv [key_id ].get_val < int8_t > (); 1100} 1101 1102uint16_t gguf_get_val_u16 (const struct gguf_context * ctx ,int64_t key_id ) { 1103GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1104GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1105return ctx -> kv [key_id ].get_val < uint16_t > (); 1106} 1107 1108int16_t gguf_get_val_i16 (const struct gguf_context * ctx ,int64_t key_id ) { 1109GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1110GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1111return ctx -> kv [key_id ].get_val < int16_t > (); 1112} 1113 1114uint32_t gguf_get_val_u32 (const struct gguf_context * ctx ,int64_t key_id ) { 1115GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1116GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1117return ctx -> kv [key_id ].get_val < uint32_t > (); 1118} 1119 1120int32_t gguf_get_val_i32 (const struct gguf_context * ctx ,int64_t key_id ) { 1121GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1122GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1123return ctx -> kv [key_id ].get_val < int32_t > (); 1124} 1125 1126float gguf_get_val_f32 (const struct gguf_context * ctx ,int64_t key_id ) { 1127GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1128GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1129return ctx -> kv [key_id ].get_val < float > (); 1130} 1131 1132uint64_t gguf_get_val_u64 (const struct gguf_context * ctx ,int64_t key_id ) { 1133GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1134GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1135return ctx -> kv [key_id ].get_val < uint64_t > (); 1136} 1137 1138int64_t gguf_get_val_i64 (const struct gguf_context * ctx ,int64_t key_id ) { 1139GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1140GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1141return ctx -> kv [key_id ].get_val < int64_t > (); 1142} 1143 1144double gguf_get_val_f64 (const struct gguf_context * ctx ,int64_t key_id ) { 1145GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1146GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1147return ctx -> kv [key_id ].get_val < double > (); 1148} 1149 1150bool gguf_get_val_bool (const struct gguf_context * ctx ,int64_t key_id ) { 1151GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1152GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1153return ctx -> kv [key_id ].get_val < bool > (); 1154} 1155 1156const char * gguf_get_val_str (const struct gguf_context * ctx ,int64_t key_id ) { 1157GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1158GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1159return ctx -> kv [key_id ].get_val < std::string > ().c_str (); 1160} 1161 1162const void * gguf_get_val_data (const struct gguf_context * ctx ,int64_t key_id ) { 1163GGML_ASSERT (key_id >=0 && key_id < gguf_get_n_kv (ctx )); 1164GGML_ASSERT (ctx -> kv [key_id ].get_ne ()== 1 ); 1165GGML_ASSERT (ctx -> kv [key_id ].get_type ()!= GGUF_TYPE_STRING ); 1166return ctx -> kv [key_id ].data .data (); 1167} 1168 1169int64_t gguf_get_n_tensors (const struct gguf_context * ctx ) { 1170return ctx -> info .size (); 1171} 1172 1173int64_t gguf_find_tensor (const struct gguf_context * ctx ,const char * name ) { 1174// return -1 if tensor not found 1175int64_t tensor_id = -1 ; 1176 1177const int64_t n_tensors = gguf_get_n_tensors (ctx ); 1178 1179for (int64_t i = 0 ;i < n_tensors ;++ i ) { 1180if (strcmp (name ,gguf_get_tensor_name (ctx ,i ))== 0 ) { 1181tensor_id = i ; 1182break ; 1183 } 1184 } 1185 1186return tensor_id ; 1187} 1188 1189size_t gguf_get_tensor_offset (const struct gguf_context * ctx ,int64_t tensor_id ) { 1190GGML_ASSERT (tensor_id >=0 && tensor_id < gguf_get_n_tensors (ctx )); 1191return ctx -> info [tensor_id ].offset ; 1192} 1193 1194const char * gguf_get_tensor_name (const struct gguf_context * ctx ,int64_t tensor_id ) { 1195GGML_ASSERT (tensor_id >=0 && tensor_id < gguf_get_n_tensors (ctx )); 1196return ctx -> info [tensor_id ].t .name ; 1197} 1198 1199const int64_t * gguf_get_tensor_ne (const struct gguf_context * ctx ,int64_t tensor_id ) { 1200GGML_ASSERT (tensor_id >=0 && tensor_id < gguf_get_n_tensors (ctx )); 1201return ctx -> info [tensor_id ].t .ne ; 1202} 1203 1204enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx ,int64_t tensor_id ) { 1205GGML_ASSERT (tensor_id >=0 && tensor_id < gguf_get_n_tensors (ctx )); 1206return ctx -> info [tensor_id ].t .type ; 1207} 1208 1209size_t gguf_get_tensor_size (const struct gguf_context * ctx ,int64_t tensor_id ) { 1210GGML_ASSERT (tensor_id >=0 && tensor_id < gguf_get_n_tensors (ctx )); 1211return ggml_nbytes (& ctx -> info [tensor_id ].t ); 1212} 1213 1214int64_t gguf_remove_key (struct gguf_context * ctx ,const char * key ) { 1215const int64_t key_id = gguf_find_key (ctx ,key ); 1216if (key_id >=0 ) { 1217ctx -> kv .erase (ctx -> kv .begin ()+ key_id ); 1218 } 1219return key_id ; 1220} 1221 1222template < typename T > 1223static void gguf_check_reserved_keys (const std::string & key ,const T val ) { 1224if (key == GGUF_KEY_GENERAL_ALIGNMENT ) { 1225if constexpr (std::is_same < T ,uint32_t > ::value ) { 1226GGML_ASSERT (val > 0 && (val & (val - 1 ))== 0 && GGUF_KEY_GENERAL_ALIGNMENT " must be power of 2" ); 1227 }else { 1228GGML_UNUSED (val ); 1229GGML_ABORT (GGUF_KEY_GENERAL_ALIGNMENT " must be type u32" ); 1230 } 1231 } 1232} 1233 1234void gguf_set_val_u8 (struct gguf_context * ctx ,const char * key ,uint8_t val ) { 1235gguf_check_reserved_keys (key ,val ); 1236gguf_remove_key (ctx ,key ); 1237ctx -> kv .emplace_back (key ,val ); 1238} 1239 1240void gguf_set_val_i8 (struct gguf_context * ctx ,const char * key ,int8_t val ) { 1241gguf_check_reserved_keys (key ,val ); 1242gguf_remove_key (ctx ,key ); 1243ctx -> kv .emplace_back (key ,val ); 1244} 1245 1246void gguf_set_val_u16 (struct gguf_context * ctx ,const char * key ,uint16_t val ) { 1247gguf_check_reserved_keys (key ,val ); 1248gguf_remove_key (ctx ,key ); 1249ctx -> kv .emplace_back (key ,val ); 1250} 1251 1252void gguf_set_val_i16 (struct gguf_context * ctx ,const char * key ,int16_t val ) { 1253gguf_check_reserved_keys (key ,val ); 1254gguf_remove_key (ctx ,key ); 1255ctx -> kv .emplace_back (key ,val ); 1256} 1257 1258void gguf_set_val_u32 (struct gguf_context * ctx ,const char * key ,uint32_t val ) { 1259gguf_check_reserved_keys (key ,val ); 1260gguf_remove_key (ctx ,key ); 1261ctx -> kv .emplace_back (key ,val ); 1262} 1263 1264void gguf_set_val_i32 (struct gguf_context * ctx ,const char * key ,int32_t val ) { 1265gguf_check_reserved_keys (key ,val ); 1266gguf_remove_key (ctx ,key ); 1267ctx -> kv .emplace_back (key ,val ); 1268} 1269 1270void gguf_set_val_f32 (struct gguf_context * ctx ,const char * key ,float val ) { 1271gguf_check_reserved_keys (key ,val ); 1272gguf_remove_key (ctx ,key ); 1273ctx -> kv .emplace_back (key ,val ); 1274} 1275 1276void gguf_set_val_u64 (struct gguf_context * ctx ,const char * key ,uint64_t val ) { 1277gguf_check_reserved_keys (key ,val ); 1278gguf_remove_key (ctx ,key ); 1279ctx -> kv .emplace_back (key ,val ); 1280} 1281 1282void gguf_set_val_i64 (struct gguf_context * ctx ,const char * key ,int64_t val ) { 1283gguf_check_reserved_keys (key ,val ); 1284gguf_remove_key (ctx ,key ); 1285ctx -> kv .emplace_back (key ,val ); 1286} 1287 1288void gguf_set_val_f64 (struct gguf_context * ctx ,const char * key ,double val ) { 1289gguf_check_reserved_keys (key ,val ); 1290gguf_remove_key (ctx ,key ); 1291ctx -> kv .emplace_back (key ,val ); 1292} 1293 1294void gguf_set_val_bool (struct gguf_context * ctx ,const char * key ,bool val ) { 1295gguf_check_reserved_keys (key ,val ); 1296gguf_remove_key (ctx ,key ); 1297ctx -> kv .emplace_back (key ,val ); 1298} 1299 1300void gguf_set_val_str (struct gguf_context * ctx ,const char * key ,const char * val ) { 1301gguf_check_reserved_keys (key ,val ); 1302gguf_remove_key (ctx ,key ); 1303ctx -> kv .emplace_back (key , std::string (val )); 1304} 1305 1306void gguf_set_arr_data (struct gguf_context * ctx ,const char * key ,enum gguf_type type ,const void * data ,size_t n ) { 1307gguf_check_reserved_keys (key ,data ); 1308gguf_remove_key (ctx ,key ); 1309 1310const size_t nbytes = n * gguf_type_size (type ); 1311 std::vector < int8_t > tmp (nbytes ); 1312if (!tmp .empty ()) { 1313memcpy (tmp .data (),data ,nbytes ); 1314 } 1315ctx -> kv .emplace_back (key ,tmp ); 1316ctx -> kv .back ().cast (type ); 1317} 1318 1319void gguf_set_arr_str (struct gguf_context * ctx ,const char * key ,const char ** data ,size_t n ) { 1320gguf_check_reserved_keys (key ,data ); 1321gguf_remove_key (ctx ,key ); 1322 1323 std::vector < std::string > tmp (n ); 1324for (size_t i = 0 ;i < n ;++ i ) { 1325tmp [i ]= data [i ]; 1326 } 1327ctx -> kv .emplace_back (key ,tmp ); 1328} 1329 1330// set or add KV pairs from another context 1331void gguf_set_kv (struct gguf_context * ctx ,const struct gguf_context * src ) { 1332const int64_t n_kv = gguf_get_n_kv (src ); 1333for (int64_t i = 0 ;i < n_kv ;++ i ) { 1334const struct gguf_kv & kv = src -> kv [i ]; 1335 1336if (!kv .is_array ) { 1337switch (kv .get_type ()) { 1338case GGUF_TYPE_UINT8 :gguf_set_val_u8 (ctx ,kv .get_key ().c_str (),kv .get_val < uint8_t > ());break ; 1339case GGUF_TYPE_INT8 :gguf_set_val_i8 (ctx ,kv .get_key ().c_str (),kv .get_val < int8_t > ());break ; 1340case GGUF_TYPE_UINT16 :gguf_set_val_u16 (ctx ,kv .get_key ().c_str (),kv .get_val < uint16_t > ());break ; 1341case GGUF_TYPE_INT16 :gguf_set_val_i16 (ctx ,kv .get_key ().c_str (),kv .get_val < int16_t > ());break ; 1342case GGUF_TYPE_UINT32 :gguf_set_val_u32 (ctx ,kv .get_key ().c_str (),kv .get_val < uint32_t > ());break ; 1343case GGUF_TYPE_INT32 :gguf_set_val_i32 (ctx ,kv .get_key ().c_str (),kv .get_val < int32_t > ());break ; 1344case GGUF_TYPE_FLOAT32 :gguf_set_val_f32 (ctx ,kv .get_key ().c_str (),kv .get_val < float > ());break ; 1345case GGUF_TYPE_UINT64 :gguf_set_val_u64 (ctx ,kv .get_key ().c_str (),kv .get_val < uint64_t > ());break ; 1346case GGUF_TYPE_INT64 :gguf_set_val_i64 (ctx ,kv .get_key ().c_str (),kv .get_val < int64_t > ());break ; 1347case GGUF_TYPE_FLOAT64 :gguf_set_val_f64 (ctx ,kv .get_key ().c_str (),kv .get_val < double > ());break ; 1348case GGUF_TYPE_BOOL :gguf_set_val_bool (ctx ,kv .get_key ().c_str (),kv .get_val < bool > ());break ; 1349case GGUF_TYPE_STRING :gguf_set_val_str (ctx ,kv .get_key ().c_str (),kv .get_val < std::string > ().c_str ());break ; 1350case GGUF_TYPE_ARRAY : 1351default :GGML_ABORT ("invalid type" ); 1352 } 1353continue ; 1354 } 1355 1356const size_t ne = kv .get_ne (); 1357 1358switch (kv .get_type ()) { 1359case GGUF_TYPE_UINT8 : 1360case GGUF_TYPE_INT8 : 1361case GGUF_TYPE_UINT16 : 1362case GGUF_TYPE_INT16 : 1363case GGUF_TYPE_UINT32 : 1364case GGUF_TYPE_INT32 : 1365case GGUF_TYPE_FLOAT32 : 1366case GGUF_TYPE_UINT64 : 1367case GGUF_TYPE_INT64 : 1368case GGUF_TYPE_FLOAT64 : 1369case GGUF_TYPE_BOOL : { 1370gguf_set_arr_data (ctx ,kv .get_key ().c_str (),kv .get_type (),kv .data .data (),ne ); 1371 }break ; 1372case GGUF_TYPE_STRING : { 1373 std::vector < const char *> tmp (ne ); 1374for (size_t j = 0 ;j < ne ;++ j ) { 1375tmp [j ]= kv .data_string [j ].c_str (); 1376 } 1377gguf_set_arr_str (ctx ,kv .get_key ().c_str (),tmp .data (),ne ); 1378 }break ; 1379case GGUF_TYPE_ARRAY : 1380default :GGML_ABORT ("invalid type" ); 1381 } 1382 } 1383} 1384 1385void gguf_add_tensor ( 1386struct gguf_context * ctx , 1387const struct ggml_tensor * tensor ) { 1388GGML_ASSERT (tensor ); 1389if (gguf_find_tensor (ctx ,tensor -> name )!= -1 ) { 1390GGML_ABORT ("duplicate tensor name: %s" ,tensor -> name ); 1391 } 1392 1393struct gguf_tensor_info ti ; 1394ti .t = * tensor ; 1395ti .offset = ctx -> info .empty () ?0 : 1396ctx -> info .back ().offset + GGML_PAD (ggml_nbytes (& ctx -> info .back ().t ),ctx -> alignment ); 1397ctx -> info .push_back (ti ); 1398} 1399 1400void gguf_set_tensor_type (struct gguf_context * ctx ,const char * name ,enum ggml_type type ) { 1401const int64_t tensor_id = gguf_find_tensor (ctx ,name ); 1402if (tensor_id < 0 ) { 1403GGML_ABORT ("tensor not found: %s" ,name ); 1404 } 1405struct ggml_tensor * tensor = & ctx -> info [tensor_id ].t ; 1406const size_t type_size = ggml_type_size (type ); 1407const int64_t blck_size = ggml_blck_size (type ); 1408 1409tensor -> type = type ; 1410GGML_ASSERT (tensor -> ne [0 ] %blck_size == 0 && "tensor row size not divisible by block size of new type" ); 1411 1412tensor -> nb [0 ]= type_size ; 1413tensor -> nb [1 ]= tensor -> nb [0 ]* (tensor -> ne [0 ]/blck_size ); 1414for (int i = 2 ;i < GGML_MAX_DIMS ;i ++ ) { 1415tensor -> nb [i ]= tensor -> nb [i - 1 ]* tensor -> ne [i - 1 ]; 1416 } 1417 1418// update offsets 1419const int64_t n_tensors = gguf_get_n_tensors (ctx ); 1420for (int64_t i = tensor_id + 1 ;i < n_tensors ;++ i ) { 1421ctx -> info [i ].offset = ctx -> info [i - 1 ].offset + GGML_PAD (ggml_nbytes (& ctx -> info [i - 1 ].t ),ctx -> alignment ); 1422 } 1423} 1424 1425void gguf_set_tensor_data (struct gguf_context * ctx ,const char * name ,const void * data ) { 1426const int64_t tensor_id = gguf_find_tensor (ctx ,name ); 1427if (tensor_id < 0 ) { 1428GGML_ABORT ("tensor not found: %s" ,name ); 1429 } 1430 1431ctx -> info [tensor_id ].t .data = (void * )(uintptr_t )data ;// double cast suppresses warning about casting away const 1432} 1433 1434struct gguf_writer_base { 1435size_t written_bytes {0u }; 1436 1437virtual ~gguf_writer_base (void )= default ; 1438 1439// we bet on devirtualization 1440virtual void write (int8_t val )= 0 ; 1441virtual void write (const std::vector < int8_t > & val )= 0 ; 1442virtual void write_tensor_data (const struct gguf_tensor_info & info ,size_t offset_data ,size_t alignment )= 0 ; 1443 1444template < typename T > 1445void write (const T & val ) { 1446for (size_t i = 0 ;i < sizeof (val );++ i ) { 1447write (reinterpret_cast < const int8_t *> (& val )[i ]); 1448 } 1449 } 1450 1451void write (const bool & val ) { 1452const int8_t val8 = val ?1 :0 ; 1453write (val8 ); 1454 } 1455 1456void write (const std::string & val ) { 1457 { 1458const uint64_t n = val .length (); 1459write (n ); 1460 } 1461for (size_t i = 0 ;i < val .length ();++ i ) { 1462write ((val .data ())[i ]); 1463 } 1464 } 1465 1466void write (const char * val ) { 1467write (std::string (val )); 1468 } 1469 1470void write (const enum ggml_type & val ) { 1471write (int32_t (val )); 1472 } 1473 1474void write (const enum gguf_type & val ) { 1475write (int32_t (val )); 1476 } 1477 1478void write (const struct gguf_kv & kv ) { 1479const uint64_t ne = kv .get_ne (); 1480 1481write (kv .get_key ()); 1482 1483if (kv .is_array ) { 1484write (GGUF_TYPE_ARRAY ); 1485write (kv .get_type ()); 1486write (ne ); 1487 }else { 1488write (kv .get_type ()); 1489 } 1490 1491switch (kv .get_type ()) { 1492case GGUF_TYPE_UINT8 : 1493case GGUF_TYPE_INT8 : 1494case GGUF_TYPE_UINT16 : 1495case GGUF_TYPE_INT16 : 1496case GGUF_TYPE_UINT32 : 1497case GGUF_TYPE_INT32 : 1498case GGUF_TYPE_FLOAT32 : 1499case GGUF_TYPE_UINT64 : 1500case GGUF_TYPE_INT64 : 1501case GGUF_TYPE_FLOAT64 : { 1502write (kv .data ); 1503 }break ; 1504case GGUF_TYPE_BOOL : { 1505for (size_t i = 0 ;i < ne ;++ i ) { 1506write (kv .get_val < bool > (i )); 1507 } 1508 }break ; 1509case GGUF_TYPE_STRING : { 1510for (size_t i = 0 ;i < ne ;++ i ) { 1511write (kv .get_val < std::string > (i )); 1512 } 1513 }break ; 1514case GGUF_TYPE_ARRAY : 1515default :GGML_ABORT ("invalid type" ); 1516 } 1517 } 1518 1519void write_tensor_meta (const struct gguf_tensor_info & info ) { 1520write (info .t .name ); 1521 1522const uint32_t n_dims = ggml_n_dims (& info .t ); 1523write (n_dims ); 1524 1525for (uint32_t j = 0 ;j < n_dims ;++ j ) { 1526write (info .t .ne [j ]); 1527 } 1528write (info .t .type ); 1529write (info .offset ); 1530 } 1531 1532void pad (const size_t alignment ) { 1533while (written_bytes %alignment != 0 ) { 1534const int8_t zero = 0 ; 1535write (zero ); 1536 } 1537 } 1538}; 1539 1540// vector buffer based writer 1541struct gguf_writer_buf final :public gguf_writer_base { 1542 std::vector < int8_t > & buf ; 1543 1544gguf_writer_buf (std::vector < int8_t > & buf ) :buf (buf ) {} 1545 1546using gguf_writer_base::write ; 1547 1548void write (const int8_t val )override { 1549buf .push_back (val ); 1550written_bytes ++ ; 1551 } 1552 1553void write (const std::vector < int8_t > & val )override { 1554buf .insert (buf .end (),val .begin (),val .end ()); 1555written_bytes += val .size (); 1556 } 1557 1558void write_tensor_data (const struct gguf_tensor_info & info ,const size_t offset_data ,const size_t alignment )override { 1559GGML_ASSERT (buf .size ()- offset_data == info .offset ); 1560 1561GGML_ASSERT (ggml_is_contiguous (& info .t )); 1562const size_t offset = buf .size (); 1563const size_t nbytes = ggml_nbytes (& info .t ); 1564 1565buf .resize (offset + nbytes ); 1566if (info .t .buffer ) { 1567ggml_backend_tensor_get (& info .t ,buf .data ()+ offset ,0 ,nbytes ); 1568 }else { 1569GGML_ASSERT (info .t .data ); 1570memcpy (buf .data ()+ offset ,info .t .data ,nbytes ); 1571 } 1572written_bytes += nbytes ; 1573 1574pad (alignment ); 1575 } 1576}; 1577 1578// file based writer 1579struct gguf_writer_file final :public gguf_writer_base { 1580FILE * file ; 1581 1582gguf_writer_file (FILE * file ) :file (file ) {} 1583 1584using gguf_writer_base::write ; 1585 1586void write (const int8_t val )override { 1587const auto real_val = static_cast < uint8_t > (val ); 1588const auto ret = fputc (real_val ,file ); 1589written_bytes ++ ; 1590if (ret != real_val ) { 1591throw std::runtime_error ("unexpected fputc result '" + std::to_string (ret )+ "' instead of '" + std::to_string ((int )real_val )+ "'" ); 1592 } 1593 } 1594 1595void write (const std::vector < int8_t > & val )override { 1596const auto ret = fwrite (val .data (),1 ,val .size (),file ); 1597written_bytes += val .size (); 1598if (ret != val .size ()) { 1599throw std::runtime_error ("unexpected fwrite number of bytes written, '" + std::to_string (ret )+ "' instead of '" + std::to_string (val .size ())+ "'" ); 1600 } 1601 } 1602 1603void write_tensor_data (const struct gguf_tensor_info & info ,const size_t offset_data ,const size_t alignment )override { 1604GGML_ASSERT (written_bytes - offset_data == info .offset ); 1605 1606GGML_ASSERT (ggml_is_contiguous (& info .t )); 1607const size_t nbytes = ggml_nbytes (& info .t ); 1608 1609 std::vector < int8_t > buf (nbytes ); 1610if (info .t .buffer ) { 1611ggml_backend_tensor_get (& info .t ,buf .data (),0 ,nbytes ); 1612 }else { 1613GGML_ASSERT (info .t .data ); 1614memcpy (buf .data (),info .t .data ,nbytes ); 1615 } 1616write (buf ); 1617 1618pad (alignment ); 1619 } 1620}; 1621 1622template < typename writer_t > 1623static void gguf_write_out (const struct gguf_context * ctx ,writer_t & gw ,bool only_meta ) { 1624const int64_t n_kv = gguf_get_n_kv (ctx ); 1625const int64_t n_tensors = gguf_get_n_tensors (ctx ); 1626 1627// write header 1628gw .write (GGUF_MAGIC [0 ]); 1629gw .write (GGUF_MAGIC [1 ]); 1630gw .write (GGUF_MAGIC [2 ]); 1631gw .write (GGUF_MAGIC [3 ]); 1632gw .write (ctx -> version ); 1633gw .write (n_tensors ); 1634gw .write (n_kv ); 1635 1636// write key-value pairs 1637for (int64_t i = 0 ;i < n_kv ;++ i ) { 1638gw .write (ctx -> kv [i ]); 1639 } 1640 1641// write tensor info 1642for (int64_t i = 0 ;i < n_tensors ;++ i ) { 1643gw .write_tensor_meta (ctx -> info [i ]); 1644 } 1645 1646// we require the data section to be aligned 1647gw .pad (ctx -> alignment ); 1648 1649if (only_meta ) { 1650return ; 1651 } 1652 1653const size_t offset_data = gw .written_bytes ; 1654 1655// write tensor data 1656for (int64_t i = 0 ;i < n_tensors ;++ i ) { 1657gw .write_tensor_data (ctx -> info [i ],offset_data ,ctx -> alignment ); 1658 } 1659} 1660 1661void gguf_write_to_buf (const struct gguf_context * ctx , std::vector < int8_t > & buf ,bool only_meta ) { 1662gguf_writer_buf gw (buf ); 1663gguf_write_out (ctx ,gw ,only_meta ); 1664} 1665 1666bool gguf_write_to_file_ptr (const struct gguf_context * ctx ,FILE * file ,bool only_meta ) { 1667GGML_ASSERT (file ); 1668 1669try { 1670gguf_writer_file gw (file ); 1671gguf_write_out (ctx ,gw ,only_meta ); 1672 }catch (const std::runtime_error & ex ) { 1673GGML_LOG_ERROR ("%s: failed to write GGUF data: %s\n" ,__func__ ,ex .what ()); 1674return false; 1675 } 1676return true; 1677} 1678 1679bool gguf_write_to_file (const struct gguf_context * ctx ,const char * fname ,bool only_meta ) { 1680FILE * file = ggml_fopen (fname ,"wb" ); 1681 1682if (!file ) { 1683GGML_LOG_ERROR ("%s: failed to open file '%s' for writing GGUF data\n" ,__func__ ,fname ); 1684return false; 1685 } 1686 1687const bool success = gguf_write_to_file_ptr (ctx ,file ,only_meta ); 1688if (!success ) { 1689GGML_LOG_ERROR ("%s: failed to write GGUF data into '%s'\n" ,__func__ ,fname ); 1690 } 1691 1692fclose (file ); 1693return success ; 1694} 1695 1696size_t gguf_get_meta_size (const struct gguf_context * ctx ) { 1697// only return size 1698 std::vector < int8_t > buf ; 1699gguf_write_to_buf (ctx ,buf ,/*only_meta =*/ true); 1700return buf .size (); 1701} 1702 1703void gguf_get_meta_data (const struct gguf_context * ctx ,void * data ) { 1704 std::vector < int8_t > buf ; 1705gguf_write_to_buf (ctx ,buf ,/*only_meta =*/ true); 1706memcpy (data ,buf .data (),buf .size ()); 1707}