tokenization_siglip2.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/siglip2/modular_siglip2.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_siglip2.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 The HuggingFace Inc. team.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from tokenizers import Tokenizer, decoders, normalizers, pre_tokenizers
  21. from tokenizers.models import BPE
  22. from ...tokenization_utils_tokenizers import TokenizersBackend
  23. VOCAB_FILES_NAMES = {"tokenizer_file": "tokenizer.json"}
  24. class Siglip2Tokenizer(TokenizersBackend):
  25. """
  26. Gemma tokenizer + SigLIP2 training default: lowercase normalization.
  27. """
  28. vocab_files_names = VOCAB_FILES_NAMES
  29. padding_side = "left"
  30. model_input_names = ["input_ids", "attention_mask"]
  31. model = BPE
  32. def __init__(
  33. self,
  34. vocab: str | dict[str, int] | None = None,
  35. merges: str | list[str] | None = None,
  36. unk_token: str = "<unk>",
  37. bos_token: str = "<bos>",
  38. eos_token: str = "<eos>",
  39. pad_token: str = "<pad>",
  40. mask_token: str = "<mask>",
  41. **kwargs,
  42. ):
  43. if vocab is None:
  44. vocab = {
  45. str(pad_token): 0,
  46. str(eos_token): 1,
  47. str(bos_token): 2,
  48. str(unk_token): 3,
  49. str(mask_token): 4,
  50. }
  51. self._vocab = vocab
  52. self._merges = merges or []
  53. self._tokenizer = Tokenizer(
  54. BPE(
  55. vocab=self._vocab,
  56. merges=self._merges,
  57. fuse_unk=True,
  58. unk_token=str(unk_token),
  59. dropout=None,
  60. byte_fallback=True,
  61. )
  62. )
  63. self._tokenizer.pre_tokenizer = pre_tokenizers.Split(
  64. pattern=" ", behavior="merged_with_previous", invert=False
  65. )
  66. self._tokenizer.decoder = decoders.Sequence(
  67. [decoders.Replace("▁", " "), decoders.ByteFallback(), decoders.Fuse()]
  68. )
  69. self._tokenizer.normalizer = normalizers.Replace(" ", "▁")
  70. super().__init__(
  71. unk_token=unk_token,
  72. bos_token=bos_token,
  73. eos_token=eos_token,
  74. pad_token=pad_token,
  75. mask_token=mask_token,
  76. **kwargs,
  77. )
  78. # Persist for save/load + push_to_hub dynamic tokenizer test
  79. if hasattr(self, "init_kwargs") and isinstance(self.init_kwargs, dict):
  80. self.init_kwargs.setdefault("tokenizer_class", self.__class__.__name__)
  81. backend = getattr(self, "_tokenizer", None)
  82. if backend is not None and backend.normalizer is not None:
  83. backend.normalizer = normalizers.Sequence([normalizers.Lowercase(), backend.normalizer])
  84. __all__ = ["Siglip2Tokenizer"]