





import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
      from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor






class        snake_case_    :






  def __init__(							self			:					Union[str, Any]       ,  _snake_case			:					Optional[int]       ,  _snake_case			:					Union[str, Any]=13       ,  _snake_case			:					str=32       ,  _snake_case			:					List[Any]=2       ,  _snake_case			:					Dict=3       ,  _snake_case			:					Optional[int]=16       ,  _snake_case			:					Optional[Any]=[1, 2, 1]       ,  _snake_case			:					Union[str, Any]=[2, 2, 4]       ,  _snake_case			:					Optional[Any]=2       ,  _snake_case			:					Union[str, Any]=2.0       ,  _snake_case			:					Optional[int]=True       ,  _snake_case			:					Union[str, Any]=0.0       ,  _snake_case			:					List[Any]=0.0       ,  _snake_case			:					Union[str, Any]=0.1       ,  _snake_case			:					List[str]="gelu"       ,  _snake_case			:					Optional[int]=False       ,  _snake_case			:					Optional[int]=True       ,  _snake_case			:					str=0.02       ,  _snake_case			:					List[Any]=1E-5       ,  _snake_case			:					List[str]=True       ,  _snake_case			:					str=None       ,  _snake_case			:					int=True       ,  _snake_case			:					List[Any]=10       ,  _snake_case			:					str=8       ,  )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       parent
   __lowerCAmelCase					:	List[Any]      =       batch_size
   __lowerCAmelCase					:	Any      =       image_size
   __lowerCAmelCase					:	Union[str, Any]      =       patch_size
   __lowerCAmelCase					:	List[Any]      =       num_channels
   __lowerCAmelCase					:	List[str]      =       embed_dim
   __lowerCAmelCase					:	Optional[Any]      =       depths
   __lowerCAmelCase					:	Any      =       num_heads
   __lowerCAmelCase					:	Union[str, Any]      =       window_size
   __lowerCAmelCase					:	Union[str, Any]      =       mlp_ratio
   __lowerCAmelCase					:	Any      =       qkv_bias
   __lowerCAmelCase					:	Dict      =       hidden_dropout_prob
   __lowerCAmelCase					:	Optional[int]      =       attention_probs_dropout_prob
   __lowerCAmelCase					:	Union[str, Any]      =       drop_path_rate
   __lowerCAmelCase					:	Union[str, Any]      =       hidden_act
   __lowerCAmelCase					:	Any      =       use_absolute_embeddings
   __lowerCAmelCase					:	Optional[int]      =       patch_norm
   __lowerCAmelCase					:	List[str]      =       layer_norm_eps
   __lowerCAmelCase					:	List[str]      =       initializer_range
   __lowerCAmelCase					:	List[str]      =       is_training
   __lowerCAmelCase					:	Tuple      =       scope
   __lowerCAmelCase					:	List[str]      =       use_labels
   __lowerCAmelCase					:	List[str]      =       type_sequence_label_size
   __lowerCAmelCase					:	List[Any]      =       encoder_stride






  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->Any:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

   __lowerCAmelCase					:	Union[str, Any]      =       None
   if self.use_labels:
    __lowerCAmelCase					:	List[Any]      =       ids_tensor([self.batch_size]       ,  self.type_sequence_label_size      )

   __lowerCAmelCase					:	List[str]      =       self.get_config()

   return config, pixel_values, labels






  def 				UpperCAmelCase__						(							self			:					Any      )->List[Any]:






   '''simple docstring'''

   return SwinvaConfig(
       image_size=self.image_size       ,  patch_size=self.patch_size       ,  num_channels=self.num_channels       ,  embed_dim=self.embed_dim       ,  depths=self.depths       ,  num_heads=self.num_heads       ,  window_size=self.window_size       ,  mlp_ratio=self.mlp_ratio       ,  qkv_bias=self.qkv_bias       ,  hidden_dropout_prob=self.hidden_dropout_prob       ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,  drop_path_rate=self.drop_path_rate       ,  hidden_act=self.hidden_act       ,  use_absolute_embeddings=self.use_absolute_embeddings       ,  path_norm=self.patch_norm       ,  layer_norm_eps=self.layer_norm_eps       ,  initializer_range=self.initializer_range       ,  encoder_stride=self.encoder_stride       ,  )






  def 				UpperCAmelCase__						(							self			:					List[Any]       ,  _snake_case			:					str       ,  _snake_case			:					Optional[Any]       ,  _snake_case			:					Dict      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Dict      =       SwinvaModel(config=_snake_case      )
   model.to(_snake_case      )
   model.eval()
   __lowerCAmelCase					:	int      =       model(_snake_case      )

   __lowerCAmelCase					:	Union[str, Any]      =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths      ) - 1))
   __lowerCAmelCase					:	List[Any]      =       int(config.embed_dim * 2 ** (len(config.depths      ) - 1)      )

   self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, expected_seq_len, expected_dim)      )






  def 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					Optional[Any]       ,  _snake_case			:					Dict       ,  _snake_case			:					Union[str, Any]      )->Tuple:






   '''simple docstring'''

   __lowerCAmelCase					:	Dict      =       SwinvaForMaskedImageModeling(config=_snake_case      )
   model.to(_snake_case      )
   model.eval()
   __lowerCAmelCase					:	Any      =       model(_snake_case      )
   self.parent.assertEqual(
       result.logits.shape       ,  (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

   # test greyscale images
   __lowerCAmelCase					:	List[Any]      =       1
   __lowerCAmelCase					:	List[Any]      =       SwinvaForMaskedImageModeling(_snake_case      )
   model.to(_snake_case      )
   model.eval()

   __lowerCAmelCase					:	Any      =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
   __lowerCAmelCase					:	Dict      =       model(_snake_case      )
   self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, 1, self.image_size, self.image_size)      )






  def 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					str       ,  _snake_case			:					Optional[int]       ,  _snake_case			:					Union[str, Any]      )->Tuple:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       self.type_sequence_label_size
   __lowerCAmelCase					:	Tuple      =       SwinvaForImageClassification(_snake_case      )
   model.to(_snake_case      )
   model.eval()
   __lowerCAmelCase					:	int      =       model(_snake_case       ,  labels=_snake_case      )
   self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.type_sequence_label_size)      )






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       self.prepare_config_and_inputs()
   __lowerCAmelCase ,					__lowerCAmelCase ,					__lowerCAmelCase					:	Optional[Any]      =       config_and_inputs
   __lowerCAmelCase					:	int      =       {"""pixel_values""": pixel_values}
   return config, inputs_dict






