test-tokenizer-0-falcon.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-falcon.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. { " " , { 204, }, },
  13. { " " , { 258, }, },
  14. { " " , { 466, }, },
  15. { "\t" , { 192, }, },
  16. { "\n" , { 193, }, },
  17. { "\t\n" , { 19125, }, },
  18. { "Hello world" , { 9856, 1079, }, },
  19. { " Hello world" , { 23090, 1079, }, },
  20. { "Hello World" , { 9856, 2889, }, },
  21. { " Hello World" , { 23090, 2889, }, },
  22. { " Hello World!" , { 23090, 2889, 12, }, },
  23. { "Hello, world!" , { 9856, 23, 1079, 12, }, },
  24. { " Hello, world!" , { 23090, 23, 1079, 12, }, },
  25. { " this is 🦙.cpp" , { 414, 304, 3346, 111, 231, 25, 29247, }, },
  26. { "w048 7tuijk dsdfhu" , { 98, 55866, 204, 34, 16682, 7149, 36190, 6869, 11481, }, },
  27. { "нещо на Български" , { 150, 133, 6207, 151, 215, 150, 134, 5052, 133, 6279, 5052, 223, 151, 216, 49679, 123, 53110, 47043, 7795, }, },
  28. { "កាន់តែពិសេសអាចខលចេញ" , { 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, }, },
  29. { "🚀 (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, }, },
  30. { "Hello" , { 9856, }, },
  31. { " Hello" , { 23090, }, },
  32. { " Hello" , { 204, 23090, }, },
  33. { " Hello" , { 258, 23090, }, },
  34. { " Hello" , { 466, 23090, }, },
  35. { " Hello\n Hello" , { 466, 23090, 742, 23090, }, },
  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_BPE) {
  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 = llama_tokenize(ctx, test_kv.first, false);
  78. printf("\n");
  79. printf("src: '%s'\n", test_kv.first.c_str());
  80. printf("res: '%s'\n", llama_detokenize_bpe(ctx, res).c_str());
  81. printf("tok: ");
  82. for (const auto & tok : res) {
  83. printf("%d ", tok);
  84. }
  85. printf("\n");
  86. bool correct = res.size() == test_kv.second.size();
  87. for (int i = 0; i < (int) res.size() && correct; ++i) {
  88. if (test_kv.second[i] != res[i]) {
  89. correct = false;
  90. }
  91. }
  92. if (!correct) {
  93. fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
  94. fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
  95. llama_detokenize_bpe(ctx, res).c_str(),
  96. llama_detokenize_bpe(ctx, test_kv.second).c_str());
  97. fprintf(stderr, "%s : expected tokens: ", __func__);
  98. for (const auto & t : test_kv.second) {
  99. fprintf(stderr, "%6d, ", t);
  100. }
  101. fprintf(stderr, "\n");
  102. fprintf(stderr, "%s : got tokens: ", __func__);
  103. for (const auto & t : res) {
  104. fprintf(stderr, "%6d, ", t);
  105. }
  106. fprintf(stderr, "\n");
  107. success = false;
  108. }
  109. }
  110. if (!fname_text.empty()) {
  111. fprintf(stderr, "%s : tokenizing: '%s'\n", __func__, fname_text.c_str());
  112. std::string text;
  113. {
  114. std::ifstream ifs(fname_text);
  115. if (!ifs) {
  116. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_text.c_str());
  117. return 1;
  118. }
  119. text = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
  120. }
  121. fprintf(stderr, "%s : text size: %zu\n", __func__, text.size());
  122. const std::vector<llama_token> res = llama_tokenize(ctx, text, true);
  123. fprintf(stderr, "%s : tokens: %zu\n", __func__, res.size());
  124. {
  125. const std::string fname_out = fname_text + ".tokcpp";
  126. std::ofstream ofs(fname_out);
  127. if (!ofs) {
  128. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
  129. return 1;
  130. }
  131. for (const auto & tok : res) {
  132. ofs << tok << " ";
  133. }
  134. ofs << "\n";
  135. }
  136. fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str());
  137. }
  138. llama_free_model(model);
  139. llama_free(ctx);
  140. llama_backend_free();
  141. return success ? 0 : 3;
  142. }