| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798 |
- # Copyright 2021 Studio Ousia and the HuggingFace Inc. team.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License
- """Tokenization classes for mLUKE."""
- import itertools
- import json
- import os
- from collections.abc import Mapping
- import numpy as np
- from tokenizers import Tokenizer, decoders, normalizers, pre_tokenizers
- from tokenizers.models import Unigram
- from ...tokenization_utils_base import (
- ENCODE_KWARGS_DOCSTRING,
- AddedToken,
- BatchEncoding,
- EncodedInput,
- PaddingStrategy,
- TensorType,
- TextInput,
- TextInputPair,
- TruncationStrategy,
- to_py_obj,
- )
- from ...tokenization_utils_tokenizers import TokenizersBackend
- from ...utils import add_end_docstrings, is_torch_tensor, logging
- logger = logging.get_logger(__name__)
- EntitySpan = tuple[int, int]
- EntitySpanInput = list[EntitySpan]
- Entity = str
- EntityInput = list[Entity]
- SPIECE_UNDERLINE = "▁"
- VOCAB_FILES_NAMES = {"vocab_file": "sentencepiece.bpe.model", "entity_vocab_file": "entity_vocab.json"}
- ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING = r"""
- 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?](../glossary#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?](../glossary#attention-mask)
- 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.tokenize()` method
- Return:
- [`BatchEncoding`]: A [`BatchEncoding`] with the following fields:
- - **input_ids** -- List of token ids to be fed to a model.
- [What are input IDs?](../glossary#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?](../glossary#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?](../glossary#attention-mask)
- - **entity_ids** -- List of entity ids to be fed to a model.
- [What are input IDs?](../glossary#input-ids)
- - **entity_position_ids** -- List of entity positions in the input sequence to be fed to a model.
- - **entity_token_type_ids** -- List of entity token type ids to be fed to a model (when
- `return_token_type_ids=True` or if *"entity_token_type_ids"* is in `self.model_input_names`).
- [What are token type IDs?](../glossary#token-type-ids)
- - **entity_attention_mask** -- List of indices specifying which entities should be attended to by the model
- (when `return_attention_mask=True` or if *"entity_attention_mask"* is in `self.model_input_names`).
- [What are attention masks?](../glossary#attention-mask)
- - **entity_start_positions** -- List of the start positions of entities in the word token sequence (when
- `task="entity_span_classification"`).
- - **entity_end_positions** -- List of the end positions of entities in the word token sequence (when
- `task="entity_span_classification"`).
- - **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`)
- """
- class MLukeTokenizer(TokenizersBackend):
- """
- Adapted from [`XLMRobertaTokenizer`] and [`LukeTokenizer`]. Based on
- [SentencePiece](https://github.com/google/sentencepiece).
- This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
- this superclass for more information regarding those methods.
- Args:
- vocab_file (`str`):
- Path to the vocabulary file.
- entity_vocab_file (`str`):
- Path to the entity vocabulary file.
- bos_token (`str`, *optional*, defaults to `"<s>"`):
- The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.
- <Tip>
- When building a sequence using special tokens, this is not the token that is used for the beginning of
- sequence. The token used is the `cls_token`.
- </Tip>
- eos_token (`str`, *optional*, defaults to `"</s>"`):
- The end of sequence token.
- <Tip>
- When building a sequence using special tokens, this is not the token that is used for the end of sequence.
- The token used is the `sep_token`.
- </Tip>
- sep_token (`str`, *optional*, defaults to `"</s>"`):
- The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
- sequence classification or for a text and a question for question answering. It is also used as the last
- token of a sequence built with special tokens.
- cls_token (`str`, *optional*, defaults to `"<s>"`):
- The classifier token which is used when doing sequence classification (classification of the whole sequence
- instead of per-token classification). It is the first token of the sequence when built with special tokens.
- unk_token (`str`, *optional*, defaults to `"<unk>"`):
- The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
- token instead.
- pad_token (`str`, *optional*, defaults to `"<pad>"`):
- The token used for padding, for example when batching sequences of different lengths.
- mask_token (`str`, *optional*, defaults to `"<mask>"`):
- The token used for masking values. This is the token used when training this model with masked language
- modeling. This is the token which the model will try to predict.
- task (`str`, *optional*):
- Task for which you want to prepare sequences. One of `"entity_classification"`,
- `"entity_pair_classification"`, or `"entity_span_classification"`. If you specify this argument, the entity
- sequence is automatically created based on the given entity span(s).
- max_entity_length (`int`, *optional*, defaults to 32):
- The maximum length of `entity_ids`.
- max_mention_length (`int`, *optional*, defaults to 30):
- The maximum number of tokens inside an entity span.
- entity_token_1 (`str`, *optional*, defaults to `<ent>`):
- The special token used to represent an entity span in a word token sequence. This token is only used when
- `task` is set to `"entity_classification"` or `"entity_pair_classification"`.
- entity_token_2 (`str`, *optional*, defaults to `<ent2>`):
- The special token used to represent an entity span in a word token sequence. This token is only used when
- `task` is set to `"entity_pair_classification"`.
- additional_special_tokens (`list[str]`, *optional*, defaults to `["<s>NOTUSED", "</s>NOTUSED"]`):
- Additional special tokens used by the tokenizer.
- sp_model_kwargs (`dict`, *optional*):
- Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
- SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
- to set:
- - `enable_sampling`: Enable subword regularization.
- - `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.
- - `nbest_size = {0,1}`: No sampling is performed.
- - `nbest_size > 1`: samples from the nbest_size results.
- - `nbest_size < 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
- using forward-filtering-and-backward-sampling algorithm.
- - `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
- BPE-dropout.
- Attributes:
- sp_model (`SentencePieceProcessor`):
- The *SentencePiece* processor that is used for every conversion (string, tokens and IDs).
- """
- vocab_files_names = VOCAB_FILES_NAMES
- model_input_names = ["input_ids", "attention_mask"]
- def __init__(
- self,
- bos_token="<s>",
- eos_token="</s>",
- sep_token="</s>",
- cls_token="<s>",
- unk_token="<unk>",
- pad_token="<pad>",
- mask_token="<mask>",
- task=None,
- max_entity_length=32,
- max_mention_length=30,
- entity_token_1="<ent>",
- entity_token_2="<ent2>",
- entity_unk_token="[UNK]",
- entity_pad_token="[PAD]",
- entity_mask_token="[MASK]",
- entity_mask2_token="[MASK2]",
- vocab: str | dict | list | None = None,
- entity_vocab: str | dict | list | None = None,
- **kwargs,
- ) -> None:
- # Mask token behave like a normal word, i.e. include the space before it
- mask_token = AddedToken(mask_token, lstrip=True, rstrip=False) if isinstance(mask_token, str) else mask_token
- # we add 2 special tokens for downstream tasks
- entity_token_1 = (
- AddedToken(entity_token_1, lstrip=False, rstrip=False)
- if isinstance(entity_token_1, str)
- else entity_token_1
- )
- entity_token_2 = (
- AddedToken(entity_token_2, lstrip=False, rstrip=False)
- if isinstance(entity_token_2, str)
- else entity_token_2
- )
- # Handle entity vocab file for backward compatibility
- entity_vocab_file = kwargs.pop("entity_vocab_file", None)
- # Check if vocab/entity_vocab are in kwargs
- if vocab is None and "vocab" in kwargs:
- vocab = kwargs.pop("vocab")
- if entity_vocab is None and "entity_vocab" in kwargs:
- entity_vocab = kwargs.pop("entity_vocab")
- # Build vocab from data (list of (token, score) tuples)
- if isinstance(vocab, list):
- # vocab is list of (token, score) tuples from SentencePieceExtractor
- self._vocab = [(token, float(score)) for token, score in vocab]
- self._vocab_size = len(self._vocab)
- elif vocab is not None:
- self._vocab = vocab
- self._vocab_size = 0
- else:
- # Create minimal vocab with <unk> to satisfy Unigram requirements
- self._vocab = [("<unk>", 0.0)]
- self._vocab_size = 0 # Will be updated when real vocab is loaded
- # Build Unigram tokenizer
- self._tokenizer = Tokenizer(Unigram(self._vocab, unk_id=0))
- # Add SentencePiece-style normalization and pre-tokenization
- self._tokenizer.normalizer = normalizers.Sequence(
- [
- normalizers.Replace("``", '"'),
- normalizers.Replace("''", '"'),
- ]
- )
- self._tokenizer.pre_tokenizer = pre_tokenizers.Metaspace(replacement="▁", prepend_scheme="always")
- self._tokenizer.decoder = decoders.Metaspace(replacement="▁", prepend_scheme="always")
- # Original fairseq vocab and spm vocab must be "aligned":
- # Vocab | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
- # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
- # fairseq | '<s>' | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's' | '▁de' | '-'
- # spm | '<unk>' | '<s>' | '</s>' | ',' | '.' | '▁' | 's' | '▁de' | '-' | '▁a'
- # Mimic fairseq token-to-id alignment for the first 4 tokens
- self.fairseq_tokens_to_ids = {"<s>": 0, "<pad>": 1, "</s>": 2, "<unk>": 3}
- # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
- self.fairseq_offset = 1
- self.fairseq_tokens_to_ids["<mask>"] = self._vocab_size + self.fairseq_offset
- self.fairseq_ids_to_tokens = {v: k for k, v in self.fairseq_tokens_to_ids.items()}
- # Load entity vocab
- if entity_vocab is not None:
- self.entity_vocab = entity_vocab
- elif entity_vocab_file is not None:
- with open(entity_vocab_file, encoding="utf-8") as entity_vocab_handle:
- self.entity_vocab = json.load(entity_vocab_handle)
- else:
- # Create minimal entity vocab with required special tokens
- self.entity_vocab = {
- entity_unk_token: 0,
- entity_pad_token: 1,
- entity_mask_token: 2,
- entity_mask2_token: 3,
- }
- for entity_special_token in [entity_unk_token, entity_pad_token, entity_mask_token, entity_mask2_token]:
- if entity_special_token not in self.entity_vocab:
- raise ValueError(
- f"Specified entity special token ``{entity_special_token}`` is not found in entity_vocab."
- )
- self.entity_unk_token_id = self.entity_vocab[entity_unk_token]
- self.entity_pad_token_id = self.entity_vocab[entity_pad_token]
- self.entity_mask_token_id = self.entity_vocab[entity_mask_token]
- self.entity_mask2_token_id = self.entity_vocab[entity_mask2_token]
- self.task = task
- if task is None or task == "entity_span_classification":
- self.max_entity_length = max_entity_length
- elif task == "entity_classification":
- self.max_entity_length = 1
- elif task == "entity_pair_classification":
- self.max_entity_length = 2
- else:
- raise ValueError(
- f"Task {task} not supported. Select task from ['entity_classification', 'entity_pair_classification',"
- " 'entity_span_classification'] only."
- )
- self.max_mention_length = max_mention_length
- # Handle extra/legacy special tokens (v4 compat). The fallback load path can pass
- # `additional_special_tokens` and/or `extra_special_tokens`, with entries serialized as dicts.
- extra_tokens: list[AddedToken | str] = []
- for key in ("extra_special_tokens", "additional_special_tokens"):
- tokens = kwargs.pop(key, None)
- if isinstance(tokens, (list, tuple)):
- for token in tokens:
- extra_tokens.append(AddedToken(**token) if isinstance(token, dict) else token)
- # Ensure MLuke entity tokens are present exactly once.
- seen = {str(token) for token in extra_tokens}
- for token in (entity_token_1, entity_token_2):
- token_str = str(token)
- if token_str not in seen:
- extra_tokens.append(token)
- seen.add(token_str)
- # Also register entity masking/padding tokens so they survive save/load cycles.
- for token in (entity_unk_token, entity_pad_token, entity_mask_token, entity_mask2_token):
- if token not in seen:
- extra_tokens.append(AddedToken(token, lstrip=False, rstrip=False, normalized=False, special=True))
- seen.add(token)
- kwargs["extra_special_tokens"] = extra_tokens
- super().__init__(
- bos_token=bos_token,
- eos_token=eos_token,
- unk_token=unk_token,
- sep_token=sep_token,
- cls_token=cls_token,
- pad_token=pad_token,
- mask_token=mask_token,
- task=task,
- max_entity_length=max_entity_length,
- max_mention_length=max_mention_length,
- entity_token_1=str(entity_token_1),
- entity_token_2=str(entity_token_2),
- entity_unk_token=entity_unk_token,
- entity_pad_token=entity_pad_token,
- entity_mask_token=entity_mask_token,
- entity_mask2_token=entity_mask2_token,
- entity_vocab=entity_vocab if entity_vocab_file is None else None, # Only store if passed as data
- **kwargs,
- )
- # Call _post_init for tokenizers created directly (not from_pretrained)
- self._post_init()
- def _post_init(self):
- """
- Post-initialization to configure the post-processor for MLuke's special token format.
- """
- super()._post_init()
- # Ensure the Python-side vocab metadata matches the fast tokenizer backend after loading
- self._vocab_size = self._tokenizer.get_vocab_size(with_added_tokens=False)
- self.fairseq_tokens_to_ids["<mask>"] = self._vocab_size + self.fairseq_offset
- self.fairseq_ids_to_tokens = {v: k for k, v in self.fairseq_tokens_to_ids.items()}
- # Configure post processor for XLM-R/MLuke format:
- # single: <s> X </s>
- # pair: <s> A </s></s> B </s>
- from tokenizers import processors
- self._tokenizer.post_processor = processors.TemplateProcessing(
- single=f"{self.cls_token}:0 $A:0 {self.sep_token}:0",
- pair=f"{self.cls_token}:0 $A:0 {self.sep_token}:0 {self.sep_token}:0 $B:1 {self.sep_token}:1",
- special_tokens=[
- (self.cls_token, self.cls_token_id),
- (self.sep_token, self.sep_token_id),
- ],
- )
- @property
- def vocab_size(self):
- return self._vocab_size + self.fairseq_offset + 1 # Add the <mask> token
- def get_vocab(self):
- vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
- vocab.update(self.added_tokens_encoder)
- return vocab
- def _convert_token_to_id(self, token):
- """Converts a token (str) in an id using the vocab."""
- if token in self.fairseq_tokens_to_ids:
- return self.fairseq_tokens_to_ids[token]
- # Look up token in vocab
- token_id = self._tokenizer.token_to_id(token)
- # Need to return unknown token if not found (token_to_id returns None)
- return token_id + self.fairseq_offset if token_id is not None else self.unk_token_id
- def _convert_id_to_token(self, index):
- """Converts an index (integer) in a token (str) using the vocab."""
- if index in self.fairseq_ids_to_tokens:
- return self.fairseq_ids_to_tokens[index]
- token = self._tokenizer.id_to_token(index - self.fairseq_offset)
- return token if token is not None else self.unk_token
- def convert_tokens_to_string(self, tokens):
- """Converts a sequence of tokens (strings for sub-words) in a single string."""
- out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip()
- return out_string
- def num_special_tokens_to_add(self, pair: bool = False) -> int:
- """
- Returns the number of added tokens when encoding a sequence with special tokens.
- Args:
- pair (`bool`, *optional*, defaults to `False`):
- Whether the number of added tokens should be computed in the case of a sequence pair or a single
- sequence.
- Returns:
- `int`: Number of special tokens added to sequences.
- """
- return 4 if pair else 2
- @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
- def __call__(
- self,
- text: TextInput | list[TextInput],
- text_pair: TextInput | list[TextInput] | None = None,
- entity_spans: EntitySpanInput | list[EntitySpanInput] | None = None,
- entity_spans_pair: EntitySpanInput | list[EntitySpanInput] | None = None,
- entities: EntityInput | list[EntityInput] | None = None,
- entities_pair: EntityInput | list[EntityInput] | None = None,
- add_special_tokens: bool = True,
- padding: bool | str | PaddingStrategy = False,
- truncation: bool | str | TruncationStrategy = None,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- stride: int = 0,
- is_split_into_words: bool | None = False,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_tensors: str | TensorType | None = None,
- return_token_type_ids: bool | None = None,
- return_attention_mask: bool | None = 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:
- # Check for seq2seq parameters that are not supported with entity-aware encoding
- if kwargs.get("text_target") is not None or kwargs.get("text_pair_target") is not None:
- if entity_spans is not None or entities is not None or self.task is not None:
- raise NotImplementedError(
- "text_target and text_pair_target are not supported when using entity-aware encoding. "
- "Please use the tokenizer without entities for seq2seq tasks."
- )
- # Delegate to parent for seq2seq encoding
- return super().__call__(
- text=text,
- text_pair=text_pair,
- add_special_tokens=add_special_tokens,
- padding=padding,
- truncation=truncation,
- max_length=max_length,
- stride=stride,
- is_split_into_words=is_split_into_words,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- return_token_type_ids=return_token_type_ids,
- return_attention_mask=return_attention_mask,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_offsets_mapping=return_offsets_mapping,
- return_length=return_length,
- verbose=verbose,
- **kwargs,
- )
- """
- Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of
- sequences, depending on the task you want to prepare them for.
- Args:
- text (`str`, `list[str]`, `list[list[str]]`):
- The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this
- tokenizer does not support tokenization based on pretokenized strings.
- text_pair (`str`, `list[str]`, `list[list[str]]`):
- The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this
- tokenizer does not support tokenization based on pretokenized strings.
- entity_spans (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*):
- The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each
- with two integers denoting character-based start and end positions of entities. If you specify
- `"entity_classification"` or `"entity_pair_classification"` as the `task` argument in the constructor,
- the length of each sequence must be 1 or 2, respectively. If you specify `entities`, the length of each
- sequence must be equal to the length of each sequence of `entities`.
- entity_spans_pair (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*):
- The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each
- with two integers denoting character-based start and end positions of entities. If you specify the
- `task` argument in the constructor, this argument is ignored. If you specify `entities_pair`, the
- length of each sequence must be equal to the length of each sequence of `entities_pair`.
- entities (`list[str]`, `list[list[str]]`, *optional*):
- The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings
- representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los
- Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of
- each sequence must be equal to the length of each sequence of `entity_spans`. If you specify
- `entity_spans` without specifying this argument, the entity sequence or the batch of entity sequences
- is automatically constructed by filling it with the [MASK] entity.
- entities_pair (`list[str]`, `list[list[str]]`, *optional*):
- The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings
- representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los
- Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of
- each sequence must be equal to the length of each sequence of `entity_spans_pair`. If you specify
- `entity_spans_pair` without specifying this argument, the entity sequence or the batch of entity
- sequences is automatically constructed by filling it with the [MASK] entity.
- max_entity_length (`int`, *optional*):
- The maximum length of `entity_ids`.
- """
- # Input type checking for clearer error
- is_valid_single_text = isinstance(text, str)
- is_valid_batch_text = isinstance(text, (list, tuple)) and (
- len(text) == 0 or isinstance(text[0], (str, list, tuple))
- )
- if not (is_valid_single_text or is_valid_batch_text):
- raise ValueError("text input must be of type `str` (single example) or `list[str]` (batch).")
- is_valid_single_text_pair = isinstance(text_pair, str)
- is_valid_batch_text_pair = isinstance(text_pair, (list, tuple)) and (
- len(text_pair) == 0 or isinstance(text_pair[0], str)
- )
- if not (text_pair is None or is_valid_single_text_pair or is_valid_batch_text_pair):
- raise ValueError("text_pair input must be of type `str` (single example) or `list[str]` (batch).")
- is_batched = bool(isinstance(text, (list, tuple)))
- # Get proper padding and truncation strategies
- padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
- padding=padding,
- truncation=truncation,
- max_length=max_length,
- pad_to_multiple_of=pad_to_multiple_of,
- verbose=verbose,
- **kwargs,
- )
- if is_batched:
- batch_text_or_text_pairs = list(zip(text, text_pair)) if text_pair is not None else text
- if entities is None:
- batch_entities_or_entities_pairs = None
- else:
- batch_entities_or_entities_pairs = (
- list(zip(entities, entities_pair)) if entities_pair is not None else entities
- )
- if entity_spans is None:
- batch_entity_spans_or_entity_spans_pairs = None
- else:
- batch_entity_spans_or_entity_spans_pairs = (
- list(zip(entity_spans, entity_spans_pair)) if entity_spans_pair is not None else entity_spans
- )
- return self._batch_encode_plus(
- batch_text_or_text_pairs=batch_text_or_text_pairs,
- batch_entity_spans_or_entity_spans_pairs=batch_entity_spans_or_entity_spans_pairs,
- batch_entities_or_entities_pairs=batch_entities_or_entities_pairs,
- add_special_tokens=add_special_tokens,
- padding_strategy=padding_strategy,
- truncation_strategy=truncation_strategy,
- max_length=max_length,
- max_entity_length=max_entity_length,
- stride=stride,
- is_split_into_words=is_split_into_words,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- return_token_type_ids=return_token_type_ids,
- return_attention_mask=return_attention_mask,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_offsets_mapping=return_offsets_mapping,
- return_length=return_length,
- verbose=verbose,
- **kwargs,
- )
- else:
- return self._encode_plus(
- text=text,
- text_pair=text_pair,
- entity_spans=entity_spans,
- entity_spans_pair=entity_spans_pair,
- entities=entities,
- entities_pair=entities_pair,
- add_special_tokens=add_special_tokens,
- padding_strategy=padding_strategy,
- truncation_strategy=truncation_strategy,
- max_length=max_length,
- max_entity_length=max_entity_length,
- stride=stride,
- is_split_into_words=is_split_into_words,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- return_token_type_ids=return_token_type_ids,
- return_attention_mask=return_attention_mask,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_offsets_mapping=return_offsets_mapping,
- return_length=return_length,
- verbose=verbose,
- **kwargs,
- )
- def _encode_plus(
- self,
- text: TextInput,
- text_pair: TextInput | None = None,
- entity_spans: EntitySpanInput | None = None,
- entity_spans_pair: EntitySpanInput | None = None,
- entities: EntityInput | None = None,
- entities_pair: EntityInput | None = None,
- add_special_tokens: bool = True,
- padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
- truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- stride: int = 0,
- is_split_into_words: bool | None = False,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_tensors: str | TensorType | None = None,
- return_token_type_ids: bool | None = None,
- return_attention_mask: bool | None = 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:
- if (
- entity_spans is None
- and entity_spans_pair is None
- and entities is None
- and entities_pair is None
- and self.task is None
- ):
- return super()._encode_plus(
- text=text,
- text_pair=text_pair,
- add_special_tokens=add_special_tokens,
- padding_strategy=padding_strategy,
- truncation_strategy=truncation_strategy,
- max_length=max_length,
- stride=stride,
- is_split_into_words=is_split_into_words,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- return_token_type_ids=return_token_type_ids,
- return_attention_mask=return_attention_mask,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_offsets_mapping=return_offsets_mapping,
- return_length=return_length,
- verbose=verbose,
- **kwargs,
- )
- if return_offsets_mapping:
- raise NotImplementedError(
- "return_offset_mapping is not available when using Python tokenizers. "
- "To use this feature, change your tokenizer to one deriving from "
- "transformers.PreTrainedTokenizerFast. "
- "More information on available tokenizers at "
- "https://github.com/huggingface/transformers/pull/2674"
- )
- if is_split_into_words:
- raise NotImplementedError("is_split_into_words is not supported in this tokenizer.")
- (
- first_ids,
- second_ids,
- first_entity_ids,
- second_entity_ids,
- first_entity_token_spans,
- second_entity_token_spans,
- ) = self._create_input_sequence(
- text=text,
- text_pair=text_pair,
- entities=entities,
- entities_pair=entities_pair,
- entity_spans=entity_spans,
- entity_spans_pair=entity_spans_pair,
- **kwargs,
- )
- # prepare_for_model will create the attention_mask and token_type_ids
- return self.prepare_for_model(
- first_ids,
- pair_ids=second_ids,
- entity_ids=first_entity_ids,
- pair_entity_ids=second_entity_ids,
- entity_token_spans=first_entity_token_spans,
- pair_entity_token_spans=second_entity_token_spans,
- add_special_tokens=add_special_tokens,
- padding=padding_strategy.value,
- truncation=truncation_strategy.value,
- max_length=max_length,
- max_entity_length=max_entity_length,
- stride=stride,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- prepend_batch_axis=True,
- return_attention_mask=return_attention_mask,
- return_token_type_ids=return_token_type_ids,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_length=return_length,
- verbose=verbose,
- )
- def _batch_encode_plus(
- self,
- batch_text_or_text_pairs: list[TextInput] | list[TextInputPair],
- batch_entity_spans_or_entity_spans_pairs: list[EntitySpanInput]
- | list[tuple[EntitySpanInput, EntitySpanInput]]
- | None = None,
- batch_entities_or_entities_pairs: list[EntityInput] | list[tuple[EntityInput, EntityInput]] | None = None,
- add_special_tokens: bool = True,
- padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
- truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- stride: int = 0,
- is_split_into_words: bool | None = False,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_tensors: str | TensorType | None = None,
- return_token_type_ids: bool | None = None,
- return_attention_mask: bool | None = 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:
- if (
- batch_entity_spans_or_entity_spans_pairs is None
- and batch_entities_or_entities_pairs is None
- and self.task is None
- ):
- if batch_text_or_text_pairs and isinstance(batch_text_or_text_pairs[0], (tuple, list)):
- texts, text_pairs = zip(*batch_text_or_text_pairs)
- texts = list(texts)
- text_pairs = list(text_pairs)
- else:
- texts = batch_text_or_text_pairs
- text_pairs = None
- return super()._encode_plus(
- text=texts,
- text_pair=text_pairs,
- add_special_tokens=add_special_tokens,
- padding_strategy=padding_strategy,
- truncation_strategy=truncation_strategy,
- max_length=max_length,
- stride=stride,
- is_split_into_words=is_split_into_words,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_tensors=return_tensors,
- return_token_type_ids=return_token_type_ids,
- return_attention_mask=return_attention_mask,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_offsets_mapping=return_offsets_mapping,
- return_length=return_length,
- verbose=verbose,
- **kwargs,
- )
- if return_offsets_mapping:
- raise NotImplementedError(
- "return_offset_mapping is not available when using Python tokenizers. "
- "To use this feature, change your tokenizer to one deriving from "
- "transformers.PreTrainedTokenizerFast."
- )
- if is_split_into_words:
- raise NotImplementedError("is_split_into_words is not supported in this tokenizer.")
- # input_ids is a list of tuples (one for each example in the batch)
- input_ids = []
- entity_ids = []
- entity_token_spans = []
- for index, text_or_text_pair in enumerate(batch_text_or_text_pairs):
- if not isinstance(text_or_text_pair, (list, tuple)):
- text, text_pair = text_or_text_pair, None
- else:
- text, text_pair = text_or_text_pair
- entities, entities_pair = None, None
- if batch_entities_or_entities_pairs is not None:
- entities_or_entities_pairs = batch_entities_or_entities_pairs[index]
- if entities_or_entities_pairs:
- if isinstance(entities_or_entities_pairs[0], str):
- entities, entities_pair = entities_or_entities_pairs, None
- else:
- entities, entities_pair = entities_or_entities_pairs
- entity_spans, entity_spans_pair = None, None
- if batch_entity_spans_or_entity_spans_pairs is not None:
- entity_spans_or_entity_spans_pairs = batch_entity_spans_or_entity_spans_pairs[index]
- if len(entity_spans_or_entity_spans_pairs) > 0 and isinstance(
- entity_spans_or_entity_spans_pairs[0], list
- ):
- entity_spans, entity_spans_pair = entity_spans_or_entity_spans_pairs
- else:
- entity_spans, entity_spans_pair = entity_spans_or_entity_spans_pairs, None
- (
- first_ids,
- second_ids,
- first_entity_ids,
- second_entity_ids,
- first_entity_token_spans,
- second_entity_token_spans,
- ) = self._create_input_sequence(
- text=text,
- text_pair=text_pair,
- entities=entities,
- entities_pair=entities_pair,
- entity_spans=entity_spans,
- entity_spans_pair=entity_spans_pair,
- **kwargs,
- )
- input_ids.append((first_ids, second_ids))
- entity_ids.append((first_entity_ids, second_entity_ids))
- entity_token_spans.append((first_entity_token_spans, second_entity_token_spans))
- batch_outputs = self._batch_prepare_for_model(
- input_ids,
- batch_entity_ids_pairs=entity_ids,
- batch_entity_token_spans_pairs=entity_token_spans,
- add_special_tokens=add_special_tokens,
- padding_strategy=padding_strategy,
- truncation_strategy=truncation_strategy,
- max_length=max_length,
- max_entity_length=max_entity_length,
- stride=stride,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_attention_mask=return_attention_mask,
- return_token_type_ids=return_token_type_ids,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_length=return_length,
- return_tensors=return_tensors,
- verbose=verbose,
- )
- return BatchEncoding(batch_outputs)
- def _check_entity_input_format(self, entities: EntityInput | None, entity_spans: EntitySpanInput | None):
- if not isinstance(entity_spans, list):
- raise TypeError("entity_spans should be given as a list")
- elif len(entity_spans) > 0 and not isinstance(entity_spans[0], tuple):
- raise ValueError(
- "entity_spans should be given as a list of tuples containing the start and end character indices"
- )
- if entities is not None:
- if not isinstance(entities, list):
- raise ValueError("If you specify entities, they should be given as a list")
- if len(entities) > 0 and not isinstance(entities[0], str):
- raise ValueError("If you specify entities, they should be given as a list of entity names")
- if len(entities) != len(entity_spans):
- raise ValueError("If you specify entities, entities and entity_spans must be the same length")
- def _create_input_sequence(
- self,
- text: TextInput,
- text_pair: TextInput | None = None,
- entities: EntityInput | None = None,
- entities_pair: EntityInput | None = None,
- entity_spans: EntitySpanInput | None = None,
- entity_spans_pair: EntitySpanInput | None = None,
- **kwargs,
- ) -> tuple[list, list, list, list, list, list]:
- def get_input_ids(text):
- # Use the underlying tokenizer directly to avoid infinite recursion
- # Then convert to fairseq-aligned IDs
- tokens = self._tokenizer.encode(text, add_special_tokens=False).tokens
- return self.convert_tokens_to_ids(tokens)
- def get_input_ids_and_entity_token_spans(text, entity_spans):
- if entity_spans is None:
- return get_input_ids(text), None
- cur = 0
- input_ids = []
- entity_token_spans = [None] * len(entity_spans)
- split_char_positions = sorted(frozenset(itertools.chain(*entity_spans)))
- char_pos2token_pos = {}
- for split_char_position in split_char_positions:
- orig_split_char_position = split_char_position
- if (
- split_char_position > 0 and text[split_char_position - 1] == " "
- ): # whitespace should be prepended to the following token
- split_char_position -= 1
- if cur != split_char_position:
- input_ids += get_input_ids(text[cur:split_char_position])
- cur = split_char_position
- char_pos2token_pos[orig_split_char_position] = len(input_ids)
- input_ids += get_input_ids(text[cur:])
- entity_token_spans = [
- (char_pos2token_pos[char_start], char_pos2token_pos[char_end]) for char_start, char_end in entity_spans
- ]
- return input_ids, entity_token_spans
- first_ids, second_ids = None, None
- first_entity_ids, second_entity_ids = None, None
- first_entity_token_spans, second_entity_token_spans = None, None
- if self.task is None:
- if entity_spans is None:
- first_ids = get_input_ids(text)
- else:
- self._check_entity_input_format(entities, entity_spans)
- first_ids, first_entity_token_spans = get_input_ids_and_entity_token_spans(text, entity_spans)
- if entities is None:
- first_entity_ids = [self.entity_mask_token_id] * len(entity_spans)
- else:
- first_entity_ids = [self.entity_vocab.get(entity, self.entity_unk_token_id) for entity in entities]
- if text_pair is not None:
- if entity_spans_pair is None:
- second_ids = get_input_ids(text_pair)
- else:
- self._check_entity_input_format(entities_pair, entity_spans_pair)
- second_ids, second_entity_token_spans = get_input_ids_and_entity_token_spans(
- text_pair, entity_spans_pair
- )
- if entities_pair is None:
- second_entity_ids = [self.entity_mask_token_id] * len(entity_spans_pair)
- else:
- second_entity_ids = [
- self.entity_vocab.get(entity, self.entity_unk_token_id) for entity in entities_pair
- ]
- elif self.task == "entity_classification":
- if not (isinstance(entity_spans, list) and len(entity_spans) == 1 and isinstance(entity_spans[0], tuple)):
- raise ValueError(
- "Entity spans should be a list containing a single tuple "
- "containing the start and end character indices of an entity"
- )
- first_entity_ids = [self.entity_mask_token_id]
- first_ids, first_entity_token_spans = get_input_ids_and_entity_token_spans(text, entity_spans)
- # add special tokens to input ids
- entity_token_start, entity_token_end = first_entity_token_spans[0]
- first_ids = (
- first_ids[:entity_token_end] + [self.extra_special_tokens_ids[0]] + first_ids[entity_token_end:]
- )
- first_ids = (
- first_ids[:entity_token_start] + [self.extra_special_tokens_ids[0]] + first_ids[entity_token_start:]
- )
- first_entity_token_spans = [(entity_token_start, entity_token_end + 2)]
- elif self.task == "entity_pair_classification":
- if not (
- isinstance(entity_spans, list)
- and len(entity_spans) == 2
- and isinstance(entity_spans[0], tuple)
- and isinstance(entity_spans[1], tuple)
- ):
- raise ValueError(
- "Entity spans should be provided as a list of two tuples, "
- "each tuple containing the start and end character indices of an entity"
- )
- head_span, tail_span = entity_spans
- first_entity_ids = [self.entity_mask_token_id, self.entity_mask2_token_id]
- first_ids, first_entity_token_spans = get_input_ids_and_entity_token_spans(text, entity_spans)
- head_token_span, tail_token_span = first_entity_token_spans
- token_span_with_special_token_ids = [
- (head_token_span, self.extra_special_tokens_ids[0]),
- (tail_token_span, self.extra_special_tokens_ids[1]),
- ]
- if head_token_span[0] < tail_token_span[0]:
- first_entity_token_spans[0] = (head_token_span[0], head_token_span[1] + 2)
- first_entity_token_spans[1] = (tail_token_span[0] + 2, tail_token_span[1] + 4)
- token_span_with_special_token_ids.reverse()
- else:
- first_entity_token_spans[0] = (head_token_span[0] + 2, head_token_span[1] + 4)
- first_entity_token_spans[1] = (tail_token_span[0], tail_token_span[1] + 2)
- for (entity_token_start, entity_token_end), special_token_id in token_span_with_special_token_ids:
- first_ids = first_ids[:entity_token_end] + [special_token_id] + first_ids[entity_token_end:]
- first_ids = first_ids[:entity_token_start] + [special_token_id] + first_ids[entity_token_start:]
- elif self.task == "entity_span_classification":
- if not (isinstance(entity_spans, list) and len(entity_spans) > 0 and isinstance(entity_spans[0], tuple)):
- raise ValueError(
- "Entity spans should be provided as a list of tuples, "
- "each tuple containing the start and end character indices of an entity"
- )
- first_ids, first_entity_token_spans = get_input_ids_and_entity_token_spans(text, entity_spans)
- first_entity_ids = [self.entity_mask_token_id] * len(entity_spans)
- else:
- raise ValueError(f"Task {self.task} not supported")
- return (
- first_ids,
- second_ids,
- first_entity_ids,
- second_entity_ids,
- first_entity_token_spans,
- second_entity_token_spans,
- )
- @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
- def _batch_prepare_for_model(
- self,
- batch_ids_pairs: list[tuple[list[int], None]],
- batch_entity_ids_pairs: list[tuple[list[int] | None, list[int] | None]],
- batch_entity_token_spans_pairs: list[tuple[list[tuple[int, int]] | None, list[tuple[int, int]] | None]],
- add_special_tokens: bool = True,
- padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
- truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- stride: int = 0,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_tensors: str | None = None,
- return_token_type_ids: bool | None = None,
- return_attention_mask: bool | None = None,
- return_overflowing_tokens: bool = False,
- return_special_tokens_mask: bool = False,
- return_length: bool = False,
- verbose: bool = True,
- ) -> BatchEncoding:
- """
- 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
- Args:
- batch_ids_pairs: list of tokenized input ids or input ids pairs
- batch_entity_ids_pairs: list of entity ids or entity ids pairs
- batch_entity_token_spans_pairs: list of entity spans or entity spans pairs
- max_entity_length: The maximum length of the entity sequence.
- """
- batch_outputs = {}
- for input_ids, entity_ids, entity_token_span_pairs in zip(
- batch_ids_pairs, batch_entity_ids_pairs, batch_entity_token_spans_pairs
- ):
- first_ids, second_ids = input_ids
- first_entity_ids, second_entity_ids = entity_ids
- first_entity_token_spans, second_entity_token_spans = entity_token_span_pairs
- outputs = self.prepare_for_model(
- first_ids,
- second_ids,
- entity_ids=first_entity_ids,
- pair_entity_ids=second_entity_ids,
- entity_token_spans=first_entity_token_spans,
- pair_entity_token_spans=second_entity_token_spans,
- add_special_tokens=add_special_tokens,
- padding=PaddingStrategy.DO_NOT_PAD.value, # we pad in batch afterward
- truncation=truncation_strategy.value,
- max_length=max_length,
- max_entity_length=max_entity_length,
- stride=stride,
- pad_to_multiple_of=None, # we pad in batch afterward
- padding_side=None, # we pad in batch afterward
- return_attention_mask=False, # we pad in batch afterward
- return_token_type_ids=return_token_type_ids,
- return_overflowing_tokens=return_overflowing_tokens,
- return_special_tokens_mask=return_special_tokens_mask,
- return_length=return_length,
- return_tensors=None, # We convert the whole batch to tensors at the end
- prepend_batch_axis=False,
- verbose=verbose,
- )
- for key, value in outputs.items():
- if key not in batch_outputs:
- batch_outputs[key] = []
- batch_outputs[key].append(value)
- batch_outputs = self.pad(
- batch_outputs,
- padding=padding_strategy.value,
- max_length=max_length,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_attention_mask=return_attention_mask,
- )
- batch_outputs = BatchEncoding(batch_outputs, tensor_type=return_tensors)
- return batch_outputs
- @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING)
- def prepare_for_model(
- self,
- ids: list[int],
- pair_ids: list[int] | None = None,
- entity_ids: list[int] | None = None,
- pair_entity_ids: list[int] | None = None,
- entity_token_spans: list[tuple[int, int]] | None = None,
- pair_entity_token_spans: list[tuple[int, int]] | None = None,
- add_special_tokens: bool = True,
- padding: bool | str | PaddingStrategy = False,
- truncation: bool | str | TruncationStrategy = None,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- stride: int = 0,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_tensors: str | TensorType | None = None,
- return_token_type_ids: bool | None = None,
- return_attention_mask: bool | None = None,
- return_overflowing_tokens: bool = False,
- return_special_tokens_mask: bool = False,
- return_offsets_mapping: bool = False,
- return_length: bool = False,
- verbose: bool = True,
- prepend_batch_axis: bool = False,
- **kwargs,
- ) -> BatchEncoding:
- """
- Prepares a sequence of input id, entity id and entity span, or a pair of sequences of inputs ids, entity ids,
- entity spans 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_first*
- or `True`, it is not possible to return overflowing tokens. Such a combination of arguments will raise an
- error.
- Args:
- ids (`list[int]`):
- Tokenized input ids of the first sequence.
- pair_ids (`list[int]`, *optional*):
- Tokenized input ids of the second sequence.
- entity_ids (`list[int]`, *optional*):
- Entity ids of the first sequence.
- pair_entity_ids (`list[int]`, *optional*):
- Entity ids of the second sequence.
- entity_token_spans (`list[tuple[int, int]]`, *optional*):
- Entity spans of the first sequence.
- pair_entity_token_spans (`list[tuple[int, int]]`, *optional*):
- Entity spans of the second sequence.
- max_entity_length (`int`, *optional*):
- The maximum length of the entity sequence.
- """
- # Backward compatibility for 'truncation_strategy', 'pad_to_max_length'
- padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
- padding=padding,
- truncation=truncation,
- max_length=max_length,
- pad_to_multiple_of=pad_to_multiple_of,
- verbose=verbose,
- **kwargs,
- )
- # Compute lengths
- pair = bool(pair_ids is not None)
- len_ids = len(ids)
- len_pair_ids = len(pair_ids) if pair else 0
- if return_token_type_ids and not add_special_tokens:
- raise ValueError(
- "Asking to return token_type_ids while setting add_special_tokens to False "
- "results in an undefined behavior. Please set add_special_tokens to True or "
- "set return_token_type_ids to None."
- )
- if (
- return_overflowing_tokens
- and truncation_strategy == TruncationStrategy.LONGEST_FIRST
- and pair_ids is not None
- ):
- raise ValueError(
- "Not possible to return overflowing tokens for pair of sequences with the "
- "`longest_first`. Please select another truncation strategy than `longest_first`, "
- "for instance `only_second` or `only_first`."
- )
- # Load from model defaults
- if return_token_type_ids is None:
- return_token_type_ids = "token_type_ids" in self.model_input_names
- if return_attention_mask is None:
- return_attention_mask = "attention_mask" in self.model_input_names
- encoded_inputs = {}
- # Compute the total size of the returned word encodings
- total_len = len_ids + len_pair_ids + (self.num_special_tokens_to_add(pair=pair) if add_special_tokens else 0)
- # Truncation: Handle max sequence length and max_entity_length
- overflowing_tokens = []
- if truncation_strategy != TruncationStrategy.DO_NOT_TRUNCATE and max_length and total_len > max_length:
- # truncate words up to max_length
- ids, pair_ids, overflowing_tokens = self.truncate_sequences(
- ids,
- pair_ids=pair_ids,
- num_tokens_to_remove=total_len - max_length,
- truncation_strategy=truncation_strategy,
- stride=stride,
- )
- if return_overflowing_tokens:
- encoded_inputs["overflowing_tokens"] = overflowing_tokens
- encoded_inputs["num_truncated_tokens"] = total_len - max_length
- # Add special tokens
- if add_special_tokens:
- sequence = self.build_inputs_with_special_tokens(ids, pair_ids)
- token_type_ids = self.create_token_type_ids_from_sequences(ids, pair_ids)
- entity_token_offset = 1 # 1 * <s> token
- pair_entity_token_offset = len(ids) + 3 # 1 * <s> token & 2 * <sep> tokens
- else:
- sequence = ids + pair_ids if pair else ids
- token_type_ids = [0] * len(ids) + ([0] * len(pair_ids) if pair else [])
- entity_token_offset = 0
- pair_entity_token_offset = len(ids)
- # Build output dictionary
- encoded_inputs["input_ids"] = sequence
- if return_token_type_ids:
- encoded_inputs["token_type_ids"] = token_type_ids
- if return_special_tokens_mask:
- if add_special_tokens:
- encoded_inputs["special_tokens_mask"] = self.get_special_tokens_mask(ids, pair_ids)
- else:
- encoded_inputs["special_tokens_mask"] = [0] * len(sequence)
- # Set max entity length
- if not max_entity_length:
- max_entity_length = self.max_entity_length
- if entity_ids is not None:
- total_entity_len = 0
- num_invalid_entities = 0
- valid_entity_ids = [ent_id for ent_id, span in zip(entity_ids, entity_token_spans) if span[1] <= len(ids)]
- valid_entity_token_spans = [span for span in entity_token_spans if span[1] <= len(ids)]
- total_entity_len += len(valid_entity_ids)
- num_invalid_entities += len(entity_ids) - len(valid_entity_ids)
- valid_pair_entity_ids, valid_pair_entity_token_spans = None, None
- if pair_entity_ids is not None:
- valid_pair_entity_ids = [
- ent_id
- for ent_id, span in zip(pair_entity_ids, pair_entity_token_spans)
- if span[1] <= len(pair_ids)
- ]
- valid_pair_entity_token_spans = [span for span in pair_entity_token_spans if span[1] <= len(pair_ids)]
- total_entity_len += len(valid_pair_entity_ids)
- num_invalid_entities += len(pair_entity_ids) - len(valid_pair_entity_ids)
- if num_invalid_entities != 0:
- logger.warning(
- f"{num_invalid_entities} entities are ignored because their entity spans are invalid due to the"
- " truncation of input tokens"
- )
- if truncation_strategy != TruncationStrategy.DO_NOT_TRUNCATE and total_entity_len > max_entity_length:
- # truncate entities up to max_entity_length
- valid_entity_ids, valid_pair_entity_ids, overflowing_entities = self.truncate_sequences(
- valid_entity_ids,
- pair_ids=valid_pair_entity_ids,
- num_tokens_to_remove=total_entity_len - max_entity_length,
- truncation_strategy=truncation_strategy,
- stride=stride,
- )
- valid_entity_token_spans = valid_entity_token_spans[: len(valid_entity_ids)]
- if valid_pair_entity_token_spans is not None:
- valid_pair_entity_token_spans = valid_pair_entity_token_spans[: len(valid_pair_entity_ids)]
- if return_overflowing_tokens:
- encoded_inputs["overflowing_entities"] = overflowing_entities
- encoded_inputs["num_truncated_entities"] = total_entity_len - max_entity_length
- final_entity_ids = valid_entity_ids + valid_pair_entity_ids if valid_pair_entity_ids else valid_entity_ids
- encoded_inputs["entity_ids"] = list(final_entity_ids)
- entity_position_ids = []
- entity_start_positions = []
- entity_end_positions = []
- for token_spans, offset in (
- (valid_entity_token_spans, entity_token_offset),
- (valid_pair_entity_token_spans, pair_entity_token_offset),
- ):
- if token_spans is not None:
- for start, end in token_spans:
- start += offset
- end += offset
- position_ids = list(range(start, end))[: self.max_mention_length]
- position_ids += [-1] * (self.max_mention_length - end + start)
- entity_position_ids.append(position_ids)
- entity_start_positions.append(start)
- entity_end_positions.append(end - 1)
- encoded_inputs["entity_position_ids"] = entity_position_ids
- if self.task == "entity_span_classification":
- encoded_inputs["entity_start_positions"] = entity_start_positions
- encoded_inputs["entity_end_positions"] = entity_end_positions
- if return_token_type_ids:
- encoded_inputs["entity_token_type_ids"] = [0] * len(encoded_inputs["entity_ids"])
- # Check lengths
- self._eventual_warn_about_too_long_sequence(encoded_inputs["input_ids"], max_length, verbose)
- # Padding
- if padding_strategy != PaddingStrategy.DO_NOT_PAD or return_attention_mask:
- encoded_inputs = self.pad(
- encoded_inputs,
- max_length=max_length,
- max_entity_length=max_entity_length,
- padding=padding_strategy.value,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_attention_mask=return_attention_mask,
- )
- if return_length:
- encoded_inputs["length"] = len(encoded_inputs["input_ids"])
- batch_outputs = BatchEncoding(
- encoded_inputs, tensor_type=return_tensors, prepend_batch_axis=prepend_batch_axis
- )
- return batch_outputs
- def pad(
- self,
- encoded_inputs: BatchEncoding
- | list[BatchEncoding]
- | dict[str, EncodedInput]
- | dict[str, list[EncodedInput]]
- | list[dict[str, EncodedInput]],
- padding: bool | str | PaddingStrategy = True,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_attention_mask: bool | None = None,
- return_tensors: str | TensorType | None = None,
- verbose: bool = True,
- ) -> BatchEncoding:
- """
- 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`) .. note:: If the `encoded_inputs` passed
- are dictionary of numpy arrays or PyTorch 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.
- Args:
- 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, or PyTorch tensors),
- see the note above for the return type.
- padding (`bool`, `str` or [`~utils.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).
- max_entity_length (`int`, *optional*):
- The maximum length of the entity sequence.
- 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).
- padding_side:
- 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.
- 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?](../glossary#attention-mask)
- return_tensors (`str` or [`~utils.TensorType`], *optional*):
- If set, will return tensors instead of list of python integers. Acceptable values are:
- - `'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.
- """
- # If we have a list of dicts, let's convert it in a dict of lists
- # We do this to allow using this method as a collate_fn function in PyTorch Dataloader
- if isinstance(encoded_inputs, (list, tuple)) and isinstance(encoded_inputs[0], Mapping):
- # Call .keys() explicitly for compatibility with TensorDict and other Mapping subclasses
- encoded_inputs = {key: [example[key] for example in encoded_inputs] for key in encoded_inputs[0].keys()}
- # The model's main input name, usually `input_ids`, has be passed for padding
- if self.model_input_names[0] not in encoded_inputs:
- raise ValueError(
- "You should supply an encoding or a list of encodings to this method "
- f"that includes {self.model_input_names[0]}, but you provided {list(encoded_inputs.keys())}"
- )
- required_input = encoded_inputs[self.model_input_names[0]]
- if not required_input:
- if return_attention_mask:
- encoded_inputs["attention_mask"] = []
- return encoded_inputs
- # If we have PyTorch/NumPy tensors/arrays as inputs, we cast them as python objects
- # and rebuild them afterwards if no return_tensors is specified
- # Note that we lose the specific device the tensor may be on for PyTorch
- first_element = required_input[0]
- if isinstance(first_element, (list, tuple)):
- # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
- index = 0
- while len(required_input[index]) == 0:
- index += 1
- if index < len(required_input):
- first_element = required_input[index][0]
- # At this state, if `first_element` is still a list/tuple, it's an empty one so there is nothing to do.
- if not isinstance(first_element, (int, list, tuple)):
- if is_torch_tensor(first_element):
- return_tensors = "pt" if return_tensors is None else return_tensors
- elif isinstance(first_element, np.ndarray):
- return_tensors = "np" if return_tensors is None else return_tensors
- else:
- raise ValueError(
- f"type of {first_element} unknown: {type(first_element)}. "
- "Should be one of a python, numpy, or pytorch object."
- )
- for key, value in encoded_inputs.items():
- encoded_inputs[key] = to_py_obj(value)
- # Convert padding_strategy in PaddingStrategy
- padding_strategy, _, max_length, _ = self._get_padding_truncation_strategies(
- padding=padding, max_length=max_length, verbose=verbose
- )
- if max_entity_length is None:
- max_entity_length = self.max_entity_length
- required_input = encoded_inputs[self.model_input_names[0]]
- if required_input and not isinstance(required_input[0], (list, tuple)):
- encoded_inputs = self._pad(
- encoded_inputs,
- max_length=max_length,
- max_entity_length=max_entity_length,
- padding_strategy=padding_strategy,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_attention_mask=return_attention_mask,
- )
- return BatchEncoding(encoded_inputs, tensor_type=return_tensors)
- batch_size = len(required_input)
- if any(len(v) != batch_size for v in encoded_inputs.values()):
- raise ValueError("Some items in the output dictionary have a different batch size than others.")
- if padding_strategy == PaddingStrategy.LONGEST:
- max_length = max(len(inputs) for inputs in required_input)
- max_entity_length = (
- max(len(inputs) for inputs in encoded_inputs["entity_ids"]) if "entity_ids" in encoded_inputs else 0
- )
- padding_strategy = PaddingStrategy.MAX_LENGTH
- batch_outputs = {}
- for i in range(batch_size):
- inputs = {k: v[i] for k, v in encoded_inputs.items()}
- outputs = self._pad(
- inputs,
- max_length=max_length,
- max_entity_length=max_entity_length,
- padding_strategy=padding_strategy,
- pad_to_multiple_of=pad_to_multiple_of,
- padding_side=padding_side,
- return_attention_mask=return_attention_mask,
- )
- for key, value in outputs.items():
- if key not in batch_outputs:
- batch_outputs[key] = []
- batch_outputs[key].append(value)
- return BatchEncoding(batch_outputs, tensor_type=return_tensors)
- def _pad(
- self,
- encoded_inputs: dict[str, EncodedInput] | BatchEncoding,
- max_length: int | None = None,
- max_entity_length: int | None = None,
- padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
- pad_to_multiple_of: int | None = None,
- padding_side: str | None = None,
- return_attention_mask: bool | None = None,
- ) -> dict:
- """
- Pad encoded inputs (on left/right and up to predefined length or max length in the batch)
- Args:
- encoded_inputs:
- Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`).
- max_length: maximum length of the returned list and optionally padding length (see below).
- Will truncate by taking into account the special tokens.
- max_entity_length: The maximum length of the entity sequence.
- padding_strategy: PaddingStrategy to use for padding.
- - PaddingStrategy.LONGEST Pad to the longest sequence in the batch
- - PaddingStrategy.MAX_LENGTH: Pad to the max length (default)
- - PaddingStrategy.DO_NOT_PAD: Do not pad
- The tokenizer padding sides are defined in self.padding_side:
- - 'left': pads on the left of the sequences
- - 'right': pads on the right of the sequences
- pad_to_multiple_of: (optional) Integer if set will pad the sequence to a multiple of the provided value.
- This is especially useful to enable the use of Tensor Core on NVIDIA hardware with compute capability
- `>= 7.5` (Volta).
- padding_side:
- 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.
- return_attention_mask:
- (optional) Set to False to avoid returning attention mask (default: set to model specifics)
- """
- entities_provided = bool("entity_ids" in encoded_inputs)
- # Load from model defaults
- if return_attention_mask is None:
- return_attention_mask = "attention_mask" in self.model_input_names
- if padding_strategy == PaddingStrategy.LONGEST:
- max_length = len(encoded_inputs["input_ids"])
- if entities_provided:
- max_entity_length = len(encoded_inputs["entity_ids"])
- if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
- max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
- if (
- entities_provided
- and max_entity_length is not None
- and pad_to_multiple_of is not None
- and (max_entity_length % pad_to_multiple_of != 0)
- ):
- max_entity_length = ((max_entity_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
- needs_to_be_padded = padding_strategy != PaddingStrategy.DO_NOT_PAD and (
- len(encoded_inputs["input_ids"]) != max_length
- or (entities_provided and len(encoded_inputs["entity_ids"]) != max_entity_length)
- )
- # Initialize attention mask if not present.
- if return_attention_mask and "attention_mask" not in encoded_inputs:
- encoded_inputs["attention_mask"] = [1] * len(encoded_inputs["input_ids"])
- if entities_provided and return_attention_mask and "entity_attention_mask" not in encoded_inputs:
- encoded_inputs["entity_attention_mask"] = [1] * len(encoded_inputs["entity_ids"])
- if needs_to_be_padded:
- difference = max_length - len(encoded_inputs["input_ids"])
- padding_side = padding_side if padding_side is not None else self.padding_side
- if entities_provided:
- entity_difference = max_entity_length - len(encoded_inputs["entity_ids"])
- if padding_side == "right":
- if return_attention_mask:
- encoded_inputs["attention_mask"] = encoded_inputs["attention_mask"] + [0] * difference
- if entities_provided:
- encoded_inputs["entity_attention_mask"] = (
- encoded_inputs["entity_attention_mask"] + [0] * entity_difference
- )
- if "token_type_ids" in encoded_inputs:
- encoded_inputs["token_type_ids"] = encoded_inputs["token_type_ids"] + [0] * difference
- if entities_provided:
- encoded_inputs["entity_token_type_ids"] = (
- encoded_inputs["entity_token_type_ids"] + [0] * entity_difference
- )
- if "special_tokens_mask" in encoded_inputs:
- encoded_inputs["special_tokens_mask"] = encoded_inputs["special_tokens_mask"] + [1] * difference
- encoded_inputs["input_ids"] = encoded_inputs["input_ids"] + [self.pad_token_id] * difference
- if entities_provided:
- encoded_inputs["entity_ids"] = (
- encoded_inputs["entity_ids"] + [self.entity_pad_token_id] * entity_difference
- )
- encoded_inputs["entity_position_ids"] = (
- encoded_inputs["entity_position_ids"] + [[-1] * self.max_mention_length] * entity_difference
- )
- if self.task == "entity_span_classification":
- encoded_inputs["entity_start_positions"] = (
- encoded_inputs["entity_start_positions"] + [0] * entity_difference
- )
- encoded_inputs["entity_end_positions"] = (
- encoded_inputs["entity_end_positions"] + [0] * entity_difference
- )
- elif padding_side == "left":
- if return_attention_mask:
- encoded_inputs["attention_mask"] = [0] * difference + encoded_inputs["attention_mask"]
- if entities_provided:
- encoded_inputs["entity_attention_mask"] = [0] * entity_difference + encoded_inputs[
- "entity_attention_mask"
- ]
- if "token_type_ids" in encoded_inputs:
- encoded_inputs["token_type_ids"] = [0] * difference + encoded_inputs["token_type_ids"]
- if entities_provided:
- encoded_inputs["entity_token_type_ids"] = [0] * entity_difference + encoded_inputs[
- "entity_token_type_ids"
- ]
- if "special_tokens_mask" in encoded_inputs:
- encoded_inputs["special_tokens_mask"] = [1] * difference + encoded_inputs["special_tokens_mask"]
- encoded_inputs["input_ids"] = [self.pad_token_id] * difference + encoded_inputs["input_ids"]
- if entities_provided:
- encoded_inputs["entity_ids"] = [self.entity_pad_token_id] * entity_difference + encoded_inputs[
- "entity_ids"
- ]
- encoded_inputs["entity_position_ids"] = [
- [-1] * self.max_mention_length
- ] * entity_difference + encoded_inputs["entity_position_ids"]
- if self.task == "entity_span_classification":
- encoded_inputs["entity_start_positions"] = [0] * entity_difference + encoded_inputs[
- "entity_start_positions"
- ]
- encoded_inputs["entity_end_positions"] = [0] * entity_difference + encoded_inputs[
- "entity_end_positions"
- ]
- else:
- raise ValueError("Invalid padding strategy:" + str(padding_side))
- return encoded_inputs
- def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str]:
- """
- Save only the entity vocabulary file. The tokenizer.json is saved by the parent TokenizersBackend.
- """
- if not os.path.isdir(save_directory):
- logger.error(f"Vocabulary path ({save_directory}) should be a directory")
- return ()
- entity_vocab_file = os.path.join(
- save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["entity_vocab_file"]
- )
- with open(entity_vocab_file, "w", encoding="utf-8") as f:
- f.write(json.dumps(self.entity_vocab, indent=2, sort_keys=True, ensure_ascii=False) + "\n")
- return (entity_vocab_file,)
- def build_inputs_with_special_tokens(
- self, token_ids_0: list[int], token_ids_1: list[int] | None = None
- ) -> list[int]:
- """
- Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
- adding special tokens. An XLM-RoBERTa sequence has the following format:
- - single sequence: `<s> X </s>`
- - pair of sequences: `<s> A </s></s> B </s>`
- Args:
- token_ids_0 (`list[int]`):
- List of IDs to which the special tokens will be added.
- token_ids_1 (`list[int]`, *optional*):
- Optional second list of IDs for sequence pairs.
- Returns:
- `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
- """
- if token_ids_1 is None:
- return [self.cls_token_id] + token_ids_0 + [self.sep_token_id]
- cls = [self.cls_token_id]
- sep = [self.sep_token_id]
- return cls + token_ids_0 + sep + sep + token_ids_1 + sep
- def get_special_tokens_mask(
- self, token_ids_0: list[int], token_ids_1: list[int] | None = None, already_has_special_tokens: bool = False
- ) -> list[int]:
- """
- Retrieve 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` method.
- Args:
- token_ids_0 (`list[int]`):
- List of IDs.
- token_ids_1 (`list[int]`, *optional*):
- Optional second list of IDs for sequence pairs.
- 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:
- `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
- """
- if already_has_special_tokens:
- return super().get_special_tokens_mask(
- token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
- )
- if token_ids_1 is None:
- return [1] + ([0] * len(token_ids_0)) + [1]
- return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1]
- def create_token_type_ids_from_sequences(
- self, token_ids_0: list[int], token_ids_1: list[int] | None = None
- ) -> list[int]:
- """
- Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does
- not make use of token type ids, therefore a list of zeros is returned.
- Args:
- token_ids_0 (`list[int]`):
- List of IDs.
- token_ids_1 (`list[int]`, *optional*):
- Optional second list of IDs for sequence pairs.
- Returns:
- `list[int]`: List of zeros.
- """
- sep = [self.sep_token_id]
- cls = [self.cls_token_id]
- if token_ids_1 is None:
- return len(cls + token_ids_0 + sep) * [0]
- return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0]
- __all__ = ["MLukeTokenizer"]
|