test-tokenizer-0.cpp 5.0 KB

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