| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
- # This file was automatically generated from src/transformers/models/siglip2/modular_siglip2.py.
- # Do NOT edit this file manually as any edits will be overwritten by the generation of
- # the file from the modular. If any change should be done, please apply the change to the
- # modular_siglip2.py file directly. One of our CI enforces this.
- # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
- # Copyright 2025 The HuggingFace Inc. team.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from tokenizers import Tokenizer, decoders, normalizers, pre_tokenizers
- from tokenizers.models import BPE
- from ...tokenization_utils_tokenizers import TokenizersBackend
- VOCAB_FILES_NAMES = {"tokenizer_file": "tokenizer.json"}
- class Siglip2Tokenizer(TokenizersBackend):
- """
- Gemma tokenizer + SigLIP2 training default: lowercase normalization.
- """
- vocab_files_names = VOCAB_FILES_NAMES
- padding_side = "left"
- model_input_names = ["input_ids", "attention_mask"]
- model = BPE
- def __init__(
- self,
- vocab: str | dict[str, int] | None = None,
- merges: str | list[str] | None = None,
- unk_token: str = "<unk>",
- bos_token: str = "<bos>",
- eos_token: str = "<eos>",
- pad_token: str = "<pad>",
- mask_token: str = "<mask>",
- **kwargs,
- ):
- if vocab is None:
- vocab = {
- str(pad_token): 0,
- str(eos_token): 1,
- str(bos_token): 2,
- str(unk_token): 3,
- str(mask_token): 4,
- }
- self._vocab = vocab
- self._merges = merges or []
- self._tokenizer = Tokenizer(
- BPE(
- vocab=self._vocab,
- merges=self._merges,
- fuse_unk=True,
- unk_token=str(unk_token),
- dropout=None,
- byte_fallback=True,
- )
- )
- self._tokenizer.pre_tokenizer = pre_tokenizers.Split(
- pattern=" ", behavior="merged_with_previous", invert=False
- )
- self._tokenizer.decoder = decoders.Sequence(
- [decoders.Replace("▁", " "), decoders.ByteFallback(), decoders.Fuse()]
- )
- self._tokenizer.normalizer = normalizers.Replace(" ", "▁")
- super().__init__(
- unk_token=unk_token,
- bos_token=bos_token,
- eos_token=eos_token,
- pad_token=pad_token,
- mask_token=mask_token,
- **kwargs,
- )
- # Persist for save/load + push_to_hub dynamic tokenizer test
- if hasattr(self, "init_kwargs") and isinstance(self.init_kwargs, dict):
- self.init_kwargs.setdefault("tokenizer_class", self.__class__.__name__)
- backend = getattr(self, "_tokenizer", None)
- if backend is not None and backend.normalizer is not None:
- backend.normalizer = normalizers.Sequence([normalizers.Lowercase(), backend.normalizer])
- __all__ = ["Siglip2Tokenizer"]
|