llama-vocab.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "llama.h"
  28. #include <string>
  29. #include <vector>
  30. #include <unordered_map>
  31. #include <map>
  32. #include <set>
  33. static const char * llama_model_vocab_type_name(enum llama_vocab_type type){
  34. switch (type) {
  35. case LLAMA_VOCAB_TYPE_NONE: return "no vocab";
  36. case LLAMA_VOCAB_TYPE_SPM: return "SPM";
  37. case LLAMA_VOCAB_TYPE_BPE: return "BPE";
  38. case LLAMA_VOCAB_TYPE_WPM: return "WPM";
  39. case LLAMA_VOCAB_TYPE_UGM: return "UGM";
  40. case LLAMA_VOCAB_TYPE_RWKV: return "RWKV";
  41. default: return "unknown";
  42. }
  43. }
  44. struct llm_tokenizer;
  45. struct llama_vocab {
  46. using id = llama_token;
  47. using token = std::string;
  48. using tattr = llama_token_attr;
  49. struct token_data {
  50. token text;
  51. float score;
  52. tattr attr;
  53. };
  54. uint32_t n_vocab = 0; // TODO: not great because has to keep in sync with hparams.n_vocab
  55. enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
  56. enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
  57. int max_token_len = 0; // used for optimizing longest token search
  58. std::unordered_map<token, id> token_to_id;
  59. std::vector<token_data> id_to_token;
  60. std::vector<id> cache_special_tokens;
  61. std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
  62. std::map<std::pair<std::string, std::string>, int> bpe_ranks;
  63. // default LLaMA special tokens
  64. // TODO: should we set all of these to LLAMA_TOKEN_NULL?
  65. id special_bos_id = 1;
  66. id special_eos_id = 2;
  67. id special_eot_id = LLAMA_TOKEN_NULL;
  68. id special_eom_id = LLAMA_TOKEN_NULL;
  69. id special_unk_id = 0;
  70. id special_sep_id = LLAMA_TOKEN_NULL;
  71. id special_pad_id = LLAMA_TOKEN_NULL;
  72. id special_cls_id = LLAMA_TOKEN_NULL; // TODO: revisit if this is really needed https://github.com/ggerganov/llama.cpp/pull/10930
  73. id special_mask_id = LLAMA_TOKEN_NULL;
  74. id linefeed_id = 13;
  75. // fim tokens
  76. id special_fim_pre_id = LLAMA_TOKEN_NULL;
  77. id special_fim_suf_id = LLAMA_TOKEN_NULL;
  78. id special_fim_mid_id = LLAMA_TOKEN_NULL;
  79. id special_fim_pad_id = LLAMA_TOKEN_NULL;
  80. id special_fim_rep_id = LLAMA_TOKEN_NULL; // repo
  81. id special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
  82. // set of all tokens that cause "end of generation"
  83. std::set<id> special_eog_ids;
  84. // tokenizer flags
  85. bool tokenizer_add_space_prefix = false;
  86. bool tokenizer_add_bos = false;
  87. bool tokenizer_add_eos = false;
  88. bool tokenizer_ignore_merges = false;
  89. bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces
  90. bool tokenizer_remove_extra_whitespaces = false;
  91. bool tokenizer_escape_whitespaces = true;
  92. bool tokenizer_treat_whitespace_as_suffix = false;
  93. std::vector<char> precompiled_charsmap;
  94. llm_tokenizer * tokenizer = nullptr;
  95. llama_vocab() = default;
  96. ~llama_vocab();
  97. int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
  98. void init_tokenizer();
  99. };
  100. //
  101. // internal API
  102. //
  103. // TODO: rename to llama_tokenize_impl
  104. // TODO: This should probably be in llama.h
  105. std::vector<llama_vocab::id> llama_tokenize_internal(
  106. const llama_vocab & vocab,
  107. std::string raw_text,
  108. bool add_special,
  109. bool parse_special = false);
  110. // TODO: move the API below as member functions of llama_vocab
  111. llama_token llama_byte_to_token_impl(const llama_vocab & vocab, uint8_t ch);
  112. const char * llama_token_get_text_impl(const struct llama_vocab & vocab, llama_token token);
  113. float llama_token_get_score_impl(const struct llama_vocab & vocab, llama_token token);
  114. llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, llama_token token);
  115. bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token);
  116. bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token);
  117. llama_token llama_token_bos_impl(const struct llama_vocab & vocab);
  118. llama_token llama_token_eos_impl(const struct llama_vocab & vocab);
  119. llama_token llama_token_eot_impl(const struct llama_vocab & vocab);
  120. llama_token llama_token_eom_impl(const struct llama_vocab & vocab);
  121. llama_token llama_token_cls_impl(const struct llama_vocab & vocab);
  122. llama_token llama_token_sep_impl(const struct llama_vocab & vocab);
  123. llama_token llama_token_nl_impl (const struct llama_vocab & vocab);
  124. llama_token llama_token_pad_impl(const struct llama_vocab & vocab);
  125. llama_token llama_token_prefix_impl(const struct llama_vocab & vocab);
  126. llama_token llama_token_middle_impl(const struct llama_vocab & vocab);
  127. llama_token llama_token_suffix_impl(const struct llama_vocab & vocab);
  128. llama_token llama_token_fim_pre_impl(const struct llama_vocab & vocab);
  129. llama_token llama_token_fim_suf_impl(const struct llama_vocab & vocab);
  130. llama_token llama_token_fim_mid_impl(const struct llama_vocab & vocab);
  131. llama_token llama_token_fim_pad_impl(const struct llama_vocab & vocab);
  132. llama_token llama_token_fim_rep_impl(const struct llama_vocab & vocab);
  133. llama_token llama_token_fim_sep_impl(const struct llama_vocab & vocab);
  134. bool llama_add_bos_token_impl(const struct llama_vocab & vocab);
  135. bool llama_add_eos_token_impl(const struct llama_vocab & vocab);
  136. int32_t llama_tokenize_impl(
  137. const struct llama_vocab & vocab,
  138. const char * text,
  139. int32_t text_len,
  140. llama_token * tokens,
  141. int32_t n_tokens_max,
  142. bool add_special,
  143. bool parse_special);
  144. // does not write null-terminator to buf
  145. int32_t llama_token_to_piece_impl(
  146. const struct llama_vocab & vocab,
  147. llama_token token,
  148. char * buf,
  149. int32_t length,
  150. int32_t lstrip,
  151. bool special);
  152. // check if token0 is contained as a prefix in token1
  153. bool llama_token_is_prefix_impl(
  154. const struct llama_vocab & vocab,
  155. llama_token token0,
  156. llama_token token1);
  157. int32_t llama_detokenize_impl(
  158. const struct llama_vocab & vocab,
  159. const llama_token * tokens,
  160. int32_t n_tokens,
  161. char * text,
  162. int32_t text_len_max,
  163. bool remove_special,
  164. bool unparse_special);
  165. std::string llama_detokenize(
  166. const struct llama_vocab & vocab,
  167. const std::vector<llama_token> & tokens,
  168. bool special);