





"""simple docstring"""



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

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


a_			=      logging.get_logger(__name__)
a_			=      {"""vocab_file""": """spiece.model"""}

a_			=      {
    """vocab_file""": {
        """albert-base-v1""": """https://huggingface.co/albert-base-v1/resolve/main/spiece.model""",
        """albert-large-v1""": """https://huggingface.co/albert-large-v1/resolve/main/spiece.model""",
        """albert-xlarge-v1""": """https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model""",
        """albert-xxlarge-v1""": """https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model""",
        """albert-base-v2""": """https://huggingface.co/albert-base-v2/resolve/main/spiece.model""",
        """albert-large-v2""": """https://huggingface.co/albert-large-v2/resolve/main/spiece.model""",
        """albert-xlarge-v2""": """https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model""",
        """albert-xxlarge-v2""": """https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model""",
    }
}

a_			=      {
    """albert-base-v1""": 512,
    """albert-large-v1""": 512,
    """albert-xlarge-v1""": 512,
    """albert-xxlarge-v1""": 512,
    """albert-base-v2""": 512,
    """albert-large-v2""": 512,
    """albert-xlarge-v2""": 512,
    """albert-xxlarge-v2""": 512,
}

a_			=      """▁"""






class 		__snake_case (		__lowercase					):





 """simple docstring"""


 _lowerCamelCase				=					VOCAB_FILES_NAMES
 _lowerCamelCase				=					PRETRAINED_VOCAB_FILES_MAP
 _lowerCamelCase				=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES

 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=False ,						__lowerCamelCase="[CLS]" ,						__lowerCamelCase="[SEP]" ,						__lowerCamelCase="<unk>" ,						__lowerCamelCase="[SEP]" ,						__lowerCamelCase="<pad>" ,						__lowerCamelCase="[CLS]" ,						__lowerCamelCase="[MASK]" ,						__lowerCamelCase = None ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						int									=					(
         AddedToken(UpperCAmelCase__ ,						lstrip=UpperCAmelCase__ ,						rstrip=UpperCAmelCase__ ,						normalized=UpperCAmelCase__  )
         if isinstance(UpperCAmelCase__ ,						UpperCAmelCase__  )
         else mask_token
     )

     __A   :						Any									=					{} if sp_model_kwargs is None else sp_model_kwargs

     super().__init__(
         do_lower_case=UpperCAmelCase__ ,						remove_space=UpperCAmelCase__ ,						keep_accents=UpperCAmelCase__ ,						bos_token=UpperCAmelCase__ ,						eos_token=UpperCAmelCase__ ,						unk_token=UpperCAmelCase__ ,						sep_token=UpperCAmelCase__ ,						pad_token=UpperCAmelCase__ ,						cls_token=UpperCAmelCase__ ,						mask_token=UpperCAmelCase__ ,						sp_model_kwargs=self.sp_model_kwargs ,						**UpperCAmelCase__ ,						)

     __A   :						Tuple									=					do_lower_case
     __A   :						List[Any]									=					remove_space
     __A   :						List[Any]									=					keep_accents
     __A   :						int									=					vocab_file

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

 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return len(self.sp_model  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




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

 def __getstate__(   self  ):



     '''simple docstring'''




     __A   :						int									=					self.__dict__.copy()
     __A   :						Any									=					None
     return state

 def __setstate__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Dict									=					d

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

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

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     if self.remove_space:
         __A   :						Dict									=					''' '''.join(inputs.strip().split()  )
     else:
         __A   :						List[Any]									=					inputs
     __A   :						Any									=					outputs.replace('''``''' ,						'''"'''  ).replace('''\'\'''' ,						'''"'''  )

     if not self.keep_accents:
         __A   :						Tuple									=					unicodedata.normalize('''NFKD''' ,						UpperCAmelCase__  )
         __A   :						Union[str, Any]									=					''''''.join([c for c in outputs if not unicodedata.combining(UpperCAmelCase__  )]  )
     if self.do_lower_case:
         __A   :						Union[str, Any]									=					outputs.lower()

     return outputs

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Dict									=					self.preprocess_text(UpperCAmelCase__  )
     __A   :						Optional[Any]									=					self.sp_model.encode(UpperCAmelCase__ ,						out_type=UpperCAmelCase__  )
     __A   :						Optional[int]									=					[]
     for piece in pieces:
         if len(UpperCAmelCase__  ) > 1 and piece[-1] == str(''','''  ) and piece[-2].isdigit():
             __A   :						int									=					self.sp_model.EncodeAsPieces(piece[:-1].replace(UpperCAmelCase__ ,						''''''  )  )
             if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
                 if len(cur_pieces[0]  ) == 1:
                     __A   :						Optional[int]									=					cur_pieces[1:]
                 else:
                     __A   :						Tuple									=					cur_pieces[0][1:]
             cur_pieces.append(piece[-1]  )
             new_pieces.extend(UpperCAmelCase__  )
         else:
             new_pieces.append(UpperCAmelCase__  )

     return new_pieces

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return self.sp_model.PieceToId(UpperCAmelCase__  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return self.sp_model.IdToPiece(UpperCAmelCase__  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					[]
     __A   :						List[Any]									=					''''''
     __A   :						List[Any]									=					False
     for token in tokens:
         # make sure that special tokens are not decoded using sentencepiece model
         if token in self.all_special_tokens:
             if not prev_is_special:
                 out_string += " "
             out_string += self.sp_model.decode(UpperCAmelCase__  ) + token
             __A   :						Dict									=					True
             __A   :						List[Any]									=					[]
         else:
             current_sub_tokens.append(UpperCAmelCase__  )
             __A   :						Optional[int]									=					False
     out_string += self.sp_model.decode(UpperCAmelCase__  )
     return out_string.strip()

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None  ):



     '''simple docstring'''




     __A   :						Tuple									=					[self.sep_token_id]
     __A   :						Tuple									=					[self.cls_token_id]
     if token_ids_a is None:
         return cls + token_ids_a + sep
     return cls + token_ids_a + sep + token_ids_a + sep

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None ,						__lowerCamelCase = False  ):



     '''simple docstring'''




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

     if token_ids_a is not None:
         return [1] + ([0] * len(UpperCAmelCase__  )) + [1] + ([0] * len(UpperCAmelCase__  )) + [1]
     return [1] + ([0] * len(UpperCAmelCase__  )) + [1]

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None  ):



     '''simple docstring'''




     __A   :						Tuple									=					[self.sep_token_id]
     __A   :						Any									=					[self.cls_token_id]

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







 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None  ):



     '''simple docstring'''




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

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

     return (out_vocab_file,)











"""simple docstring"""



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


a_			=      logging.get_logger(__name__)

a_			=      {
    """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""",
    # See all Dinat models at https://huggingface.co/models?filter=dinat
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					"""dinat"""

 _lowerCamelCase				=					{
     """num_attention_heads""": """num_heads""",
     """num_hidden_layers""": """num_layers""",
 }

 def __init__(   self ,						__lowerCamelCase=4 ,						__lowerCamelCase=3 ,						__lowerCamelCase=64 ,						__lowerCamelCase=[3, 4, 6, 5] ,						__lowerCamelCase=[2, 4, 8, 16] ,						__lowerCamelCase=7 ,						__lowerCamelCase=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] ,						__lowerCamelCase=3.0 ,						__lowerCamelCase=True ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=None ,						__lowerCamelCase=None ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

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




