okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1// tools/vision/own/gguf_lite.h - a small GGUF reader for Strata's own vision tower (no ggml, no llama.cpp). 2// 3// Reads the header (key/value pairs, tensor directory) of a GGUF v2/v3 file and the data of single tensors, converted to float32: 4// F32, F16, BF16 and Q8_0 are handled (what an mmproj file comes in); any other tensor type is an error that names it. 5// Shapes are kept as the file stores them (ne[0] is the fastest dimension: a Linear weight of "[in, out]" is `out` rows of `in` 6// contiguous values, y = W x). 7#pragma once 8 9#include <cstdint> 10#include <cstdio> 11#include <cstring> 12#include <map> 13#include <string> 14#include <vector> 15 16namespace strata_vit { 17 18enum GgufValueType :uint32_t { 19GV_UINT8 = 0 ,GV_INT8 = 1 ,GV_UINT16 = 2 ,GV_INT16 = 3 ,GV_UINT32 = 4 ,GV_INT32 = 5 ,GV_FLOAT32 = 6 ,GV_BOOL = 7 , 20GV_STRING = 8 ,GV_ARRAY = 9 ,GV_UINT64 = 10 ,GV_INT64 = 11 ,GV_FLOAT64 = 12 , 21}; 22 23// ggml's tensor type ids (the ones this reader knows) 24enum GgufTensorType :uint32_t {GT_F32 = 0 ,GT_F16 = 1 ,GT_Q8_0 = 8 ,GT_BF16 = 30 }; 25 26struct GgufTensorInfo { 27std ::string name ; 28std ::vector < int64_t > ne ;// as stored: ne[0] fastest 29uint32_t type = 0 ; 30uint64_t offset = 0 ;// from the start of the data section 31uint64_t nbytes = 0 ;// 0 when the type is not one this reader handles 32int64_t nelem ()const { 33int64_t n = 1 ; 34for (int64_t d :ne )n *=d ; 35return n ; 36 } 37}; 38 39class Gguf { 40public : 41Gguf ()= default ; 42Gguf (const Gguf & )= delete ; 43Gguf & operator = (const Gguf & )= delete ; 44 ~Gguf (); 45 46// Reads the header and the tensor directory (never the tensor data). False and `err` when the file is not a GGUF this reader handles. 47bool open (const std ::string & path ,std ::string & err ); 48 49const std ::string & path ()const {return path_ ; } 50uint64_t file_size ()const {return file_size_ ; } 51 52bool has (const std ::string & key )const {return kv_ .count (key )!= 0 ; } 53bool get_u32 (const std ::string & key ,uint32_t & v )const ;// any integer type holding a value in 0 .. 2^32-1 (bool counts) 54bool get_f32 (const std ::string & key ,float & v )const ;// a float or an integer 55bool get_bool (const std ::string & key ,bool & v )const ;// a bool or an integer 56bool get_str (const std ::string & key ,std ::string & v )const ; 57bool get_f32_array (const std ::string & key ,std ::vector < float >& v )const ;// an array of floats or integers 58bool get_int_array (const std ::string & key ,std ::vector < int64_t >& v )const ;// an array of integers or bools 59 60const GgufTensorInfo * tensor (const std ::string & name )const ; 61const std ::vector < GgufTensorInfo >& tensors ()const {return tensors_ ; } 62 63// the tensor's values as float32 (t.nelem() of them); false and `err` on a type this reader does not handle or a short file 64bool read_f32 (const GgufTensorInfo & t ,float * dst ,std ::string & err )const ; 65// the tensor's bytes as stored (t.nbytes of them) 66bool read_raw (const GgufTensorInfo & t ,void * dst ,std ::string & err )const ; 67 68private : 69struct Value { 70uint32_t type = 0 ,elem = 0 ;// elem: the array's element type 71int64_t i = 0 ; 72double d = 0 ; 73std ::string s ; 74std ::vector < int64_t > ai ; 75std ::vector < double > ad ; 76std ::vector < std ::string > as ; 77uint64_t n = 0 ;// array length 78 }; 79bool read_at ( uint64_t pos, void * dst, uint64_t n, std ::string & err) const; 80std :: string path_; 81uint64_t file_size_ = 0 , data_start_ = 0 ; 82std ::map < std::string, Value > kv_; 83std ::vector < GgufTensorInfo > tensors_; 84std ::map < std::string, size_t > by_name_; 85}; 86 87// half and bfloat16 to float, one value 88float gguf_f16_to_f32 ( uint16_t h); 89inline float gguf_bf16_to_f32 ( uint16_t b) { 90const uint32_t u = ( uint32_t ) b << 16 ; 91float f; 92static_assert ( sizeof f == sizeof u, "float is 32 bits" ); 93std :: memcpy ( & f, & u, sizeof f); 94return f; 95} 96 97} // namespace strata_vit