Base class for slow and fast tokenizers. Handle shared (mostly boiler plate) methods for slow and fast tokenizers.
| 1013 | |
| 1014 | |
| 1015 | class PreTrainedTokenizerBase(SpecialTokensMixin): |
| 1016 | """ Base class for slow and fast tokenizers. |
| 1017 | |
| 1018 | Handle shared (mostly boiler plate) methods for slow and fast tokenizers. |
| 1019 | """ |
| 1020 | |
| 1021 | vocab_files_names: Dict[str, str] = {} |
| 1022 | pretrained_vocab_files_map: Dict[str, Dict[str, str]] = {} |
| 1023 | pretrained_init_configuration: Dict[str, Dict[str, Any]] = {} |
| 1024 | max_model_input_sizes: Dict[str, int] = {} |
| 1025 | model_input_names: List[str] = ["token_type_ids", "attention_mask"] |
| 1026 | |
| 1027 | padding_side: str = "right" |
| 1028 | |
| 1029 | def __init__(self, **kwargs): |
| 1030 | # inputs and kwargs for saving and re-loading (see ``from_pretrained`` and ``save_pretrained``) |
| 1031 | self.init_inputs = () |
| 1032 | self.init_kwargs = kwargs |
| 1033 | |
| 1034 | # For backward compatibility we fallback to set model_max_length from max_len if provided |
| 1035 | model_max_length = kwargs.pop("model_max_length", kwargs.pop("max_len", None)) |
| 1036 | self.model_max_length = model_max_length if model_max_length is not None else VERY_LARGE_INTEGER |
| 1037 | |
| 1038 | # Padding side is right by default and overridden in subclasses. If specified in the kwargs, it is changed. |
| 1039 | self.padding_side = kwargs.pop("padding_side", self.padding_side) |
| 1040 | assert self.padding_side in [ |
| 1041 | "right", |
| 1042 | "left", |
| 1043 | ], f"Padding side should be selected between 'right' and 'left', current value: {self.padding_side}" |
| 1044 | self.model_input_names = kwargs.pop("model_input_names", self.model_input_names) |
| 1045 | |
| 1046 | super().__init__(**kwargs) |
| 1047 | |
| 1048 | @property |
| 1049 | def max_len(self) -> int: |
| 1050 | """ Kept here for backward compatibility. |
| 1051 | Now renamed to `model_max_length` to avoid ambiguity. |
| 1052 | """ |
| 1053 | return self.model_max_length |
| 1054 | |
| 1055 | @property |
| 1056 | def max_len_single_sentence(self) -> int: |
| 1057 | return self.model_max_length - self.num_special_tokens_to_add(pair=False) |
| 1058 | |
| 1059 | @property |
| 1060 | def max_len_sentences_pair(self) -> int: |
| 1061 | return self.model_max_length - self.num_special_tokens_to_add(pair=True) |
| 1062 | |
| 1063 | @max_len_single_sentence.setter |
| 1064 | def max_len_single_sentence(self, value) -> int: |
| 1065 | """ For backward compatibility, allow to try to setup 'max_len_single_sentence' """ |
| 1066 | if value == self.model_max_length - self.num_special_tokens_to_add(pair=False) and self.verbose: |
| 1067 | logger.warning( |
| 1068 | "Setting 'max_len_single_sentence' is now deprecated. " "This value is automatically set up." |
| 1069 | ) |
| 1070 | else: |
| 1071 | raise ValueError( |
| 1072 | "Setting 'max_len_single_sentence' is now deprecated. " "This value is automatically set up." |
nothing calls this directly
no outgoing calls
no test coverage detected