PreTrainedTokenizerBase

class transformers.PreTrainedTokenizerBase  < source > ( **kwargs  )   Parameters   model_max_length (int, optional) —
The maximum length (in number of tokens) for the inputs to the transformer model. When the tokenizer is
loaded with from_pretrained(), this will be set to the
value stored for the associated model in max_model_input_sizes (see above). If no value is provided, will
default to VERY_LARGE_INTEGER (int(1e30)).   padding_side (str, optional) —
The side on which the model should have padding applied. Should be selected between [‘right’, ‘left’].
Default value is picked from the class attribute of the same name.   truncation_side (str, optional) —
The side on which the model should have truncation applied. Should be selected between [‘right’, ‘left’].
Default value is picked from the class attribute of the same name.   chat_template (str, optional) —
A Jinja template string that will be used to format lists of chat messages. See
https://huggingface.co/docs/transformers/chat_


PreTrainedTokenizerBase

templating for a full description.   model_input_names (List[string], optional) —
The list of inputs accepted by the forward pass of the model (like "token_type_ids" or
"attention_mask"). Default value is picked from the class attribute of the same name.   bos_token (str or tokenizers.AddedToken, optional) —
A special token representing the beginning of a sentence. Will be associated to self.bos_token and
self.bos_token_id.   eos_token (str or tokenizers.AddedToken, optional) —
A special token representing the end of a sentence. Will be associated to self.eos_token and
self.eos_token_id.   unk_token (str or tokenizers.AddedToken, optional) —
A special token representing an out-of-vocabulary token. Will be associated to self.unk_token and
self.unk_token_id.   sep_token (str or tokenizers.AddedToken, optional) —
A special token separating two different sentences in the same input (used by BERT for instance). Will be
associated to self.sep_token and self.sep_token_id.   pad_token (str or


PreTrainedTokenizerBase

 tokenizers.AddedToken, optional) —
A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
attention mechanisms or loss computation. Will be associated to self.pad_token and self.pad_token_id.   cls_token (str or tokenizers.AddedToken, optional) —
A special token representing the class of the input (used by BERT for instance). Will be associated to
self.cls_token and self.cls_token_id.   mask_token (str or tokenizers.AddedToken, optional) —
A special token representing a masked token (used by masked-language modeling pretraining objectives, like
BERT). Will be associated to self.mask_token and self.mask_token_id.   additional_special_tokens (tuple or list of str or tokenizers.AddedToken, optional) —
A tuple or a list of additional special tokens. Add them here to ensure they are skipped when decoding with
skip_special_tokens is set to True. If they are not part of the vocabulary, they will be added at the end
of the vocabulary.   clea


PreTrainedTokenizerBase

