






"""simple docstring"""



import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

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


a  :						Optional[int]					       =							logging.get_logger(__name__)


a  :						List[Any]					       =							{"vocab_file": "vocab.json", "merges_file": "merges.txt"}

# See all MVP models at https://huggingface.co/models?filter=mvp
a  :						List[Any]					       =							{
    "vocab_file": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json",
    },
    "added_tokens.json": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json",
    },
    "merges_file": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt",
    },
}

a  :						Any					       =							{
    "RUCAIBox/mvp": 1024,
}




@lru_cache()
def       snake_case__		(   )    ->Tuple:
 UpperCAmelCase__ 						=				(
     list(range(ord("""!"""			)       ,	ord("""~"""			) + 1			)			) + list(range(ord("""¡"""			)       ,	ord("""¬"""			) + 1			)			) + list(range(ord("""®"""			)       ,	ord("""ÿ"""			) + 1			)			)
 )
 UpperCAmelCase__ 						=				bs[:]
 UpperCAmelCase__ 						=				0
 for b in range(2**8			):
  if b not in bs:
   bs.append(_lowerCamelCase			)
   cs.append(2**8 + n			)
   n += 1
 UpperCAmelCase__ 						=				[chr(_lowerCamelCase			) for n in cs]
 return dict(zip(_lowerCamelCase       ,	_lowerCamelCase			)			)




def       snake_case__		(   _SCREAMING_SNAKE_CASE			)    ->Tuple:
 UpperCAmelCase__ 						=				set()
 UpperCAmelCase__ 						=				word[0]
 for char in word[1:]:
  pairs.add((prev_char, char)			)
  UpperCAmelCase__ 						=				char
 return pairs


