
import os
import re
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   						=    logging.get_logger(__name__)
_SCREAMING_SNAKE_CASE   						=    {"""vocab_file""": """spiece.model"""}

_SCREAMING_SNAKE_CASE   						=    {
    """vocab_file""": {
        """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/spiece.model""",
        """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/spiece.model""",
    },
}

_SCREAMING_SNAKE_CASE   						=    {
    """google/fnet-base""": 512,
    """google/fnet-large""": 512,
}

_SCREAMING_SNAKE_CASE   						=    """▁"""







class 					UpperCAmelCase_				(							_a    ):
    UpperCAmelCase_			:       Any						 =							VOCAB_FILES_NAMES
    UpperCAmelCase_			:       Any						 =							PRETRAINED_VOCAB_FILES_MAP
    UpperCAmelCase_			:       int						 =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    UpperCAmelCase_			:       Dict						 =							['input_ids', 'token_type_ids']




    def __init__(					self  :   str      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Any=False      ,      lowerCAmelCase  :   List[str]=True      ,      lowerCAmelCase  :   Optional[int]=True      ,      lowerCAmelCase  :   str="<unk>"      ,      lowerCAmelCase  :   Optional[int]="[SEP]"      ,      lowerCAmelCase  :   Any="<pad>"      ,      lowerCAmelCase  :   Any="[CLS]"      ,      lowerCAmelCase  :   Optional[Any]="[MASK]"      ,      lowerCAmelCase  :   Optional[Dict[str, Any]] = None      ,      **lowerCAmelCase  :   Any      ,      )	-> None:
      # 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.
      a__     		=     (
          AddedToken(lowerCAmelCase      ,      lstrip=lowerCAmelCase      ,      rstrip=lowerCAmelCase      ,      normalized=lowerCAmelCase   )
          if isinstance(lowerCAmelCase      ,      lowerCAmelCase   )
          else mask_token
      )

      a__     		=     {} if sp_model_kwargs is None else sp_model_kwargs

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

      a__     		=     do_lower_case
      a__     		=     remove_space
      a__     		=     keep_accents
      a__     		=     vocab_file

      a__     		=     spm.SentencePieceProcessor(**self.sp_model_kwargs   )
      self.sp_model.Load(lowerCAmelCase   )




    @property
    def 						__lowerCamelCase (					self  :   Dict   )	-> Optional[int]:
      return len(self.sp_model   )




    def 						__lowerCamelCase (					self  :   Dict   )	-> List[Any]:
      a__     		=     {self.convert_ids_to_tokens(lowerCAmelCase   ): i for i in range(self.vocab_size   )}
      vocab.update(self.added_tokens_encoder   )
      return vocab




    def __getstate__(					self  :   Union[str, Any]   )	-> Any:
      a__     		=     self.__dict__.copy()
      a__     		=     None
      return state




    def __setstate__(					self  :   Any      ,      lowerCAmelCase  :   List[str]   )	-> str:
      a__     		=     d

      # for backward compatibility
      if not hasattr(self      ,      "sp_model_kwargs"   ):
        a__     		=     {}

      a__     		=     spm.SentencePieceProcessor(**self.sp_model_kwargs   )
      self.sp_model.Load(self.vocab_file   )




    def 						__lowerCamelCase (					self  :   Tuple      ,      lowerCAmelCase  :   List[str]   )	-> Tuple:
      if self.remove_space:
        a__     		=     " ".join(inputs.strip().split()   )
      else:
        a__     		=     inputs
      a__     		=     outputs.replace("``"      ,      "\""   ).replace("''"      ,      "\""   )

      if not self.keep_accents:
        a__     		=     unicodedata.normalize("NFKD"      ,      lowerCAmelCase   )
        a__     		=     "".join([c for c in outputs if not unicodedata.combining(lowerCAmelCase   )]   )
      if self.do_lower_case:
        a__     		=     outputs.lower()

      return outputs




    def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   str   )	-> List[str]:
      a__     		=     self.preprocess_text(lowerCAmelCase   )
      a__     		=     self.sp_model.encode(lowerCAmelCase      ,      out_type=lowerCAmelCase   )
      a__     		=     []
      for piece in pieces:
        if len(lowerCAmelCase   ) > 1 and piece[-1] == str(","   ) and piece[-2].isdigit():
          a__     		=     self.sp_model.EncodeAsPieces(piece[:-1].replace(lowerCAmelCase      ,      ""   )   )
          if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
            if len(cur_pieces[0]   ) == 1:
              a__     		=     cur_pieces[1:]
            else:
              a__     		=     cur_pieces[0][1:]
          cur_pieces.append(piece[-1]   )
          new_pieces.extend(lowerCAmelCase   )
        else:
          new_pieces.append(lowerCAmelCase   )

      return new_pieces




    def 						__lowerCamelCase (					self  :   int      ,      lowerCAmelCase  :   List[Any]   )	-> Any:
      return self.sp_model.PieceToId(lowerCAmelCase   )




    def 						__lowerCamelCase (					self  :   Optional[int]      ,      lowerCAmelCase  :   Optional[Any]   )	-> Any:
      return self.sp_model.IdToPiece(lowerCAmelCase   )




    def 						__lowerCamelCase (					self  :   Dict      ,      lowerCAmelCase  :   Dict   )	-> int:
      a__     		=     []
      a__     		=     ""
      a__     		=     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(lowerCAmelCase   ) + token
          a__     		=     True
          a__     		=     []
        else:
          current_sub_tokens.append(lowerCAmelCase   )
          a__     		=     False
      out_string += self.sp_model.decode(lowerCAmelCase   )
      return out_string.strip()




    def 						__lowerCamelCase (					self  :   Union[str, Any]      ,      lowerCAmelCase  :   List[int]      ,      lowerCAmelCase  :   bool = False      ,      lowerCAmelCase  :   bool = None      ,      lowerCAmelCase  :   bool = True      ,      **lowerCAmelCase  :   Any      ,      )	-> str:
      a__     		=     kwargs.pop("use_source_tokenizer"      ,      lowerCAmelCase   )

      a__     		=     self.convert_ids_to_tokens(lowerCAmelCase      ,      skip_special_tokens=lowerCAmelCase   )

      # To avoid mixing byte-level and unicode for byte-level BPT
      # we need to build string separately for added tokens and byte-level tokens
      # cf. https://github.com/huggingface/transformers/issues/1133
      a__     		=     []
      a__     		=     []
      for token in filtered_tokens:
        if skip_special_tokens and token in self.all_special_ids:
          continue
        if token in self.added_tokens_encoder:
          if current_sub_text:
            sub_texts.append(self.convert_tokens_to_string(lowerCAmelCase   )   )
            a__     		=     []
          sub_texts.append(lowerCAmelCase   )
        else:
          current_sub_text.append(lowerCAmelCase   )
      if current_sub_text:
        sub_texts.append(self.convert_tokens_to_string(lowerCAmelCase   )   )

      # Mimic the behavior of the Rust tokenizer:
      # No space after <unk>
      if spaces_between_special_tokens:
        a__     		=     re.sub(r"(<unk>) "      ,      r"\1"      ,      " ".join(lowerCAmelCase   )   )
      else:
        a__     		=     "".join(lowerCAmelCase   )

      a__     		=     (
          clean_up_tokenization_spaces
          if clean_up_tokenization_spaces is not None
          else self.clean_up_tokenization_spaces
      )
      if clean_up_tokenization_spaces:
        a__     		=     self.clean_up_tokenization(lowerCAmelCase   )
        return clean_text
      else:
        return text




    def 						__lowerCamelCase (					self  :   Tuple      ,      lowerCAmelCase  :   List[int]      ,      lowerCAmelCase  :   Optional[List[int]] = None   )	-> List[int]:
      a__     		=     [self.sep_token_id]
      a__     		=     [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 						__lowerCamelCase (					self  :   Dict      ,      lowerCAmelCase  :   List[int]      ,      lowerCAmelCase  :   Optional[List[int]] = None      ,      lowerCAmelCase  :   bool = False   )	-> List[int]:

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

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




    def 						__lowerCamelCase (					self  :   str      ,      lowerCAmelCase  :   List[int]      ,      lowerCAmelCase  :   Optional[List[int]] = None   )	-> List[int]:
      a__     		=     [self.sep_token_id]
      a__     		=     [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 						__lowerCamelCase (					self  :   List[Any]      ,      lowerCAmelCase  :   str      ,      lowerCAmelCase  :   Optional[str] = None   )	-> Tuple[str]:
      if not os.path.isdir(lowerCAmelCase   ):
        logger.error(f"Vocabulary path ({save_directory}) should be a directory"   )
        return
      a__     		=     os.path.join(
          lowerCAmelCase      ,      (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]   )

      if os.path.abspath(self.vocab_file   ) != os.path.abspath(lowerCAmelCase   ) and os.path.isfile(self.vocab_file   ):
        copyfile(self.vocab_file      ,      lowerCAmelCase   )
      elif not os.path.isfile(self.vocab_file   ):
        with open(lowerCAmelCase      ,      "wb"   ) as fi:
          a__     		=     self.sp_model.serialized_model_proto()
          fi.write(lowerCAmelCase   )

      return (out_vocab_file,)


import copy

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


_SCREAMING_SNAKE_CASE   						=    logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE   						=    {
    """facebook/sam-vit-huge""": """https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json""",
    """facebook/sam-vit-large""": """https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json""",
    """facebook/sam-vit-base""": """https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json""",
}







class 					UpperCAmelCase_				(							_a    ):




    def __init__(					self  :   List[str]      ,      lowerCAmelCase  :   str=256      ,      lowerCAmelCase  :   Any=1024      ,      lowerCAmelCase  :   List[str]=16      ,      lowerCAmelCase  :   Union[str, Any]=16      ,      lowerCAmelCase  :   int=4      ,      lowerCAmelCase  :   List[Any]="gelu"      ,      lowerCAmelCase  :   str=1e-6      ,      **lowerCAmelCase  :   Union[str, Any]      ,      )	-> Union[str, Any]:
      super().__init__(**lowerCAmelCase   )
      a__     		=     hidden_size
      a__     		=     image_size
      a__     		=     patch_size
      a__     		=     image_size // patch_size
      a__     		=     mask_input_channels
      a__     		=     num_point_embeddings
      a__     		=     hidden_act
      a__     		=     layer_norm_eps







class 					UpperCAmelCase_				(							_a    ):




    def __init__(					self  :   Dict      ,      lowerCAmelCase  :   str=256      ,      lowerCAmelCase  :   Union[str, Any]="relu"      ,      lowerCAmelCase  :   Optional[int]=2048      ,      lowerCAmelCase  :   Any=2      ,      lowerCAmelCase  :   Any=8      ,      lowerCAmelCase  :   Optional[Any]=2      ,      lowerCAmelCase  :   Optional[Any]=3      ,      lowerCAmelCase  :   Union[str, Any]=3      ,      lowerCAmelCase  :   List[str]=256      ,      lowerCAmelCase  :   Optional[Any]=1e-6      ,      **lowerCAmelCase  :   Any      ,      )	-> int:
      super().__init__(**lowerCAmelCase   )
      a__     		=     hidden_size
      a__     		=     hidden_act
      a__     		=     mlp_dim
      a__     		=     num_hidden_layers
      a__     		=     num_attention_heads
      a__     		=     attention_downsample_rate
      a__     		=     num_multimask_outputs
      a__     		=     iou_head_depth
      a__     		=     iou_head_hidden_dim
      a__     		=     layer_norm_eps







class 					UpperCAmelCase_				(							_a    ):




    def __init__(					self  :   Optional[Any]      ,      lowerCAmelCase  :   Optional[Any]=768      ,      lowerCAmelCase  :   int=256      ,      lowerCAmelCase  :   Tuple=12      ,      lowerCAmelCase  :   List[str]=12      ,      lowerCAmelCase  :   Tuple=3      ,      lowerCAmelCase  :   Any=1024      ,      lowerCAmelCase  :   Dict=16      ,      lowerCAmelCase  :   Dict="gelu"      ,      lowerCAmelCase  :   List[Any]=1e-0_6      ,      lowerCAmelCase  :   Any=0.0      ,      lowerCAmelCase  :   Optional[Any]=1e-1_0      ,      lowerCAmelCase  :   Tuple=True      ,      lowerCAmelCase  :   Tuple=4.0      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   List[str]=True      ,      lowerCAmelCase  :   Dict=14      ,      lowerCAmelCase  :   Optional[int]=[2, 5, 8, 11]      ,      lowerCAmelCase  :   List[str]=128      ,      lowerCAmelCase  :   List[str]=None      ,      **lowerCAmelCase  :   str      ,      )	-> Optional[int]:
      super().__init__(**lowerCAmelCase   )

      a__     		=     hidden_size
      a__     		=     output_channels
      a__     		=     num_hidden_layers
      a__     		=     num_attention_heads
      a__     		=     num_channels
      a__     		=     image_size
      a__     		=     patch_size
      a__     		=     hidden_act
      a__     		=     layer_norm_eps
      a__     		=     attention_dropout
      a__     		=     initializer_range
      a__     		=     qkv_bias
      a__     		=     mlp_ratio
      a__     		=     use_abs_pos
      a__     		=     use_rel_pos
      a__     		=     window_size
      a__     		=     global_attn_indexes
      a__     		=     num_pos_feats
      a__     		=     int(hidden_size * mlp_ratio   ) if mlp_dim is None else mlp_dim







class 					UpperCAmelCase_				(							_a    ):
    UpperCAmelCase_			:       Optional[Any]						 =							'sam'
    UpperCAmelCase_			:       List[Any]						 =							True




    def __init__(					self  :   Dict      ,      lowerCAmelCase  :   Any=None      ,      lowerCAmelCase  :   Optional[Any]=None      ,      lowerCAmelCase  :   Optional[Any]=None      ,      lowerCAmelCase  :   str=0.02      ,      **lowerCAmelCase  :   int      ,      )	-> Dict:
      super().__init__(**lowerCAmelCase   )
      a__     		=     vision_config if vision_config is not None else {}
      a__     		=     prompt_encoder_config if prompt_encoder_config is not None else {}
      a__     		=     mask_decoder_config if mask_decoder_config is not None else {}

      if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
        a__     		=     vision_config.to_dict()
      if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
        a__     		=     prompt_encoder_config.to_dict()
      if isinstance(lowerCAmelCase      ,      lowerCAmelCase   ):
        a__     		=     mask_decoder_config.to_dict()

      a__     		=     SamVisionConfig(**lowerCAmelCase   )
      a__     		=     SamPromptEncoderConfig(**lowerCAmelCase   )
      a__     		=     SamMaskDecoderConfig(**lowerCAmelCase   )
      a__     		=     initializer_range




    def 						__lowerCamelCase (					self  :   Optional[Any]   )	-> Union[str, Any]:
      a__     		=     copy.deepcopy(self.__dict__   )
      a__     		=     self.vision_config.to_dict()
      a__     		=     self.prompt_encoder_config.to_dict()
      a__     		=     self.mask_decoder_config.to_dict()
      a__     		=     self.__class__.model_type
      return output

