|
| 1 | +#include "llama.h" |
| 2 | +#include "common.h" |
| 3 | +#include "console.h" |
| 4 | + |
| 5 | +#include <cassert> |
| 6 | +#include <cstdio> |
| 7 | +#include <cstring> |
| 8 | +#include <string> |
| 9 | +#include <codecvt> |
| 10 | +#include <map> |
| 11 | +#include <vector> |
| 12 | +#include <locale> |
| 13 | + |
| 14 | +typedef int codepoint; |
| 15 | + |
| 16 | +std::string codepoint_to_utf8(codepoint cp) { |
| 17 | + std::string result; |
| 18 | + if (0x00 <= cp && cp <= 0x7f) { |
| 19 | + result.push_back(cp); |
| 20 | + } else if (0x80 <= cp && cp <= 0x7ff) { |
| 21 | + result.push_back(0xc0 | ((cp >> 6) & 0x1f)); |
| 22 | + result.push_back(0x80 | (cp & 0x3f)); |
| 23 | + } else if (0x800 <= cp && cp <= 0xffff) { |
| 24 | + result.push_back(0xe0 | ((cp >> 12) & 0x0f)); |
| 25 | + result.push_back(0x80 | ((cp >> 6) & 0x3f)); |
| 26 | + result.push_back(0x80 | (cp & 0x3f)); |
| 27 | + } else if (0x10000 <= cp && cp <= 0x10ffff) { |
| 28 | + result.push_back(0xf0 | ((cp >> 18) & 0x07)); |
| 29 | + result.push_back(0x80 | ((cp >> 12) & 0x3f)); |
| 30 | + result.push_back(0x80 | ((cp >> 6) & 0x3f)); |
| 31 | + result.push_back(0x80 | (cp & 0x3f)); |
| 32 | + } else { |
| 33 | + throw std::invalid_argument("invalid codepoint"); |
| 34 | + } |
| 35 | + return result; |
| 36 | +} |
| 37 | + |
| 38 | +int main(int argc, char **argv) { |
| 39 | + if (argc < 2) { |
| 40 | + fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + |
| 44 | + const std::string fname = argv[1]; |
| 45 | + |
| 46 | + fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str()); |
| 47 | + |
| 48 | + llama_model * model; |
| 49 | + llama_context * ctx; |
| 50 | + |
| 51 | + llama_backend_init(false); |
| 52 | + |
| 53 | + // load the vocab |
| 54 | + { |
| 55 | + auto lparams = llama_context_default_params(); |
| 56 | + |
| 57 | + lparams.vocab_only = true; |
| 58 | + |
| 59 | + model = llama_load_model_from_file(fname.c_str(), lparams); |
| 60 | + |
| 61 | + if (model == NULL) { |
| 62 | + fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + ctx = llama_new_context_with_model(model, lparams); |
| 67 | + |
| 68 | + if (ctx == NULL) { |
| 69 | + fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); |
| 70 | + llama_free_model(model); |
| 71 | + return 1; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + GGML_ASSERT(llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_SPM); |
| 76 | + |
| 77 | +#ifdef _WIN32 |
| 78 | + // We need this for unicode console support |
| 79 | + console::init(false, false); |
| 80 | + atexit([]() { console::cleanup(); }); |
| 81 | +#endif |
| 82 | + |
| 83 | + const int n_vocab = llama_n_vocab(ctx); |
| 84 | + |
| 85 | + for (int i = 0; i < n_vocab; ++i) { |
| 86 | + std::string str = llama_detokenize_spm(ctx, std::vector<int>(1, i)); |
| 87 | + std::vector<llama_token> tokens = llama_tokenize(ctx, str, false); |
| 88 | + std::string check = llama_detokenize_spm(ctx, tokens); |
| 89 | + if (check != str) { |
| 90 | + fprintf(stderr, "%s : error: token %d detokenizes to >%s<(%llu) but tokenization of this detokenizes to >%s<(%llu)\n", |
| 91 | + __func__, i, str.c_str(), str.length(), check.c_str(), check.length()); |
| 92 | + if(i != 3) |
| 93 | + return 2; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + for (codepoint cp = 0x0000; cp < 0xffff; ++cp) { |
| 98 | + if (cp < 0xd800 || cp > 0xdfff) { |
| 99 | + std::string str = codepoint_to_utf8(cp); |
| 100 | + std::vector<llama_token> tokens = llama_tokenize(ctx, str, false); |
| 101 | + std::string check = llama_detokenize_spm(ctx, tokens); |
| 102 | + if (str != check) { |
| 103 | + fprintf(stderr, "%s : error: codepoint %d detokenizes to >%s<(%llu) instead of >%s<(%llu)\n", |
| 104 | + __func__, cp, check.c_str(), check.length(), str.c_str(), str.length()); |
| 105 | + if(cp != 0 && cp != 9601) |
| 106 | + return 3; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + for (codepoint cp = 0x10000; cp < 0x0010ffff; ++cp) { |
| 111 | + std::string str = codepoint_to_utf8(cp); |
| 112 | + std::vector<llama_token> tokens = llama_tokenize(ctx, str, false); |
| 113 | + std::string check = llama_detokenize_spm(ctx, tokens); |
| 114 | + if (str != check) { |
| 115 | + fprintf(stderr, "%s : error: codepoint %d detokenizes to >%s<(%llu) instead of >%s<(%llu)\n", |
| 116 | + __func__, cp, check.c_str(), check.length(), str.c_str(), str.length()); |
| 117 | + return 4; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + llama_free_model(model); |
| 122 | + llama_free(ctx); |
| 123 | + |
| 124 | + llama_backend_free(); |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
0 commit comments