test-tokenizer-0-llama.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "llama.h"
  2. #include "common.h"
  3. #include <cstdio>
  4. #include <string>
  5. #include <map>
  6. #include <vector>
  7. #include <fstream>
  8. // generate using test-tokenizer-0-llama.py
  9. static const std::map<std::string, std::vector<llama_token>> & k_tests() {
  10. static std::map<std::string, std::vector<llama_token>> _k_tests = {
  11. { "" , { }, },
  12. { " " , { 259, }, },
  13. { " " , { 1678, }, },
  14. { " " , { 268, }, },
  15. { "\t" , { 29871, 12, }, },
  16. { "\n" , { 29871, 13, }, },
  17. { "\t\n" , { 29871, 12, 13, }, },
  18. { "Hello world" , { 15043, 3186, }, },
  19. { " Hello world" , { 29871, 15043, 3186, }, },
  20. { "Hello World" , { 15043, 2787, }, },
  21. { " Hello World" , { 29871, 15043, 2787, }, },
  22. { " Hello World!" , { 29871, 15043, 2787, 29991, }, },
  23. { "Hello, world!" , { 15043, 29892, 3186, 29991, }, },
  24. { " Hello, world!" , { 29871, 15043, 29892, 3186, 29991, }, },
  25. { " this is 🦙.cpp" , { 29871, 445, 338, 29871, 243, 162, 169, 156, 29889, 8223, }, },
  26. { "w048 7tuijk dsdfhu" , { 281, 29900, 29946, 29947, 29871, 29955, 9161, 13535, 18031, 2176, 6905, }, },
  27. { "нещо на Български" , { 1538, 4851, 665, 1386, 29713, 1305, }, },
  28. { "កាន់តែពិសេសអាចខលចេញ" , { 29871, 31849, 31324, 31934, 228, 162, 142, 228, 161, 146, 228, 162, 133, 228, 161, 153, 228, 161, 186, 31708, 228, 162, 132, 31708, 228, 161, 165, 31324, 228, 161, 136, 228, 161, 132, 228, 161, 158, 228, 161, 136, 228, 162, 132, 228, 161, 140, }, },
  29. { "🚀 (normal) 😶‍🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)", { 29871, 243, 162, 157, 131, 313, 8945, 29897, 29871, 243, 162, 155, 185, 30722, 243, 162, 143, 174, 30598, 313, 20787, 953, 3848, 275, 16125, 630, 29897, 29871, 31681, 313, 6194, 953, 29877, 2397, 393, 756, 967, 1914, 5993, 29897, }, },
  30. { "Hello" , { 15043, }, },
  31. { " Hello" , { 29871, 15043, }, },
  32. { " Hello" , { 259, 15043, }, },
  33. { " Hello" , { 1678, 15043, }, },
  34. { " Hello" , { 268, 15043, }, },
  35. { " Hello\n Hello" , { 268, 15043, 13, 1678, 15043, }, },
  36. };
  37. return _k_tests;
  38. }
  39. int main(int argc, char **argv) {
  40. if (argc < 2) {
  41. fprintf(stderr, "Usage: %s vocab-file [text-file]\n", argv[0]);
  42. return 1;
  43. }
  44. const std::string fname = argv[1];
  45. std::string fname_text;
  46. if (argc > 2) {
  47. fname_text = argv[2];
  48. }
  49. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  50. llama_model * model;
  51. llama_context * ctx;
  52. llama_backend_init(false);
  53. // load the vocab
  54. {
  55. auto lparams = llama_context_default_params();
  56. lparams.vocab_only = true;
  57. model = llama_load_model_from_file(fname.c_str(), lparams);
  58. if (model == NULL) {
  59. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  60. return 1;
  61. }
  62. ctx = llama_new_context_with_model(model, lparams);
  63. if (ctx == NULL) {
  64. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  65. llama_free_model(model);
  66. return 1;
  67. }
  68. }
  69. if (llama_vocab_type(ctx) != LLAMA_VOCAB_TYPE_SPM) {
  70. fprintf(stderr, "%s : error: vocab type is not SPM\n", __func__);
  71. llama_free_model(model);
  72. llama_free(ctx);
  73. return 2;
  74. }
  75. bool success = true;
  76. for (const auto & test_kv : k_tests()) {
  77. const std::vector<llama_token> res_bos = llama_tokenize(ctx, test_kv.first, true);
  78. const std::vector<llama_token> res_nobos = llama_tokenize(ctx, test_kv.first, false);
  79. printf("\n");
  80. printf("src: '%s'\n", test_kv.first.c_str());
  81. printf("res: '%s'\n", llama_detokenize_spm(ctx, res_bos).c_str());
  82. printf("tok: ");
  83. for (const auto & tok : res_bos) {
  84. printf("%d ", tok);
  85. }
  86. printf("\n");
  87. bool correct = res_nobos.size() == test_kv.second.size() && res_bos.size() == res_nobos.size() + 1 && res_bos[0] == 1;
  88. for (int i = 0; i < (int) res_nobos.size() && correct; ++i) {
  89. if (test_kv.second[i] != res_bos[i + 1]) {
  90. correct = false;
  91. }
  92. if (test_kv.second[i] != res_nobos[i]) {
  93. correct = false;
  94. }
  95. }
  96. if (!correct) {
  97. fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
  98. fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
  99. llama_detokenize_spm(ctx, res_nobos).c_str(),
  100. llama_detokenize_spm(ctx, test_kv.second).c_str());
  101. fprintf(stderr, "%s : expected tokens: ", __func__);
  102. for (const auto & t : test_kv.second) {
  103. fprintf(stderr, "%6d, ", t);
  104. }
  105. fprintf(stderr, "\n");
  106. fprintf(stderr, "%s : got tokens: ", __func__);
  107. for (const auto & t : res_nobos) {
  108. fprintf(stderr, "%6d, ", t);
  109. }
  110. fprintf(stderr, "\n");
  111. success = false;
  112. }
  113. }
  114. if (!fname_text.empty()) {
  115. fprintf(stderr, "%s : tokenizing: '%s'\n", __func__, fname_text.c_str());
  116. std::string text;
  117. {
  118. std::ifstream ifs(fname_text);
  119. if (!ifs) {
  120. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_text.c_str());
  121. return 1;
  122. }
  123. text = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
  124. }
  125. fprintf(stderr, "%s : text size: %zu\n", __func__, text.size());
  126. const std::vector<llama_token> res = llama_tokenize(ctx, text, true);
  127. fprintf(stderr, "%s : tokens: %zu\n", __func__, res.size());
  128. {
  129. const std::string fname_out = fname_text + ".tokcpp";
  130. std::ofstream ofs(fname_out);
  131. if (!ofs) {
  132. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
  133. return 1;
  134. }
  135. for (const auto & tok : res) {
  136. ofs << tok << " ";
  137. }
  138. ofs << "\n";
  139. }
  140. fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str());
  141. }
  142. llama_free_model(model);
  143. llama_free(ctx);
  144. llama_backend_free();
  145. return success ? 0 : 3;
  146. }