class 	_UpperCamelCase ( lowercase__							):



    '''simple docstring'''


    __lowercase    :   str											=						VOCAB_FILES_NAMES
    __lowercase    :   Any											=						PRETRAINED_VOCAB_FILES_MAP
    __lowercase    :   Tuple											=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __lowercase    :   List[str]											=						['input_ids', 'attention_mask']


    def __init__(      self   ,			__lowercase   ,			__lowercase   ,			__lowercase="replace"   ,			__lowercase="<s>"   ,			__lowercase="</s>"   ,			__lowercase="</s>"   ,			__lowercase="<s>"   ,			__lowercase="<unk>"   ,			__lowercase="<pad>"   ,			__lowercase="<mask>"   ,			__lowercase=False   ,			**__lowercase   ,			):
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else bos_token
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else eos_token
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else sep_token
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else cls_token
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else unk_token
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else pad_token

     # Mask token behave like a normal word, i.e. include the space before it
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else mask_token

     super().__init__(
         errors=__lowercase   ,			bos_token=__lowercase   ,			eos_token=__lowercase   ,			unk_token=__lowercase   ,			sep_token=__lowercase   ,			cls_token=__lowercase   ,			pad_token=__lowercase   ,			mask_token=__lowercase   ,			add_prefix_space=__lowercase   ,			**__lowercase   ,			)
     with open(__lowercase   ,			encoding="""utf-8"""			) as vocab_handle:
      UpperCAmelCase__ 						=				json.load(__lowercase			)
     UpperCAmelCase__ 						=				{v: k for k, v in self.encoder.items()}
     UpperCAmelCase__ 						=				errors  # how to handle errors in decoding
     UpperCAmelCase__ 						=				bytes_to_unicode()
     UpperCAmelCase__ 						=				{v: k for k, v in self.byte_encoder.items()}
     with open(__lowercase   ,			encoding="""utf-8"""			) as merges_handle:
      UpperCAmelCase__ 						=				merges_handle.read().split("""\n"""			)[1:-1]
     UpperCAmelCase__ 						=				[tuple(merge.split()			) for merge in bpe_merges]
     UpperCAmelCase__ 						=				dict(zip(__lowercase   ,			range(len(__lowercase			)			)			)			)
     UpperCAmelCase__ 						=				{}
     UpperCAmelCase__ 						=				add_prefix_space

     # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
     UpperCAmelCase__ 						=				re.compile(r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""			)


    @property
    def 		A__      (      self			):
     return len(self.encoder			)


    def 		A__      (      self			):
     return dict(self.encoder   ,			**self.added_tokens_encoder			)


    def 		A__      (      self   ,			__lowercase			):
     if token in self.cache:
      return self.cache[token]
     UpperCAmelCase__ 						=				tuple(__lowercase			)
     UpperCAmelCase__ 						=				get_pairs(__lowercase			)

     if not pairs:
      return token

     while True:
      UpperCAmelCase__ 						=				min(__lowercase   ,			key=lambda __lowercase			: self.bpe_ranks.get(__lowercase   ,			float("""inf"""			)			)			)
      if bigram not in self.bpe_ranks:
       break
      UpperCAmelCase__ 						=				bigram
      UpperCAmelCase__ 						=				[]
      UpperCAmelCase__ 						=				0
      while i < len(__lowercase			):
       try:
        UpperCAmelCase__ 						=				word.index(__lowercase   ,			__lowercase			)
       except ValueError:
        new_word.extend(word[i:]			)
        break
       else:
        new_word.extend(word[i:j]			)
        UpperCAmelCase__ 						=				j

       if word[i] == first and i < len(__lowercase			) - 1 and word[i + 1] == second:
        new_word.append(first + second			)
        i += 2
       else:
        new_word.append(word[i]			)
        i += 1
      UpperCAmelCase__ 						=				tuple(__lowercase			)
      UpperCAmelCase__ 						=				new_word
      if len(__lowercase			) == 1:
       break
      else:
       UpperCAmelCase__ 						=				get_pairs(__lowercase			)
     UpperCAmelCase__ 						=				" ".join(__lowercase			)
     UpperCAmelCase__ 						=				word
     return word


    def 		A__      (      self   ,			__lowercase			):
     UpperCAmelCase__ 						=				[]
     for token in re.findall(self.pat   ,			__lowercase			):
      UpperCAmelCase__ 						=				"".join(
          self.byte_encoder[b] for b in token.encode("""utf-8"""			)			)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
      bpe_tokens.extend(bpe_token for bpe_token in self.bpe(__lowercase			).split(""" """			)			)
     return bpe_tokens


    def 		A__      (      self   ,			__lowercase			):
     return self.encoder.get(__lowercase   ,			self.encoder.get(self.unk_token			)			)


    def 		A__      (      self   ,			__lowercase			):
     return self.decoder.get(__lowercase			)


    def 		A__      (      self   ,			__lowercase			):
     UpperCAmelCase__ 						=				"".join(__lowercase			)
     UpperCAmelCase__ 						=				bytearray([self.byte_decoder[c] for c in text]			).decode("""utf-8"""   ,			errors=self.errors			)
     return text


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

     with open(__lowercase   ,			"""w"""   ,			encoding="""utf-8"""			) as f:
      f.write(json.dumps(self.encoder   ,			indent=2   ,			sort_keys=__lowercase   ,			ensure_ascii=__lowercase			) + """\n"""			)

     UpperCAmelCase__ 						=				0
     with open(__lowercase   ,			"""w"""   ,			encoding="""utf-8"""			) as writer:
      writer.write("""#version: 0.2\n"""			)
      for bpe_tokens, token_index in sorted(self.bpe_ranks.items()   ,			key=lambda __lowercase			: kv[1]			):
       if index != token_index:
        logger.warning(
            F'''Saving vocabulary to {merge_file}: BPE merge indices are not consecutive.'''
            """ Please check that the tokenizer is not corrupted!"""			)
        UpperCAmelCase__ 						=				token_index
       writer.write(""" """.join(__lowercase			) + """\n"""			)
       index += 1

     return vocab_file, merge_file


    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     if token_ids_a is None:
      return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
     UpperCAmelCase__ 						=				[self.cls_token_id]
     UpperCAmelCase__ 						=				[self.sep_token_id]
     return cls + token_ids_a + sep + sep + token_ids_a + sep


    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None   ,			__lowercase = False			):
     if already_has_special_tokens:
      return super().get_special_tokens_mask(
          token_ids_a=__lowercase   ,			token_ids_a=__lowercase   ,			already_has_special_tokens=__lowercase			)

     if token_ids_a is None:
      return [1] + ([0] * len(__lowercase			)) + [1]
     return [1] + ([0] * len(__lowercase			)) + [1, 1] + ([0] * len(__lowercase			)) + [1]


    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     UpperCAmelCase__ 						=				[self.sep_token_id]
     UpperCAmelCase__ 						=				[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 + sep + token_ids_a + sep			) * [0]






    def 		A__      (      self   ,			__lowercase   ,			__lowercase=False   ,			**__lowercase			):
     UpperCAmelCase__ 						=				kwargs.pop("""add_prefix_space"""   ,			self.add_prefix_space			)
     if (is_split_into_words or add_prefix_space) and (len(__lowercase			) > 0 and not text[0].isspace()):
      UpperCAmelCase__ 						=				" " + text
     return (text, kwargs)










import copy

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


__lowercase							:List[Any]							=	logging.get_logger(__name__)

