test-tokenizer-0.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "utils.h"
  2. #include <cstdio>
  3. #include <string>
  4. #include <map>
  5. static const std::map<std::string, std::vector<llama_vocab::id>> k_tests = {
  6. { "Hello World", { 1, 10994, 2787, }, },
  7. { " Hello World", { 1, 15043, 2787, }, },
  8. { " Hello World!", { 1, 15043, 2787, 29991, }, },
  9. { " this is 🦙.cpp", { 1, 445, 338, 29871, 243, 162, 169, 156, 29889, 8223, }, },
  10. { "w048 7tuijk dsdfhu", { 1, 29893, 29900, 29946, 29947, 29871, 29955, 9161, 13535, 18031, 2176, 6905, }, },
  11. { "нещо на Български", { 1, 821, 4851, 665, 1386, 29713, 1305, }, },
  12. };
  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_vocab vocab;
  21. if (!llama_vocab_load(fname, vocab)) {
  22. fprintf(stderr, "%s : failed to load vocab from: '%s'\n", __func__, fname.c_str());
  23. return 1;
  24. }
  25. const int n_vocab = vocab.id_to_token.size();
  26. if (n_vocab != 32000) {
  27. fprintf(stderr, "%s : expected 32000 tokens, got %d\n", __func__, n_vocab);
  28. return 2;
  29. }
  30. for (const auto & test_kv : k_tests) {
  31. const auto res = llama_tokenize(vocab, test_kv.first, true);
  32. bool correct = res.size() == test_kv.second.size();
  33. for (int i = 0; i < (int) res.size() && correct; ++i) {
  34. if (res[i] != test_kv.second[i]) {
  35. correct = false;
  36. }
  37. }
  38. if (!correct) {
  39. fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
  40. fprintf(stderr, "%s : expected tokens: ", __func__);
  41. for (const auto & t : test_kv.second) {
  42. fprintf(stderr, "%6d, ", t);
  43. }
  44. fprintf(stderr, "\n");
  45. fprintf(stderr, "%s : got tokens: ", __func__);
  46. for (const auto & t : res) {
  47. fprintf(stderr, "%6d, ", t);
  48. }
  49. fprintf(stderr, "\n");
  50. return 3;
  51. }
  52. }
  53. return 0;
  54. }