test-tokenizer-0.cpp 11 KB

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