llama-vocab.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - 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-impl.h"
  28. #include <string>
  29. #include <vector>
  30. #include <unordered_map>
  31. #include <map>
  32. #include <set>
  33. struct llm_tokenizer;
  34. struct llama_vocab {
  35. using id = llama_token;
  36. using token = std::string;
  37. using tattr = llama_token_attr;
  38. struct token_data {
  39. token text;
  40. float score;
  41. tattr attr;
  42. };
  43. uint32_t n_vocab = 0; // TODO: not great because has to keep in sync with hparams.n_vocab
  44. enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
  45. enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
  46. int max_token_len = 0; // used for optimizing longest token search
  47. std::unordered_map<token, id> token_to_id;
  48. std::vector<token_data> id_to_token;
  49. std::vector<id> cache_special_tokens;
  50. std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
  51. std::map<std::pair<std::string, std::string>, int> bpe_ranks;
  52. // default LLaMA special tokens
  53. id special_bos_id = 1;
  54. id special_eos_id = 2;
  55. id special_unk_id = 0;
  56. id special_sep_id = -1;
  57. id special_pad_id = -1;
  58. id special_cls_id = -1;
  59. id special_mask_id = -1;
  60. id linefeed_id = 13;
  61. id special_prefix_id = -1;
  62. id special_suffix_id = -1;
  63. id special_middle_id = -1;
  64. id special_eot_id = -1; // TODO: move above after "eos_id", and here add "file separator" token
  65. id special_eom_id = -1;
  66. // set of all tokens that cause "end of generation"
  67. std::set<id> special_eog_ids;
  68. // tokenizer flags
  69. bool tokenizer_add_space_prefix = false;
  70. bool tokenizer_add_bos = false;
  71. bool tokenizer_add_eos = false;
  72. bool tokenizer_ignore_merges = false;
  73. bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces
  74. bool tokenizer_remove_extra_whitespaces = false;
  75. bool tokenizer_escape_whitespaces = true;
  76. bool tokenizer_treat_whitespace_as_suffix = false;
  77. std::vector<char> precompiled_charsmap;
  78. llm_tokenizer * tokenizer = nullptr;
  79. llama_vocab() = default;
  80. ~llama_vocab();
  81. int find_bpe_rank(const std::string & token_left, const std::string & token_right) const;
  82. void init_tokenizer();
  83. };
  84. //
  85. // internal API
  86. //
  87. // TODO: rename to llama_tokenize_impl
  88. // TODO: This should probably be in llama.h
  89. std::vector<llama_vocab::id> llama_tokenize_internal(
  90. const llama_vocab & vocab,
  91. std::string raw_text,
  92. bool add_special,
  93. bool parse_special = false);
  94. // TODO: move the API below as member functions of llama_vocab
  95. llama_token llama_byte_to_token_impl(const llama_vocab & vocab, uint8_t ch);
  96. const char * llama_token_get_text_impl(const struct llama_vocab & vocab, llama_token token);
  97. float llama_token_get_score_impl(const struct llama_vocab & vocab, llama_token token);
  98. llama_token_attr llama_token_get_attr_impl(const struct llama_vocab & vocab, llama_token token);
  99. bool llama_token_is_eog_impl(const struct llama_vocab & vocab, llama_token token);
  100. bool llama_token_is_control_impl(const struct llama_vocab & vocab, llama_token token);
  101. llama_token llama_token_bos_impl(const struct llama_vocab & vocab);
  102. llama_token llama_token_eos_impl(const struct llama_vocab & vocab);
  103. llama_token llama_token_cls_impl(const struct llama_vocab & vocab);
  104. llama_token llama_token_sep_impl(const struct llama_vocab & vocab);
  105. llama_token llama_token_nl_impl (const struct llama_vocab & vocab);
  106. llama_token llama_token_pad_impl(const struct llama_vocab & vocab);
  107. bool llama_add_bos_token_impl(const struct llama_vocab & vocab);
  108. bool llama_add_eos_token_impl(const struct llama_vocab & vocab);
  109. llama_token llama_token_prefix_impl(const struct llama_vocab & vocab);
  110. llama_token llama_token_middle_impl(const struct llama_vocab & vocab);
  111. llama_token llama_token_suffix_impl(const struct llama_vocab & vocab);
  112. llama_token llama_token_eot_impl (const struct llama_vocab & vocab);
  113. llama_token llama_token_eom_impl (const struct llama_vocab & vocab);
  114. int32_t llama_tokenize_impl(
  115. const struct llama_vocab & vocab,
  116. const char * text,
  117. int32_t text_len,
  118. llama_token * tokens,
  119. int32_t n_tokens_max,
  120. bool add_special,
  121. bool parse_special);
  122. // does not write null-terminator to buf
  123. int32_t llama_token_to_piece_impl(
  124. const struct llama_vocab & vocab,
  125. llama_token token,
  126. char * buf,
  127. int32_t length,
  128. int32_t lstrip,
  129. bool special);
  130. int32_t llama_detokenize_impl(
  131. const struct llama_vocab & vocab,
  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);