test-tokenizer-1.cpp 4.0 KB

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