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


__SCREAMING_SNAKE_CASE       :     Union[str, Any]     =  logging.get_logger(__name__)
__SCREAMING_SNAKE_CASE       :     Tuple     =  {"""vocab_file""": """spiece.model"""}

__SCREAMING_SNAKE_CASE       :     int     =  {
    """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""",
    }
}

__SCREAMING_SNAKE_CASE       :     int     =  {
    """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,
}

__SCREAMING_SNAKE_CASE       :     Tuple     =  """▁"""

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  Tuple						=						VOCAB_FILES_NAMES
      __UpperCamelCase:  List[Any]						=						PRETRAINED_VOCAB_FILES_MAP
      __UpperCamelCase:  int						=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES




      def __init__(       self						:				Dict						,					A						:				Optional[int]						,					A						:				Any=True						,					A						:				List[Any]=True						,					A						:				Optional[Any]=False						,					A						:				Any="[CLS]"						,					A						:				List[str]="[SEP]"						,					A						:				str="<unk>"						,					A						:				Any="[SEP]"						,					A						:				int="<pad>"						,					A						:				Union[str, Any]="[CLS]"						,					A						:				List[str]="[MASK]"						,					A						:				Optional[Dict[str, Any]] = None						,					**A						:				int						,					):
             # Mask token behave like a normal word, i.e. include the space before it and
             # is included in the raw text, there should be a match in a non-normalized sentence.
             _UpperCAmelCase     :  Any							      =    (
                 AddedToken(A						,					lstrip=A						,					rstrip=A						,					normalized=A       )
                 if isinstance(A						,					A       )
                 else mask_token
             )

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

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

             _UpperCAmelCase     :  List[Any]							      =    do_lower_case
             _UpperCAmelCase     :  str							      =    remove_space
             _UpperCAmelCase     :  Optional[Any]							      =    keep_accents
             _UpperCAmelCase     :  Optional[int]							      =    vocab_file

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




      @property
      def      _A	(       self						:				str       ):
             return len(self.sp_model       )




      def      _A	(       self						:				Any       ):
             _UpperCAmelCase     :  Any							      =    {self.convert_ids_to_tokens(A       ): i for i in range(self.vocab_size       )}
             vocab.update(self.added_tokens_encoder       )
             return vocab




      def __getstate__(       self						:				Optional[int]       ):
             _UpperCAmelCase     :  int							      =    self.__dict__.copy()
             _UpperCAmelCase     :  List[str]							      =    None
             return state




      def __setstate__(       self						:				List[str]						,					A						:				int       ):
             _UpperCAmelCase     :  Dict							      =    d

             # for backward compatibility
             if not hasattr(self						,					"sp_model_kwargs"       ):
                    _UpperCAmelCase     :  Optional[int]							      =    {}

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




      def      _A	(       self						:				List[Any]						,					A						:				Optional[int]       ):
             if self.remove_space:
                    _UpperCAmelCase     :  int							      =    " ".join(inputs.strip().split()       )
             else:
                    _UpperCAmelCase     :  Tuple							      =    inputs
             _UpperCAmelCase     :  Optional[Any]							      =    outputs.replace("``"						,					"\""       ).replace("''"						,					"\""       )

             if not self.keep_accents:
                    _UpperCAmelCase     :  Union[str, Any]							      =    unicodedata.normalize("NFKD"						,					A       )
                    _UpperCAmelCase     :  List[Any]							      =    "".join([c for c in outputs if not unicodedata.combining(A       )]       )
             if self.do_lower_case:
                    _UpperCAmelCase     :  int							      =    outputs.lower()

             return outputs




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

             return new_pieces




      def      _A	(       self						:				Union[str, Any]						,					A						:				Optional[Any]       ):
             return self.sp_model.PieceToId(A       )




      def      _A	(       self						:				int						,					A						:				Optional[Any]       ):
             return self.sp_model.IdToPiece(A       )




      def      _A	(       self						:				int						,					A						:				Tuple       ):
             _UpperCAmelCase     :  List[str]							      =    []
             _UpperCAmelCase     :  List[Any]							      =    ""
             _UpperCAmelCase     :  Tuple							      =    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(A       ) + token
                           _UpperCAmelCase     :  Optional[int]							      =    True
                           _UpperCAmelCase     :  List[str]							      =    []
                    else:
                           current_sub_tokens.append(A       )
                           _UpperCAmelCase     :  List[Any]							      =    False
             out_string += self.sp_model.decode(A       )
             return out_string.strip()




      def      _A	(       self						:				List[Any]						,					A						:				List[int]						,					A						:				Optional[List[int]] = None       ):
             _UpperCAmelCase     :  List[str]							      =    [self.sep_token_id]
             _UpperCAmelCase     :  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      _A	(       self						:				Tuple						,					A						:				List[int]						,					A						:				Optional[List[int]] = None						,					A						:				bool = False       ):

             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 not None:
                    return [1] + ([0] * len(A       )) + [1] + ([0] * len(A       )) + [1]
             return [1] + ([0] * len(A       )) + [1]




      def      _A	(       self						:				int						,					A						:				List[int]						,					A						:				Optional[List[int]] = None       ):
             _UpperCAmelCase     :  Tuple							      =    [self.sep_token_id]
             _UpperCAmelCase     :  List[str]							      =    [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      _A	(       self						:				Union[str, Any]						,					A						:				str						,					A						:				Optional[str] = None       ):
             if not os.path.isdir(A       ):
                    logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""       )
                    return
             _UpperCAmelCase     :  Union[str, Any]							      =    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     :  int							      =    self.sp_model.serialized_model_proto()
                           fi.write(A       )

             return (out_vocab_file,)


"""simple docstring"""






from typing import List

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A			   = logging.get_logger(__name__)

__A			   = {
    "snap-research/efficientformer-l1-300": (
        "https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json"
    ),
}


class     UpperCAmelCase						(_UpperCAmelCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Optional[Any]						    = "efficientformer"






    def __init__(   self    ,			_UpperCAmelCase = [3, 2, 6, 4]    ,			_UpperCAmelCase = [48, 96, 224, 448]    ,			_UpperCAmelCase = [True, True, True, True]    ,			_UpperCAmelCase = 448    ,			_UpperCAmelCase = 32    ,			_UpperCAmelCase = 4    ,			_UpperCAmelCase = 7    ,			_UpperCAmelCase = 5    ,			_UpperCAmelCase = 8    ,			_UpperCAmelCase = 4    ,			_UpperCAmelCase = 0.0    ,			_UpperCAmelCase = 16    ,			_UpperCAmelCase = 3    ,			_UpperCAmelCase = 3    ,			_UpperCAmelCase = 3    ,			_UpperCAmelCase = 2    ,			_UpperCAmelCase = 1    ,			_UpperCAmelCase = 0.0    ,			_UpperCAmelCase = 1    ,			_UpperCAmelCase = True    ,			_UpperCAmelCase = True    ,			_UpperCAmelCase = 1e-5    ,			_UpperCAmelCase = "gelu"    ,			_UpperCAmelCase = 0.02    ,			_UpperCAmelCase = 1e-1_2    ,			_UpperCAmelCase = 224    ,			_UpperCAmelCase = 1e-0_5    ,			**_UpperCAmelCase    ,			):
       super().__init__(**_UpperCAmelCase							)

       lowercase__:   List[str]					=      hidden_act
       lowercase__:   Union[str, Any]					=      hidden_dropout_prob
       lowercase__:   Union[str, Any]					=      hidden_sizes
       lowercase__:   Any					=      num_hidden_layers
       lowercase__:   int					=      num_attention_heads
       lowercase__:   Dict					=      initializer_range
       lowercase__:   Optional[int]					=      layer_norm_eps
       lowercase__:   int					=      patch_size
       lowercase__:   int					=      num_channels
       lowercase__:   str					=      depths
       lowercase__:   List[str]					=      mlp_expansion_ratio
       lowercase__:   List[str]					=      downsamples
       lowercase__:   List[str]					=      dim
       lowercase__:   Optional[Any]					=      key_dim
       lowercase__:   Union[str, Any]					=      attention_ratio
       lowercase__:   Any					=      resolution
       lowercase__:   Any					=      pool_size
       lowercase__:   List[Any]					=      downsample_patch_size
       lowercase__:   Optional[int]					=      downsample_stride
       lowercase__:   Union[str, Any]					=      downsample_pad
       lowercase__:   List[Any]					=      drop_path_rate
       lowercase__:   Optional[Any]					=      num_metaad_blocks
       lowercase__:   Any					=      distillation
       lowercase__:   Optional[int]					=      use_layer_scale
       lowercase__:   List[str]					=      layer_scale_init_value
       lowercase__:   Dict					=      image_size
       lowercase__:   List[str]					=      batch_norm_eps
