from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


UpperCamelCase  							=						logging.get_logger(__name__)


# General docstring
UpperCamelCase  							=						'''MobileNetV1Config'''

# Base docstring
UpperCamelCase  							=						'''google/mobilenet_v1_1.0_224'''
UpperCamelCase  							=						[1, 1024, 7, 7]

# Image classification docstring
UpperCamelCase  							=						'''google/mobilenet_v1_1.0_224'''
UpperCamelCase  							=						'''tabby, tabby cat'''


UpperCamelCase  							=						[
    '''google/mobilenet_v1_1.0_224''',
    '''google/mobilenet_v1_0.75_192''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]

def 			lowercase_				(			_lowerCamelCase							:       List[str]		,	_lowerCamelCase							:       List[Any]		,	_lowerCamelCase							:       str=None):
    lowercase__ :   Optional[int]    					=				{}

    if isinstance(_lowerCamelCase		,	_lowerCamelCase):
        lowercase__ :   Any    					=				model.mobilenet_va
    else:
        lowercase__ :   List[str]    					=				model

    lowercase__ :   List[str]    					=				"MobilenetV1/Conv2d_0/"
    lowercase__ :   Any    					=				backbone.conv_stem.convolution.weight
    lowercase__ :   Optional[Any]    					=				backbone.conv_stem.normalization.bias
    lowercase__ :   Any    					=				backbone.conv_stem.normalization.weight
    lowercase__ :   Dict    					=				backbone.conv_stem.normalization.running_mean
    lowercase__ :   Optional[Any]    					=				backbone.conv_stem.normalization.running_var

    for i in range(13):
        lowercase__ :   Tuple    					=				i + 1
        lowercase__ :   int    					=				i * 2

        lowercase__ :   Optional[Any]    					=				backbone.layer[pt_index]
        lowercase__ :   Optional[int]    					=				f'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
        lowercase__ :   Dict    					=				pointer.convolution.weight
        lowercase__ :   str    					=				pointer.normalization.bias
        lowercase__ :   Dict    					=				pointer.normalization.weight
        lowercase__ :   str    					=				pointer.normalization.running_mean
        lowercase__ :   Dict    					=				pointer.normalization.running_var

        lowercase__ :   Union[str, Any]    					=				backbone.layer[pt_index + 1]
        lowercase__ :   str    					=				f'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
        lowercase__ :   int    					=				pointer.convolution.weight
        lowercase__ :   Optional[Any]    					=				pointer.normalization.bias
        lowercase__ :   Tuple    					=				pointer.normalization.weight
        lowercase__ :   Dict    					=				pointer.normalization.running_mean
        lowercase__ :   Optional[int]    					=				pointer.normalization.running_var

    if isinstance(_lowerCamelCase		,	_lowerCamelCase):
        lowercase__ :   str    					=				"MobilenetV1/Logits/Conv2d_1c_1x1/"
        lowercase__ :   List[Any]    					=				model.classifier.weight
        lowercase__ :   int    					=				model.classifier.bias

    return tf_to_pt_map

def 			lowercase_				(			_lowerCamelCase							:       Union[str, Any]		,	_lowerCamelCase							:       str		,	_lowerCamelCase							:       Tuple):
    try:
        import numpy as np
        import tensorflow as tf
    except ImportError:
        logger.error(
            "Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see "
            "https://www.tensorflow.org/install/ for installation instructions.")
        raise

    # Load weights from TF model
    lowercase__ :   Optional[Any]    					=				tf.train.list_variables(_lowerCamelCase)
    lowercase__ :   Any    					=				{}
    for name, shape in init_vars:
        logger.info(f'''Loading TF weight {name} with shape {shape}''')
        lowercase__ :   Any    					=				tf.train.load_variable(_lowerCamelCase		,	_lowerCamelCase)
        lowercase__ :   Tuple    					=				array

    # Build TF to PyTorch weights loading map
    lowercase__ :   int    					=				_build_tf_to_pytorch_map(_lowerCamelCase		,	_lowerCamelCase		,	_lowerCamelCase)

    for name, pointer in tf_to_pt_map.items():
        logger.info(f'''Importing {name}''')
        if name not in tf_weights:
            logger.info(f'''{name} not in tf pre-trained weights, skipping''')
            continue

        lowercase__ :   Tuple    					=				tf_weights[name]

        if "depthwise_weights" in name:
            logger.info("Transposing depthwise")
            lowercase__ :   int    					=				np.transpose(_lowerCamelCase		,	(2, 3, 0, 1))
        elif "weights" in name:
            logger.info("Transposing")
            if len(pointer.shape) == 2:  # copying into linear layer
                lowercase__ :   List[Any]    					=				array.squeeze().transpose()
            else:
                lowercase__ :   List[Any]    					=				np.transpose(_lowerCamelCase		,	(3, 2, 0, 1))

        if pointer.shape != array.shape:
            raise ValueError(f'''Pointer shape {pointer.shape} and array shape {array.shape} mismatched''')

        logger.info(f'''Initialize PyTorch weight {name} {array.shape}''')
        lowercase__ :   Tuple    					=				torch.from_numpy(_lowerCamelCase)

        tf_weights.pop(_lowerCamelCase		,	_lowerCamelCase)
        tf_weights.pop(name + "/RMSProp"		,	_lowerCamelCase)
        tf_weights.pop(name + "/RMSProp_1"		,	_lowerCamelCase)
        tf_weights.pop(name + "/ExponentialMovingAverage"		,	_lowerCamelCase)

    logger.info(f'''Weights not copied to PyTorch model: {", ".join(tf_weights.keys())}''')
    return model

def 			lowercase_				(			_lowerCamelCase							:       torch.Tensor		,	_lowerCamelCase							:       nn.Convad):
    lowercase__					,   lowercase__ :   Optional[Any]    					=				features.shape[-2:]
    lowercase__					,   lowercase__ :   int    					=				conv_layer.stride
    lowercase__					,   lowercase__ :   Optional[Any]    					=				conv_layer.kernel_size

    if in_height % stride_height == 0:
        lowercase__ :   Union[str, Any]    					=				max(kernel_height - stride_height		,	0)
    else:
        lowercase__ :   List[str]    					=				max(kernel_height - (in_height % stride_height)		,	0)

    if in_width % stride_width == 0:
        lowercase__ :   List[Any]    					=				max(kernel_width - stride_width		,	0)
    else:
        lowercase__ :   Optional[int]    					=				max(kernel_width - (in_width % stride_width)		,	0)

    lowercase__ :   Tuple    					=				pad_along_width // 2
    lowercase__ :   Tuple    					=				pad_along_width - pad_left
    lowercase__ :   List[Any]    					=				pad_along_height // 2
    lowercase__ :   List[Any]    					=				pad_along_height - pad_top

    lowercase__ :   Dict    					=				(pad_left, pad_right, pad_top, pad_bottom)
    return nn.functional.pad(_lowerCamelCase		,	_lowerCamelCase		,	"constant"		,	0.0)



class   snake_case_       (		nn.Module       ):






   def __init__(      self				:     List[str]	,						lowercase_				:     MobileNetVaConfig	,						lowercase_				:     int	,						lowercase_				:     int	,						lowercase_				:     int	,						lowercase_				:     Optional[int] = 1	,						lowercase_				:     Optional[int] = 1	,						lowercase_				:     bool = False	,						lowercase_				:     Optional[bool] = True	,						lowercase_				:     Optional[bool or str] = True	,						)					->						None:
       super().__init__()
       lowercase__ :   int    					=				config

       if in_channels % groups != 0:
           raise ValueError(F'''Input channels ({in_channels}) are not divisible by {groups} groups.'''  )
       if out_channels % groups != 0:
           raise ValueError(F'''Output channels ({out_channels}) are not divisible by {groups} groups.'''  )

       lowercase__ :   Optional[int]    					=				0 if config.tf_padding else int((kernel_size - 1) / 2  )

       lowercase__ :   str    					=				nn.Convad(
           in_channels=lowercase_	,						out_channels=lowercase_	,						kernel_size=lowercase_	,						stride=lowercase_	,						padding=lowercase_	,						groups=lowercase_	,						bias=lowercase_	,						padding_mode="zeros"	,						)

       if use_normalization:
           lowercase__ :   Dict    					=				nn.BatchNormad(
               num_features=lowercase_	,						eps=config.layer_norm_eps	,						momentum=0.99_97	,						affine=lowercase_	,						track_running_stats=lowercase_	,						)
       else:
           lowercase__ :   Optional[int]    					=				None

       if use_activation:
           if isinstance(lowercase_	,						lowercase_  ):
               lowercase__ :   str    					=				ACTaFN[use_activation]
           elif isinstance(config.hidden_act	,						lowercase_  ):
               lowercase__ :   int    					=				ACTaFN[config.hidden_act]
           else:
               lowercase__ :   Tuple    					=				config.hidden_act
       else:
           lowercase__ :   Optional[Any]    					=				None







   def       __UpperCamelCase     (      self				:     Any	,						lowercase_				:     torch.Tensor  )					->						torch.Tensor:
       if self.config.tf_padding:
           lowercase__ :   int    					=				apply_tf_padding(lowercase_	,						self.convolution  )
       lowercase__ :   List[str]    					=				self.convolution(lowercase_  )
       if self.normalization is not None:
           lowercase__ :   Any    					=				self.normalization(lowercase_  )
       if self.activation is not None:
           lowercase__ :   Any    					=				self.activation(lowercase_  )
       return features



class   snake_case_       (		__A       ):
   __A	:		int         =				MobileNetVaConfig
   __A	:		List[Any]         =				load_tf_weights_in_mobilenet_va
   __A	:		Tuple         =				"mobilenet_v1"
   __A	:		str         =				"pixel_values"
   __A	:		Dict         =				False






   def       __UpperCamelCase     (      self				:     Any	,						lowercase_				:     Union[nn.Linear, nn.Convad]  )					->						None:
       if isinstance(lowercase_	,						(nn.Linear, nn.Convad)  ):
           module.weight.data.normal_(mean=0.0	,						std=self.config.initializer_range  )
           if module.bias is not None:
               module.bias.data.zero_()
       elif isinstance(lowercase_	,						nn.BatchNormad  ):
           module.bias.data.zero_()
           module.weight.data.fill_(1.0  )



UpperCamelCase  							=						R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

UpperCamelCase  							=						R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`MobileNetV1ImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
'''



@add_start_docstrings(
    "The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."  ,__A  ,)
class   snake_case_       (		__A       ):






   def __init__(      self				:     Optional[int]	,						lowercase_				:     MobileNetVaConfig	,						lowercase_				:     bool = True  )					->						Union[str, Any]:
       super().__init__(lowercase_  )
       lowercase__ :   str    					=				config

       lowercase__ :   List[Any]    					=				32
       lowercase__ :   Union[str, Any]    					=				max(int(depth * config.depth_multiplier  )	,						config.min_depth  )

       lowercase__ :   Optional[int]    					=				MobileNetVaConvLayer(
           lowercase_	,						in_channels=config.num_channels	,						out_channels=lowercase_	,						kernel_size=3	,						stride=2	,						)

       lowercase__ :   Tuple    					=				[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

       lowercase__ :   Any    					=				nn.ModuleList()
       for i in range(13  ):
           lowercase__ :   List[Any]    					=				out_channels

           if strides[i] == 2 or i == 0:
               depth *= 2
               lowercase__ :   str    					=				max(int(depth * config.depth_multiplier  )	,						config.min_depth  )

           self.layer.append(
               MobileNetVaConvLayer(
                   lowercase_	,						in_channels=lowercase_	,						out_channels=lowercase_	,						kernel_size=3	,						stride=strides[i]	,						groups=lowercase_	,						)  )

           self.layer.append(
               MobileNetVaConvLayer(
                   lowercase_	,						in_channels=lowercase_	,						out_channels=lowercase_	,						kernel_size=1	,						)  )

       lowercase__ :   Optional[Any]    					=				nn.AdaptiveAvgPoolad((1, 1)  ) if add_pooling_layer else None

       # Initialize weights and apply final processing
       self.post_init()






   def       __UpperCamelCase     (      self				:     Any	,						lowercase_				:     Dict  )					->						Optional[int]:
       raise NotImplementedError







   @add_start_docstrings_to_model_forward(lowercase_  )
   @add_code_sample_docstrings(
       checkpoint=_CHECKPOINT_FOR_DOC	,						output_type=lowercase_	,						config_class=_CONFIG_FOR_DOC	,						modality="vision"	,						expected_output=_EXPECTED_OUTPUT_SHAPE	,						)
   def       __UpperCamelCase     (      self				:     int	,						lowercase_				:     Optional[torch.Tensor] = None	,						lowercase_				:     Optional[bool] = None	,						lowercase_				:     Optional[bool] = None	,						)					->						Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
       lowercase__ :   str    					=				(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       lowercase__ :   Optional[Any]    					=				return_dict if return_dict is not None else self.config.use_return_dict

       if pixel_values is None:
           raise ValueError("You have to specify pixel_values"  )

       lowercase__ :   Dict    					=				self.conv_stem(lowercase_  )

       lowercase__ :   Union[str, Any]    					=				() if output_hidden_states else None

       for i, layer_module in enumerate(self.layer  ):
           lowercase__ :   List[Any]    					=				layer_module(lowercase_  )

           if output_hidden_states:
               lowercase__ :   Optional[Any]    					=				all_hidden_states + (hidden_states,)

       lowercase__ :   int    					=				hidden_states

       if self.pooler is not None:
           lowercase__ :   Any    					=				torch.flatten(self.pooler(lowercase_  )	,						start_dim=1  )
       else:
           lowercase__ :   List[str]    					=				None

       if not return_dict:
           return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None  )

       return BaseModelOutputWithPoolingAndNoAttention(
           last_hidden_state=lowercase_	,						pooler_output=lowercase_	,						hidden_states=lowercase_	,						)



@add_start_docstrings(
    "\n    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "  ,__A  ,)
class   snake_case_       (		__A       ):






   def __init__(      self				:     Optional[Any]	,						lowercase_				:     MobileNetVaConfig  )					->						None:
       super().__init__(lowercase_  )

       lowercase__ :   int    					=				config.num_labels
       lowercase__ :   Optional[int]    					=				MobileNetVaModel(lowercase_  )

       lowercase__ :   Optional[int]    					=				self.mobilenet_va.layer[-1].convolution.out_channels

       # Classifier head
       lowercase__ :   Optional[Any]    					=				nn.Dropout(config.classifier_dropout_prob	,						inplace=lowercase_  )
       lowercase__ :   Dict    					=				nn.Linear(lowercase_	,						config.num_labels  ) if config.num_labels > 0 else nn.Identity()

       # Initialize weights and apply final processing
       self.post_init()







   @add_start_docstrings_to_model_forward(lowercase_  )
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT	,						output_type=lowercase_	,						config_class=_CONFIG_FOR_DOC	,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,						)
   def       __UpperCamelCase     (      self				:     Any	,						lowercase_				:     Optional[torch.Tensor] = None	,						lowercase_				:     Optional[bool] = None	,						lowercase_				:     Optional[torch.Tensor] = None	,						lowercase_				:     Optional[bool] = None	,						)					->						Union[tuple, ImageClassifierOutputWithNoAttention]:
       lowercase__ :   Dict    					=				return_dict if return_dict is not None else self.config.use_return_dict

       lowercase__ :   Any    					=				self.mobilenet_va(lowercase_	,						output_hidden_states=lowercase_	,						return_dict=lowercase_  )

       lowercase__ :   int    					=				outputs.pooler_output if return_dict else outputs[1]

       lowercase__ :   Any    					=				self.classifier(self.dropout(lowercase_  )  )

       lowercase__ :   Optional[int]    					=				None
       if labels is not None:
           if self.config.problem_type is None:
               if self.num_labels == 1:
                   lowercase__ :   Dict    					=				"regression"
               elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                   lowercase__ :   Optional[Any]    					=				"single_label_classification"
               else:
                   lowercase__ :   Optional[int]    					=				"multi_label_classification"

           if self.config.problem_type == "regression":
               lowercase__ :   Tuple    					=				MSELoss()
               if self.num_labels == 1:
                   lowercase__ :   List[str]    					=				loss_fct(logits.squeeze()	,						labels.squeeze()  )
               else:
                   lowercase__ :   Optional[int]    					=				loss_fct(lowercase_	,						lowercase_  )
           elif self.config.problem_type == "single_label_classification":
               lowercase__ :   Any    					=				CrossEntropyLoss()
               lowercase__ :   Optional[int]    					=				loss_fct(logits.view(-1	,						self.num_labels  )	,						labels.view(-1  )  )
           elif self.config.problem_type == "multi_label_classification":
               lowercase__ :   Optional[Any]    					=				BCEWithLogitsLoss()
               lowercase__ :   Tuple    					=				loss_fct(lowercase_	,						lowercase_  )

       if not return_dict:
           lowercase__ :   List[Any]    					=				(logits,) + outputs[2:]
           return ((loss,) + output) if loss is not None else output

       return ImageClassifierOutputWithNoAttention(
           loss=lowercase_	,						logits=lowercase_	,						hidden_states=outputs.hidden_states	,						)







'''simple docstring'''
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__    					=      logging.get_logger(__name__)

UpperCamelCase__    					=      '''▁'''

UpperCamelCase__    					=      {'''vocab_file''': '''sentencepiece.bpe.model'''}

UpperCamelCase__    					=      {
    '''vocab_file''': {
        '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''',
    }
}

UpperCamelCase__    					=      {
    '''facebook/xglm-564M''': 2_0_4_8,
}




class 			lowerCamelCase_   (	__a      ):
      lowerCAmelCase__		     =     VOCAB_FILES_NAMES
      lowerCAmelCase__		     =     PRETRAINED_VOCAB_FILES_MAP
      lowerCAmelCase__		     =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      lowerCAmelCase__		     =     ['input_ids', 'attention_mask']






      def __init__(					self				:    Optional[int]						,						_A				:    Optional[Any]						,						_A				:    Optional[Any]="<s>"						,						_A				:    List[str]="</s>"						,						_A				:    Optional[Any]="</s>"						,						_A				:    List[str]="<s>"						,						_A				:    Tuple="<unk>"						,						_A				:    List[str]="<pad>"						,						_A				:    Optional[Dict[str, Any]] = None						,						**_A				:    Union[str, Any]						,						):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				{} if sp_model_kwargs is None else sp_model_kwargs

          # Compatibility with the original tokenizer
          UpperCAmelCase__			:		Optional[int]													=				7
          UpperCAmelCase__			:		Optional[int]													=				[f"""<madeupword{i}>""" for i in range(self.num_madeup_words )]

          UpperCAmelCase__			:		Tuple													=				kwargs.get('''additional_special_tokens'''						,						[] )
          kwargs["additional_special_tokens"] += [
              word for word in madeup_words if word not in kwargs["additional_special_tokens"]
          ]

          super().__init__(
              bos_token=_A						,						eos_token=_A						,						unk_token=_A						,						sep_token=_A						,						cls_token=_A						,						pad_token=_A						,						sp_model_kwargs=self.sp_model_kwargs						,						**_A						,						)

          UpperCAmelCase__			:		Any													=				spm.SentencePieceProcessor(**self.sp_model_kwargs )
          self.sp_model.Load(str(_A ) )
          UpperCAmelCase__			:		List[str]													=				vocab_file

          # 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'

          # The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
          UpperCAmelCase__			:		Any													=				1

          # Mimic fairseq token-to-id alignment for the first 4 token
          UpperCAmelCase__			:		Any													=				{'''<s>''': 0, '''<pad>''': 1, '''</s>''': 2, '''<unk>''': 3}

          UpperCAmelCase__			:		int													=				len(self.sp_model )
          UpperCAmelCase__			:		Optional[int]													=				{f"""<madeupword{i}>""": sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words )}
          self.fairseq_tokens_to_ids.update(_A )

          UpperCAmelCase__			:		Tuple													=				{v: k for k, v in self.fairseq_tokens_to_ids.items()}






      def __getstate__(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				self.__dict__.copy()
          UpperCAmelCase__			:		Optional[int]													=				None
          UpperCAmelCase__			:		Optional[Any]													=				self.sp_model.serialized_model_proto()
          return state






      def __setstate__(					self				:    Union[str, Any]						,						_A				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				d

          # for backward compatibility
          if not hasattr(self						,						'''sp_model_kwargs''' ):
              UpperCAmelCase__			:		Dict													=				{}

          UpperCAmelCase__			:		str													=				spm.SentencePieceProcessor(**self.sp_model_kwargs )
          self.sp_model.LoadFromSerializedProto(self.sp_model_proto )






      def lowercase_							(					self				:    Any						,						_A				:    List[int]						,						_A				:    Optional[List[int]] = None ):


          '''simple docstring'''


          if token_ids_a is None:
              return [self.sep_token_id] + token_ids_a
          UpperCAmelCase__			:		Dict													=				[self.sep_token_id]
          return sep + token_ids_a + sep + sep + token_ids_a






      def lowercase_							(					self				:    str						,						_A				:    List[int]						,						_A				:    Optional[List[int]] = None						,						_A				:    bool = False ):


          '''simple docstring'''


          if already_has_special_tokens:
              return super().get_special_tokens_mask(
                  token_ids_a=_A						,						token_ids_a=_A						,						already_has_special_tokens=_A )

          if token_ids_a is None:
              return [1] + ([0] * len(_A ))
          return [1] + ([0] * len(_A )) + [1, 1] + ([0] * len(_A ))






      def lowercase_							(					self				:    Any						,						_A				:    List[int]						,						_A				:    Optional[List[int]] = None ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[int]													=				[self.sep_token_id]

          if token_ids_a is None:
              return len(sep + token_ids_a ) * [0]
          return len(sep + token_ids_a + sep + sep + token_ids_a ) * [0]






      @property
      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          return len(self.sp_model ) + self.fairseq_offset + self.num_madeup_words






      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				{self.convert_ids_to_tokens(_A ): i for i in range(self.vocab_size )}
          vocab.update(self.added_tokens_encoder )
          return vocab






      def lowercase_							(					self				:    Optional[Any]						,						_A				:    str ):


          '''simple docstring'''


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






      def lowercase_							(					self				:    List[str]						,						_A				:    List[Any] ):


          '''simple docstring'''


          if token in self.fairseq_tokens_to_ids:
              return self.fairseq_tokens_to_ids[token]
          UpperCAmelCase__			:		Union[str, Any]													=				self.sp_model.PieceToId(_A )

          # Need to return unknown token if the SP model returned 0
          return spm_id + self.fairseq_offset if spm_id else self.unk_token_id






      def lowercase_							(					self				:    List[Any]						,						_A				:    str ):


          '''simple docstring'''


          if index in self.fairseq_ids_to_tokens:
              return self.fairseq_ids_to_tokens[index]
          return self.sp_model.IdToPiece(index - self.fairseq_offset )






      def lowercase_							(					self				:    int						,						_A				:    Optional[int] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[str]													=				''''''.join(_A ).replace(_A						,						''' ''' ).strip()
          return out_string






      def lowercase_							(					self				:    Any						,						_A				:    str						,						_A				:    Optional[str] = None ):


          '''simple docstring'''


          if not os.path.isdir(_A ):
              logger.error(f"""Vocabulary path ({save_directory}) should be a directory""" )
              return
          UpperCAmelCase__			:		List[str]													=				os.path.join(
              _A						,						(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file'''] )

          if os.path.abspath(self.vocab_file ) != os.path.abspath(_A ) and os.path.isfile(self.vocab_file ):
              copyfile(self.vocab_file						,						_A )
          elif not os.path.isfile(self.vocab_file ):
              with open(_A						,						'''wb''' ) as fi:
                  UpperCAmelCase__			:		List[Any]													=				self.sp_model.serialized_model_proto()
                  fi.write(_A )

          return (out_vocab_file,)