__lowercase							:Optional[Any]							=	{
    "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  _a  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  List[Any]				,						a					:  int=2_56				,						a					:  Tuple=10_24				,						a					:  Optional[Any]=16				,						a					:  Dict=16				,						a					:  str=4				,						a					:  Union[str, Any]="gelu"				,						a					:  Optional[Any]=1E-6				,						**a					:  Dict				,						)						->Union[str, Any]:
													super().__init__(**a     )
													SCREAMING_SNAKE_CASE__  :					Any =       hidden_size
													SCREAMING_SNAKE_CASE__  :					List[Any] =       image_size
													SCREAMING_SNAKE_CASE__  :					List[str] =       patch_size
													SCREAMING_SNAKE_CASE__  :					str =       image_size // patch_size
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       mask_input_channels
													SCREAMING_SNAKE_CASE__  :					Any =       num_point_embeddings
													SCREAMING_SNAKE_CASE__  :					str =       hidden_act
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       layer_norm_eps







class  _a  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  Union[str, Any]				,						a					:  Optional[int]=2_56				,						a					:  Dict="relu"				,						a					:  Dict=20_48				,						a					:  Union[str, Any]=2				,						a					:  str=8				,						a					:  str=2				,						a					:  List[Any]=3				,						a					:  str=3				,						a					:  List[str]=2_56				,						a					:  str=1E-6				,						**a					:  Optional[int]				,						)						->Dict:
													super().__init__(**a     )
													SCREAMING_SNAKE_CASE__  :					List[Any] =       hidden_size
													SCREAMING_SNAKE_CASE__  :					List[Any] =       hidden_act
													SCREAMING_SNAKE_CASE__  :					Dict =       mlp_dim
													SCREAMING_SNAKE_CASE__  :					List[Any] =       num_hidden_layers
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       num_attention_heads
													SCREAMING_SNAKE_CASE__  :					List[Any] =       attention_downsample_rate
													SCREAMING_SNAKE_CASE__  :					List[str] =       num_multimask_outputs
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       iou_head_depth
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       iou_head_hidden_dim
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       layer_norm_eps







class  _a  (					lowercase__				):






							"""simple docstring"""

							def __init__(   self					:  Tuple				,						a					:  str=7_68				,						a					:  Tuple=2_56				,						a					:  Tuple=12				,						a					:  List[str]=12				,						a					:  Optional[Any]=3				,						a					:  str=10_24				,						a					:  str=16				,						a					:  Union[str, Any]="gelu"				,						a					:  Optional[int]=1E-06				,						a					:  Optional[Any]=0.0				,						a					:  str=1E-10				,						a					:  Optional[int]=True				,						a					:  Dict=4.0				,						a					:  Optional[Any]=True				,						a					:  List[str]=True				,						a					:  str=14				,						a					:  List[str]=[2, 5, 8, 11]				,						a					:  int=1_28				,						a					:  int=None				,						**a					:  List[str]				,						)						->Union[str, Any]:
													super().__init__(**a     )

													SCREAMING_SNAKE_CASE__  :					Tuple =       hidden_size
													SCREAMING_SNAKE_CASE__  :					List[Any] =       output_channels
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       num_hidden_layers
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       num_attention_heads
													SCREAMING_SNAKE_CASE__  :					List[str] =       num_channels
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       image_size
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       patch_size
													SCREAMING_SNAKE_CASE__  :					List[str] =       hidden_act
													SCREAMING_SNAKE_CASE__  :					str =       layer_norm_eps
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       attention_dropout
													SCREAMING_SNAKE_CASE__  :					List[Any] =       initializer_range
													SCREAMING_SNAKE_CASE__  :					Tuple =       qkv_bias
													SCREAMING_SNAKE_CASE__  :					Tuple =       mlp_ratio
													SCREAMING_SNAKE_CASE__  :					Tuple =       use_abs_pos
													SCREAMING_SNAKE_CASE__  :					Dict =       use_rel_pos
													SCREAMING_SNAKE_CASE__  :					Tuple =       window_size
													SCREAMING_SNAKE_CASE__  :					Optional[int] =       global_attn_indexes
													SCREAMING_SNAKE_CASE__  :					Any =       num_pos_feats
													SCREAMING_SNAKE_CASE__  :					int =       int(hidden_size * mlp_ratio     ) if mlp_dim is None else mlp_dim







class  _a  (					lowercase__				):






							"""simple docstring"""

							snake_case_							=    "sam"
							snake_case_							=    True

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

													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					Tuple =       vision_config.to_dict()
													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					Dict =       prompt_encoder_config.to_dict()
													if isinstance(a				,						a     ):
																			SCREAMING_SNAKE_CASE__  :					List[Any] =       mask_decoder_config.to_dict()

													SCREAMING_SNAKE_CASE__  :					str =       SamVisionConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					str =       SamPromptEncoderConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					int =       SamMaskDecoderConfig(**a     )
													SCREAMING_SNAKE_CASE__  :					List[str] =       initializer_range






							def   A_	(   self					:  str     )						->Dict:
													SCREAMING_SNAKE_CASE__  :					int =       copy.deepcopy(self.__dict__     )
													SCREAMING_SNAKE_CASE__  :					str =       self.vision_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Union[str, Any] =       self.prompt_encoder_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Optional[Any] =       self.mask_decoder_config.to_dict()
													SCREAMING_SNAKE_CASE__  :					Tuple =       self.__class__.model_type
													return output