test-tokenizer-1.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. int main(int argc, char **argv) {
  23. if (argc < 2) {
  24. fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]);
  25. return 1;
  26. }
  27. const std::string fname = argv[1];
  28. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  29. llama_model * model;
  30. llama_context * ctx;
  31. llama_backend_init(false);
  32. // load the vocab
  33. {
  34. auto lparams = llama_context_default_params();
  35. lparams.vocab_only = true;
  36. model = llama_load_model_from_file(fname.c_str(), lparams);
  37. if (model == NULL) {
  38. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  39. return 1;
  40. }
  41. ctx = llama_new_context_with_model(model, lparams);
  42. if (ctx == NULL) {
  43. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  44. llama_free_model(model);
  45. return 1;
  46. }
  47. }
  48. GGML_ASSERT(llama_vocab_type(ctx) == LLAMA_VOCAB_TYPE_BPE);
  49. const int n_vocab = llama_n_vocab(ctx);
  50. for (int i = 0; i < n_vocab; ++i) {
  51. std::string forward = llama_token_to_piece(ctx, i);
  52. std::vector<llama_token> tokens = llama_tokenize(ctx, forward, false);
  53. if (tokens.size() == 1) {
  54. if (i != tokens[0]) {
  55. std::string backward = llama_token_to_piece(ctx, tokens[0]);
  56. fprintf(stderr, "%s : error: token %d is string %s but bpe returns token %d %s\n",
  57. __func__, i, llama_token_to_piece(ctx, i).c_str(), tokens[0], backward.c_str());
  58. return 2;
  59. }
  60. }
  61. }
  62. #ifdef _WIN32
  63. std::wstring_convert<typename std::codecvt_utf8<char16_t>, char16_t> u16converter;
  64. for (char16_t ch = 0x0000; ch < 0xffff; ++ch) {
  65. std::u16string u16str(1, ch);
  66. std::string str = u16converter.to_bytes(u16str);
  67. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  68. if (tokens.size() == 1) {
  69. fprintf(stderr, "%s : info: %s tokenized to %d \n",
  70. __func__, str.c_str(), tokens[0]);
  71. }
  72. }
  73. std::wstring_convert<typename std::codecvt_utf8<char32_t>, char32_t> u32converter;
  74. for (char32_t ch = 0x0000; ch < 0x0010ffff; ++ch) {
  75. std::u32string u32str(1, ch);
  76. std::string str = u32converter.to_bytes(u32str);
  77. std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
  78. if (tokens.size() == 1) {
  79. fprintf(stderr, "%s : info: %s tokenized to %d \n", __func__, str.c_str(), tokens[0]);
  80. }
  81. }
  82. #endif
  83. llama_free_model(model);
  84. llama_free(ctx);
  85. llama_backend_free();
  86. return 0;
  87. }