@require_torch
class        snake_case_    (		__lowercase					,__lowercase					,unittest.TestCase						):
  A_      							=   (
      (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
  )
  A_      							=   (
      {'feature-extraction': SwinvaModel, 'image-classification': SwinvaForImageClassification}
      if is_torch_available()
      else {}
  )

  A_      							=   False
  A_      							=   False
  A_      							=   False
  A_      							=   False






  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->Union[str, Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       SwinvaModelTester(self      )
   __lowerCAmelCase					:	Any      =       ConfigTester(self       ,  config_class=_snake_case       ,  embed_dim=37      )






  def 				UpperCAmelCase__						(							self			:					int      )->Tuple:






   '''simple docstring'''

   self.config_tester.create_and_test_config_to_json_string()
   self.config_tester.create_and_test_config_to_json_file()
   self.config_tester.create_and_test_config_from_and_save_pretrained()
   self.config_tester.create_and_test_config_with_num_labels()
   self.config_tester.check_config_can_be_init_without_params()
   self.config_tester.check_config_arguments_init()






  def 				UpperCAmelCase__						(							self			:					Tuple      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*_snake_case      )






  @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""      )
  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Tuple:






   '''simple docstring'''

   pass






  @unittest.skip(reason="""Swinv2 does not use inputs_embeds"""      )
  def 				UpperCAmelCase__						(							self			:					List[str]      )->List[Any]:






   '''simple docstring'''

   pass






  def 				UpperCAmelCase__						(							self			:					List[str]      )->Any:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	Tuple      =       self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	int      =       model_class(_snake_case      )
    self.assertIsInstance(model.get_input_embeddings()       ,  (nn.Module)      )
    __lowerCAmelCase					:	int      =       model.get_output_embeddings()
    self.assertTrue(x is None or isinstance(_snake_case       ,  nn.Linear      )      )






  def 				UpperCAmelCase__						(							self			:					List[str]      )->Union[str, Any]:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	str      =       self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	int      =       model_class(_snake_case      )
    __lowerCAmelCase					:	Optional[int]      =       inspect.signature(model.forward      )
    # signature.parameters is an OrderedDict => so arg_names order is deterministic
    __lowerCAmelCase					:	Tuple      =       [*signature.parameters.keys()]

    __lowerCAmelCase					:	Union[str, Any]      =       ["""pixel_values"""]
    self.assertListEqual(arg_names[:1]       ,  _snake_case      )






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->int:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	Optional[int]      =       self.model_tester.prepare_config_and_inputs_for_common()
   __lowerCAmelCase					:	Optional[Any]      =       True

   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	List[str]      =       True
    __lowerCAmelCase					:	Union[str, Any]      =       False
    __lowerCAmelCase					:	Dict      =       True
    __lowerCAmelCase					:	Tuple      =       model_class(_snake_case      )
    model.to(_snake_case      )
    model.eval()
    with torch.no_grad():
     __lowerCAmelCase					:	Union[str, Any]      =       model(**self._prepare_for_class(_snake_case       ,  _snake_case      )      )
    __lowerCAmelCase					:	Union[str, Any]      =       outputs.attentions
    __lowerCAmelCase					:	str      =       len(self.model_tester.depths      )
    self.assertEqual(len(_snake_case      )       ,  _snake_case      )

    # check that output_attentions also work using config
    del inputs_dict["output_attentions"]
    __lowerCAmelCase					:	str      =       True
    __lowerCAmelCase					:	Tuple      =       config.window_size**2
    __lowerCAmelCase					:	Tuple      =       model_class(_snake_case      )
    model.to(_snake_case      )
    model.eval()
    with torch.no_grad():
     __lowerCAmelCase					:	Optional[Any]      =       model(**self._prepare_for_class(_snake_case       ,  _snake_case      )      )
    __lowerCAmelCase					:	Optional[int]      =       outputs.attentions
    self.assertEqual(len(_snake_case      )       ,  _snake_case      )

    self.assertListEqual(
        list(attentions[0].shape[-3:]      )       ,  [self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,  )
    __lowerCAmelCase					:	List[Any]      =       len(_snake_case      )

    # Check attention is always last and order is fine
    __lowerCAmelCase					:	List[str]      =       True
    __lowerCAmelCase					:	Any      =       True
    __lowerCAmelCase					:	int      =       model_class(_snake_case      )
    model.to(_snake_case      )
    model.eval()
    with torch.no_grad():
     __lowerCAmelCase					:	Optional[int]      =       model(**self._prepare_for_class(_snake_case       ,  _snake_case      )      )

    if hasattr(self.model_tester       ,  """num_hidden_states_types"""      ):
     __lowerCAmelCase					:	List[str]      =       self.model_tester.num_hidden_states_types
    else:
     # also another +1 for reshaped_hidden_states
     __lowerCAmelCase					:	Any      =       2
    self.assertEqual(out_len + added_hidden_states       ,  len(_snake_case      )      )

    __lowerCAmelCase					:	Optional[Any]      =       outputs.attentions

    self.assertEqual(len(_snake_case      )       ,  _snake_case      )

    self.assertListEqual(
        list(self_attentions[0].shape[-3:]      )       ,  [self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,  )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]       ,  _snake_case			:					List[str]       ,  _snake_case			:					Optional[Any]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Optional[int]      )->Tuple:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[int]      =       model_class(_snake_case      )
   model.to(_snake_case      )
   model.eval()

   with torch.no_grad():
    __lowerCAmelCase					:	Tuple      =       model(**self._prepare_for_class(_snake_case       ,  _snake_case      )      )

   __lowerCAmelCase					:	str      =       outputs.hidden_states

   __lowerCAmelCase					:	Dict      =       getattr(
       self.model_tester       ,  """expected_num_hidden_layers"""       ,  len(self.model_tester.depths      ) + 1      )
   self.assertEqual(len(_snake_case      )       ,  _snake_case      )

   # Swinv2 has a different seq_length
   __lowerCAmelCase					:	Any      =       (
       config.patch_size
       if isinstance(config.patch_size       ,  collections.abc.Iterable      )
       else (config.patch_size, config.patch_size)
   )

   __lowerCAmelCase					:	Any      =       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

   self.assertListEqual(
       list(hidden_states[0].shape[-2:]      )       ,  [num_patches, self.model_tester.embed_dim]       ,  )

   __lowerCAmelCase					:	Optional[Any]      =       outputs.reshaped_hidden_states
   self.assertEqual(len(_snake_case      )       ,  _snake_case      )

   __lowerCAmelCase ,					__lowerCAmelCase ,					__lowerCAmelCase ,					__lowerCAmelCase					:	Optional[int]      =       reshaped_hidden_states[0].shape
   __lowerCAmelCase					:	int      =       (
       reshaped_hidden_states[0].view(_snake_case       ,  _snake_case       ,  height * width      ).permute(0       ,  2       ,  1      )
   )
   self.assertListEqual(
       list(reshaped_hidden_states.shape[-2:]      )       ,  [num_patches, self.model_tester.embed_dim]       ,  )






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	Any      =       self.model_tester.prepare_config_and_inputs_for_common()

   __lowerCAmelCase					:	Tuple      =       (
       self.model_tester.image_size
       if isinstance(self.model_tester.image_size       ,  collections.abc.Iterable      )
       else (self.model_tester.image_size, self.model_tester.image_size)
   )

   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	int      =       True
    self.check_hidden_states_output(_snake_case       ,  _snake_case       ,  _snake_case       ,  _snake_case      )

    # check that output_hidden_states also work using config
    del inputs_dict["output_hidden_states"]
    __lowerCAmelCase					:	Tuple      =       True

    self.check_hidden_states_output(_snake_case       ,  _snake_case       ,  _snake_case       ,  _snake_case      )






  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	Tuple      =       self.model_tester.prepare_config_and_inputs_for_common()
   __lowerCAmelCase					:	Tuple      =       3

   __lowerCAmelCase					:	str      =       (
       self.model_tester.image_size
       if isinstance(self.model_tester.image_size       ,  collections.abc.Iterable      )
       else (self.model_tester.image_size, self.model_tester.image_size)
   )
   __lowerCAmelCase					:	Optional[int]      =       (
       config.patch_size
       if isinstance(config.patch_size       ,  collections.abc.Iterable      )
       else (config.patch_size, config.patch_size)
   )

   __lowerCAmelCase					:	List[Any]      =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
   __lowerCAmelCase					:	Optional[Any]      =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	Union[str, Any]      =       True
    self.check_hidden_states_output(_snake_case       ,  _snake_case       ,  _snake_case       ,  (padded_height, padded_width)      )

    # check that output_hidden_states also work using config
    del inputs_dict["output_hidden_states"]
    __lowerCAmelCase					:	str      =       True
    self.check_hidden_states_output(_snake_case       ,  _snake_case       ,  _snake_case       ,  (padded_height, padded_width)      )






  def 				UpperCAmelCase__						(							self			:					int      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	Dict      =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*_snake_case      )






  @slow
  def 				UpperCAmelCase__						(							self			:					Dict      )->Optional[Any]:






   '''simple docstring'''

   for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowerCAmelCase					:	List[str]      =       SwinvaModel.from_pretrained(_snake_case      )
    self.assertIsNotNone(_snake_case      )






  def 				UpperCAmelCase__						(							self			:					int      )->Union[str, Any]:






   '''simple docstring'''

   __lowerCAmelCase ,					__lowerCAmelCase					:	List[Any]      =       self.model_tester.prepare_config_and_inputs_for_common()

   __lowerCAmelCase					:	int      =       _config_zero_init(_snake_case      )
   for model_class in self.all_model_classes:
    __lowerCAmelCase					:	Optional[Any]      =       model_class(config=_snake_case      )
    for name, param in model.named_parameters():
     if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
      self.assertIn(
          ((param.data.mean() * 1E9).round() / 1E9).item()       ,  [0.0, 1.0]       ,  msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''       ,  )






@require_vision
@require_torch
class        snake_case_    (		unittest.TestCase						):






  @cached_property
  def 				UpperCAmelCase__						(							self			:					Tuple      )->List[str]:






   '''simple docstring'''

   return (
       AutoImageProcessor.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""      )
       if is_vision_available()
       else None
   )






  @slow
  def 				UpperCAmelCase__						(							self			:					Dict      )->Union[str, Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       SwinvaForImageClassification.from_pretrained("""microsoft/swinv2-tiny-patch4-window8-256"""      ).to(
       _snake_case      )
   __lowerCAmelCase					:	Tuple      =       self.default_image_processor

   __lowerCAmelCase					:	List[Any]      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
   __lowerCAmelCase					:	Union[str, Any]      =       image_processor(images=_snake_case       ,  return_tensors="""pt"""      ).to(_snake_case      )

   # forward pass
   with torch.no_grad():
    __lowerCAmelCase					:	Optional[int]      =       model(**_snake_case      )

   # verify the logits
   __lowerCAmelCase					:	List[Any]      =       torch.Size((1, 1000)      )
   self.assertEqual(outputs.logits.shape       ,  _snake_case      )
   __lowerCAmelCase					:	str      =       torch.tensor([-0.3_947, -0.4_306, 0.0_026]      ).to(_snake_case      )
   self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,  _snake_case       ,  atol=1E-4      )      )






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_UpperCAmelCase             =							'▁'

