



"""simple docstring"""





import warnings
from typing import Dict, List, Optional, Tuple

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


__lowerCAmelCase       :					Union[str, Any]      					=					logging.get_logger(__name__)



class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__		):





  """simple docstring"""






  _lowerCamelCase			  =       ['''input_ids''', '''attention_mask''']





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







        '''simple docstring'''

        if extra_ids > 0 and additional_special_tokens is None:
              snake_case_   : Dict						=			[f'<extra_id_{i}>' for i in range(_lowercase   )]
        elif extra_ids > 0 and additional_special_tokens is not None:
              # Check that we have the right number of extra_id special tokens
              snake_case_   : Optional[Any]						=			len(set(filter(lambda _lowercase   : bool("""extra_id""" in str(_lowercase   )   )							,    _lowercase   )   )   )
              if extra_tokens != extra_ids:
                    raise ValueError(
                        f'Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are'
                        """ provided to ByT5Tokenizer. In this case the additional_special_tokens must include the"""
                        """ extra_ids tokens"""   )

        snake_case_   : List[Any]						=			AddedToken(_lowercase							,    lstrip=_lowercase							,    rstrip=_lowercase   ) if isinstance(_lowercase							,    _lowercase   ) else pad_token
        snake_case_   : Dict						=			AddedToken(_lowercase							,    lstrip=_lowercase							,    rstrip=_lowercase   ) if isinstance(_lowercase							,    _lowercase   ) else eos_token
        snake_case_   : str						=			AddedToken(_lowercase							,    lstrip=_lowercase							,    rstrip=_lowercase   ) if isinstance(_lowercase							,    _lowercase   ) else unk_token

        super().__init__(
            eos_token=_lowercase							,    unk_token=_lowercase							,    pad_token=_lowercase							,    extra_ids=_lowercase							,    additional_special_tokens=_lowercase							,    **_lowercase							,    )

        snake_case_   : List[str]						=			extra_ids

        snake_case_   : Tuple						=			2**8  # utf is 8 bits

        # define special tokens dict
        snake_case_   : Dict[int, str]						=			{
            self.pad_token: 0,
            self.eos_token: 1,
            self.unk_token: 2,
        }
        snake_case_   : int						=			len(self.special_tokens_encoder   )
        snake_case_   : Tuple						=			len(_lowercase   )
        for i, token in enumerate(_lowercase   ):
              snake_case_   : List[str]						=			self.vocab_size + i - n
        snake_case_   : Dict[str, int]						=			{v: k for k, v in self.special_tokens_encoder.items()}





  @property
  def    UpperCAmelCase__							(					self   ) ->						int:







        '''simple docstring'''

        return self._utf_vocab_size + self._num_special_tokens + self._extra_ids





  def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase = None							,    _lowercase = False   ) ->						List[int]:







        '''simple docstring'''

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

        # normal case: some special tokens
        if token_ids_a is None:
              return ([0] * len(_lowercase   )) + [1]
        return ([0] * len(_lowercase   )) + [1] + ([0] * len(_lowercase   )) + [1]





  def    UpperCAmelCase__							(					self							,    _lowercase   ) ->						List[int]:







        '''simple docstring'''

        if len(_lowercase   ) > 0 and token_ids[-1] == self.eos_token_id:
              warnings.warn(
                  f'This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated'
                  """ eos tokens being added."""   )
              return token_ids
        else:
              return token_ids + [self.eos_token_id]





  def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase = None   ) ->						List[int]:







        '''simple docstring'''

        snake_case_   : Optional[int]						=			[self.eos_token_id]

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





  def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase = None   ) ->						List[int]:







        '''simple docstring'''

        snake_case_   : List[str]						=			self._add_eos_if_not_present(_lowercase   )
        if token_ids_a is None:
              return token_ids_a
        else:
              snake_case_   : List[Any]						=			self._add_eos_if_not_present(_lowercase   )
              return token_ids_a + token_ids_a





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







        '''simple docstring'''

        snake_case_   : int						=			[chr(_lowercase   ) for i in text.encode("""utf-8"""   )]
        return tokens





  def    UpperCAmelCase__							(					self							,    _lowercase   ) ->						int:







        '''simple docstring'''

        if token in self.special_tokens_encoder:
              snake_case_   : Tuple						=			self.special_tokens_encoder[token]
        elif token in self.added_tokens_encoder:
              snake_case_   : List[Any]						=			self.added_tokens_encoder[token]
        elif len(_lowercase   ) != 1:
              snake_case_   : List[Any]						=			self.unk_token_id
        else:
              snake_case_   : List[Any]						=			ord(_lowercase   ) + self._num_special_tokens
        return token_id





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







        '''simple docstring'''

        if index in self.special_tokens_decoder:
              snake_case_   : Dict						=			self.special_tokens_decoder[index]
        else:
              snake_case_   : Any						=			chr(index - self._num_special_tokens   )
        return token





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







        '''simple docstring'''

        snake_case_   : List[str]						=			b""""""
        for token in tokens:
              if token in self.special_tokens_decoder:
                    snake_case_   : List[Any]						=			self.special_tokens_decoder[token].encode("""utf-8"""   )
              elif token in self.added_tokens_decoder:
                    snake_case_   : Optional[Any]						=			self.special_tokens_decoder[token].encode("""utf-8"""   )
              elif token in self.special_tokens_encoder:
                    snake_case_   : Optional[Any]						=			token.encode("""utf-8"""   )
              elif token in self.added_tokens_encoder:
                    snake_case_   : Optional[int]						=			token.encode("""utf-8"""   )
              else:
                    snake_case_   : Optional[Any]						=			bytes([ord(_lowercase   )]   )
              bstring += tok_string
        snake_case_   : List[Any]						=			bstring.decode("""utf-8"""							,    errors="""ignore"""   )
        return string





  def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase = None   ) ->						Tuple[str]:







        '''simple docstring'''

        return ()






"""simple docstring"""





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


__lowerCAmelCase       :					str      					=					logging.get_logger(__name__)

__lowerCAmelCase       :					Tuple      					=					{
    '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''',
    # See all Nat models at https://huggingface.co/models?filter=nat
}



class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase			  =       '''nat'''

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





		def __init__(					self							,    _lowercase=4							,    _lowercase=3							,    _lowercase=6_4							,    _lowercase=[3, 4, 6, 5]							,    _lowercase=[2, 4, 8, 1_6]							,    _lowercase=7							,    _lowercase=3.0							,    _lowercase=True							,    _lowercase=0.0							,    _lowercase=0.0							,    _lowercase=0.1							,    _lowercase="gelu"							,    _lowercase=0.02							,    _lowercase=1E-5							,    _lowercase=0.0							,    _lowercase=None							,    _lowercase=None							,    **_lowercase							,    ) ->						Optional[Any]:







								'''simple docstring'''

								super().__init__(**_lowercase   )

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