n_up_tokenization_spaces (bool, optional, defaults to True) —
Whether or not the model should cleanup the spaces that were added when splitting the input text during the
tokenization process.   split_special_tokens (bool, optional, defaults to False) —
Whether or not the special tokens should be split during the tokenization process. The default behavior is
to not split special tokens. This means that if <s> is the bos_token, then tokenizer.tokenize("<s>") = ['<s>]. Otherwise, if split_special_tokens=True, then tokenizer.tokenize("<s>") will be give ['<', 's', '>']. This argument is only supported for slow tokenizers for the moment.    Base class for PreTrainedTokenizer and PreTrainedTokenizerFast. Handles shared (mostly boiler plate) methods for those two classes. Class attributes (overridden by derived classes) vocab_files_names (Dict[str, str]) — A dictionary with, as keys, the __init__ keyword name of each
vocabulary file required by the model, and as associated values, the filena


PreTrainedTokenizerBase

me for saving the associated file
(string). pretrained_vocab_files_map (Dict[str, Dict[str, str]]) — A dictionary of dictionaries, with the
high-level keys being the __init__ keyword name of each vocabulary file required by the model, the
low-level being the short-cut-names of the pretrained models with, as associated values, the url to the
associated pretrained vocabulary file. max_model_input_sizes (Dict[str, Optional[int]]) — A dictionary with, as keys, the short-cut-names
of the pretrained models, and as associated values, the maximum length of the sequence inputs of this model,
or None if the model has no maximum input size. pretrained_init_configuration (Dict[str, Dict[str, Any]]) — A dictionary with, as keys, the
short-cut-names of the pretrained models, and as associated values, a dictionary of specific arguments to
pass to the __init__ method of the tokenizer class for this pretrained model when loading the tokenizer
with the from_pretrained() method. model_input_names (List[


PreTrainedTokenizerBase

str]) — A list of inputs expected in the forward pass of the model. padding_side (str) — The default value for the side on which the model should have padding applied.
Should be 'right' or 'left'. truncation_side (str) — The default value for the side on which the model should have truncation
applied. Should be 'right' or 'left'.  __call__  < source > ( text: typing.Union[str, typing.List[str], typing.List[typing.List[str]]] = None text_pair: typing.Union[str, typing.List[str], typing.List[typing.List[str]], NoneType] = None text_target: typing.Union[str, typing.List[str], typing.List[typing.List[str]]] = None text_pair_target: typing.Union[str, typing.List[str], typing.List[typing.List[str]], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 is_spl


PreTrainedTokenizerBase

it_into_words: bool = False pad_to_multiple_of: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_mapping: bool = False return_length: bool = False verbose: bool = True **kwargs  ) → BatchEncoding  Parameters   text (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_pair (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the se


PreTrainedTokenizerBase

quences are provided as list of strings (pretokenized), you must set
is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_target (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
you must set is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_pair_target (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
you must set is_split_into_words=True (to lift the ambiguity with a batch of sequences).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the


PreTrainedTokenizerBase

 underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acce


PreTrainedTokenizerBase

ptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to


PreTrainedTokenizerBase

 use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int, optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER o


PreTrainedTokenizerBase

r token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   return_token_type_ids (bool, optional) —
Whether to return token type IDs. If left to the default, will return the token type IDs according to
the specific tokenizer’s default, defined by the return_outputs attribute.
What are token type IDs?   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer’s default, defined by the return_out


PreTrainedTokenizerBase

puts attribute.
What are attention masks?   return_overflowing_tokens (bool, optional, defaults to False) —
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with truncation_strategy = longest_first or True, an error is raised instead
of returning overflowing tokens.   return_special_tokens_mask (bool, optional, defaults to False) —
Whether or not to return special tokens mask information.   return_offsets_mapping (bool, optional, defaults to False) —
Whether or not to return (char_start, char_end) for each token.
This is only available on fast tokenizers inheriting from PreTrainedTokenizerFast, if using
Python’s tokenizer, this method will raise NotImplementedError.   return_length  (bool, optional, defaults to False) —
Whether or not to return the lengths of the encoded inputs.   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.
**kwargs — passed to the self.to


PreTrainedTokenizerBase

kenize() method   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to a model.
What are input IDs?
token_type_ids — List of token type ids to be fed to a model (when return_token_type_ids=True or
if “token_type_ids” is in self.model_input_names).
What are token type IDs?
attention_mask — List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=True or if “attention_mask” is in self.model_input_names).
What are attention masks?
overflowing_tokens — List of overflowing tokens sequences (when a max_length is specified and
return_overflowing_tokens=True).
num_truncated_tokens — Number of tokens truncated (when a max_length is specified and
return_overflowing_tokens=True).
special_tokens_mask — List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
regular sequence tokens (when add_special_tokens=True and return_special_tokens_mask=True).
length — The length of the


PreTrainedTokenizerBase

inputs (when return_length=True)
  Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of
sequences.  apply_chat_template  < source > ( conversation: typing.Union[typing.List[typing.Dict[str, str]], ForwardRef('Conversation')] chat_template: typing.Optional[str] = None add_generation_prompt: bool = False tokenize: bool = True padding: bool = False truncation: bool = False max_length: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None **tokenizer_kwargs  ) → List[int]  Parameters   conversation (Union[List[Dict[str, str]], “Conversation”]) — A Conversation object or list of dicts
with “role” and “content” keys, representing the chat history so far.   chat_template (str, optional) — A Jinja template to use for this conversion. If
this is not passed, the model’s default chat template will be used instead.   add_generation_prompt (bool, optional) — Whether to end the promp


PreTrainedTokenizerBase

t with the token(s) that indicate
the start of an assistant message. This is useful when you want to generate a response from the model.
Note that this argument will be passed to the chat template, and so it must be supported in the
template for this argument to have any effect.   tokenize (bool, defaults to True) —
Whether to tokenize the output. If False, the output will be a string.   padding (bool, defaults to False) —
Whether to pad sequences to the maximum length. Has no effect if tokenize is False.   truncation (bool, defaults to False) —
Whether to truncate sequences at the maximum length. Has no effect if tokenize is False.   max_length (int, optional) —
Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is False. If
not specified, the tokenizer’s max_length attribute will be used as a default.   return_tensors (str or TensorType, optional) —
If set, will return tensors of a particular framework. Has no effect if tokenize is False. Acceptab


PreTrainedTokenizerBase

le
values are:
'tf': Return TensorFlow tf.Tensor objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return NumPy np.ndarray objects.
'jax': Return JAX jnp.ndarray objects.
**tokenizer_kwargs — Additional kwargs to pass to the tokenizer.
   Returns
List[int]

A list of token ids representing the tokenized chat so far, including control tokens. This
output is ready to pass to the model, either directly or via methods like generate().
  Converts a Conversation object or a list of dictionaries with "role" and "content" keys to a list of token
ids. This method is intended for use with chat models, and will read the tokenizer’s chat_template attribute to
determine the format and control tokens to use when converting. When chat_template is None, it will fall back
to the default_chat_template specified at the class level.  as_target_tokenizer  < source > ( )    Temporarily sets the tokenizer for encoding the targets. Useful for tokenizer associated to
sequence-to-sequence models tha


PreTrainedTokenizerBase

t need a slightly different processing for the labels.  batch_decode  < source > ( sequences: typing.Union[typing.List[int], typing.List[typing.List[int]], ForwardRef('np.ndarray'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor')] skip_special_tokens: bool = False clean_up_tokenization_spaces: bool = None **kwargs  ) → List[str]  Parameters   sequences (Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]) —
List of tokenized input ids. Can be obtained using the __call__ method.   skip_special_tokens (bool, optional, defaults to False) —
Whether or not to remove special tokens in the decoding.   clean_up_tokenization_spaces (bool, optional) —
Whether or not to clean up the tokenization spaces. If None, will default to
self.clean_up_tokenization_spaces.   kwargs (additional keyword arguments, optional) —
Will be passed to the underlying model specific decode method.   Returns
List[str]

The list of decoded sentences.
  Convert a list of lists of token ids into


PreTrainedTokenizerBase

a list of strings by calling decode.  batch_encode_plus  < source > ( batch_text_or_text_pairs: typing.Union[typing.List[str], typing.List[typing.Tuple[str, str]], typing.List[typing.List[str]], typing.List[typing.Tuple[typing.List[str], typing.List[str]]], typing.List[typing.List[int]], typing.List[typing.Tuple[typing.List[int], typing.List[int]]]] add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 is_split_into_words: bool = False pad_to_multiple_of: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_ma


PreTrainedTokenizerBase

pping: bool = False return_length: bool = False verbose: bool = True **kwargs  ) → BatchEncoding  Parameters   batch_text_or_text_pairs (List[str], List[Tuple[str, str]], List[List[str]], List[Tuple[List[str], List[str]]], and for not-fast tokenizers, also List[List[int]], List[Tuple[List[int], List[int]]]) —
Batch of sequences or pair of sequences to be encoded. This can be a list of
string/string-sequences/int-sequences or a list of pair of string/string-sequences/int-sequence (see
details in encode_plus).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following va


PreTrainedTokenizerBase

lues:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for


PreTrainedTokenizerBase

the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int,


PreTrainedTokenizerBase

 optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will


PreTrainedTokenizerBase

return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   return_token_type_ids (bool, optional) —
Whether to return token type IDs. If left to the default, will return the token type IDs according to
the specific tokenizer’s default, defined by the return_outputs attribute.
What are token type IDs?   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer’s default, defined by the return_outputs attribute.
What are attention masks?   return_overflowing_tokens (bool, optional, defaults to False) —
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with truncation_strategy = longest_first or True, an error is raised instead
of returning overflowing tokens.


PreTrainedTokenizerBase

return_special_tokens_mask (bool, optional, defaults to False) —
Whether or not to return special tokens mask information.   return_offsets_mapping (bool, optional, defaults to False) —
Whether or not to return (char_start, char_end) for each token.
This is only available on fast tokenizers inheriting from PreTrainedTokenizerFast, if using
Python’s tokenizer, this method will raise NotImplementedError.   return_length  (bool, optional, defaults to False) —
Whether or not to return the lengths of the encoded inputs.   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.
**kwargs — passed to the self.tokenize() method   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to a model.
What are input IDs?
token_type_ids — List of token type ids to be fed to a model (when return_token_type_ids=True or
if “token_type_ids” is in self.model_input_names).
What are token type IDs?
attention_ma


PreTrainedTokenizerBase

sk — List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=True or if “attention_mask” is in self.model_input_names).
What are attention masks?
overflowing_tokens — List of overflowing tokens sequences (when a max_length is specified and
return_overflowing_tokens=True).
num_truncated_tokens — Number of tokens truncated (when a max_length is specified and
return_overflowing_tokens=True).
special_tokens_mask — List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
regular sequence tokens (when add_special_tokens=True and return_special_tokens_mask=True).
length — The length of the inputs (when return_length=True)
  Tokenize and prepare for the model a list of sequences or a list of pairs of sequences. This method is deprecated, __call__ should be used instead.  build_inputs_with_special_tokens  < source > ( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None  ) → List[int]  Parameters


PreTrainedTokenizerBase

token_ids_0 (List[int]) — The first tokenized sequence.   token_ids_1 (List[int], optional) — The second tokenized sequence.   Returns
List[int]

The model input with special tokens.
  Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. This implementation does not add special tokens and this method should be overridden in a subclass.  clean_up_tokenization  < source > ( out_string: str  ) → str  Parameters   out_string (str) — The text to clean up.   Returns
str

The cleaned-up string.
  Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms.  convert_tokens_to_string  < source > ( tokens: typing.List[str]  ) → str  Parameters   tokens (List[str]) — The token to join in a string.   Returns
str

The joined tokens.
  Converts a sequence of tokens in a single string. The most simple way to do it is " ".join(tokens) but we
often want to remov


PreTrainedTokenizerBase

e sub-word tokenization artifacts at the same time.  create_token_type_ids_from_sequences  < source > ( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None  ) → List[int]  Parameters   token_ids_0 (List[int]) — The first tokenized sequence.   token_ids_1 (List[int], optional) — The second tokenized sequence.   Returns
List[int]

The token type ids.
  Create the token type IDs corresponding to the sequences passed. What are token type
IDs? Should be overridden in a subclass if the model has a special way of building those.  decode  < source > ( token_ids: typing.Union[int, typing.List[int], ForwardRef('np.ndarray'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor')] skip_special_tokens: bool = False clean_up_tokenization_spaces: bool = None **kwargs  ) → str  Parameters   token_ids (Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]) —
List of tokenized input ids. Can be obtained using the __call__ method.   skip_special_tokens (bool, option


PreTrainedTokenizerBase

al, defaults to False) —
Whether or not to remove special tokens in the decoding.   clean_up_tokenization_spaces (bool, optional) —
Whether or not to clean up the tokenization spaces. If None, will default to
self.clean_up_tokenization_spaces.   kwargs (additional keyword arguments, optional) —
Will be passed to the underlying model specific decode method.   Returns
str

The decoded sentence.
  Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special
tokens and clean up tokenization spaces. Similar to doing self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids)).  encode  < source > ( text: typing.Union[str, typing.List[str], typing.List[int]] text_pair: typing.Union[str, typing.List[str], typing.List[int], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.Truncation


PreTrainedTokenizerBase

Strategy] = None max_length: typing.Optional[int] = None stride: int = 0 return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None **kwargs  ) → List[int], torch.Tensor, tf.Tensor or np.ndarray  Parameters   text (str, List[str] or List[int]) —
The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the
tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   text_pair (str, List[str] or List[int], optional) —
Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using
the tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
aut


PreTrainedTokenizerBase

omatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a


PreTrainedTokenizerBase

 token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined mode


PreTrainedTokenizerBase

l maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int, optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the


PreTrainedTokenizerBase

provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
**kwargs — Passed along to the .tokenize() method.   Returns
List[int], torch.Tensor, tf.Tensor or np.ndarray

The tokenized ids of the text.
  Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary. Same as doing self.convert_tokens_to_ids(self.tokenize(text)).  encode_plus  < source > ( text: typing.Union[str, typing.List[str], typing.List[int]] text_pair: typing.Union[str, typing.List[str], typing.List[int], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStr


PreTrainedTokenizerBase

ategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 is_split_into_words: bool = False pad_to_multiple_of: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_mapping: bool = False return_length: bool = False verbose: bool = True **kwargs  ) → BatchEncoding  Parameters   text (str, List[str] or List[int] (the latter only for not-fast tokenizers)) —
The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the
tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   text_pair (str, List[str] or List[int], optional) —
Option


PreTrainedTokenizerBase

al second sequence to be encoded. This can be a string, a list of strings (tokenized string using
the tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad'


PreTrainedTokenizerBase

(default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument


PreTrainedTokenizerBase

 is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int, optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument def


PreTrainedTokenizerBase

ines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   return_token_type_ids (bool, optional) —
Whether to return token type IDs. If left to the default, will re


PreTrainedTokenizerBase

turn the token type IDs according to
the specific tokenizer’s default, defined by the return_outputs attribute.
What are token type IDs?   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer’s default, defined by the return_outputs attribute.
What are attention masks?   return_overflowing_tokens (bool, optional, defaults to False) —
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with truncation_strategy = longest_first or True, an error is raised instead
of returning overflowing tokens.   return_special_tokens_mask (bool, optional, defaults to False) —
Whether or not to return special tokens mask information.   return_offsets_mapping (bool, optional, defaults to False) —
Whether or not to return (char_start, char_end) for each token.
This is only available on fast tokenizers inheriting from Pr


PreTrainedTokenizerBase

eTrainedTokenizerFast, if using
Python’s tokenizer, this method will raise NotImplementedError.   return_length  (bool, optional, defaults to False) —
Whether or not to return the lengths of the encoded inputs.   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.
**kwargs — passed to the self.tokenize() method   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to a model.
What are input IDs?
token_type_ids — List of token type ids to be fed to a model (when return_token_type_ids=True or
if “token_type_ids” is in self.model_input_names).
What are token type IDs?
attention_mask — List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=True or if “attention_mask” is in self.model_input_names).
What are attention masks?
overflowing_tokens — List of overflowing tokens sequences (when a max_length is specified and
return_overflowing_toke


PreTrainedTokenizerBase

ns=True).
num_truncated_tokens — Number of tokens truncated (when a max_length is specified and
return_overflowing_tokens=True).
special_tokens_mask — List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
regular sequence tokens (when add_special_tokens=True and return_special_tokens_mask=True).
length — The length of the inputs (when return_length=True)
  Tokenize and prepare for the model a sequence or a pair of sequences. This method is deprecated, __call__ should be used instead.  from_pretrained  < source > ( pretrained_model_name_or_path: typing.Union[str, os.PathLike] *init_inputs cache_dir: typing.Union[str, os.PathLike, NoneType] = None force_download: bool = False local_files_only: bool = False token: typing.Union[str, bool, NoneType] = None revision: str = 'main' **kwargs  )   Parameters   pretrained_model_name_or_path (str or os.PathLike) —
Can be either:
A string, the model id of a predefined tokenizer hosted inside a model repo on huggingface.co.
Val


PreTrainedTokenizerBase

id model ids can be located at the root-level, like bert-base-uncased, or namespaced under a
user or organization name, like dbmdz/bert-base-german-cased.
A path to a directory containing vocabulary files required by the tokenizer, for instance saved
using the save_pretrained() method, e.g.,
./my_model_directory/.
(Deprecated, not applicable to all derived classes) A path or url to a single saved vocabulary
file (if and only if the tokenizer only requires a single vocabulary file like Bert or XLNet), e.g.,
./my_model_directory/vocab.txt.
   cache_dir (str or os.PathLike, optional) —
Path to a directory in which a downloaded predefined tokenizer vocabulary files should be cached if the
standard cache should not be used.   force_download (bool, optional, defaults to False) —
Whether or not to force the (re-)download the vocabulary files and override the cached versions if they
exist.   resume_download (bool, optional, defaults to False) —
Whether or not to delete incompletely received f


PreTrainedTokenizerBase

iles. Attempt to resume the download if such a file
exists.   proxies (Dict[str, str], optional) —
A dictionary of proxy servers to use by protocol or endpoint, e.g., {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.   token (str or bool, optional) —
The token to use as HTTP bearer authorization for remote files. If True, will use the token generated
when running huggingface-cli login (stored in ~/.huggingface).   local_files_only (bool, optional, defaults to False) —
Whether or not to only rely on local files and not to attempt to download any files.   revision (str, optional, defaults to "main") —
The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
git-based system for storing models and other artifacts on huggingface.co, so revision can be any
identifier allowed by git.   subfolder (str, optional) —
In case the relevant files are located inside a subfolder of the model repo on huggin


PreTrainedTokenizerBase

gface.co (e.g. for
facebook/rag-token-base), specify it here.   inputs (additional positional arguments, optional) —
Will be passed along to the Tokenizer __init__ method.   kwargs (additional keyword arguments, optional) —
Will be passed to the Tokenizer __init__ method. Can be used to set special tokens like bos_token,
eos_token, unk_token, sep_token, pad_token, cls_token, mask_token,
additional_special_tokens. See parameters in the __init__ for more details.    Instantiate a PreTrainedTokenizerBase (or a derived class) from a predefined
tokenizer. Passing token=True is required when you want to use a private model.  Examples:   Copied # We can't instantiate directly the base class *PreTrainedTokenizerBase* so let's show our examples on a derived class: BertTokenizer
# Download vocabulary from huggingface.co and cache.
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
# Download vocabulary from huggingface.co (user-uploaded) and cache.
tokenizer = BertTokenizer.from_pre


PreTrainedTokenizerBase

trained("dbmdz/bert-base-german-cased")
# If vocabulary files are in a directory (e.g. tokenizer was saved using *save_pretrained('./test/saved_model/')*)
tokenizer = BertTokenizer.from_pretrained("./test/saved_model/")
# If the tokenizer uses a single vocabulary file, you can point directly to this file
tokenizer = BertTokenizer.from_pretrained("./test/saved_model/my_vocab.txt")
# You can link tokens to special vocabulary when instantiating
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased", unk_token="<unk>")
# You should be sure '<unk>' is in the vocabulary when doing that.
# Otherwise use tokenizer.add_special_tokens({'unk_token': '<unk>'}) instead)
assert tokenizer.unk_token == "<unk>"  get_special_tokens_mask  < source > ( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None already_has_special_tokens: bool = False  ) → A list of integers in the range [0, 1]  Parameters   token_ids_0 (List[int]) —
List of ids of the first sequence.   tok


PreTrainedTokenizerBase

en_ids_1 (List[int], optional) —
List of ids of the second sequence.   already_has_special_tokens (bool, optional, defaults to False) —
Whether or not the token list is already formatted with special tokens for the model.   Returns
A list of integers in the range [0, 1]

1 for a special token, 0 for a sequence token.
  Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model or encode_plus methods.  get_vocab  < source > ( ) → Dict[str, int]  Returns
Dict[str, int]

The vocabulary.
  Returns the vocabulary as a dictionary of token to index. tokenizer.get_vocab()[token] is equivalent to tokenizer.convert_tokens_to_ids(token) when token is in the
vocab.  pad  < source > ( encoded_inputs: typing.Union[transformers.tokenization_utils_base.BatchEncoding, typing.List[transformers.tokenization_utils_base.BatchEncoding], typing.Dict[str, typing.List[int]], typing.Dict[str, typing.Lis


PreTrainedTokenizerBase

t[typing.List[int]]], typing.List[typing.Dict[str, typing.List[int]]]] padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = True max_length: typing.Optional[int] = None pad_to_multiple_of: typing.Optional[int] = None return_attention_mask: typing.Optional[bool] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None verbose: bool = True  )   Parameters   encoded_inputs (BatchEncoding, list of BatchEncoding, Dict[str, List[int]], Dict[str, List[List[int]] or List[Dict[str, List[int]]]) —
Tokenized inputs. Can represent one input (BatchEncoding or Dict[str, List[int]]) or a batch of
tokenized inputs (list of BatchEncoding, Dict[str, List[List[int]]] or List[Dict[str,
List[int]]]) so you can use this method during preprocessing as well as in a PyTorch Dataloader
collate function.
Instead of List[int] you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see
the note above for the return type.   paddi


PreTrainedTokenizerBase

ng (bool, str or PaddingStrategy, optional, defaults to True) —
Select a strategy to pad the returned sequences (according to the model’s padding side and padding
index) among:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   max_length (int, optional) —
Maximum length of the returned list and optionally padding length (see above).   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the d


PreTrainedTokenizerBase

efault, will return the attention mask according
to the specific tokenizer’s default, defined by the return_outputs attribute.
What are attention masks?   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.    Pad a single encoded input or a batch of encoded inputs up to predefined length or to the max sequence length
in the batch. Padding side (left/right) padding token ids are defined at the tokenizer level (with self.padding_side,
self.pad_token_id and self.pad_token_type_id). Please note that with a fast tokenizer, using the __call__ method is faster than using a method to encode the
text followed by a call to the pad method to get a padded encoding. If the encoded_inputs pa


PreTrainedTokenizerBase

ssed are dictionary of numpy arrays, PyTorch tensors or TensorFlow tensors, the
result will use the same type unless you provide a different tensor type with return_tensors. In the case of
PyTorch tensors, you will lose the specific device of your tensors however.  prepare_for_model  < source > ( ids: typing.List[int] pair_ids: typing.Optional[typing.List[int]] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 pad_to_multiple_of: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_mapping: bool = F


PreTrainedTokenizerBase

alse return_length: bool = False verbose: bool = True prepend_batch_axis: bool = False **kwargs  ) → BatchEncoding  Parameters   ids (List[int]) —
Tokenized input ids of the first sequence. Can be obtained from a string by chaining the tokenize and
convert_tokens_to_ids methods.   pair_ids (List[int], optional) —
Tokenized input ids of the second sequence. Can be obtained from a string by chaining the tokenize
and convert_tokens_to_ids methods.   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch


PreTrainedTokenizerBase

 (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
trunca


PreTrainedTokenizerBase

te the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int, optional, defaults to 0) —
If set to a number along with max_len


PreTrainedTokenizerBase

gth, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable val


PreTrainedTokenizerBase

ues are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   return_token_type_ids (bool, optional) —
Whether to return token type IDs. If left to the default, will return the token type IDs according to
the specific tokenizer’s default, defined by the return_outputs attribute.
What are token type IDs?   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer’s default, defined by the return_outputs attribute.
What are attention masks?   return_overflowing_tokens (bool, optional, defaults to False) —
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with truncation_strategy = longest_first or True, an error is raised instead
of returning overflowing tokens.   return_special_tokens_mask (bool, optional, defaults to False) —


PreTrainedTokenizerBase

Whether or not to return special tokens mask information.   return_offsets_mapping (bool, optional, defaults to False) —
Whether or not to return (char_start, char_end) for each token.
This is only available on fast tokenizers inheriting from PreTrainedTokenizerFast, if using
Python’s tokenizer, this method will raise NotImplementedError.   return_length  (bool, optional, defaults to False) —
Whether or not to return the lengths of the encoded inputs.   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.
**kwargs — passed to the self.tokenize() method   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to a model.
What are input IDs?
token_type_ids — List of token type ids to be fed to a model (when return_token_type_ids=True or
if “token_type_ids” is in self.model_input_names).
What are token type IDs?
attention_mask — List of indices specifying which tokens should be attended t


PreTrainedTokenizerBase

o by the model (when
return_attention_mask=True or if “attention_mask” is in self.model_input_names).
What are attention masks?
overflowing_tokens — List of overflowing tokens sequences (when a max_length is specified and
return_overflowing_tokens=True).
num_truncated_tokens — Number of tokens truncated (when a max_length is specified and
return_overflowing_tokens=True).
special_tokens_mask — List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
regular sequence tokens (when add_special_tokens=True and return_special_tokens_mask=True).
length — The length of the inputs (when return_length=True)
  Prepares a sequence of input id, or a pair of sequences of inputs ids so that it can be used by the model. It
adds special tokens, truncates sequences if overflowing while taking into account the special tokens and
manages a moving window (with user defined stride) for overflowing tokens. Please Note, for pair_ids
different than None and truncation_strategy = longest_firs


PreTrainedTokenizerBase

t or True, it is not possible to return
overflowing tokens. Such a combination of arguments will raise an error.  prepare_seq2seq_batch  < source > ( src_texts: typing.List[str] tgt_texts: typing.Optional[typing.List[str]] = None max_length: typing.Optional[int] = None max_target_length: typing.Optional[int] = None padding: str = 'longest' return_tensors: str = None truncation: bool = True **kwargs  ) → BatchEncoding  Parameters   src_texts (List[str]) —
List of documents to summarize or source language texts.   tgt_texts (list, optional) —
List of summaries or target language texts.   max_length (int, optional) —
Controls the maximum length for encoder inputs (documents to summarize or source language texts) If
left unset or set to None, this will use the predefined model maximum length if a maximum length is
required by one of the truncation/padding parameters. If the model has no specific maximum input length
(like XLNet) truncation/padding to a maximum length will be deactivated.


PreTrainedTokenizerBase

  max_target_length (int, optional) —
Controls the maximum length of decoder inputs (target language texts or summaries) If left unset or set
to None, this will use the max_length value.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   truncation (bool, str


PreTrainedTokenizerBase

 or TruncationStrategy, optional, defaults to True) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of p


PreTrainedTokenizerBase

airs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
**kwargs —
Additional keyword arguments passed along to self.__call__.
   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to the encoder.
attention_mask — List of indices specifying which tokens should be attended to by the model.
labels — List of token ids for tgt_texts.
The full set of keys [input_ids, attention_mask, labels], will only be returned if tgt_texts is passed.
Otherwise, input_ids, attention_mask will be the only keys.
  Prepare model inputs for translation. For best performance, translate one sentence at a time.  push_to_hub  < source > ( repo_id: str use_temp_dir: typing.Optional[bool] = None commit_message: typing.Optional[str] = None private: typing.Optional[bool] = None token: typing.Union[bool, str, NoneType] = None max_shard_size: typi


PreTrainedTokenizerBase

ng.Union[int, str, NoneType] = '10GB' create_pr: bool = False safe_serialization: bool = False revision: str = None **deprecated_kwargs  )   Parameters   repo_id (str) —
The name of the repository you want to push your tokenizer to. It should contain your organization name
when pushing to a given organization.   use_temp_dir (bool, optional) —
Whether or not to use a temporary directory to store the files saved before they are pushed to the Hub.
Will default to True if there is no directory named like repo_id, False otherwise.   commit_message (str, optional) —
Message to commit while pushing. Will default to "Upload tokenizer".   private (bool, optional) —
Whether or not the repository created should be private.   token (bool or str, optional) —
The token to use as HTTP bearer authorization for remote files. If True, will use the token generated
when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url
is not specified.   max_shard_size (int or s


PreTrainedTokenizerBase

tr, optional, defaults to "10GB") —
Only applicable for models. The maximum size for a checkpoint before being sharded. Checkpoints shard
will then be each of size lower than this size. If expressed as a string, needs to be digits followed
by a unit (like "5MB").   create_pr (bool, optional, defaults to False) —
Whether or not to create a PR with the uploaded files or directly commit.   safe_serialization (bool, optional, defaults to False) —
Whether or not to convert the model weights in safetensors format for safer serialization.   revision (str, optional) —
Branch to push the uploaded files to.    Upload the tokenizer files to the 🤗 Model Hub.  Examples:   Copied from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
# Push the tokenizer to your namespace with the name "my-finetuned-bert".
tokenizer.push_to_hub("my-finetuned-bert")
# Push the tokenizer to an organization with the name "my-finetuned-bert".
tokenizer.push_to_hub("huggingfa


PreTrainedTokenizerBase

ce/my-finetuned-bert")  register_for_auto_class  < source > ( auto_class = 'AutoTokenizer'  )   Parameters   auto_class (str or type, optional, defaults to "AutoTokenizer") —
The auto class to register this new tokenizer with.    Register this class with a given auto class. This should only be used for custom tokenizers as the ones in the
library are already mapped with AutoTokenizer. This API is experimental and may have some slight breaking changes in the next releases.  save_pretrained  < source > ( save_directory: typing.Union[str, os.PathLike] legacy_format: typing.Optional[bool] = None filename_prefix: typing.Optional[str] = None push_to_hub: bool = False **kwargs  ) → A tuple of str  Parameters   save_directory (str or os.PathLike) — The path to a directory where the tokenizer will be saved.   legacy_format (bool, optional) —
Only applicable for a fast tokenizer. If unset (default), will save the tokenizer in the unified JSON
format as well as in legacy format if it exists, i.e


PreTrainedTokenizerBase

. with tokenizer specific vocabulary and a separate
added_tokens files.
If False, will only save the tokenizer in the unified JSON format. This format is incompatible with
“slow” tokenizers (not powered by the tokenizers library), so the tokenizer will not be able to be
loaded in the corresponding “slow” tokenizer.
If True, will save the tokenizer in legacy format. If the “slow” tokenizer doesn’t exits, a value
error is raised.   filename_prefix (str, optional) —
A prefix to add to the names of the files saved by the tokenizer.   push_to_hub (bool, optional, defaults to False) —
Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
repository you want to push to with repo_id (will default to the name of save_directory in your
namespace).   kwargs (Dict[str, Any], optional) —
Additional key word arguments passed along to the push_to_hub() method.   Returns
A tuple of str

The files saved.
  Save the full tokenizer state. This method mak


PreTrainedTokenizerBase

e sure the full tokenizer can then be re-loaded using the
~tokenization_utils_base.PreTrainedTokenizer.from_pretrained class method.. Warning,None This won’t save modifications you may have applied to the tokenizer after the instantiation (for
instance, modifying tokenizer.do_lower_case after creation).  save_vocabulary  < source > ( save_directory: str filename_prefix: typing.Optional[str] = None  ) → Tuple(str)  Parameters   save_directory (str) —
The directory in which to save the vocabulary.   filename_prefix (str, optional) —
An optional prefix to add to the named of the saved files.   Returns
Tuple(str)

Paths to the files saved.
  Save only the vocabulary of the tokenizer (vocabulary + added tokens). This method won’t save the configuration and special token mappings of the tokenizer. Use
_save_pretrained() to save the whole state of the tokenizer.  tokenize  < source > ( text: str pair: typing.Optional[str] = None add_special_tokens: bool = False **kwargs  ) → List[str]  Pa


PreTrainedTokenizerBase

rameters   text (str) —
The sequence to be encoded.   pair (str, optional) —
A second sequence to be encoded with the first.   add_special_tokens (bool, optional, defaults to False) —
Whether or not to add the special tokens associated with the corresponding model.   kwargs (additional keyword arguments, optional) —
Will be passed to the underlying model specific encode method. See details in
call()   Returns
List[str]

The list of tokens.
  Converts a string in a sequence of tokens, replacing unknown tokens with the unk_token.  truncate_sequences  < source > ( ids: typing.List[int] pair_ids: typing.Optional[typing.List[int]] = None num_tokens_to_remove: int = 0 truncation_strategy: typing.Union[str, transformers.tokenization_utils_base.TruncationStrategy] = 'longest_first' stride: int = 0  ) → Tuple[List[int], List[int], List[int]]  Parameters   ids (List[int]) —
Tokenized input ids of the first sequence. Can be obtained from a string by chaining the tokenize and
convert_tokens_to


PreTrainedTokenizerBase

_ids methods.   pair_ids (List[int], optional) —
Tokenized input ids of the second sequence. Can be obtained from a string by chaining the tokenize
and convert_tokens_to_ids methods.   num_tokens_to_remove (int, optional, defaults to 0) —
Number of tokens to remove using the truncation strategy.   truncation_strategy (str or TruncationStrategy, optional, defaults to False) —
The strategy to follow for truncation. Can be:
'longest_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will truncate
token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a
batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or


PreTrainedTokenizerBase

 a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths greater
than the model maximum admissible input size).
   stride (int, optional, defaults to 0) —
If set to a positive number, the overflowing tokens returned will contain some tokens from the main
sequence returned. The value of this argument defines the number of additional tokens.   Returns
Tuple[List[int], List[int], List[int]]

The truncated ids, the truncated pair_ids and the list of
overflowing tokens. Note: The longest_first strategy returns empty list of overflowing tokens if a pair
of sequences (or a batch of pairs) is provided.
  Truncates a sequence pair in-place followin


SpecialTokensMixin

class transformers.SpecialTokensMixin  < source > ( verbose = True **kwargs  )   Parameters   bos_token (str or tokenizers.AddedToken, optional) —
A special token representing the beginning of a sentence.   eos_token (str or tokenizers.AddedToken, optional) —
A special token representing the end of a sentence.   unk_token (str or tokenizers.AddedToken, optional) —
A special token representing an out-of-vocabulary token.   sep_token (str or tokenizers.AddedToken, optional) —
A special token separating two different sentences in the same input (used by BERT for instance).   pad_token (str or tokenizers.AddedToken, optional) —
A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
attention mechanisms or loss computation.   cls_token (str or tokenizers.AddedToken, optional) —
A special token representing the class of the input (used by BERT for instance).   mask_token (str or tokenizers.AddedToken, optional) —
A spe


SpecialTokensMixin

cial token representing a masked token (used by masked-language modeling pretraining objectives, like
BERT).   additional_special_tokens (tuple or list of str or tokenizers.AddedToken, optional) —
A tuple or a list of additional tokens, which will be marked as special, meaning that they will be
skipped when decoding if skip_special_tokens is set to True.    A mixin derived by PreTrainedTokenizer and PreTrainedTokenizerFast to handle specific behaviors related to
special tokens. In particular, this class hold the attributes which can be used to directly access these special
tokens in a model-independent manner and allow to set and update the special tokens.  add_special_tokens  < source > ( special_tokens_dict: typing.Dict[str, typing.Union[str, tokenizers.AddedToken]] replace_additional_special_tokens = True  ) → int  Parameters   special_tokens_dict (dictionary str to str or tokenizers.AddedToken) —
Keys should be in the list of predefined special attributes: [b


SpecialTokensMixin

os_token, eos_token, unk_token,
sep_token, pad_token, cls_token, mask_token, additional_special_tokens].
Tokens are only added if they are not already in the vocabulary (tested by checking if the tokenizer
assign the index of the unk_token to them).   replace_additional_special_tokens (bool, optional,, defaults to True) —
If True, the existing list of additional special tokens will be replaced by the list provided in
special_tokens_dict. Otherwise, self._additional_special_tokens is just extended. In the former
case, the tokens will NOT be removed from the tokenizer’s full vocabulary - they are only being flagged
as non-special tokens. Remember, this only affects which tokens are skipped during decoding, not the
added_tokens_encoder and added_tokens_decoder. This means that the previous
additional_special_tokens are still added tokens, and will not be split by the model.   Returns
int

Number of tokens added to the vocabulary.
  Add a dictionary of special tok


SpecialTokensMixin

ens (eos, pad, cls, etc.) to the encoder and link them to class attributes. If
special tokens are NOT in the vocabulary, they are added to it (indexed starting from the last index of the
current vocabulary). When adding new tokens to the vocabulary, you should make sure to also resize the token embedding matrix of the
model so that its embedding matrix matches the tokenizer. In order to do that, please use the resize_token_embeddings() method. Using add_special_tokens will ensure your special tokens can be used in several ways: Special tokens can be skipped when decoding using skip_special_tokens = True. Special tokens are carefully handled by the tokenizer (they are never split), similar to AddedTokens. You can easily refer to special tokens using tokenizer class attributes like tokenizer.cls_token. This
makes it easy to develop model-agnostic training and fine-tuning scripts. When possible, special tokens are already registered for provided pretrained models (f


SpecialTokensMixin

or instance
BertTokenizer cls_token is already registered to be :obj’[CLS]’ and XLM’s one is also registered to be
'</s>').  Examples:   Copied # Let's see how to add a new classification token to GPT-2
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2Model.from_pretrained("gpt2")
special_tokens_dict = {"cls_token": "<CLS>"}
num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
print("We have added", num_added_toks, "tokens")
# Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e., the length of the tokenizer.
model.resize_token_embeddings(len(tokenizer))
assert tokenizer.cls_token == "<CLS>"  add_tokens  < source > ( new_tokens: typing.Union[str, tokenizers.AddedToken, typing.List[typing.Union[str, tokenizers.AddedToken]]] special_tokens: bool = False  ) → int  Parameters   new_tokens (str, tokenizers.AddedToken or a list of str or tokenizers.AddedToken) —
Tokens are only added if they are not alrea


SpecialTokensMixin

dy in the vocabulary. tokenizers.AddedToken wraps a string
token to let you personalize its behavior: whether this token should only match against a single word,
whether this token should strip all potential whitespaces on the left side, whether this token should
strip all potential whitespaces on the right side, etc.   special_tokens (bool, optional, defaults to False) —
Can be used to specify if the token is a special token. This mostly change the normalization behavior
(special tokens like CLS or [MASK] are usually not lower-cased for instance).
See details for tokenizers.AddedToken in HuggingFace tokenizers library.   Returns
int

Number of tokens added to the vocabulary.
  Add a list of new tokens to the tokenizer class. If the new tokens are not in the vocabulary, they are added to
it with indices starting from length of the current vocabulary and and will be isolated before the tokenization
algorithm is applied. Added tokens and tokens from the vocabula


SpecialTokensMixin

ry of the tokenization algorithm are therefore
not treated in the same way. Note, when adding new tokens to the vocabulary, you should make sure to also resize the token embedding matrix
of the model so that its embedding matrix matches the tokenizer. In order to do that, please use the resize_token_embeddings() method.  Examples:   Copied # Let's see how to increase the vocabulary of Bert model and tokenizer
tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased")
model = BertModel.from_pretrained("bert-base-uncased")
num_added_toks = tokenizer.add_tokens(["new_tok1", "my_new-tok2"])
print("We have added", num_added_toks, "tokens")
# Notice: resize_token_embeddings expect to receive the full size of the new vocabulary, i.e., the length of the tokenizer.
model.resize_token_embeddings(len(tokenizer))  sanitize_special_tokens  < source > ( )    The sanitize_special_tokens is now deprecated kept for backward compatibility and will be removed in
transformer


Enums and namedtuples

class transformers.tokenization_utils_base.TruncationStrategy  < source > ( value names = None module = None qualname = None type = None start = 1  )    Possible values for the truncation argument in PreTrainedTokenizerBase.call(). Useful for tab-completion in
an IDE.
 class transformers.CharSpan  < source > ( start: int end: int  )   Parameters   start (int) — Index of the first character in the original string.   end (int) — Index of the character following the last character in the original string.    Character span in the original string.
 class transformers.TokenSpan  < source > ( start: int end: int  )   Parameters   start (int) — Index of the first token in the span.   end (int) — Index of the token following the last token in the span.    Token span in an encoded string (list of tokens).


Custom Layers and Utilities

This page lists all the custom layers used by the library, as well as the utility functions it provides for modeling.
Most of those are only useful if you are studying the code of the models in the library.


Pytorch custom modules

class transformers.Conv1D  < source > ( nf nx  )   Parameters   nf (int) — The number of output features.   nx (int) — The number of input features.    1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2). Basically works like a linear layer but the weights are transposed.
 class transformers.modeling_utils.PoolerStartLogits  < source > ( config: PretrainedConfig  )   Parameters   config (PretrainedConfig) —
The config used by the model, will be used to grab the hidden_size of the model.    Compute SQuAD start logits from sequence hidden states.  forward  < source > ( hidden_states: FloatTensor p_mask: typing.Optional[torch.FloatTensor] = None  ) → torch.FloatTensor  Parameters   hidden_states (torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.   p_mask (torch.FloatTensor of shape (batch_size, seq_len), optional) —
M


Pytorch custom modules

ask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.   Returns
torch.FloatTensor

The start logits for SQuAD.

 class transformers.modeling_utils.PoolerEndLogits  < source > ( config: PretrainedConfig  )   Parameters   config (PretrainedConfig) —
The config used by the model, will be used to grab the hidden_size of the model and the layer_norm_eps
to use.    Compute SQuAD end logits from sequence hidden states.  forward  < source > ( hidden_states: FloatTensor start_states: typing.Optional[torch.FloatTensor] = None start_positions: typing.Optional[torch.LongTensor] = None p_mask: typing.Optional[torch.FloatTensor] = None  ) → torch.FloatTensor  Parameters   hidden_states (torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.   start_states (torch.FloatTensor of shape (batch_size, seq_len


Pytorch custom modules

, hidden_size), optional) —
The hidden states of the first tokens for the labeled span.   start_positions (torch.LongTensor of shape (batch_size,), optional) —
The position of the first token for the labeled span.   p_mask (torch.FloatTensor of shape (batch_size, seq_len), optional) —
Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.   Returns
torch.FloatTensor

The end logits for SQuAD.
  One of start_states or start_positions should be not None. If both are set, start_positions overrides
start_states.
 class transformers.modeling_utils.PoolerAnswerClass  < source > ( config  )   Parameters   config (PretrainedConfig) —
The config used by the model, will be used to grab the hidden_size of the model.    Compute SQuAD 2.0 answer class from classification and start tokens hidden states.  forward  < source > ( hidden_states: FloatTenso


Pytorch custom modules

r start_states: typing.Optional[torch.FloatTensor] = None start_positions: typing.Optional[torch.LongTensor] = None cls_index: typing.Optional[torch.LongTensor] = None  ) → torch.FloatTensor  Parameters   hidden_states (torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
The final hidden states of the model.   start_states (torch.FloatTensor of shape (batch_size, seq_len, hidden_size), optional) —
The hidden states of the first tokens for the labeled span.   start_positions (torch.LongTensor of shape (batch_size,), optional) —
The position of the first token for the labeled span.   cls_index (torch.LongTensor of shape (batch_size,), optional) —
Position of the CLS token for each sentence in the batch. If None, takes the last token.   Returns
torch.FloatTensor

The SQuAD 2.0 answer class.
  One of start_states or start_positions should be not None. If both are set, start_positions overrides


Pytorch custom modules

start_states.
 class transformers.modeling_utils.SquadHeadOutput  < source > ( loss: typing.Optional[torch.FloatTensor] = None start_top_log_probs: typing.Optional[torch.FloatTensor] = None start_top_index: typing.Optional[torch.LongTensor] = None end_top_log_probs: typing.Optional[torch.FloatTensor] = None end_top_index: typing.Optional[torch.LongTensor] = None cls_logits: typing.Optional[torch.FloatTensor] = None  )   Parameters   loss (torch.FloatTensor of shape (1,), optional, returned if both start_positions and end_positions are provided) —
Classification loss as the sum of start token, end token (and is_impossible if provided) classification
losses.   start_top_log_probs (torch.FloatTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) —
Log probabilities for the top config.start_n_top start token possibilities (beam-search).   star


Pytorch custom modules

t_top_index (torch.LongTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) —
Indices for the top config.start_n_top start token possibilities (beam-search).   end_top_log_probs (torch.FloatTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) —
Log probabilities for the top config.start_n_top * config.end_n_top end token possibilities
(beam-search).   end_top_index (torch.LongTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) —
Indices for the top config.start_n_top * config.end_n_top end token possibilities (beam-search).   cls_logits (torch.FloatTensor of shape (batch_size,), optional, returned if start_positions or end_positions is not provided) —
Log probabili


Pytorch custom modules

ties for the is_impossible label of the answers.    Base class for outputs of question answering models using a SQuADHead.
 class transformers.modeling_utils.SQuADHead  < source > ( config  )   Parameters   config (PretrainedConfig) —
The config used by the model, will be used to grab the hidden_size of the model and the layer_norm_eps
to use.    A SQuAD head inspired by XLNet.  forward  < source > ( hidden_states: FloatTensor start_positions: typing.Optional[torch.LongTensor] = None end_positions: typing.Optional[torch.LongTensor] = None cls_index: typing.Optional[torch.LongTensor] = None is_impossible: typing.Optional[torch.LongTensor] = None p_mask: typing.Optional[torch.FloatTensor] = None return_dict: bool = False  ) → transformers.modeling_utils.SquadHeadOutput or tuple(torch.FloatTensor)  Parameters   hidden_states (torch.FloatTensor of shape (batch_size, seq_len, hidden_size)) —
Final hidden states


Pytorch custom modules

 of the model on the sequence tokens.   start_positions (torch.LongTensor of shape (batch_size,), optional) —
Positions of the first token for the labeled span.   end_positions (torch.LongTensor of shape (batch_size,), optional) —
Positions of the last token for the labeled span.   cls_index (torch.LongTensor of shape (batch_size,), optional) —
Position of the CLS token for each sentence in the batch. If None, takes the last token.   is_impossible (torch.LongTensor of shape (batch_size,), optional) —
Whether the question has a possible answer in the paragraph or not.   p_mask (torch.FloatTensor of shape (batch_size, seq_len), optional) —
Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
should be masked.   return_dict (bool, optional, defaults to False) —
Whether or not to return a ModelOutput instead of a plain tuple.   Returns
transformers.modeling_u


Pytorch custom modules

tils.SquadHeadOutput or tuple(torch.FloatTensor)

A transformers.modeling_utils.SquadHeadOutput or a tuple of
torch.FloatTensor (if return_dict=False is passed or when config.return_dict=False) comprising various
elements depending on the configuration (<class 'transformers.configuration_utils.PretrainedConfig'>) and inputs.
loss (torch.FloatTensor of shape (1,), optional, returned if both start_positions and end_positions are provided) — Classification loss as the sum of start token, end token (and is_impossible if provided) classification
losses.
start_top_log_probs (torch.FloatTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the top config.start_n_top start token possibilities (beam-search).
start_top_index (torch.LongTensor of shape (batch_size, config.start_n_top), optional, returned if start_positions o


Pytorch custom modules

r end_positions is not provided) — Indices for the top config.start_n_top start token possibilities (beam-search).
end_top_log_probs (torch.FloatTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the top config.start_n_top * config.end_n_top end token possibilities
(beam-search).
end_top_index (torch.LongTensor of shape (batch_size, config.start_n_top * config.end_n_top), optional, returned if start_positions or end_positions is not provided) — Indices for the top config.start_n_top * config.end_n_top end token possibilities (beam-search).
cls_logits (torch.FloatTensor of shape (batch_size,), optional, returned if start_positions or end_positions is not provided) — Log probabilities for the is_impossible label of the answers.

 class transformers.modeling_utils.SequenceSummary  < source > ( co


Pytorch custom modules

nfig: PretrainedConfig  )   Parameters   config (PretrainedConfig) —
The config used by the model. Relevant arguments in the config class of the model are (refer to the actual
config class of your model for the default values it uses):
summary_type (str) — The method to use to make this summary. Accepted values are:
"last" — Take the last token hidden state (like XLNet)
"first" — Take the first token hidden state (like Bert)
"mean" — Take the mean of all tokens hidden states
"cls_index" — Supply a Tensor of classification token position (GPT/GPT-2)
"attn" — Not implemented now, use multi-head attention
summary_use_proj (bool) — Add a projection after the vector extraction.
summary_proj_to_labels (bool) — If True, the projection outputs to config.num_labels classes
(otherwise to config.hidden_size).
summary_activation (Optional[str]) — Set to "tanh" to add a tanh activation to the output,
another string or


Pytorch custom modules

None will add no activation.
summary_first_dropout (float) — Optional dropout probability before the projection and activation.
summary_last_dropout (float)— Optional dropout probability after the projection and activation.
    Compute a single vector summary of a sequence hidden states.  forward  < source > ( hidden_states: FloatTensor cls_index: typing.Optional[torch.LongTensor] = None  ) → torch.FloatTensor  Parameters   hidden_states (torch.FloatTensor of shape [batch_size, seq_len, hidden_size]) —
The hidden states of the last layer.   cls_index (torch.LongTensor of shape [batch_size] or [batch_size, ...] where … are optional leading dimensions of hidden_states, optional) —
Used if summary_type == "cls_index" and takes the last token of the sequence as classification token.   Returns
torch.FloatTensor

The summary of the sequence hidden states.
  Compute a single vector summary of a sequence hidden


PyTorch Helper Functions

transformers.apply_chunking_to_forward  < source > ( forward_fn: typing.Callable[..., torch.Tensor] chunk_size: int chunk_dim: int *input_tensors  ) → torch.Tensor  Parameters   forward_fn (Callable[..., torch.Tensor]) —
The forward function of the model.   chunk_size (int) —
The chunk size of a chunked tensor: num_chunks = len(input_tensors[0]) / chunk_size.   chunk_dim (int) —
The dimension over which the input_tensors should be chunked.   input_tensors (Tuple[torch.Tensor]) —
The input tensors of forward_fn which will be chunked   Returns
torch.Tensor

A tensor with the same shape as the forward_fn would have given if applied`.
  This function chunks the input_tensors into smaller input tensor parts of size chunk_size over the dimension
chunk_dim. It then applies a layer forward_fn to each chunk independently to save memory. If the forward_fn is independent across the chunk_dim this function will yield the sa


PyTorch Helper Functions

me result as directly
applying forward_fn to input_tensors.  Examples:   Copied # rename the usual forward() fn to forward_chunk()
def forward_chunk(self, hidden_states):
    hidden_states = self.decoder(hidden_states)
    return hidden_states
# implement a chunked forward function
def forward(self, hidden_states):
    return apply_chunking_to_forward(self.forward_chunk, self.chunk_size_lm_head, self.seq_len_dim, hidden_states)
 transformers.pytorch_utils.find_pruneable_heads_and_indices  < source > ( heads: typing.List[int] n_heads: int head_size: int already_pruned_heads: typing.Set[int]  ) → Tuple[Set[int], torch.LongTensor]  Parameters   heads (List[int]) — List of the indices of heads to prune.   n_heads (int) — The number of heads in the model.   head_size (int) — The size of each head.   already_pruned_heads (Set[int]) — A set of already pruned heads.   Returns
Tuple[Set[int], torch.LongTensor]

A tuple w


PyTorch Helper Functions

ith the indices of heads to prune taking already_pruned_heads
into account and the indices of rows/columns to keep in the layer weight.
  Finds the heads and their indices taking already_pruned_heads into account.
 transformers.prune_layer  < source > ( layer: typing.Union[torch.nn.modules.linear.Linear, transformers.pytorch_utils.Conv1D] index: LongTensor dim: typing.Optional[int] = None  ) → torch.nn.Linear or Conv1D  Parameters   layer (Union[torch.nn.Linear, Conv1D]) — The layer to prune.   index (torch.LongTensor) — The indices to keep in the layer.   dim (int, optional) — The dimension on which to keep the indices.   Returns
torch.nn.Linear or Conv1D

The pruned layer as a new layer with requires_grad=True.
  Prune a Conv1D or linear layer to keep only entries in index. Used to remove heads.
 transformers.pytorch_utils.prune_conv1d_layer  < source > ( layer: Conv1D index: LongTensor dim: int = 1  ) → Conv1


PyTorch Helper Functions

D  Parameters   layer (Conv1D) — The layer to prune.   index (torch.LongTensor) — The indices to keep in the layer.   dim (int, optional, defaults to 1) — The dimension on which to keep the indices.   Returns
Conv1D

The pruned layer as a new layer with requires_grad=True.
  Prune a Conv1D layer to keep only entries in index. A Conv1D work as a Linear layer (see e.g. BERT) but the weights
are transposed. Used to remove heads.
 transformers.pytorch_utils.prune_linear_layer  < source > ( layer: Linear index: LongTensor dim: int = 0  ) → torch.nn.Linear  Parameters   layer (torch.nn.Linear) — The layer to prune.   index (torch.LongTensor) — The indices to keep in the layer.   dim (int, optional, defaults to 0) — The dimension on which to keep the indices.   Returns
torch.nn.Linear

The pruned layer as a new layer with requires_grad=True.
  Prune a linear layer to keep only entries in index. Used to remove heads.


TensorFlow custom layers

class transformers.modeling_tf_utils.TFConv1D  < source > ( *args **kwargs  )   Parameters   nf (int) —
The number of output features.   nx (int) —
The number of input features.   initializer_range (float, optional, defaults to 0.02) —
The standard deviation to use to initialize the weights.   kwargs (Dict[str, Any], optional) —
Additional keyword arguments passed along to the __init__ of tf.keras.layers.Layer.    1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2). Basically works like a linear layer but the weights are transposed.
 class transformers.TFSequenceSummary  < source > ( *args **kwargs  )   Parameters   config (PretrainedCon


TensorFlow custom layers

fig) —
The config used by the model. Relevant arguments in the config class of the model are (refer to the actual
config class of your model for the default values it uses):
summary_type (str) — The method to use to make this summary. Accepted values are:
"last" — Take the last token hidden state (like XLNet)
"first" — Take the first token hidden state (like Bert)
"mean" — Take the mean of all tokens hidden states
"cls_index" — Supply a Tensor of classification token position (GPT/GPT-2)
"attn" — Not implemented now, use multi-head attention
summary_use_proj (bool) — Add a projection after the vector extraction.
summary_proj_to_labels (bool) — If True, the projection outputs t


TensorFlow custom layers

o config.num_labels classes
(otherwise to config.hidden_size).
summary_activation (Optional[str]) — Set to "tanh" to add a tanh activation to the output,
another string or None will add no activation.
summary_first_dropout (float) — Optional dropout probability before the projection and activation.
summary_last_dropout (float)— Optional dropout probability after the projection and activation.
   initializer_range (float, defaults to 0.02) — The standard deviation to use to initialize the weights.   kwargs (Dict[str, Any], optional) —
Additional keyword arguments passed along to the __init__ of tf.keras.layers.Layer.    Compute a single vector summary of a sequence hidden state


TensorFlow loss functions

class transformers.modeling_tf_utils.TFCausalLanguageModelingLoss  < source > ( )    Loss function suitable for causal language modeling (CLM), that is, the task of guessing the next token. Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.
 class transformers.modeling_tf_utils.TFMaskedLanguageModelingLoss  < source > ( )    Loss function suitable for masked language modeling (MLM), that is, the task of guessing the masked tokens. Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.
 class transformers.mo


TensorFlow loss functions

deling_tf_utils.TFMultipleChoiceLoss  < source > ( )    Loss function suitable for multiple choice tasks.
 class transformers.modeling_tf_utils.TFQuestionAnsweringLoss  < source > ( )    Loss function suitable for question answering.
 class transformers.modeling_tf_utils.TFSequenceClassificationLoss  < source > ( )    Loss function suitable for sequence classification.
 class transformers.modeling_tf_utils.TFTokenClassificationLoss  < source > ( )    Loss function suitable for token classification. Any label of -100 will be ignored (along with the corresponding logits) in the loss computation.


TensorFlow Helper Functions

transformers.modeling_tf_utils.get_initializer  < source > ( initializer_range: float = 0.02  ) → tf.keras.initializers.TruncatedNormal  Parameters   initializer_range (float, defaults to 0.02) — Standard deviation of the initializer range.   Returns
tf.keras.initializers.TruncatedNormal

The truncated normal initializer.
  Creates a tf.keras.initializers.TruncatedNormal with the given range.
 transformers.modeling_tf_utils.keras_serializable  < source > ( )   Parameters   cls (a tf.keras.layers.Layers subclass) —
Typically a TF.MainLayer class in this project, in general must accept a config argument to its
initializer.    Decorate a Keras Layer class to support Keras serialization. This is done by: Adding a transformers_config dict to the Ke


TensorFlow Helper Functions

ras config dictionary in get_config (called by Keras at
serialization time. Wrapping __init__ to accept that transformers_config dict (passed by Keras at deserialization time) and
convert it to a config object for the actual layer initializer. Registering the class as a custom object in Keras (if the Tensorflow version supports this), so that it does not
need to be supplied in custom_objects in the call to tf.keras.models.load_model.
 transformers.shape_list  < source > ( tensor: typing.Union[tensorflow.python.framework.ops.Tensor, numpy.ndarray]  ) → List[int]  Parameters   tensor (tf.Tensor or np.ndarray) — The tensor we want the shape of.   Returns
List[int]

The shape of the tensor as a list.
  Deal with dynamic shape in tensorflow cleanly