test-tokenizer-0.cpp 5.7 KB

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