llama-vocab.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include "llama.h"
  3. #include <string>
  4. #include <vector>
  5. #include <memory>
  6. // pre-tokenization types
  7. enum llama_vocab_pre_type {
  8. LLAMA_VOCAB_PRE_TYPE_DEFAULT = 0,
  9. LLAMA_VOCAB_PRE_TYPE_LLAMA3 = 1,
  10. LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_LLM = 2,
  11. LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER = 3,
  12. LLAMA_VOCAB_PRE_TYPE_FALCON = 4,
  13. LLAMA_VOCAB_PRE_TYPE_MPT = 5,
  14. LLAMA_VOCAB_PRE_TYPE_STARCODER = 6,
  15. LLAMA_VOCAB_PRE_TYPE_GPT2 = 7,
  16. LLAMA_VOCAB_PRE_TYPE_REFACT = 8,
  17. LLAMA_VOCAB_PRE_TYPE_COMMAND_R = 9,
  18. LLAMA_VOCAB_PRE_TYPE_STABLELM2 = 10,
  19. LLAMA_VOCAB_PRE_TYPE_QWEN2 = 11,
  20. LLAMA_VOCAB_PRE_TYPE_OLMO = 12,
  21. LLAMA_VOCAB_PRE_TYPE_DBRX = 13,
  22. LLAMA_VOCAB_PRE_TYPE_SMAUG = 14,
  23. LLAMA_VOCAB_PRE_TYPE_PORO = 15,
  24. LLAMA_VOCAB_PRE_TYPE_CHATGLM3 = 16,
  25. LLAMA_VOCAB_PRE_TYPE_CHATGLM4 = 17,
  26. LLAMA_VOCAB_PRE_TYPE_VIKING = 18,
  27. LLAMA_VOCAB_PRE_TYPE_JAIS = 19,
  28. LLAMA_VOCAB_PRE_TYPE_TEKKEN = 20,
  29. LLAMA_VOCAB_PRE_TYPE_SMOLLM = 21,
  30. LLAMA_VOCAB_PRE_TYPE_CODESHELL = 22,
  31. LLAMA_VOCAB_PRE_TYPE_BLOOM = 23,
  32. LLAMA_VOCAB_PRE_TYPE_GPT3_FINNISH = 24,
  33. LLAMA_VOCAB_PRE_TYPE_EXAONE = 25,
  34. LLAMA_VOCAB_PRE_TYPE_CHAMELEON = 26,
  35. LLAMA_VOCAB_PRE_TYPE_MINERVA = 27,
  36. LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM = 28,
  37. LLAMA_VOCAB_PRE_TYPE_GPT4O = 29,
  38. LLAMA_VOCAB_PRE_TYPE_SUPERBPE = 30,
  39. LLAMA_VOCAB_PRE_TYPE_TRILLION = 31,
  40. LLAMA_VOCAB_PRE_TYPE_BAILINGMOE = 32,
  41. LLAMA_VOCAB_PRE_TYPE_LLAMA4 = 33,
  42. LLAMA_VOCAB_PRE_TYPE_PIXTRAL = 34,
  43. LLAMA_VOCAB_PRE_TYPE_SEED_CODER = 35,
  44. LLAMA_VOCAB_PRE_TYPE_HUNYUAN = 36,
  45. LLAMA_VOCAB_PRE_TYPE_KIMI_K2 = 37,
  46. LLAMA_VOCAB_PRE_TYPE_HUNYUAN_DENSE = 38,
  47. };
  48. struct LLM_KV;
  49. struct llama_model_loader;
  50. struct llama_vocab {
  51. struct token_data {
  52. std::string text;
  53. float score;
  54. llama_token_attr attr;
  55. };
  56. llama_vocab();
  57. ~llama_vocab();
  58. void load(llama_model_loader & ml, const LLM_KV & kv);
  59. std::string get_tokenizer_model() const;
  60. std::string get_tokenizer_pre() const;
  61. enum llama_vocab_type get_type() const;
  62. enum llama_vocab_pre_type get_pre_type() const;
  63. uint32_t n_tokens() const;
  64. uint32_t n_token_types() const;
  65. std::string type_name() const;
  66. bool is_normal (llama_token id) const;
  67. bool is_unknown (llama_token id) const;
  68. bool is_control (llama_token id) const;
  69. bool is_byte (llama_token id) const;
  70. bool is_user_defined(llama_token id) const;
  71. bool is_unused (llama_token id) const;
  72. bool is_eog (llama_token id) const;
  73. uint8_t token_to_byte(llama_token id) const;
  74. llama_token byte_to_token(uint8_t ch) const;
  75. llama_token text_to_token(const std::string & text) const;
  76. const token_data & get_token_data(llama_token id) const;
  77. const char * token_get_text (llama_token id) const;
  78. float token_get_score(llama_token id) const;
  79. llama_token_attr token_get_attr (llama_token id) const;
  80. llama_token token_bos() const;
  81. llama_token token_eos() const;
  82. llama_token token_eot() const;
  83. llama_token token_eom() const;
  84. llama_token token_unk() const;
  85. llama_token token_sep() const;
  86. llama_token token_nl () const;
  87. llama_token token_pad() const;
  88. llama_token token_mask() const;
  89. llama_token token_prefix() const;
  90. llama_token token_middle() const;
  91. llama_token token_suffix() const;
  92. llama_token token_fim_pre() const;
  93. llama_token token_fim_suf() const;
  94. llama_token token_fim_mid() const;
  95. llama_token token_fim_pad() const;
  96. llama_token token_fim_rep() const;
  97. llama_token token_fim_sep() const;
  98. bool get_add_space_prefix () const;
  99. bool get_add_bos () const;
  100. bool get_add_eos () const;
  101. bool get_add_sep () const;
  102. bool get_ignore_merges () const;
  103. bool get_clean_spaces () const;
  104. bool get_remove_extra_whitespaces () const;
  105. bool get_escape_whitespaces () const;
  106. bool get_treat_whitespace_as_suffix() const;
  107. int max_token_len() const;
  108. int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
  109. std::vector<std::string> get_bpe_merges() const;
  110. std::vector<char> get_precompiled_charsmap() const;
  111. int32_t tokenize(
  112. const char * text,
  113. int32_t text_len,
  114. llama_token * tokens,
  115. int32_t n_tokens_max,
  116. bool add_special,
  117. bool parse_special) const;
  118. std::vector<llama_token> tokenize(
  119. const std::string & raw_text,
  120. bool add_special,
  121. bool parse_special = false) const;
  122. // does not write null-terminator to buf
  123. int32_t token_to_piece(
  124. llama_token token,
  125. char * buf,
  126. int32_t length,
  127. int32_t lstrip,
  128. bool special) const;
  129. // use cached data
  130. const std::string & token_to_piece(llama_token token) const;
  131. int32_t detokenize(
  132. const llama_token * tokens,
  133. int32_t n_tokens,
  134. char * text,
  135. int32_t text_len_max,
  136. bool remove_special,
  137. bool unparse_special) const;
  138. std::string detokenize(
  139. const std::vector<llama_token> & tokens,
  140. bool special) const;
  141. void print_info() const;
  142. private:
  143. struct impl;
  144. std::unique_ptr<impl> pimpl;
  145. };