test-tokenizer-0-llama.cpp 7.3 KB

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