Team Ai
Apppublic

KBaba7/llama.cpp

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