




import json
import os
import re
import unicodedata
from typing import Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace
from ...utils import logging


lowercase_			:       int						       =				logging.get_logger(__name__)

lowercase_			:       str						       =				{
    'vocab_file': 'vocab.json',
    'merges_file': 'merges.txt',
}

lowercase_			:       Dict						       =				{
    'vocab_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/vocab.json'},
    'merges_file': {'openai-gpt': 'https://huggingface.co/openai-gpt/resolve/main/merges.txt'},
}

lowercase_			:       Tuple						       =				{
    'openai-gpt': 5_12,
}


def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  text.strip()
						if not text:
												return []
						_UpperCAmelCase									=  text.split()
						return tokens






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    Tuple					,					snake_case__					:    List[str]=True					,					snake_case__					:    str=None					,					snake_case__					:    Tuple=True					,					snake_case__					:    Tuple=None					,					snake_case__					:    str=True					,					):




									"""simple docstring"""


									if never_split is None:
															_UpperCAmelCase									=  []
									_UpperCAmelCase									=  do_lower_case
									_UpperCAmelCase									=  set(__lowerCamelCase   )
									_UpperCAmelCase									=  tokenize_chinese_chars
									_UpperCAmelCase									=  strip_accents
									_UpperCAmelCase									=  do_split_on_punc







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    Dict					,					snake_case__					:    Union[str, Any]=None   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.never_split.union(set(__lowerCamelCase   )   ) if never_split else self.never_split
									_UpperCAmelCase									=  self._clean_text(__lowerCamelCase   )

									# This was added on November 1st, 2018 for the multilingual and Chinese
									# models. This is also applied to the English models now, but it doesn't
									# matter since the English models were not trained on any Chinese data
									# and generally don't have any Chinese data in them (there are Chinese
									# characters in the vocabulary because Wikipedia does have some Chinese
									# words in the English Wikipedia.).
									if self.tokenize_chinese_chars:
															_UpperCAmelCase									=  self._tokenize_chinese_chars(__lowerCamelCase   )
									# prevents treating the same character with different unicode codepoints as different characters
									_UpperCAmelCase									=  unicodedata.normalize("NFC"					,					__lowerCamelCase   )
									_UpperCAmelCase									=  whitespace_tokenize(__lowerCamelCase   )
									_UpperCAmelCase									=  []
									for token in orig_tokens:
															if token not in never_split:
																					if self.do_lower_case:
																											_UpperCAmelCase									=  token.lower()
																											if self.strip_accents is not False:
																																	_UpperCAmelCase									=  self._run_strip_accents(__lowerCamelCase   )
																					elif self.strip_accents:
																											_UpperCAmelCase									=  self._run_strip_accents(__lowerCamelCase   )
															split_tokens.extend(self._run_split_on_punc(__lowerCamelCase					,					__lowerCamelCase   )   )

									_UpperCAmelCase									=  whitespace_tokenize(" ".join(__lowerCamelCase   )   )
									return output_tokens







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  unicodedata.normalize("NFD"					,					__lowerCamelCase   )
									_UpperCAmelCase									=  []
									for char in text:
															_UpperCAmelCase									=  unicodedata.category(__lowerCamelCase   )
															if cat == "Mn":
																					continue
															output.append(__lowerCamelCase   )
									return "".join(__lowerCamelCase   )







			def UpperCamelCase    ( self					:    Any					,					snake_case__					:    str					,					snake_case__					:    Any=None   ):




									"""simple docstring"""


									if not self.do_split_on_punc or (never_split is not None and text in never_split):
															return [text]
									_UpperCAmelCase									=  list(__lowerCamelCase   )
									_UpperCAmelCase									=  0
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  []
									while i < len(__lowerCamelCase   ):
															_UpperCAmelCase									=  chars[i]
															if _is_punctuation(__lowerCamelCase   ):
																					output.append([char]   )
																					_UpperCAmelCase									=  True
															else:
																					if start_new_word:
																											output.append([]   )
																					_UpperCAmelCase									=  False
																					output[-1].append(__lowerCamelCase   )
															i += 1

									return ["".join(__lowerCamelCase   ) for x in output]







			def UpperCamelCase    ( self					:    Optional[Any]					,					snake_case__					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  []
									for char in text:
															_UpperCAmelCase									=  ord(__lowerCamelCase   )
															if self._is_chinese_char(__lowerCamelCase   ):
																					output.append(" "   )
																					output.append(__lowerCamelCase   )
																					output.append(" "   )
															else:
																					output.append(__lowerCamelCase   )
									return "".join(__lowerCamelCase   )







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    Dict   ):




									"""simple docstring"""


									if (
									    (cp >= 0X4E_00 and cp <= 0X9F_FF)
									    or (cp >= 0X34_00 and cp <= 0X4D_BF)  #
									    or (cp >= 0X2_00_00 and cp <= 0X2_A6_DF)  #
									    or (cp >= 0X2_A7_00 and cp <= 0X2_B7_3F)  #
									    or (cp >= 0X2_B7_40 and cp <= 0X2_B8_1F)  #
									    or (cp >= 0X2_B8_20 and cp <= 0X2_CE_AF)  #
									    or (cp >= 0XF9_00 and cp <= 0XFA_FF)
									    or (cp >= 0X2_F8_00 and cp <= 0X2_FA_1F)  #
									):  #
															return True

									return False







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    Union[str, Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  []
									for char in text:
															_UpperCAmelCase									=  ord(__lowerCamelCase   )
															if cp == 0 or cp == 0XFF_FD or _is_control(__lowerCamelCase   ):
																					continue
															if _is_whitespace(__lowerCamelCase   ):
																					output.append(" "   )
															else:
																					output.append(__lowerCamelCase   )
									return "".join(__lowerCamelCase   )




def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  set()
						_UpperCAmelCase									=  word[0]
						for char in word[1:]:
												pairs.add((prev_char, char)		)
												_UpperCAmelCase									=  char
						return pairs


def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  text.replace("—"			,					"-"		)
						_UpperCAmelCase									=  text.replace("–"			,					"-"		)
						_UpperCAmelCase									=  text.replace("―"			,					"-"		)
						_UpperCAmelCase									=  text.replace("…"			,					"..."		)
						_UpperCAmelCase									=  text.replace("´"			,					"'"		)
						_UpperCAmelCase									=  re.sub(R"(-+|~+|!+|\"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)"			,					R" \1 "			,					UpperCamelCase__		)
						_UpperCAmelCase									=  re.sub(R"\s*\n\s*"			,					" \n "			,					UpperCamelCase__		)
						_UpperCAmelCase									=  re.sub(R"[^\S\n]+"			,					" "			,					UpperCamelCase__		)
						return text.strip()






class 					__lowerCAmelCase				( UpperCAmelCase__				):
			snake_case_   :				Optional[int]				  =  VOCAB_FILES_NAMES
			snake_case_   :				int				  =  PRETRAINED_VOCAB_FILES_MAP
			snake_case_   :				int				  =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			snake_case_   :				Tuple				  =  ["input_ids", "attention_mask"]







			def __init__( self					:    Optional[int]					,					snake_case__					:    Tuple					,					snake_case__					:    List[Any]					,					snake_case__					:    Tuple="<unk>"					,					**snake_case__					:    Optional[Any]   ):




									"""simple docstring"""


									super().__init__(unk_token=__lowerCamelCase					,					**__lowerCamelCase   )

									try:
															import ftfy
															from spacy.lang.en import English

															_UpperCAmelCase									=  English()
															_UpperCAmelCase									=  _nlp.tokenizer
															_UpperCAmelCase									=  ftfy.fix_text
									except ImportError:
															logger.warning("ftfy or spacy is not installed using BERT BasicTokenizer instead of SpaCy & ftfy."   )
															_UpperCAmelCase									=  BasicTokenizer(do_lower_case=__lowerCamelCase   )
															_UpperCAmelCase									=  None

									with open(__lowerCamelCase					,					encoding="utf-8"   ) as vocab_handle:
															_UpperCAmelCase									=  json.load(__lowerCamelCase   )
									_UpperCAmelCase									=  {v: k for k, v in self.encoder.items()}
									with open(__lowerCamelCase					,					encoding="utf-8"   ) as merges_handle:
															_UpperCAmelCase									=  merges_handle.read().split("\n"   )[1:-1]
									_UpperCAmelCase									=  [tuple(merge.split()   ) for merge in merges]
									_UpperCAmelCase									=  dict(zip(__lowerCamelCase					,					range(len(__lowerCamelCase   )   )   )   )
									_UpperCAmelCase									=  {}







			@property
			def UpperCamelCase    ( self					:    Dict   ):




									"""simple docstring"""


									return True







			@property
			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									return len(self.encoder   )







			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									return dict(self.encoder					,					**self.added_tokens_encoder   )







			def UpperCamelCase    ( self					:    Dict					,					snake_case__					:    int   ):




									"""simple docstring"""


									_UpperCAmelCase									=  tuple(token[:-1]   ) + (token[-1] + "</w>",)
									if token in self.cache:
															return self.cache[token]
									_UpperCAmelCase									=  get_pairs(__lowerCamelCase   )

									if not pairs:
															return token + "</w>"

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

																					if word[i] == first and i < len(__lowerCamelCase   ) - 1 and word[i + 1] == second:
																											new_word.append(first + second   )
																											i += 2
																					else:
																											new_word.append(word[i]   )
																											i += 1
															_UpperCAmelCase									=  tuple(__lowerCamelCase   )
															_UpperCAmelCase									=  new_word
															if len(__lowerCamelCase   ) == 1:
																					break
															else:
																					_UpperCAmelCase									=  get_pairs(__lowerCamelCase   )
									_UpperCAmelCase									=  " ".join(__lowerCamelCase   )
									if word == "\n  </w>":
															_UpperCAmelCase									=  "\n</w>"
									_UpperCAmelCase									=  word
									return word







			def UpperCamelCase    ( self					:    List[str]					,					snake_case__					:    Dict   ):




									"""simple docstring"""


									_UpperCAmelCase									=  []
									if self.fix_text is None:
															# Using BERT's BasicTokenizer
															_UpperCAmelCase									=  self.nlp.tokenize(__lowerCamelCase   )
															for token in text:
																					split_tokens.extend(list(self.bpe(__lowerCamelCase   ).split(" "   )   )   )
									else:
															# Using SpaCy & ftfy (original tokenization process of OpenAI GPT)
															_UpperCAmelCase									=  self.nlp(text_standardize(self.fix_text(__lowerCamelCase   )   )   )
															for token in text:
																					split_tokens.extend(list(self.bpe(token.text.lower()   ).split(" "   )   )   )
									return split_tokens







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    List[Any]   ):




									"""simple docstring"""


									return self.encoder.get(__lowerCamelCase					,					self.encoder.get(self.unk_token   )   )







			def UpperCamelCase    ( self					:    int					,					snake_case__					:    Optional[Any]   ):




									"""simple docstring"""


									return self.decoder.get(__lowerCamelCase					,					self.unk_token   )







			def UpperCamelCase    ( self					:    Any					,					snake_case__					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  "".join(__lowerCamelCase   ).replace("</w>"					,					" "   ).strip()
									return out_string







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    Dict					,					snake_case__					:    Any = None   ):




									"""simple docstring"""


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

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

									_UpperCAmelCase									=  0
									with open(__lowerCamelCase					,					"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 snake_case__   : 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(__lowerCamelCase   ) + "\n"   )
																					index += 1

									return vocab_file, merge_file










import copy

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


lowercase_			:       Tuple						       =				logging.get_logger(__name__)

lowercase_			:       List[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 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    Optional[int]					,					snake_case__					:    int=256					,					snake_case__					:    Dict=1_024					,					snake_case__					:    Dict=16					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any=4					,					snake_case__					:    Tuple="gelu"					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  hidden_size
									_UpperCAmelCase									=  image_size
									_UpperCAmelCase									=  patch_size
									_UpperCAmelCase									=  image_size // patch_size
									_UpperCAmelCase									=  mask_input_channels
									_UpperCAmelCase									=  num_point_embeddings
									_UpperCAmelCase									=  hidden_act
									_UpperCAmelCase									=  layer_norm_eps






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    str					,					snake_case__					:    List[str]=256					,					snake_case__					:    Tuple="relu"					,					snake_case__					:    int=2_048					,					snake_case__					:    Optional[int]=2					,					snake_case__					:    Dict=8					,					snake_case__					:    List[Any]=2					,					snake_case__					:    List[Any]=3					,					snake_case__					:    List[Any]=3					,					snake_case__					:    Optional[int]=256					,					snake_case__					:    Tuple=1e-6					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  hidden_size
									_UpperCAmelCase									=  hidden_act
									_UpperCAmelCase									=  mlp_dim
									_UpperCAmelCase									=  num_hidden_layers
									_UpperCAmelCase									=  num_attention_heads
									_UpperCAmelCase									=  attention_downsample_rate
									_UpperCAmelCase									=  num_multimask_outputs
									_UpperCAmelCase									=  iou_head_depth
									_UpperCAmelCase									=  iou_head_hidden_dim
									_UpperCAmelCase									=  layer_norm_eps






class 					__lowerCAmelCase				( UpperCAmelCase__				):







			def __init__( self					:    List[str]					,					snake_case__					:    str=768					,					snake_case__					:    Dict=256					,					snake_case__					:    Any=12					,					snake_case__					:    Union[str, Any]=12					,					snake_case__					:    Any=3					,					snake_case__					:    List[str]=1_024					,					snake_case__					:    Union[str, Any]=16					,					snake_case__					:    Any="gelu"					,					snake_case__					:    List[str]=1e-06					,					snake_case__					:    Union[str, Any]=0.0					,					snake_case__					:    Optional[int]=1e-10					,					snake_case__					:    int=True					,					snake_case__					:    Dict=4.0					,					snake_case__					:    Optional[Any]=True					,					snake_case__					:    Tuple=True					,					snake_case__					:    List[Any]=14					,					snake_case__					:    List[str]=[2, 5, 8, 11]					,					snake_case__					:    List[Any]=128					,					snake_case__					:    List[Any]=None					,					**snake_case__					:    Any					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )

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






class 					__lowerCAmelCase				( UpperCAmelCase__				):
			snake_case_   :				Tuple				  =  "sam"
			snake_case_   :				Tuple				  =  True







			def __init__( self					:    Optional[int]					,					snake_case__					:    Optional[Any]=None					,					snake_case__					:    Any=None					,					snake_case__					:    int=None					,					snake_case__					:    List[Any]=0.02					,					**snake_case__					:    str					,					):




									"""simple docstring"""


									super().__init__(**snake_case__   )
									_UpperCAmelCase									=  vision_config if vision_config is not None else {}
									_UpperCAmelCase									=  prompt_encoder_config if prompt_encoder_config is not None else {}
									_UpperCAmelCase									=  mask_decoder_config if mask_decoder_config is not None else {}

									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  vision_config.to_dict()
									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  prompt_encoder_config.to_dict()
									if isinstance(snake_case__					,					snake_case__   ):
															_UpperCAmelCase									=  mask_decoder_config.to_dict()

									_UpperCAmelCase									=  SamVisionConfig(**snake_case__   )
									_UpperCAmelCase									=  SamPromptEncoderConfig(**snake_case__   )
									_UpperCAmelCase									=  SamMaskDecoderConfig(**snake_case__   )
									_UpperCAmelCase									=  initializer_range







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  copy.deepcopy(self.__dict__   )
									_UpperCAmelCase									=  self.vision_config.to_dict()
									_UpperCAmelCase									=  self.prompt_encoder_config.to_dict()
									_UpperCAmelCase									=  self.mask_decoder_config.to_dict()
									_UpperCAmelCase									=  self.__class__.model_type
									return output




