test-tokenizer-0-falcon.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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-falcon.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. { " " , { 204, }, },
  14. { " " , { 258, }, },
  15. { " " , { 466, }, },
  16. { "\t" , { 192, }, },
  17. { "\n" , { 193, }, },
  18. { "\t\n" , { 19125, }, },
  19. { "Hello world" , { 9856, 1079, }, },
  20. { " Hello world" , { 23090, 1079, }, },
  21. { "Hello World" , { 9856, 2889, }, },
  22. { " Hello World" , { 23090, 2889, }, },
  23. { " Hello World!" , { 23090, 2889, 12, }, },
  24. { "Hello, world!" , { 9856, 23, 1079, 12, }, },
  25. { " Hello, world!" , { 23090, 23, 1079, 12, }, },
  26. { " this is 🦙.cpp" , { 414, 304, 3346, 111, 231, 25, 29247, }, },
  27. { "w048 7tuijk dsdfhu" , { 98, 55866, 204, 34, 16682, 7149, 36190, 6869, 11481, }, },
  28. { "нещо на Български" , { 150, 133, 6207, 151, 215, 150, 134, 5052, 133, 6279, 5052, 223, 151, 216, 49679, 123, 53110, 47043, 7795, }, },
  29. { "កាន់តែពិសេសអាចខលចេញ" , { 38154, 206, 38154, 126, 38154, 225, 167, 237, 217, 38154, 221, 167, 237, 208, 38154, 228, 38154, 127, 38154, 237, 167, 237, 207, 38154, 237, 38154, 107, 38154, 126, 38154, 211, 38154, 207, 38154, 233, 38154, 211, 167, 237, 207, 38154, 215, }, },
  30. { "🚀 (normal) 😶‍🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)", { 2571, 232, 206, 204, 19, 11003, 20, 8196, 126, 283, 219, 48778, 116, 13392, 204, 19, 51831, 732, 63209, 1741, 7955, 522, 20, 22438, 211, 204, 19, 7927, 53360, 325, 504, 701, 946, 10930, 20, }, },
  31. { "Hello" , { 9856, }, },
  32. { " Hello" , { 23090, }, },
  33. { " Hello" , { 204, 23090, }, },
  34. { " Hello" , { 258, 23090, }, },
  35. { " Hello" , { 466, 23090, }, },
  36. { " Hello\n Hello" , { 466, 23090, 742, 23090, }, },
  37. { "\n =" , { 1212, 40, }, },
  38. { "' era" , { 18, 4932, }, },
  39. };
  40. return _k_tests;
  41. }
  42. int main(int argc, char **argv) {
  43. if (argc < 2) {
  44. fprintf(stderr, "Usage: %s vocab-file [text-file]\n", argv[0]);
  45. return 1;
  46. }
  47. const std::string fname = argv[1];
  48. std::string fname_text;
  49. if (argc > 2) {
  50. fname_text = argv[2];
  51. }
  52. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  53. llama_model * model;
  54. llama_context * ctx;
  55. llama_backend_init(false);
  56. // load the vocab
  57. {
  58. auto mparams = llama_model_default_params();
  59. mparams.vocab_only = true;
  60. model = llama_load_model_from_file(fname.c_str(), mparams);
  61. if (model == NULL) {
  62. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  63. return 1;
  64. }
  65. auto cparams = llama_context_default_params();
  66. ctx = llama_new_context_with_model(model, cparams);
  67. if (ctx == NULL) {
  68. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  69. llama_free_model(model);
  70. return 1;
  71. }
  72. }
  73. if (llama_vocab_type(model) != LLAMA_VOCAB_TYPE_BPE) {
  74. fprintf(stderr, "%s : error: vocab type is not BPE\n", __func__);
  75. llama_free_model(model);
  76. llama_free(ctx);
  77. return 2;
  78. }
  79. #ifdef _WIN32
  80. // We need this for unicode console support
  81. console::init(false, false);
  82. atexit([]() { console::cleanup(); });
  83. #endif
  84. bool success = true;
  85. for (const auto & test_kv : k_tests()) {
  86. const std::vector<llama_token> res = 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_bpe(ctx, res).c_str());
  90. printf("tok: ");
  91. for (const auto & tok : res) {
  92. printf("%d ", tok);
  93. }
  94. printf("\n");
  95. bool correct = res.size() == test_kv.second.size();
  96. for (int i = 0; i < (int) res.size() && correct; ++i) {
  97. if (test_kv.second[i] != res[i]) {
  98. correct = false;
  99. }
  100. }
  101. if (!correct) {
  102. fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
  103. fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
  104. llama_detokenize_bpe(ctx, res).c_str(),
  105. llama_detokenize_bpe(ctx, test_kv.second).c_str());
  106. fprintf(stderr, "%s : expected tokens: ", __func__);
  107. for (const auto & t : test_kv.second) {
  108. fprintf(stderr, "%6d, ", t);
  109. }
  110. fprintf(stderr, "\n");
  111. fprintf(stderr, "%s : got tokens: ", __func__);
  112. for (const auto & t : res) {
  113. fprintf(stderr, "%6d, ", t);
  114. }
  115. fprintf(stderr, "\n");
  116. success = false;
  117. }
  118. }
  119. if (!fname_text.empty()) {
  120. fprintf(stderr, "%s : tokenizing: '%s'\n", __func__, fname_text.c_str());
  121. std::string text;
  122. {
  123. std::ifstream ifs(fname_text);
  124. if (!ifs) {
  125. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_text.c_str());
  126. return 1;
  127. }
  128. text = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
  129. }
  130. fprintf(stderr, "%s : text size: %zu\n", __func__, text.size());
  131. const std::vector<llama_token> res = llama_tokenize(ctx, text, false);
  132. fprintf(stderr, "%s : tokens: %zu\n", __func__, res.size());
  133. {
  134. const std::string fname_out = fname_text + ".tokcpp";
  135. std::ofstream ofs(fname_out);
  136. if (!ofs) {
  137. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
  138. return 1;
  139. }
  140. for (const auto & tok : res) {
  141. ofs << tok << " '" << llama_detokenize_bpe(ctx, std::vector<int>{tok}) << "'" << std::endl;
  142. }
  143. }
  144. fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str());
  145. }
  146. llama_free_model(model);
  147. llama_free(ctx);
  148. llama_backend_free();
  149. return success ? 0 : 3;
  150. }