tokenization_siglip.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # Copyright 2024 The HuggingFace Inc. team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tokenization class for SigLIP model."""
  15. import os
  16. import re
  17. import string
  18. import warnings
  19. from shutil import copyfile
  20. from typing import TYPE_CHECKING, Any
  21. import sentencepiece as spm
  22. from ...tokenization_utils_base import AddedToken
  23. from ...tokenization_utils_sentencepiece import SentencePieceBackend
  24. if TYPE_CHECKING:
  25. from ...tokenization_utils_base import TextInput
  26. from ...utils import logging, requires_backends
  27. from ...utils.import_utils import requires
  28. logger = logging.get_logger(__name__)
  29. VOCAB_FILES_NAMES = {"vocab_file": "spiece.model"}
  30. SPIECE_UNDERLINE = "▁"
  31. @requires(backends=("sentencepiece",))
  32. class SiglipTokenizer(SentencePieceBackend):
  33. """
  34. Construct a Siglip tokenizer. Based on [SentencePiece](https://github.com/google/sentencepiece).
  35. This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
  36. this superclass for more information regarding those methods.
  37. Args:
  38. vocab_file (`str`):
  39. [SentencePiece](https://github.com/google/sentencepiece) file (generally has a *.spm* extension) that
  40. contains the vocabulary necessary to instantiate a tokenizer.
  41. eos_token (`str`, *optional*, defaults to `"</s>"`):
  42. The end of sequence token.
  43. unk_token (`str`, *optional*, defaults to `"<unk>"`):
  44. The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
  45. token instead.
  46. pad_token (`str`, *optional*, defaults to `"</s>"`):
  47. The token used for padding, for example when batching sequences of different lengths.
  48. additional_special_tokens (`list[str]`, *optional*):
  49. Additional special tokens used by the tokenizer.
  50. sp_model_kwargs (`dict`, *optional*):
  51. Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
  52. SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
  53. to set:
  54. - `enable_sampling`: Enable subword regularization.
  55. - `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.
  56. - `nbest_size = {0,1}`: No sampling is performed.
  57. - `nbest_size > 1`: samples from the nbest_size results.
  58. - `nbest_size < 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
  59. using forward-filtering-and-backward-sampling algorithm.
  60. - `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
  61. BPE-dropout.
  62. model_max_length (`int`, *optional*, defaults to 64):
  63. The maximum length (in number of tokens) for model inputs.
  64. do_lower_case (`bool`, *optional*, defaults to `True`):
  65. Whether or not to lowercase the input when tokenizing.
  66. """
  67. vocab_files_names = VOCAB_FILES_NAMES
  68. model_input_names = ["input_ids", "attention_mask"]
  69. def __init__(
  70. self,
  71. vocab_file,
  72. eos_token="</s>",
  73. unk_token="<unk>",
  74. pad_token="</s>",
  75. additional_special_tokens=None,
  76. sp_model_kwargs: dict[str, Any] | None = None,
  77. model_max_length=64,
  78. do_lower_case=True,
  79. **kwargs,
  80. ) -> None:
  81. requires_backends(self, "protobuf")
  82. pad_token = (
  83. AddedToken(pad_token, rstrip=True, lstrip=True, normalized=False, special=True)
  84. if isinstance(pad_token, str)
  85. else pad_token
  86. )
  87. unk_token = (
  88. AddedToken(unk_token, rstrip=True, lstrip=True, normalized=False, special=True)
  89. if isinstance(unk_token, str)
  90. else unk_token
  91. )
  92. eos_token = (
  93. AddedToken(eos_token, rstrip=True, lstrip=True, normalized=False, special=True)
  94. if isinstance(eos_token, str)
  95. else eos_token
  96. )
  97. self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
  98. self.do_lower_case = do_lower_case
  99. super().__init__(
  100. vocab_file=vocab_file,
  101. eos_token=eos_token,
  102. unk_token=unk_token,
  103. pad_token=pad_token,
  104. additional_special_tokens=additional_special_tokens,
  105. sp_model_kwargs=self.sp_model_kwargs,
  106. model_max_length=model_max_length,
  107. do_lower_case=do_lower_case,
  108. **kwargs,
  109. )
  110. @property
  111. def vocab_size(self):
  112. return self.sp_model.get_piece_size()
  113. def get_vocab(self):
  114. vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
  115. vocab.update(self.added_tokens_encoder)
  116. return vocab
  117. def get_special_tokens_mask(
  118. self, token_ids_0: list[int], token_ids_1: list[int] | None = None, already_has_special_tokens: bool = False
  119. ) -> list[int]:
  120. """
  121. Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
  122. special tokens using the tokenizer `prepare_for_model` method.
  123. Args:
  124. token_ids_0 (`list[int]`):
  125. List of IDs.
  126. token_ids_1 (`list[int]`, *optional*):
  127. Optional second list of IDs for sequence pairs.
  128. already_has_special_tokens (`bool`, *optional*, defaults to `False`):
  129. Whether or not the token list is already formatted with special tokens for the model.
  130. Returns:
  131. `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
  132. """
  133. if already_has_special_tokens:
  134. return super().get_special_tokens_mask(
  135. token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
  136. )
  137. # normal case: some special tokens
  138. if token_ids_1 is None:
  139. return ([0] * len(token_ids_0)) + [1]
  140. return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1]
  141. def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]:
  142. """Do not add eos again if user already added it."""
  143. if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id:
  144. warnings.warn(
  145. f"This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated"
  146. " eos tokens being added."
  147. )
  148. return token_ids
  149. else:
  150. return token_ids + [self.eos_token_id]
  151. def create_token_type_ids_from_sequences(
  152. self, token_ids_0: list[int], token_ids_1: list[int] | None = None
  153. ) -> list[int]:
  154. """
  155. Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make
  156. use of token type ids, therefore a list of zeros is returned.
  157. Args:
  158. token_ids_0 (`list[int]`):
  159. List of IDs.
  160. token_ids_1 (`list[int]`, *optional*):
  161. Optional second list of IDs for sequence pairs.
  162. Returns:
  163. `list[int]`: List of zeros.
  164. """
  165. eos = [self.eos_token_id]
  166. if token_ids_1 is None:
  167. return len(token_ids_0 + eos) * [0]
  168. return len(token_ids_0 + eos + token_ids_1 + eos) * [0]
  169. def build_inputs_with_special_tokens(
  170. self, token_ids_0: list[int], token_ids_1: list[int] | None = None
  171. ) -> list[int]:
  172. """
  173. Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
  174. adding special tokens. A sequence has the following format:
  175. - single sequence: `X </s>`
  176. - pair of sequences: `A </s> B </s>`
  177. Args:
  178. token_ids_0 (`list[int]`):
  179. List of IDs to which the special tokens will be added.
  180. token_ids_1 (`list[int]`, *optional*):
  181. Optional second list of IDs for sequence pairs.
  182. Returns:
  183. `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
  184. """
  185. token_ids_0 = self._add_eos_if_not_present(token_ids_0)
  186. if token_ids_1 is None:
  187. return token_ids_0
  188. else:
  189. token_ids_1 = self._add_eos_if_not_present(token_ids_1)
  190. return token_ids_0 + token_ids_1
  191. def __getstate__(self):
  192. state = self.__dict__.copy()
  193. state["sp_model"] = None
  194. return state
  195. def __setstate__(self, d):
  196. self.__dict__ = d
  197. # for backward compatibility
  198. if not hasattr(self, "sp_model_kwargs"):
  199. self.sp_model_kwargs = {}
  200. self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
  201. self.sp_model.Load(self.vocab_file)
  202. def remove_punctuation(self, text: str) -> str:
  203. return text.translate(str.maketrans("", "", string.punctuation))
  204. # source: https://github.com/google-research/big_vision/blob/3b8e5ab6ad4f96e32b32826f9e1b8fd277914f9c/big_vision/evaluators/proj/image_text/prompt_engineering.py#L94
  205. def canonicalize_text(self, text, *, keep_punctuation_exact_string=None):
  206. """Returns canonicalized `text` (puncuation removed).
  207. Args:
  208. text (`str`):
  209. String to be canonicalized.
  210. keep_punctuation_exact_string (`str`, *optional*):
  211. If provided, then this exact string is kept. For example providing '{}' will keep any occurrences of '{}'
  212. (but will still remove '{' and '}' that appear separately).
  213. """
  214. if self.do_lower_case:
  215. text = text.lower()
  216. if keep_punctuation_exact_string:
  217. text = keep_punctuation_exact_string.join(
  218. self.remove_punctuation(part) for part in text.split(keep_punctuation_exact_string)
  219. )
  220. else:
  221. text = self.remove_punctuation(text)
  222. text = re.sub(r"\s+", " ", text)
  223. text = text.strip()
  224. return text
  225. def tokenize(self, text: "TextInput", add_special_tokens=False, **kwargs) -> list[str]:
  226. """
  227. Converts a string to a list of tokens.
  228. """
  229. tokens = super().tokenize(SPIECE_UNDERLINE + text.replace(SPIECE_UNDERLINE, " "), **kwargs)
  230. if len(tokens) > 1 and tokens[0] == SPIECE_UNDERLINE and tokens[1] in self.all_special_tokens:
  231. tokens = tokens[1:]
  232. return tokens
  233. @property
  234. def unk_token_length(self):
  235. return len(self.sp_model.encode(str(self.unk_token)))
  236. def _tokenize(self, text, **kwargs):
  237. """
  238. Returns a tokenized string.
  239. We de-activated the `add_dummy_prefix` option, thus the sentencepiece internals will always strip any
  240. SPIECE_UNDERLINE.
  241. For example: `self.sp_model.encode(f"{SPIECE_UNDERLINE}Hey", out_type = str)` will give `['H', 'e', 'y']` instead of `['▁He', 'y']`.
  242. Thus we always encode `f"{unk_token}text"` and strip the `unk_token`. Here is an example with `unk_token = "<unk>"` and `unk_token_length = 4`.
  243. `self.tokenizer.sp_model.encode("<unk> Hey", out_type = str)[4:]`.
  244. """
  245. text = self.canonicalize_text(text, keep_punctuation_exact_string=None)
  246. tokens = self.sp_model.encode(text, out_type=str)
  247. # 1. Encode string + prefix ex: "<unk> Hey"
  248. tokens = self.sp_model.encode(self.unk_token + text, out_type=str)
  249. # 2. Remove self.unk_token from ['<','unk','>', '▁Hey']
  250. return tokens[self.unk_token_length :] if len(tokens) >= self.unk_token_length else tokens
  251. def _convert_token_to_id(self, token):
  252. """Converts a token (str) in an id using the vocab."""
  253. return self.sp_model.piece_to_id(token)
  254. def _convert_id_to_token(self, index):
  255. """Converts an index (integer) in a token (str) using the vocab."""
  256. token = self.sp_model.IdToPiece(index)
  257. return token
  258. def convert_tokens_to_string(self, tokens):
  259. """Converts a sequence of tokens (string) in a single string."""
  260. current_sub_tokens = []
  261. out_string = ""
  262. prev_is_special = False
  263. for token in tokens:
  264. # make sure that special tokens are not decoded using sentencepiece model
  265. if token in self.all_special_tokens:
  266. if not prev_is_special:
  267. out_string += " "
  268. out_string += self.sp_model.decode(current_sub_tokens) + token
  269. prev_is_special = True
  270. current_sub_tokens = []
  271. else:
  272. current_sub_tokens.append(token)
  273. prev_is_special = False
  274. out_string += self.sp_model.decode(current_sub_tokens)
  275. return out_string.strip()
  276. def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str]:
  277. if not os.path.isdir(save_directory):
  278. logger.error(f"Vocabulary path ({save_directory}) should be a directory")
  279. return
  280. out_vocab_file = os.path.join(
  281. save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
  282. )
  283. if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
  284. copyfile(self.vocab_file, out_vocab_file)
  285. elif not os.path.isfile(self.vocab_file):
  286. with open(out_vocab_file, "wb") as fi:
  287. content_spiece_model = self.sp_model.serialized_model_proto()
  288. fi.write(content_spiece_model)
  289. return (out_vocab_file,)
  290. __all__ = ["SiglipTokenizer"]