test-tokenizer-0.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. //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. // { " " , { 220, }, },
  13. // { " " , { 256, }, },
  14. // { " " , { 262, }, },
  15. // { "\t" , { 197, }, },
  16. // { "\n" , { 198, }, },
  17. // { "\n\n" , { 271, }, },
  18. // { "\n\n\n" , { 1432, }, },
  19. // { "\t\n" , { 1602, }, },
  20. // { "Hello world" , { 9906, 1917, }, },
  21. // { " Hello world" , { 22691, 1917, }, },
  22. // { "Hello World" , { 9906, 4435, }, },
  23. // { " Hello World" , { 22691, 4435, }, },
  24. // { " Hello World!" , { 22691, 4435, 0, }, },
  25. // { "Hello, world!" , { 9906, 11, 1917, 0, }, },
  26. // { " Hello, world!" , { 22691, 11, 1917, 0, }, },
  27. // { " this is 🦙.cpp" , { 420, 374, 11410, 99, 247, 13, 11055, }, },
  28. // { "w048 7tuijk dsdfhu" , { 86, 23904, 220, 22, 83, 2005, 42908, 11729, 3013, 17156, }, },
  29. // { "нещо на Български" , { 79862, 102118, 13373, 64571, 34694, 3114, 112203, 80112, }, },
  30. // { "កាន់តែពិសេសអាចខលចេញ" , { 21549, 222, 98629, 241, 45358, 233, 21549, 237, 45358, 224, 21549, 244, 21549, 115, 21549, 253, 45358, 223, 21549, 253, 21549, 95, 98629, 227, 21549, 223, 21549, 249, 21549, 227, 45358, 223, 21549, 231, }, },
  31. // { "🚀 (normal) 😶‍🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)", { 9468, 248, 222, 320, 8416, 8, 27623, 114, 102470, 9468, 234, 104, 31643, 320, 36773, 100166, 98634, 8, 26602, 227, 320, 3323, 43465, 430, 706, 1202, 1866, 4037, 8, }, },
  32. // { "Hello" , { 9906, }, },
  33. // { " Hello" , { 22691, }, },
  34. // { " Hello" , { 220, 22691, }, },
  35. // { " Hello" , { 256, 22691, }, },
  36. // { " Hello" , { 262, 22691, }, },
  37. // { " Hello\n Hello" , { 262, 22691, 198, 262, 22691, }, },
  38. // { " (" , { 320, }, },
  39. // { "\n =" , { 198, 284, }, },
  40. // { "' era" , { 6, 11639, }, },
  41. // { "Hello, y'all! How are you 😁 ?我想在apple工作1314151天~", { 9906, 11, 379, 65948, 0, 2650, 527, 499, 27623, 223, 949, 37046, 101067, 19000, 23182, 102301, 9263, 18136, 16, 36827, 21909, }, },
  42. // { "3" , { 18, }, },
  43. // { "33" , { 1644, }, },
  44. // { "333" , { 8765, }, },
  45. // { "3333" , { 8765, 18, }, },
  46. // { "33333" , { 8765, 1644, }, },
  47. // { "333333" , { 8765, 8765, }, },
  48. // { "3333333" , { 8765, 8765, 18, }, },
  49. // { "33333333" , { 8765, 8765, 1644, }, },
  50. // { "333333333" , { 8765, 8765, 8765, }, },
  51. // };
  52. //
  53. // return _k_tests;
  54. //}
  55. using llama_tests = std::map<std::string, std::vector<llama_token>>;
  56. static llama_tests read_tests(const std::string & fname_inp, const std::string & fname_out) {
  57. llama_tests tests;
  58. std::ifstream ifs_inp(fname_inp);
  59. if (!ifs_inp) {
  60. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_inp.c_str());
  61. return tests;
  62. }
  63. std::string sraw((std::istreambuf_iterator<char>(ifs_inp)), std::istreambuf_iterator<char>());
  64. std::ifstream ifs_out(fname_out);
  65. if (!ifs_out) {
  66. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
  67. return tests;
  68. }
  69. std::vector<std::string> sout;
  70. for (std::string line; std::getline(ifs_out, line);) {
  71. sout.push_back(line);
  72. }
  73. const std::string sep = "\n__ggml_vocab_test__\n";
  74. std::vector<std::string> sinp;
  75. size_t pos = 0;
  76. while (pos < sraw.size()) {
  77. const size_t next = sraw.find(sep, pos);
  78. if (next == std::string::npos) {
  79. sinp.push_back(sraw.substr(pos));
  80. break;
  81. }
  82. sinp.push_back(sraw.substr(pos, next - pos));
  83. pos = next + sep.size();
  84. }
  85. if (sinp.size() != sout.size()) {
  86. fprintf(stderr, "%s : error: input and output files have different number of tests\n", __func__);
  87. return tests;
  88. }
  89. for (size_t i = 0; i < sinp.size(); ++i) {
  90. const std::string & s = sinp[i];
  91. const std::string & o = string_strip(sout[i]);
  92. std::vector<llama_token> toks;
  93. size_t pos = 0;
  94. while (pos < o.size()) {
  95. size_t next = o.find(' ', pos);
  96. if (next == std::string::npos) {
  97. next = o.size();
  98. }
  99. const std::string stok = o.substr(pos, next - pos);
  100. toks.push_back(std::stoi(stok));
  101. pos = next + 1;
  102. }
  103. tests[s] = toks;
  104. }
  105. return tests;
  106. }
  107. int main(int argc, char **argv) {
  108. if (argc < 2) {
  109. fprintf(stderr, "Usage: %s vocab-file [text-file]\n", argv[0]);
  110. return 1;
  111. }
  112. const std::string fname = argv[1];
  113. const std::string fname_inp = fname + ".inp";
  114. const std::string fname_out = fname + ".out";
  115. std::string fname_text;
  116. if (argc > 2) {
  117. fname_text = argv[2];
  118. }
  119. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  120. llama_model * model;
  121. llama_context * ctx;
  122. llama_backend_init();
  123. // load the vocab
  124. {
  125. auto mparams = llama_model_default_params();
  126. mparams.vocab_only = true;
  127. model = llama_load_model_from_file(fname.c_str(), mparams);
  128. if (model == NULL) {
  129. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  130. return 1;
  131. }
  132. auto cparams = llama_context_default_params();
  133. ctx = llama_new_context_with_model(model, cparams);
  134. if (ctx == NULL) {
  135. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  136. llama_free_model(model);
  137. return 1;
  138. }
  139. }
  140. #ifdef _WIN32
  141. // We need this for unicode console support
  142. console::init(false, false);
  143. atexit([]() { console::cleanup(); });
  144. #endif
  145. bool success = true;
  146. const auto k_tests = [&]() -> llama_tests {
  147. if (!fname_text.empty()) {
  148. return {};
  149. }
  150. const auto res = read_tests(fname_inp, fname_out);
  151. if (res.empty()) {
  152. fprintf(stderr, "%s : error: no tests found\n", __func__);
  153. exit(1);
  154. }
  155. return res;
  156. }();
  157. const bool add_special = false;
  158. for (const auto & test_kv : k_tests) {
  159. const std::vector<llama_token> res = llama_tokenize(ctx, test_kv.first, add_special, true);
  160. printf("\n");
  161. printf("src: '%s'\n", test_kv.first.c_str());
  162. printf("res: '%s'\n", llama_detokenize(ctx, res).c_str());
  163. printf("tok: ");
  164. for (const auto & tok : res) {
  165. printf("%d ", tok);
  166. }
  167. printf("\n");
  168. bool correct = res.size() == test_kv.second.size();
  169. for (int i = 0; i < (int) res.size() && correct; ++i) {
  170. if (test_kv.second[i] != res[i]) {
  171. correct = false;
  172. }
  173. }
  174. if (!correct) {
  175. fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
  176. fprintf(stderr, "%s : detokenized to: '%s' instead of '%s'\n", __func__,
  177. llama_detokenize(ctx, res).c_str(),
  178. llama_detokenize(ctx, test_kv.second).c_str());
  179. fprintf(stderr, "%s : expected tokens: ", __func__);
  180. for (const auto & t : test_kv.second) {
  181. fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
  182. }
  183. fprintf(stderr, "\n");
  184. fprintf(stderr, "%s : got tokens: ", __func__);
  185. for (const auto & t : res) {
  186. fprintf(stderr, "%6d '%s', ", t, llama_token_to_piece(ctx, t).c_str());
  187. }
  188. fprintf(stderr, "\n");
  189. success = false;
  190. }
  191. }
  192. if (!fname_text.empty()) {
  193. fprintf(stderr, "%s : tokenizing: '%s'\n", __func__, fname_text.c_str());
  194. std::string text;
  195. {
  196. std::ifstream ifs(fname_text);
  197. if (!ifs) {
  198. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_text.c_str());
  199. return 1;
  200. }
  201. text = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
  202. }
  203. fprintf(stderr, "%s : text size: %zu\n", __func__, text.size());
  204. std::vector<llama_token> res;
  205. {
  206. const auto t_start = ggml_time_us();
  207. res = llama_tokenize(ctx, text, add_special, true);
  208. const auto t_end = ggml_time_us();
  209. fprintf(stderr, "%s : tokenized in %.3f ms (cpp)\n", __func__, (t_end - t_start) / 1000.0);
  210. }
  211. fprintf(stderr, "%s : tokens: %zu\n", __func__, res.size());
  212. {
  213. const std::string fname_out = fname_text + ".tokcpp";
  214. std::ofstream ofs(fname_out);
  215. if (!ofs) {
  216. fprintf(stderr, "%s : error: could not open file '%s'\n", __func__, fname_out.c_str());
  217. return 1;
  218. }
  219. for (const auto & tok : res) {
  220. //ofs << tok << " '" << string_strip(llama_detokenize(ctx, std::vector<int>{tok})) << "'" << std::endl;
  221. ofs << tok << "\n";
  222. }
  223. }
  224. fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str());
  225. }
  226. llama_free_model(model);
  227. llama_free(ctx);
  228. llama_backend_free();
  229. printf("\n");
  230. printf("Tests %s\n", success ? "passed" : "failed");
  231. return success ? 0 : 3;
  232. }