test-tokenizer-1-llama.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "llama.h"
  2. #include "common.h"
  3. #include "unicode.h"
  4. #include "console.h"
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <string>
  9. #include <codecvt>
  10. #include <map>
  11. #include <vector>
  12. #include <locale>
  13. int main(int argc, char **argv) {
  14. if (argc < 2) {
  15. fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]);
  16. return 1;
  17. }
  18. const std::string fname = argv[1];
  19. fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
  20. llama_model * model;
  21. llama_context * ctx;
  22. llama_backend_init(false);
  23. // load the vocab
  24. {
  25. auto mparams = llama_model_default_params();
  26. mparams.vocab_only = true;
  27. model = llama_load_model_from_file(fname.c_str(), mparams);
  28. if (model == NULL) {
  29. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  30. return 1;
  31. }
  32. auto cparams = llama_context_default_params();
  33. ctx = llama_new_context_with_model(model, cparams);
  34. if (ctx == NULL) {
  35. fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
  36. llama_free_model(model);
  37. return 1;
  38. }
  39. }
  40. GGML_ASSERT(llama_vocab_type(model) == LLAMA_VOCAB_TYPE_SPM);
  41. #ifdef _WIN32
  42. // We need this for unicode console support
  43. console::init(false, false);
  44. atexit([]() { console::cleanup(); });
  45. #endif
  46. const int n_vocab = llama_n_vocab(model);
  47. for (int i = 0; i < n_vocab; ++i) {
  48. std::string str = llama_detokenize_spm(ctx, std::vector<int>(1, i));
  49. std::vector<llama_token> tokens = llama_tokenize(ctx, str, false);
  50. std::string check = llama_detokenize_spm(ctx, tokens);
  51. if (check != str) {
  52. fprintf(stderr, "%s : error: token %d detokenizes to '%s'(%zu) but tokenization of this detokenizes to '%s'(%zu)\n",
  53. __func__, i, str.c_str(), str.length(), check.c_str(), check.length());
  54. return 2;
  55. }
  56. }
  57. for (uint32_t cp = 0x0000; cp < 0xffff; ++cp) {
  58. if (cp < 0xd800 || cp > 0xdfff) {
  59. std::string str = codepoint_to_utf8(cp);
  60. std::vector<llama_token> tokens = llama_tokenize(ctx, str, false);
  61. std::string check = llama_detokenize_spm(ctx, tokens);
  62. if (cp != 9601 && str != check) {
  63. fprintf(stderr, "%s : error: codepoint %d detokenizes to '%s'(%zu) instead of '%s'(%zu)\n",
  64. __func__, cp, check.c_str(), check.length(), str.c_str(), str.length());
  65. return 3;
  66. }
  67. }
  68. }
  69. for (uint32_t cp = 0x10000; cp < 0x0010ffff; ++cp) {
  70. std::string str = codepoint_to_utf8(cp);
  71. std::vector<llama_token> tokens = llama_tokenize(ctx, str, false);
  72. std::string check = llama_detokenize_spm(ctx, tokens);
  73. if (str != check) {
  74. fprintf(stderr, "%s : error: codepoint %d detokenizes to '%s'(%zu) instead of '%s'(%zu)\n",
  75. __func__, cp, check.c_str(), check.length(), str.c_str(), str.length());
  76. return 4;
  77. }
  78. }
  79. llama_free_model(model);
  80. llama_free(ctx);
  81. llama_backend_free();
  82. return 0;
  83. }