KBaba7/llama.cpp
0
1#include "llama.h"2#include "common.h"3#include "unicode.h"4#include "console.h"5 6#include <cassert>7#include <codecvt>8#include <cstdio>9#include <cstring>10#include <locale>11#include <string>12#include <thread>13#include <vector>14#include <atomic>15 16int main(int argc, char ** argv) {17 if (argc < 2) {18 fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]);19 return 1;20 }21 22 const std::string fname = argv[1];23 24 fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());25 26 llama_model * model;27 llama_context * ctx;28 29 llama_backend_init();30 31 // load the vocab32 {33 auto mparams = llama_model_default_params();34 35 mparams.vocab_only = true;36 37 model = llama_model_load_from_file(fname.c_str(), mparams);38 39 if (model == NULL) {40 fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());41 return 1;42 }43 44 auto cparams = llama_context_default_params();45 46 ctx = llama_init_from_model(model, cparams);47 48 if (ctx == NULL) {49 fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());50 llama_model_free(model);51 return 1;52 }53 }54 55 const llama_vocab * vocab = llama_model_get_vocab(model);56 57 //GGML_ASSERT(llama_vocab_type(model) == LLAMA_VOCAB_TYPE_SPM);58 if (llama_vocab_type(vocab) != LLAMA_VOCAB_TYPE_SPM) {59 return 99;60 }61 62#ifdef _WIN3263 // We need this for unicode console support64 console::init(false, false);65 atexit([]() { console::cleanup(); });66#endif67 68 const int n_vocab = llama_vocab_n_tokens(vocab);69 70 for (int i = 0; i < n_vocab; ++i) {71 std::string str = common_detokenize(ctx, std::vector<int>(1, i), true);72 std::vector<llama_token> tokens = common_tokenize(ctx, str, false, true);73 std::string check = common_detokenize(ctx, tokens);74 if (check != str) {75 fprintf(stderr, "%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n",76 __func__, i, str.c_str(), str.length(), check.c_str(), check.length());77 return 2;78 }79 }80 81 // unicode82 {83 const int nthread = std::thread::hardware_concurrency();84 85 std::vector<std::thread> threads(nthread);86 87 std::atomic_int errcode = {};88 89 for (int i = 0; i < nthread; ++i) {90 threads[i] = std::thread([i, nthread, ctx, &errcode]() {91 for (uint32_t cp = i; !errcode && cp < 0x00110000; cp += nthread) {92 if ((0x0000D800 <= cp && cp <= 0x0000DFFF) || // surrogates \p{Cs}93 (0x00040000 <= cp && cp <= 0x000E0000)) { // undefined \p{Cn}94 continue;95 }96 97 std::string str = unicode_cpt_to_utf8(cp);98 std::vector<llama_token> tokens = common_tokenize(ctx, str, false, true);99 std::string check = common_detokenize(ctx, tokens);100 if (cp != 9601 && str != check) {101 fprintf(stderr, "error: codepoint 0x%x detokenizes to '%s'(%zu) instead of '%s'(%zu)\n",102 cp, check.c_str(), check.length(), str.c_str(), str.length());103 errcode = 3;104 }105 }106 });107 }108 109 for (auto & t : threads) {110 t.join();111 }112 113 if(errcode) {114 return errcode;115 }116 }117 118 llama_model_free(model);119 llama_free(ctx);120 121 llama_backend_free();122 123 return 0;124}125 