class_docstr: 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).
class_docstr_tok: ["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","can","not","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",")","."]
docstr: 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.
docstr_tok: ["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","."]
code: def prepare_for_model(
      self,
      ids: List[int],
      pair_ids: Optional[List[int]] = None,
      entity_ids: Optional[List[int]] = None,
      pair_entity_ids: Optional[List[int]] = None,
      entity_token_spans: Optional[List[Tuple[int, int]]] = None,
      pair_entity_token_spans: Optional[List[Tuple[int, int]]] = None,
      add_special_tokens: bool = True,
      padding: Union[bool, str, PaddingStrategy] = False,
      truncation: Union[bool, str, TruncationStrategy] = None,
      max_length: Optional[int] = None,
      max_entity_length: Optional[int] = None,
      stride: int = 0,
      pad_to_multiple_of: Optional[int] = None,
      return_tensors: Optional[Union[str, TensorType]] = None,
      return_token_type_ids: Optional[bool] = None,
      return_attention_mask: 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,
      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,
              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
  
imports: import itertools
  import json
  import os
  from collections.abc import Mapping
  from shutil import copyfile
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import sentencepiece
  from ...tokenization_utils import PreTrainedTokenizer
  from ...tokenization_utils_base import ENCODE_KWARGS_DOCSTRING, AddedToken, BatchEncoding, EncodedInput, PaddingStrategy, TensorType, TextInput, TextInputPair, TruncationStrategy, to_py_obj
  from ...utils import add_end_docstrings, is_tf_tensor, is_torch_tensor, logging
label_desc: Use image node_id 16 for calling the MLukeTokenizer obj's underlying member method code with example usage: obj.prepare_for_model(ids, pair_ids, entity_ids, pair_entity_ids, entity_token_spans, pair_entity_token_spans, add_special_tokens, padding, truncation, max_length, max_entity_length, stride, pad_to_multiple_of, return_tensors, return_token_type_ids, return_attention_mask, return_overflowing_tokens, return_special_tokens_mask, return_offsets_mapping, return_length, verbose, prepend_batch_axis) and returns: batch_outputs