char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
27df9199d
char/aliceai-t5-moe
1#include "imatrix-loader.h" 2#include "common.h" 3#include "log.h" 4#include "gguf.h" 5 6#include <cmath> 7#include <cstring> 8#include <fstream> 9 10static bool common_imatrix_load_legacy (const std::string & fname ,common_imatrix & imatrix ) { 11 std::ifstream in (fname , std::ios::binary ); 12if (!in ) { 13LOG_ERR ("%s: failed to open %s\n" ,__func__ ,fname .c_str ()); 14return false; 15 } 16 17int n_entries ; 18in .read ((char * )& n_entries ,sizeof (n_entries )); 19if (in .fail ()|| n_entries < 1 ) { 20LOG_ERR ("%s: no data in file %s\n" ,__func__ ,fname .c_str ()); 21return false; 22 } 23 24for (int i = 0 ;i < n_entries ;++ i ) { 25int32_t len = 0 ; 26in .read ((char * )& len ,sizeof (len )); 27 std::vector < char > name_as_vec (len + 1 ); 28in .read ((char * )name_as_vec .data (),len ); 29if (in .fail ()) { 30LOG_ERR ("%s: failed reading name for entry %d from %s\n" ,__func__ ,i + 1 ,fname .c_str ()); 31return false; 32 } 33name_as_vec [len ]= 0 ; 34 std::string name {name_as_vec .data () }; 35 36int32_t ncall = 0 ; 37in .read ((char * )& ncall ,sizeof (ncall )); 38int32_t nval = 0 ; 39in .read ((char * )& nval ,sizeof (nval )); 40if (in .fail ()|| nval < 1 ) { 41LOG_ERR ("%s: failed reading number of values for entry %d\n" ,__func__ ,i ); 42return false; 43 } 44 45auto & e = imatrix .entries [std::move (name )]; 46e .sums .resize (nval ); 47in .read ((char * )e .sums .data (),nval * sizeof (float )); 48if (in .fail ()) { 49LOG_ERR ("%s: failed reading data for entry %d\n" ,__func__ ,i ); 50return false; 51 } 52 53e .counts .resize (1 ); 54e .counts [0 ]= ncall ; 55 } 56 57// the trailing data (chunk count + dataset name) is optional 58if (in .peek ()!= EOF ) { 59int32_t n_calls = 0 ; 60in .read ((char * )& n_calls ,sizeof (n_calls )); 61imatrix .chunk_count = n_calls ; 62 63if (!in .fail ()) { 64int32_t len = 0 ; 65in .read ((char * )& len ,sizeof (len )); 66if (!in .fail ()&& len > 0 ) { 67 std::vector < char > dataset (len + 1 ,0 ); 68in .read (dataset .data (),len ); 69if (!in .fail ()) { 70imatrix .datasets .push_back (dataset .data ()); 71 } 72 } 73 } 74 } 75 76imatrix .chunk_size = 0 ; 77imatrix .is_legacy = true; 78 79return true; 80} 81 82bool common_imatrix_load (const std::string & fname ,common_imatrix & imatrix ) { 83struct ggml_context * ctx = nullptr ; 84struct gguf_init_params meta_gguf_params = { 85/* .no_alloc = */ false, 86/* .ctx = */ & ctx , 87 }; 88struct gguf_context * ctx_gguf = gguf_init_from_file (fname .c_str (),meta_gguf_params ); 89if (!ctx_gguf ) { 90return common_imatrix_load_legacy (fname ,imatrix ); 91 } 92 93const int32_t n_entries = gguf_get_n_tensors (ctx_gguf ); 94if (n_entries < 1 ) { 95LOG_ERR ("%s: no data in file %s\n" ,__func__ ,fname .c_str ()); 96gguf_free (ctx_gguf ); 97ggml_free (ctx ); 98return false; 99 } 100 101const int64_t datasets_key = gguf_find_key (ctx_gguf ,LLM_KV_IMATRIX_DATASETS ); 102const int64_t chunk_count_key = gguf_find_key (ctx_gguf ,LLM_KV_IMATRIX_CHUNK_COUNT ); 103const int64_t chunk_size_key = gguf_find_key (ctx_gguf ,LLM_KV_IMATRIX_CHUNK_SIZE ); 104 105if (datasets_key != -1 && gguf_get_kv_type (ctx_gguf ,datasets_key )== GGUF_TYPE_ARRAY && 106gguf_get_arr_type (ctx_gguf ,datasets_key )== GGUF_TYPE_STRING ) { 107const int64_t n = gguf_get_arr_n (ctx_gguf ,datasets_key ); 108imatrix .datasets .reserve (imatrix .datasets .size ()+ n ); 109for (int64_t i = 0 ;i < n ;++ i ) { 110imatrix .datasets .push_back (gguf_get_arr_str (ctx_gguf ,datasets_key ,i )); 111 } 112 } 113 114imatrix .has_metadata = (datasets_key != -1 && chunk_count_key != -1 && chunk_size_key != -1 ); 115imatrix .chunk_count = (chunk_count_key != -1 ) ?gguf_get_val_u32 (ctx_gguf ,chunk_count_key ) :0 ; 116imatrix .chunk_size = (chunk_size_key != -1 ) ?gguf_get_val_u32 (ctx_gguf ,chunk_size_key ) :0 ; 117 118const std::string in_sum2_suffix {".in_sum2" }; 119const std::string counts_suffix {".counts" }; 120 121 std::map < std::string , std::pair < struct ggml_tensor * ,struct ggml_tensor *>> sums_counts_for ; 122 123for (struct ggml_tensor * cur = ggml_get_first_tensor (ctx );cur ;cur = ggml_get_next_tensor (ctx ,cur )) { 124 std::string name = cur -> name ; 125 126if (name .empty ()) {continue ; } 127 128if (string_remove_suffix (name ,in_sum2_suffix )) { 129sums_counts_for [std::move (name )].first = cur ; 130 }else if (string_remove_suffix (name ,counts_suffix )) { 131sums_counts_for [std::move (name )].second = cur ; 132 } 133 } 134 135for (const auto & sc :sums_counts_for ) { 136const std::string & name = sc .first ; 137const struct ggml_tensor * in_sum2 = sc .second .first ; 138const struct ggml_tensor * counts = sc .second .second ; 139 140if (!in_sum2 || !counts ) { 141LOG_ERR ("%s: mismatched sums and counts for %s\n" ,__func__ ,name .c_str ()); 142gguf_free (ctx_gguf ); 143ggml_free (ctx ); 144return false; 145 } 146 147if (in_sum2 -> type != GGML_TYPE_F32 || counts -> type != GGML_TYPE_F32 ) { 148LOG_ERR ("%s: sums and counts for %s must be F32\n" ,__func__ ,name .c_str ()); 149gguf_free (ctx_gguf ); 150ggml_free (ctx ); 151return false; 152 } 153 154auto & e = imatrix .entries [name ]; 155 156const int64_t nval = ggml_nelements (in_sum2 ); 157const int64_t ncounts = ggml_nelements (counts ); 158 159e .sums .resize (nval ); 160for (int64_t j = 0 ;j < nval ;++ j ) { 161e .sums [j ]= ((const float * )in_sum2 -> data )[j ]; 162 } 163 164e .counts .resize (ncounts ); 165for (int64_t j = 0 ;j < ncounts ;++ j ) { 166e .counts [j ]= std::lround (((const float * )counts -> data )[j ]); 167 } 168 } 169 170gguf_free (ctx_gguf ); 171ggml_free (ctx ); 172return true; 173}