test-tokenizer-1.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. GGML_ASSERT(llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_BPE);
  56. const int n_vocab = llama_n_vocab(ctx);
  57. for (int i = 0; i < n_vocab; ++i) {
  58. std::string forward = llama_token_to_str(ctx, i);
  59. std::vector<llama_token> tokens = llama_tokenize(ctx, forward, false);
  60. if (tokens.size() == 1) {
  61. if (i != tokens[0]) {
  62. std::string backward = llama_token_to_str(ctx, tokens[0]);
  63. fprintf(stderr, "%s : error: token %d is string %s but bpe returns token %d %s\n",
  64. __func__, i, llama_token_to_str(ctx, i).c_str(), tokens[0], backward.c_str());
  65. return 2;
  66. }
  67. }
  68. }
  69. #ifdef _WIN32
  70. std::wstring_convert<typename std::codecvt_utf8<char16_t>, char16_t> u16converter;
  71. for (char16_t ch = 0x0000; ch < 0xffff; ++ch) {
  72. std::u16string u16str(1, ch);
  73. std::string str = u16converter.to_bytes(u16str);
  74. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  75. if (tokens.size() == 1) {
  76. fprintf(stderr, "%s : info: %s tokenized to %d \n",
  77. __func__, str.c_str(), tokens[0]);
  78. }
  79. }
  80. std::wstring_convert<typename std::codecvt_utf8<char32_t>, char32_t> u32converter;
  81. for (char32_t ch = 0x0000; ch < 0x0010ffff; ++ch) {
  82. std::u32string u32str(1, ch);
  83. std::string str = u32converter.to_bytes(u32str);
  84. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  85. if (tokens.size() == 1) {
  86. fprintf(stderr, "%s : info: %s tokenized to %d \n", __func__, str.c_str(), tokens[0]);
  87. }
  88. }
  89. #endif
  90. llama_free_model(model);
  91. llama_free(ctx);
  92. llama_backend_free();
  93. return 0;
  94. }