llama-vocab.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "llama.h"
  3. #include <string>
  4. #include <vector>
  5. #include <memory>
  6. struct LLM_KV;
  7. struct llama_model_loader;
  8. struct llama_vocab {
  9. struct token_data {
  10. std::string text;
  11. float score;
  12. llama_token_attr attr;
  13. };
  14. llama_vocab();
  15. ~llama_vocab();
  16. void load(llama_model_loader & ml, const LLM_KV & kv);
  17. enum llama_vocab_type get_type() const;
  18. enum llama_vocab_pre_type get_pre_type() const;
  19. uint32_t n_tokens() const;
  20. uint32_t n_token_types() const;
  21. std::string type_name() const;
  22. bool is_normal (llama_token id) const;
  23. bool is_unknown (llama_token id) const;
  24. bool is_control (llama_token id) const;
  25. bool is_byte (llama_token id) const;
  26. bool is_user_defined(llama_token id) const;
  27. bool is_unused (llama_token id) const;
  28. bool is_eog (llama_token id) const;
  29. uint8_t token_to_byte(llama_token id) const;
  30. llama_token byte_to_token(uint8_t ch) const;
  31. llama_token text_to_token(const std::string & text) const;
  32. const token_data & get_token_data(llama_token id) const;
  33. const char * token_get_text (llama_token id) const;
  34. float token_get_score(llama_token id) const;
  35. llama_token_attr token_get_attr (llama_token id) const;
  36. llama_token token_bos() const;
  37. llama_token token_eos() const;
  38. llama_token token_eot() const;
  39. llama_token token_eom() const;
  40. llama_token token_unk() const;
  41. llama_token token_sep() const;
  42. llama_token token_nl () const;
  43. llama_token token_pad() const;
  44. llama_token token_prefix() const;
  45. llama_token token_middle() const;
  46. llama_token token_suffix() const;
  47. llama_token token_fim_pre() const;
  48. llama_token token_fim_suf() const;
  49. llama_token token_fim_mid() const;
  50. llama_token token_fim_pad() const;
  51. llama_token token_fim_rep() const;
  52. llama_token token_fim_sep() const;
  53. bool get_add_space_prefix () const;
  54. bool get_add_bos () const;
  55. bool get_add_eos () const;
  56. bool get_ignore_merges () const;
  57. bool get_clean_spaces () const;
  58. bool get_remove_extra_whitespaces () const;
  59. bool get_escape_whitespaces () const;
  60. bool get_treat_whitespace_as_suffix() const;
  61. int max_token_len() const;
  62. int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
  63. int32_t tokenize(
  64. const char * text,
  65. int32_t text_len,
  66. llama_token * tokens,
  67. int32_t n_tokens_max,
  68. bool add_special,
  69. bool parse_special) const;
  70. std::vector<llama_token> tokenize(
  71. const std::string & raw_text,
  72. bool add_special,
  73. bool parse_special = false) const;
  74. // does not write null-terminator to buf
  75. int32_t token_to_piece(
  76. llama_token token,
  77. char * buf,
  78. int32_t length,
  79. int32_t lstrip,
  80. bool special) const;
  81. // use cached data
  82. const std::string & token_to_piece(llama_token token) const;
  83. int32_t detokenize(
  84. const llama_token * tokens,
  85. int32_t n_tokens,
  86. char * text,
  87. int32_t text_len_max,
  88. bool remove_special,
  89. bool unparse_special) const;
  90. std::string detokenize(
  91. const std::vector<llama_token> & tokens,
  92. bool special) const;
  93. void print_info() const;
  94. private:
  95. struct impl;
  96. std::unique_ptr<impl> pimpl;
  97. };