base_tokenizer.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. from typing import Dict, List, Optional, Tuple, Union
  2. from tokenizers import AddedToken, EncodeInput, Encoding, InputSequence, Tokenizer
  3. from tokenizers.decoders import Decoder
  4. from tokenizers.models import Model
  5. from tokenizers.normalizers import Normalizer
  6. from tokenizers.pre_tokenizers import PreTokenizer
  7. from tokenizers.processors import PostProcessor
  8. Offsets = Tuple[int, int]
  9. class BaseTokenizer:
  10. def __init__(self, tokenizer: Tokenizer, parameters=None):
  11. self._tokenizer = tokenizer
  12. self._parameters = parameters if parameters is not None else {}
  13. def __repr__(self):
  14. return "Tokenizer(vocabulary_size={}, {})".format(
  15. self._tokenizer.get_vocab_size(),
  16. ", ".join(k + "=" + str(v) for k, v in self._parameters.items()),
  17. )
  18. def num_special_tokens_to_add(self, is_pair: bool) -> int:
  19. """
  20. Return the number of special tokens that would be added for single/pair sentences.
  21. :param is_pair: Boolean indicating if the input would be a single sentence or a pair
  22. :return:
  23. """
  24. return self._tokenizer.num_special_tokens_to_add(is_pair)
  25. def get_vocab(self, with_added_tokens: bool = True) -> Dict[str, int]:
  26. """Returns the vocabulary
  27. Args:
  28. with_added_tokens: boolean:
  29. Whether to include the added tokens in the vocabulary
  30. Returns:
  31. The vocabulary
  32. """
  33. return self._tokenizer.get_vocab(with_added_tokens=with_added_tokens)
  34. def get_added_tokens_decoder(self) -> Dict[int, AddedToken]:
  35. """Returns the added reverse vocabulary
  36. Returns:
  37. The added vocabulary mapping ints to AddedTokens
  38. """
  39. return self._tokenizer.get_added_tokens_decoder()
  40. def get_vocab_size(self, with_added_tokens: bool = True) -> int:
  41. """Return the size of vocabulary, with or without added tokens.
  42. Args:
  43. with_added_tokens: (`optional`) bool:
  44. Whether to count in added special tokens or not
  45. Returns:
  46. Size of vocabulary
  47. """
  48. return self._tokenizer.get_vocab_size(with_added_tokens=with_added_tokens)
  49. def enable_padding(
  50. self,
  51. direction: Optional[str] = "right",
  52. pad_to_multiple_of: Optional[int] = None,
  53. pad_id: Optional[int] = 0,
  54. pad_type_id: Optional[int] = 0,
  55. pad_token: Optional[str] = "[PAD]",
  56. length: Optional[int] = None,
  57. ):
  58. """Change the padding strategy
  59. Args:
  60. direction: (`optional`) str:
  61. Can be one of: `right` or `left`
  62. pad_to_multiple_of: (`optional`) unsigned int:
  63. If specified, the padding length should always snap to the next multiple of
  64. the given value. For example if we were going to pad with a length of 250 but
  65. `pad_to_multiple_of=8` then we will pad to 256.
  66. pad_id: (`optional`) unsigned int:
  67. The indice to be used when padding
  68. pad_type_id: (`optional`) unsigned int:
  69. The type indice to be used when padding
  70. pad_token: (`optional`) str:
  71. The pad token to be used when padding
  72. length: (`optional`) unsigned int:
  73. If specified, the length at which to pad. If not specified
  74. we pad using the size of the longest sequence in a batch
  75. """
  76. return self._tokenizer.enable_padding(
  77. direction=direction,
  78. pad_to_multiple_of=pad_to_multiple_of,
  79. pad_id=pad_id,
  80. pad_type_id=pad_type_id,
  81. pad_token=pad_token,
  82. length=length,
  83. )
  84. def no_padding(self):
  85. """Disable padding"""
  86. return self._tokenizer.no_padding()
  87. @property
  88. def padding(self) -> Optional[dict]:
  89. """Get the current padding parameters
  90. Returns:
  91. None if padding is disabled, a dict with the currently set parameters
  92. if the padding is enabled.
  93. """
  94. return self._tokenizer.padding
  95. def enable_truncation(self, max_length: int, stride: Optional[int] = 0, strategy: Optional[str] = "longest_first"):
  96. """Change the truncation options
  97. Args:
  98. max_length: unsigned int:
  99. The maximum length at which to truncate
  100. stride: (`optional`) unsigned int:
  101. The length of the previous first sequence to be included
  102. in the overflowing sequence
  103. strategy: (`optional`) str:
  104. Can be one of `longest_first`, `only_first` or `only_second`
  105. """
  106. return self._tokenizer.enable_truncation(max_length, stride=stride, strategy=strategy)
  107. def no_truncation(self):
  108. """Disable truncation"""
  109. return self._tokenizer.no_truncation()
  110. @property
  111. def truncation(self) -> Optional[dict]:
  112. """Get the current truncation parameters
  113. Returns:
  114. None if truncation is disabled, a dict with the current truncation parameters if
  115. truncation is enabled
  116. """
  117. return self._tokenizer.truncation
  118. def add_tokens(self, tokens: List[Union[str, AddedToken]]) -> int:
  119. """Add the given tokens to the vocabulary
  120. Args:
  121. tokens: List[Union[str, AddedToken]]:
  122. A list of tokens to add to the vocabulary. Each token can either be
  123. a string, or an instance of AddedToken
  124. Returns:
  125. The number of tokens that were added to the vocabulary
  126. """
  127. return self._tokenizer.add_tokens(tokens)
  128. def add_special_tokens(self, special_tokens: List[Union[str, AddedToken]]) -> int:
  129. """Add the given special tokens to the vocabulary, and treat them as special tokens.
  130. The special tokens will never be processed by the model, and will be
  131. removed while decoding.
  132. Args:
  133. tokens: List[Union[str, AddedToken]]:
  134. A list of special tokens to add to the vocabulary. Each token can either be
  135. a string, or an instance of AddedToken
  136. Returns:
  137. The number of tokens that were added to the vocabulary
  138. """
  139. return self._tokenizer.add_special_tokens(special_tokens)
  140. def normalize(self, sequence: str) -> str:
  141. """Normalize the given sequence
  142. Args:
  143. sequence: str:
  144. The sequence to normalize
  145. Returns:
  146. The normalized string
  147. """
  148. return self._tokenizer.normalizer.normalize_str(sequence)
  149. def encode(
  150. self,
  151. sequence: InputSequence,
  152. pair: Optional[InputSequence] = None,
  153. is_pretokenized: bool = False,
  154. add_special_tokens: bool = True,
  155. ) -> Encoding:
  156. """Encode the given sequence and pair. This method can process raw text sequences as well
  157. as already pre-tokenized sequences.
  158. Args:
  159. sequence: InputSequence:
  160. The sequence we want to encode. This sequence can be either raw text or
  161. pre-tokenized, according to the `is_pretokenized` argument:
  162. - If `is_pretokenized=False`: `InputSequence` is expected to be `str`
  163. - If `is_pretokenized=True`: `InputSequence` is expected to be
  164. `Union[List[str], Tuple[str]]`
  165. is_pretokenized: bool:
  166. Whether the input is already pre-tokenized.
  167. add_special_tokens: bool:
  168. Whether to add the special tokens while encoding.
  169. Returns:
  170. An Encoding
  171. """
  172. if sequence is None:
  173. raise ValueError("encode: `sequence` can't be `None`")
  174. return self._tokenizer.encode(sequence, pair, is_pretokenized, add_special_tokens)
  175. def encode_batch(
  176. self,
  177. inputs: List[EncodeInput],
  178. is_pretokenized: bool = False,
  179. add_special_tokens: bool = True,
  180. ) -> List[Encoding]:
  181. """Encode the given inputs. This method accept both raw text sequences as well as already
  182. pre-tokenized sequences.
  183. Args:
  184. inputs: List[EncodeInput]:
  185. A list of single sequences or pair sequences to encode. Each `EncodeInput` is
  186. expected to be of the following form:
  187. `Union[InputSequence, Tuple[InputSequence, InputSequence]]`
  188. Each `InputSequence` can either be raw text or pre-tokenized,
  189. according to the `is_pretokenized` argument:
  190. - If `is_pretokenized=False`: `InputSequence` is expected to be `str`
  191. - If `is_pretokenized=True`: `InputSequence` is expected to be
  192. `Union[List[str], Tuple[str]]`
  193. is_pretokenized: bool:
  194. Whether the input is already pre-tokenized.
  195. add_special_tokens: bool:
  196. Whether to add the special tokens while encoding.
  197. Returns:
  198. A list of Encoding
  199. """
  200. if inputs is None:
  201. raise ValueError("encode_batch: `inputs` can't be `None`")
  202. return self._tokenizer.encode_batch(inputs, is_pretokenized, add_special_tokens)
  203. async def async_encode_batch(
  204. self,
  205. inputs: List[EncodeInput],
  206. is_pretokenized: bool = False,
  207. add_special_tokens: bool = True,
  208. ) -> List[Encoding]:
  209. """Asynchronously encode a batch (tracks character offsets).
  210. Args:
  211. inputs: A list of single or pair sequences to encode.
  212. is_pretokenized: Whether inputs are already pre-tokenized.
  213. add_special_tokens: Whether to add special tokens.
  214. Returns:
  215. A list of Encoding.
  216. """
  217. if inputs is None:
  218. raise ValueError("async_encode_batch: `inputs` can't be `None`")
  219. # Exposed by the Rust bindings via pyo3_async_runtimes::tokio::future_into_py
  220. return await self._tokenizer.async_encode_batch(inputs, is_pretokenized, add_special_tokens)
  221. async def async_encode_batch_fast(
  222. self,
  223. inputs: List[EncodeInput],
  224. is_pretokenized: bool = False,
  225. add_special_tokens: bool = True,
  226. ) -> List[Encoding]:
  227. """Asynchronously encode a batch (no character offsets, faster).
  228. Args:
  229. inputs: A list of single or pair sequences to encode.
  230. is_pretokenized: Whether inputs are already pre-tokenized.
  231. add_special_tokens: Whether to add special tokens.
  232. Returns:
  233. A list of Encoding.
  234. """
  235. if inputs is None:
  236. raise ValueError("async_encode_batch_fast: `inputs` can't be `None`")
  237. return await self._tokenizer.async_encode_batch_fast(inputs, is_pretokenized, add_special_tokens)
  238. def decode(self, ids: List[int], skip_special_tokens: Optional[bool] = True) -> str:
  239. """Decode the given list of ids to a string sequence
  240. Args:
  241. ids: List[unsigned int]:
  242. A list of ids to be decoded
  243. skip_special_tokens: (`optional`) boolean:
  244. Whether to remove all the special tokens from the output string
  245. Returns:
  246. The decoded string
  247. """
  248. if ids is None:
  249. raise ValueError("None input is not valid. Should be a list of integers.")
  250. return self._tokenizer.decode(ids, skip_special_tokens=skip_special_tokens)
  251. def decode_batch(self, sequences: List[List[int]], skip_special_tokens: Optional[bool] = True) -> str:
  252. """Decode the list of sequences to a list of string sequences
  253. Args:
  254. sequences: List[List[unsigned int]]:
  255. A list of sequence of ids to be decoded
  256. skip_special_tokens: (`optional`) boolean:
  257. Whether to remove all the special tokens from the output strings
  258. Returns:
  259. A list of decoded strings
  260. """
  261. if sequences is None:
  262. raise ValueError("None input is not valid. Should be list of list of integers.")
  263. return self._tokenizer.decode_batch(sequences, skip_special_tokens=skip_special_tokens)
  264. def token_to_id(self, token: str) -> Optional[int]:
  265. """Convert the given token to its corresponding id
  266. Args:
  267. token: str:
  268. The token to convert
  269. Returns:
  270. The corresponding id if it exists, None otherwise
  271. """
  272. return self._tokenizer.token_to_id(token)
  273. def id_to_token(self, id: int) -> Optional[str]:
  274. """Convert the given token id to its corresponding string
  275. Args:
  276. token: id:
  277. The token id to convert
  278. Returns:
  279. The corresponding string if it exists, None otherwise
  280. """
  281. return self._tokenizer.id_to_token(id)
  282. def save_model(self, directory: str, prefix: Optional[str] = None):
  283. """Save the current model to the given directory
  284. Args:
  285. directory: str:
  286. A path to the destination directory
  287. prefix: (Optional) str:
  288. An optional prefix, used to prefix each file name
  289. """
  290. return self._tokenizer.model.save(directory, prefix=prefix)
  291. def save(self, path: str, pretty: bool = True):
  292. """Save the current Tokenizer at the given path
  293. Args:
  294. path: str:
  295. A path to the destination Tokenizer file
  296. """
  297. return self._tokenizer.save(path, pretty)
  298. def to_str(self, pretty: bool = False):
  299. """Get a serialized JSON version of the Tokenizer as a str
  300. Args:
  301. pretty: bool:
  302. Whether the JSON string should be prettified
  303. Returns:
  304. str
  305. """
  306. return self._tokenizer.to_str(pretty)
  307. def post_process(
  308. self, encoding: Encoding, pair: Optional[Encoding] = None, add_special_tokens: bool = True
  309. ) -> Encoding:
  310. """Apply all the post-processing steps to the given encodings.
  311. The various steps are:
  312. 1. Truncate according to global params (provided to `enable_truncation`)
  313. 2. Apply the PostProcessor
  314. 3. Pad according to global params. (provided to `enable_padding`)
  315. Args:
  316. encoding: Encoding:
  317. The main Encoding to post process
  318. pair: Optional[Encoding]:
  319. An optional pair Encoding
  320. add_special_tokens: bool:
  321. Whether to add special tokens
  322. Returns:
  323. The resulting Encoding
  324. """
  325. return self._tokenizer.post_process(encoding, pair, add_special_tokens)
  326. @property
  327. def model(self) -> Model:
  328. return self._tokenizer.model
  329. @model.setter
  330. def model(self, model: Model):
  331. self._tokenizer.model = model
  332. @property
  333. def normalizer(self) -> Normalizer:
  334. return self._tokenizer.normalizer
  335. @normalizer.setter
  336. def normalizer(self, normalizer: Normalizer):
  337. self._tokenizer.normalizer = normalizer
  338. @property
  339. def pre_tokenizer(self) -> PreTokenizer:
  340. return self._tokenizer.pre_tokenizer
  341. @pre_tokenizer.setter
  342. def pre_tokenizer(self, pre_tokenizer: PreTokenizer):
  343. self._tokenizer.pre_tokenizer = pre_tokenizer
  344. @property
  345. def post_processor(self) -> PostProcessor:
  346. return self._tokenizer.post_processor
  347. @post_processor.setter
  348. def post_processor(self, post_processor: PostProcessor):
  349. self._tokenizer.post_processor = post_processor
  350. @property
  351. def decoder(self) -> Decoder:
  352. return self._tokenizer.decoder
  353. @decoder.setter
  354. def decoder(self, decoder: Decoder):
  355. self._tokenizer.decoder = decoder