





"""simple docstring"""
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase_       =		logging.get_logger(__name__)

lowerCamelCase_       =		{
    '''microsoft/swin-tiny-patch4-window7-224''': (
        '''https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'''
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}






class  _SCREAMING_SNAKE_CASE(       lowerCamelCase__   ,     lowerCamelCase__  ):
       SCREAMING_SNAKE_CASE_       :				List[str]     						=  '''swin'''

       SCREAMING_SNAKE_CASE_       :				Tuple     						=  {
           '''num_attention_heads''': '''num_heads''',
           '''num_hidden_layers''': '''num_layers''',
       }




       def __init__(						self		,SCREAMING_SNAKE_CASE__=2_24		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=96		,SCREAMING_SNAKE_CASE__=[2, 2, 6, 2]		,SCREAMING_SNAKE_CASE__=[3, 6, 12, 24]		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=4.0		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.0		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=False		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=1E-5		,SCREAMING_SNAKE_CASE__=32		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=None		,**SCREAMING_SNAKE_CASE__		,)      ->				Optional[Any]:





             """simple docstring"""






             super().__init__(**SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :Any		    =  image_size
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  patch_size
             __SCREAMING_SNAKE_CASE       :Tuple		    =  num_channels
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  embed_dim
             __SCREAMING_SNAKE_CASE       :List[str]		    =  depths
             __SCREAMING_SNAKE_CASE       :List[str]		    =  len(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :Tuple		    =  num_heads
             __SCREAMING_SNAKE_CASE       :str		    =  window_size
             __SCREAMING_SNAKE_CASE       :List[str]		    =  mlp_ratio
             __SCREAMING_SNAKE_CASE       :List[str]		    =  qkv_bias
             __SCREAMING_SNAKE_CASE       :Any		    =  hidden_dropout_prob
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  attention_probs_dropout_prob
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  drop_path_rate
             __SCREAMING_SNAKE_CASE       :int		    =  hidden_act
             __SCREAMING_SNAKE_CASE       :Any		    =  use_absolute_embeddings
             __SCREAMING_SNAKE_CASE       :str		    =  layer_norm_eps
             __SCREAMING_SNAKE_CASE       :List[str]		    =  initializer_range
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  encoder_stride
             # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
             # this indicates the channel dimension after the last stage of the model
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  int(embed_dim * 2 ** (len(SCREAMING_SNAKE_CASE__							) - 1)							)
             __SCREAMING_SNAKE_CASE       :Tuple		    =  ["stem"] + [f'''stage{idx}''' for idx in range(1		,len(SCREAMING_SNAKE_CASE__							) + 1							)]
             __SCREAMING_SNAKE_CASE       :Tuple		    =  get_aligned_output_features_output_indices(
                 out_features=SCREAMING_SNAKE_CASE__		,out_indices=SCREAMING_SNAKE_CASE__		,stage_names=self.stage_names							)






class  _SCREAMING_SNAKE_CASE(       lowerCamelCase__  ):
       SCREAMING_SNAKE_CASE_       :				Union[str, Any]     						=  version.parse('''1.11'''  )




       @property
       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             return OrderedDict(
                 [
                     ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
                 ]							)




       @property
       def    _UpperCamelCase					(						self							)      ->				List[Any]:





             """simple docstring"""






             return 1E-4






"""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


lowerCamelCase_       =		logging.get_logger(__name__)

lowerCamelCase_       =		{"vocab_file": "spiece.model"}

lowerCamelCase_       =		{
    "vocab_file": {
        "bert_for_seq_generation": (
            "https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model"
        ),
    }
}

lowerCamelCase_       =		{"bert_for_seq_generation": 5_1_2}






class  _SCREAMING_SNAKE_CASE(       A  ):
       SCREAMING_SNAKE_CASE_       :				str     						=  VOCAB_FILES_NAMES
       SCREAMING_SNAKE_CASE_       :				Optional[Any]     						=  PRETRAINED_VOCAB_FILES_MAP
       SCREAMING_SNAKE_CASE_       :				Optional[int]     						=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       SCREAMING_SNAKE_CASE_       :				List[int]     						=  []
       SCREAMING_SNAKE_CASE_       :				List[Any]     						=  ['''input_ids''', '''attention_mask''']




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__="<s>"		,SCREAMING_SNAKE_CASE__="</s>"		,SCREAMING_SNAKE_CASE__="<unk>"		,SCREAMING_SNAKE_CASE__="<pad>"		,SCREAMING_SNAKE_CASE__="<::::>"		,SCREAMING_SNAKE_CASE__ = None		,**SCREAMING_SNAKE_CASE__		,)      ->				None:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  {} if sp_model_kwargs is None else sp_model_kwargs

             # Add extra_ids to the special token list
             super().__init__(
                 bos_token=SCREAMING_SNAKE_CASE__		,eos_token=SCREAMING_SNAKE_CASE__		,unk_token=SCREAMING_SNAKE_CASE__		,pad_token=SCREAMING_SNAKE_CASE__		,sep_token=SCREAMING_SNAKE_CASE__		,sp_model_kwargs=self.sp_model_kwargs		,**SCREAMING_SNAKE_CASE__		,)

             __SCREAMING_SNAKE_CASE       :Dict		    =  vocab_file

             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  spm.SentencePieceProcessor(**self.sp_model_kwargs							)
             self.sp_model.Load(SCREAMING_SNAKE_CASE__							)




       @property
       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             return self.sp_model.get_piece_size()




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[Any]		    =  {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__							): i for i in range(self.vocab_size							)}
             vocab.update(self.added_tokens_encoder							)
             return vocab




       def __getstate__(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.__dict__.copy()
             __SCREAMING_SNAKE_CASE       :Dict		    =  None
             return state




       def __setstate__(						self		,SCREAMING_SNAKE_CASE__							)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  d

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

             __SCREAMING_SNAKE_CASE       :str		    =  spm.SentencePieceProcessor(**self.sp_model_kwargs							)
             self.sp_model.Load(self.vocab_file							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				List[str]:





             """simple docstring"""






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




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             return self.sp_model.piece_to_id(SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  self.sp_model.IdToPiece(SCREAMING_SNAKE_CASE__							)
             return token




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  []
             __SCREAMING_SNAKE_CASE       :List[str]		    =  ''''''
             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(SCREAMING_SNAKE_CASE__							) + token
                         __SCREAMING_SNAKE_CASE       :Dict		    =  []
                   else:
                         current_sub_tokens.append(SCREAMING_SNAKE_CASE__							)
             out_string += self.sp_model.decode(SCREAMING_SNAKE_CASE__							)
             return out_string.strip()




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = None							)      ->				Tuple[str]:





             """simple docstring"""






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

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

             return (out_vocab_file,)