char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
147a52163
char/aliceai-t5-moe
1#include "llama.h" 2#include "common.h" 3#include "console.h" 4 5#include "../src/unicode.h" 6 7#include <cassert> 8#include <codecvt> 9#include <cstdio> 10#include <cstring> 11#include <locale> 12#include <string> 13#include <thread> 14#include <vector> 15#include <atomic> 16 17int main (int argc ,char ** argv ) { 18if (argc < 2 || argc > 3 ) { 19fprintf (stderr ,"Usage: %s <vocab-file> [--ignore-merges]\n" ,argv [0 ]); 20return 1 ; 21 } 22 23const std::string fname = argv [1 ]; 24bool ignore_merges = false; 25if (argc == 3 ) { 26if (std::strcmp (argv [2 ],"--ignore-merges" )!= 0 ) { 27fprintf (stderr ,"Usage: %s <vocab-file> [--ignore-merges]\n" ,argv [0 ]); 28return 1 ; 29 } 30ignore_merges = true; 31 } 32 33fprintf (stderr ,"%s : reading vocab from: '%s'\n" ,__func__ ,fname .c_str ()); 34 35if (ignore_merges ) { 36fprintf (stderr ,"%s : ignoring merges for tokens inside vocab\n" ,__func__ ); 37 } 38 39llama_model * model ; 40llama_context * ctx ; 41 42llama_backend_init (); 43 44// load the vocab 45 { 46auto mparams = llama_model_default_params (); 47 48mparams .vocab_only = true; 49 50model = llama_model_load_from_file (fname .c_str (),mparams ); 51 52if (model == NULL ) { 53fprintf (stderr ,"%s: error: failed to load vocab '%s'\n" ,__func__ ,fname .c_str ()); 54return 1 ; 55 } 56 57auto cparams = llama_context_default_params (); 58 59ctx = llama_init_from_model (model ,cparams ); 60 61if (ctx == NULL ) { 62fprintf (stderr ,"%s: error: failed to load vocab '%s'\n" ,__func__ ,fname .c_str ()); 63llama_model_free (model ); 64return 1 ; 65 } 66 } 67 68const llama_vocab * vocab = llama_model_get_vocab (model ); 69 70//GGML_ASSERT(llama_vocab_type(vocab) == LLAMA_VOCAB_TYPE_BPE); 71if (llama_vocab_type (vocab )!= LLAMA_VOCAB_TYPE_BPE ) { 72return 99 ; 73 } 74 75#ifdef _WIN32 76// We need this for unicode console support 77 console::init (false, false); 78atexit ([]() { console::cleanup (); }); 79#endif 80 81const int n_vocab = llama_vocab_n_tokens (vocab ); 82 83for (int i = 0 ;i < n_vocab ;++ i ) { 84 std::string str = common_detokenize (ctx , std::vector < int > (1 ,i )); 85try { 86auto cps = unicode_cpts_from_utf8 (str ); 87 std::vector < llama_token > tokens = common_tokenize (ctx ,str , false, true); 88if (ignore_merges && tokens .size ()> 1 ) { 89fprintf (stderr , 90"%s : error: token %d detokenizes to '%s'(%zu) but " 91"tokenization of this to multiple tokens: [" , 92__func__ ,i ,str .c_str (),str .length ()); 93fprintf (stderr ,"%d" ,tokens [0 ]); 94for (size_t i = 1 ;i < tokens .size ();i ++ ) { 95fprintf (stderr ,", %d" ,tokens [i ]); 96 } 97fprintf (stderr ,"]\n" ); 98return 2 ; 99 } 100 std::string check = common_detokenize (ctx ,tokens ); 101if (check != str ) { 102fprintf (stderr ,"%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n" , 103__func__ ,i ,str .c_str (),str .length (),check .c_str (),check .length ()); 104return 2 ; 105 } 106 } 107catch (const std::invalid_argument & ) { 108//fprintf(stderr, "%s : info: utf8 conversion %d '%s'\n", __func__, i, str.c_str()); 109 } 110 } 111 112// unicode 113 { 114const int nthread = std::thread::hardware_concurrency (); 115 116 std::vector < std::thread > threads (nthread ); 117 118 std::atomic_int errcode = {}; 119 120for (int i = 0 ;i < nthread ;++ i ) { 121threads [i ]= std::thread ([i ,nthread ,ctx ,& errcode ]() { 122for (uint32_t cp = i ; !errcode && cp < 0x00110000 ;cp += nthread ) { 123if ((0x0000D800 <=cp && cp <=0x0000DFFF )|| // surrogates \p{Cs} 124 (0x00040000 <=cp && cp <=0x000E0000 )) {// undefined \p{Cn} 125continue ; 126 } 127 128 std::string str = unicode_cpt_to_utf8 (cp ); 129 std::vector < llama_token > tokens = common_tokenize (ctx ,str , false); 130 std::string check = common_detokenize (ctx ,tokens ); 131if (cp != 9601 && str != check ) { 132fprintf (stderr ,"error: codepoint 0x%x detokenizes to '%s'(%zu) instead of '%s'(%zu)\n" , 133cp ,check .c_str (),check .length (),str .c_str (),str .length ()); 134errcode = 3 ; 135 } 136 } 137 }); 138 } 139 140for (auto & t :threads ) { 141t .join (); 142 } 143 144if (errcode ) { 145return errcode ; 146 } 147 } 148 149llama_free (ctx ); 150llama_model_free (model ); 151 152llama_backend_free (); 153 154return 0 ; 155}