_UpperCAmelCase             =							{'vocab_file': 'spiece.model'}

_UpperCAmelCase             =							{
    'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'}
}

_UpperCAmelCase             =							{
    'google/pegasus-xsum': 512,
}


_UpperCAmelCase             =							logging.get_logger(__name__)






class        snake_case_    (		__lowercase						):
  A_      							=   VOCAB_FILES_NAMES

  A_      							=   VOCAB_FILES_NAMES
  A_      							=   PRETRAINED_VOCAB_FILES_MAP
  A_      							=   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  A_      							=   ['input_ids', 'attention_mask']






  def __init__(							self			:					List[Any]       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Union[str, Any]="<pad>"       ,  _snake_case			:					int="</s>"       ,  _snake_case			:					Any="<unk>"       ,  _snake_case			:					Union[str, Any]="<mask_2>"       ,  _snake_case			:					Any="<mask_1>"       ,  _snake_case			:					Optional[int]=None       ,  _snake_case			:					List[str]=103       ,  _snake_case			:					Optional[Dict[str, Any]] = None       ,  **_snake_case			:					Optional[int]       ,  )->None:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       offset
   if additional_special_tokens is not None:
    if not isinstance(_snake_case       ,  _snake_case      ):
     raise TypeError(
         F'''additional_special_tokens should be of type {type(_snake_case      )}, but is'''
         F''' {type(_snake_case      )}'''      )

    __lowerCAmelCase					:	List[str]      =       (
        ([mask_token_sent] + additional_special_tokens)
        if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
        else additional_special_tokens
    )
    # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
    additional_special_tokens_extended += [
        F'''<unk_{i}>''' for i in range(len(_snake_case      )       ,  self.offset - 1      )
    ]

    if len(set(_snake_case      )      ) != len(_snake_case      ):
     raise ValueError(
         """Please make sure that the provided additional_special_tokens do not contain an incorrectly"""
         F''' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.'''      )
    __lowerCAmelCase					:	Dict      =       additional_special_tokens_extended
   else:
    __lowerCAmelCase					:	Tuple      =       [mask_token_sent] if mask_token_sent is not None else []
    additional_special_tokens += [F'''<unk_{i}>''' for i in range(2       ,  self.offset      )]

   __lowerCAmelCase					:	Optional[int]      =       {} if sp_model_kwargs is None else sp_model_kwargs

   super().__init__(
       eos_token=_snake_case       ,  unk_token=_snake_case       ,  mask_token=_snake_case       ,  pad_token=_snake_case       ,  mask_token_sent=_snake_case       ,  offset=_snake_case       ,  additional_special_tokens=_snake_case       ,  sp_model_kwargs=self.sp_model_kwargs       ,  **_snake_case       ,  )
   __lowerCAmelCase					:	Optional[Any]      =       mask_token_sent
   __lowerCAmelCase					:	Any      =       vocab_file
   __lowerCAmelCase					:	Any      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
   self.sp_model.Load(_snake_case      )

   # add special tokens to encoder dict
   __lowerCAmelCase					:	Dict[int, str]      =       {
       0: self.pad_token,
       1: self.eos_token,
   }

   if self.mask_token_sent is not None:
    self.encoder.update(
        {
            2: self.mask_token_sent,
            3: self.mask_token,
        }      )

   if self.offset > 0:
    # entries 2-104 are only used for pretraining and called <mask_1>, <mask_2>, unk_2, ...unk_102
    # mask_token_sent is already added to list -> so start at 1
    self.encoder.update({i + 3: additional_special_tokens[i] for i in range(1       ,  self.offset - 1      )}      )

   __lowerCAmelCase					:	Dict[str, int]      =       {v: k for k, v in self.encoder.items()}






  @property
  def 				UpperCAmelCase__						(							self			:					str      )->int:






   '''simple docstring'''

   return len(self.sp_model      ) + self.offset






  def 				UpperCAmelCase__						(							self			:					Dict      )->Dict[str, int]:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       {self.convert_ids_to_tokens(_snake_case      ): i for i in range(self.vocab_size      )}
   vocab.update(self.added_tokens_encoder      )
   return vocab






  def __getstate__(							self			:					Optional[int]      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       self.__dict__.copy()
   __lowerCAmelCase					:	Union[str, Any]      =       None
   return state






  def __setstate__(							self			:					Any       ,  _snake_case			:					str      )->Any:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       d

   # for backward compatibility
   if not hasattr(self       ,  """sp_model_kwargs"""      ):
    __lowerCAmelCase					:	Any      =       {}

   __lowerCAmelCase					:	Optional[int]      =       spm.SentencePieceProcessor(**self.sp_model_kwargs      )
   self.sp_model.Load(self.vocab_file      )






  def 				UpperCAmelCase__						(							self			:					Dict       ,  _snake_case			:					str      )->List[str]:






   '''simple docstring'''

   return self.sp_model.encode(_snake_case       ,  out_type=_snake_case      )






  def 				UpperCAmelCase__						(							self			:					Tuple       ,  _snake_case			:					str      )->int:






   '''simple docstring'''

   if token in self.decoder:
    return self.decoder[token]
   elif token in self.added_tokens_decoder:
    return self.added_tokens_decoder[token]
   __lowerCAmelCase					:	Any      =       self.sp_model.piece_to_id(_snake_case      )
   return sp_id + self.offset






  def 				UpperCAmelCase__						(							self			:					List[Any]       ,  _snake_case			:					int      )->str:






   '''simple docstring'''

   if index in self.encoder:
    return self.encoder[index]
   elif index in self.added_tokens_encoder:
    return self.added_tokens_encoder[index]
   else:
    __lowerCAmelCase					:	Optional[int]      =       self.sp_model.IdToPiece(index - self.offset      )
   return token






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]       ,  _snake_case			:					Optional[int]      )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       []
   __lowerCAmelCase					:	Dict      =       """"""
   for token in tokens:
    # make sure that special tokens are not decoded using sentencepiece model
    if token in self.all_special_tokens:
     out_string += self.sp_model.decode(_snake_case      ) + token
     __lowerCAmelCase					:	int      =       []
    else:
     current_sub_tokens.append(_snake_case      )
   out_string += self.sp_model.decode(_snake_case      )
   return out_string.strip()






  def 				UpperCAmelCase__						(							self			:					Optional[int]       ,  _snake_case			:					Dict=False      )->int:






   '''simple docstring'''

   return 1






  def 				UpperCAmelCase__						(							self			:					Tuple       ,  _snake_case			:					Tuple      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       set(self.all_special_ids      )  # call it once instead of inside list comp
   all_special_ids.remove(self.unk_token_id      )  # <unk> is only sometimes special

   return [1 if x in all_special_ids else 0 for x in seq]






  def 				UpperCAmelCase__						(							self			:					List[str]       ,  _snake_case			:					List       ,  _snake_case			:					Optional[List] = None       ,  _snake_case			:					bool = False      )->List[int]:






   '''simple docstring'''

   if already_has_special_tokens:
    return self._special_token_mask(_snake_case      )
   elif token_ids_a is None:
    return self._special_token_mask(_snake_case      ) + [1]
   else:
    return self._special_token_mask(token_ids_a + token_ids_a      ) + [1]






  def 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					Tuple=None      )->List[int]:






   '''simple docstring'''

   if token_ids_a is None:
    return token_ids_a + [self.eos_token_id]
   # We don't expect to process pairs, but leave the pair logic for API consistency
   return token_ids_a + token_ids_a + [self.eos_token_id]






  def 				UpperCAmelCase__						(							self			:					Any       ,  _snake_case			:					str       ,  _snake_case			:					Optional[str] = None      )->Tuple[str]:






   '''simple docstring'''

   if not os.path.isdir(_snake_case      ):
    logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''      )
    return
   __lowerCAmelCase					:	Optional[int]      =       os.path.join(
       _snake_case       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]      )

   if os.path.abspath(self.vocab_file      ) != os.path.abspath(_snake_case      ) and os.path.isfile(self.vocab_file      ):
    copyfile(self.vocab_file       ,  _snake_case      )
   elif not os.path.isfile(self.vocab_file      ):
    with open(_snake_case       ,  """wb"""      ) as fi:
     __lowerCAmelCase					:	Tuple      =       self.sp_model.serialized_model_proto()
     fi.write(_snake_case      )

   return (out_vocab_file,)