diff options
author | Haoxiang Fei <tonyfettes@tonyfettes.com> | 2024-05-11 16:12:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-11 11:12:06 +0300 |
commit | f99e1e456eaf69cc38c1982a2693ce41c0f897ef (patch) | |
tree | f6bb7dd98afdc852fa428c77c53bf8e72fb69b5e /tests/test-tokenizer-1-bpe.cpp | |
parent | 5ae3426b0b64672991563d4c28b2018b9f961467 (diff) |
llama : lookup word in vocab before doing BPE merges (#7193)
* fix: llama-3 ignore_merges
* test: add test for llama-3 bpe ignore_merges
* fix: set ignore_merges only for llama-3
* fix: test-tokenizer-1-bpe --ingore-merges detection
* fix: copy to fix fallthrough
* fix: change ignore_merges to bool
* fix: add ignore merges tests to cmake
* llama : alternative merge ignore logic
---------
Co-authored-by: Haoxiang Fei <feihaoxiang@idea.edu.cn>
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Diffstat (limited to 'tests/test-tokenizer-1-bpe.cpp')
-rw-r--r-- | tests/test-tokenizer-1-bpe.cpp | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/tests/test-tokenizer-1-bpe.cpp b/tests/test-tokenizer-1-bpe.cpp index a0e2caf9..209a04ad 100644 --- a/tests/test-tokenizer-1-bpe.cpp +++ b/tests/test-tokenizer-1-bpe.cpp @@ -13,15 +13,27 @@ #include <vector> int main(int argc, char **argv) { - if (argc < 2) { - fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]); + if (argc < 2 || argc > 3) { + fprintf(stderr, "Usage: %s <vocab-file> [--ignore-merges]\n", argv[0]); return 1; } const std::string fname = argv[1]; + bool ignore_merges = false; + if (argc == 3) { + if (std::strcmp(argv[2], "--ignore-merges") != 0) { + fprintf(stderr, "Usage: %s <vocab-file> [--ignore-merges]\n", argv[0]); + return 1; + } + ignore_merges = true; + } fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str()); + if (ignore_merges) { + fprintf(stderr, "%s : ignoring merges for tokens inside vocab\n", __func__); + } + llama_model * model; llama_context * ctx; @@ -65,7 +77,19 @@ int main(int argc, char **argv) { std::string str = llama_detokenize_bpe(ctx, std::vector<int>(1, i)); try { auto cps = unicode_cpts_from_utf8(str); - std::vector<llama_token> tokens = llama_tokenize(ctx, str, false); + std::vector<llama_token> tokens = llama_tokenize(ctx, str, false, true); + if (ignore_merges && tokens.size() > 1) { + fprintf(stderr, + "%s : error: token %d detokenizes to '%s'(%zu) but " + "tokenization of this to multiple tokens: [", + __func__, i, str.c_str(), str.length()); + fprintf(stderr, "%d", tokens[0]); + for (size_t i = 1; i < tokens.size(); i++) { + fprintf(stderr, ", %d", tokens[i]); + } + fprintf(stderr, "]\n"); + return 2; + } std::string check = llama_detokenize_bpe(ctx, tokens); if (check != str) { fprintf(stderr, "%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n", |