test-tokenizer-1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "llama.h"
  2. #include "common.h"
  3. #include <cassert>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <string>
  7. #include <codecvt>
  8. #include <map>
  9. #include <vector>
  10. #include <locale>
  11. static std::string escape_whitespace(const std::string& text) {
  12. std::string result;
  13. bool escaping = false;
  14. result += "\xe2\x96\x81";
  15. for (size_t offs = 0; offs < text.length(); ++offs) {
  16. if (text[offs] == ' ') {
  17. if (!escaping) {
  18. result += "\xe2\x96\x81";
  19. escaping = true;
  20. }
  21. }
  22. else {
  23. escaping = false;
  24. result += text[offs];
  25. }
  26. }
  27. return result;
  28. }
  29. static std::string unescape_whitespace(llama_context * ctx, const std::vector<llama_token> & tokens) {
  30. std::string result;
  31. for (size_t i = 0; i < tokens.size(); ++i) {
  32. result += llama_token_to_str(ctx, tokens[i]);
  33. }
  34. return result;
  35. }
  36. int main(int argc, char **argv) {
  37. if (argc < 2) {
  38. fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]);
  39. return 1;
  40. }
  41. const std::string fname = argv[1];
  42. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  43. llama_model * model;
  44. llama_context * ctx;
  45. llama_backend_init(false);
  46. // load the vocab
  47. {
  48. auto lparams = llama_context_default_params();
  49. lparams.vocab_only = true;
  50. model = llama_load_model_from_file(fname.c_str(), lparams);
  51. if (model == NULL) {
  52. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  53. return 1;
  54. }
  55. ctx = llama_new_context_with_model(model, lparams);
  56. if (ctx == NULL) {
  57. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  58. llama_free_model(model);
  59. return 1;
  60. }
  61. }
  62. const int n_vocab = llama_n_vocab(ctx);
  63. for (int i = 0; i < n_vocab; ++i) {
  64. std::string forward = llama_token_to_str_bpe(ctx, i);
  65. std::vector<llama_token> tokens = llama_tokenize_bpe(ctx, forward, false);
  66. if (tokens.size() == 1) {
  67. if (i != tokens[0]) {
  68. std::string backward = llama_token_to_str(ctx, tokens[0]);
  69. fprintf(stderr, "%s : error: token %d is string %s but bpe returns token %d %s\n",
  70. __func__, i, llama_token_to_str(ctx, i).c_str(), tokens[0], backward.c_str());
  71. return 2;
  72. }
  73. } else {
  74. llama_token_type type = llama_token_get_type(ctx, i);
  75. if (type == LLAMA_TOKEN_TYPE_UNKNOWN || type == LLAMA_TOKEN_TYPE_CONTROL || type == LLAMA_TOKEN_TYPE_BYTE) {
  76. fprintf(stderr, "%s : info: token %d is string %s and bpe returns tokens %s\n",
  77. __func__, i, llama_token_to_str(ctx, i).c_str(), unescape_whitespace(ctx, tokens).c_str());
  78. } else {
  79. fprintf(stderr, "%s : error: token %d is string %s but bpe returns tokens %s\n",
  80. __func__, i, llama_token_to_str(ctx, i).c_str(), unescape_whitespace(ctx, tokens).c_str());
  81. return 2;
  82. }
  83. }
  84. }
  85. #ifdef _WIN32
  86. std::wstring_convert<typename std::codecvt_utf8<char16_t>, char16_t> u16converter;
  87. for (char16_t ch = 0x0000; ch < 0xffff; ++ch) {
  88. std::u16string u16str(1, ch);
  89. std::string str = u16converter.to_bytes(u16str);
  90. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  91. if (tokens.size() == 1) {
  92. fprintf(stderr, "%s : info: %s tokenized to %d \n",
  93. __func__, str.c_str(), tokens[0]);
  94. }
  95. }
  96. std::wstring_convert<typename std::codecvt_utf8<char32_t>, char32_t> u32converter;
  97. for (char32_t ch = 0x0000; ch < 0x0010ffff; ++ch) {
  98. std::u32string u32str(1, ch);
  99. std::string str = u32converter.to_bytes(u32str);
  100. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  101. if (tokens.size() == 1) {
  102. fprintf(stderr, "%s : info: %s tokenized to %d \n", __func__, str.c_str(), tokens[0]);
  103. }
  104. }
  105. #endif
  106. llama_free_model(model);
  107. llama_free(ctx);
  108. llama_backend_free();
  109. return 0;
  110. }