




import posixpath
from pathlib import Path
from unittest.mock import patch

import pytest
from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
from fsspec.registry import _registry as _fsspec_registry






class 					__lowerCAmelCase				( __lowerCamelCase				):
			snake_case_   :				List[str]				  =  'mock'







			def __init__( self					:    Tuple					,					*snake_case__					:    Any					,					snake_case__					:    str					,					**snake_case__					:    str   ):




									"""simple docstring"""


									super().__init__()
									_UpperCAmelCase									=  LocalFileSystem(*snake_case__					,					**snake_case__   )
									_UpperCAmelCase									=  Path(snake_case__   ).resolve().as_posix() + "/"







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.mkdir(snake_case__					,					*snake_case__					,					**snake_case__   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.makedirs(snake_case__					,					*snake_case__					,					**snake_case__   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.rmdir(snake_case__   )







			def UpperCamelCase    ( self					:    int					,					snake_case__					:    List[str]					,					snake_case__					:    Optional[Any]=True					,					*snake_case__					:    Union[str, Any]					,					**snake_case__					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									_UpperCAmelCase									=  self._fs.ls(snake_case__					,					detail=snake_case__					,					*snake_case__					,					**snake_case__   )
									if detail:
															return [{**info, "name": info["name"][len(self.local_root_dir   ) :]} for info in out]
									else:
															return [name[len(self.local_root_dir   ) :] for name in out]







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    Dict					,					*snake_case__					:    Dict					,					**snake_case__					:    Optional[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									_UpperCAmelCase									=  dict(self._fs.info(snake_case__					,					*snake_case__					,					**snake_case__   )   )
									_UpperCAmelCase									=  out["name"][len(self.local_root_dir   ) :]
									return out







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.cp_file(snake_case__					,					snake_case__					,					*snake_case__					,					**snake_case__   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.rm_file(snake_case__					,					*snake_case__					,					**snake_case__   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.rm(snake_case__					,					*snake_case__					,					**snake_case__   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs._open(snake_case__					,					*snake_case__					,					**snake_case__   )







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.created(snake_case__   )







			def UpperCamelCase    ( self					:    Any					,					snake_case__					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  posixpath.join(self.local_root_dir					,					self._strip_protocol(snake_case__   )   )
									return self._fs.modified(snake_case__   )







			@classmethod
			def UpperCamelCase    ( cls					:    int					,					snake_case__					:    Optional[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  stringify_path(snake_case__   )
									if path.startswith("mock://"   ):
															_UpperCAmelCase									=  path[7:]
									return path






class 					__lowerCAmelCase				( __lowerCamelCase				):
			snake_case_   :				Optional[int]				  =  'tmp'
			snake_case_   :				List[Any]				  =  None







			def __init__( self					:    Optional[int]					,					*snake_case__					:    str					,					**snake_case__					:    Dict   ):




									"""simple docstring"""


									assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
									super().__init__(*snake_case__					,					**snake_case__					,					local_root_dir=self.tmp_dir					,					auto_mkdir=snake_case__   )







			@classmethod
			def UpperCamelCase    ( cls					:    str					,					snake_case__					:    List[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  stringify_path(snake_case__   )
									if path.startswith("tmp://"   ):
															_UpperCAmelCase									=  path[6:]
									return path




@pytest.fixture
def 							__SCREAMING_SNAKE_CASE						(    ):

						'''simple docstring'''


						_UpperCAmelCase									=  MockFileSystem
						_UpperCAmelCase									=  TmpDirFileSystem
						yield
						del _fsspec_registry["mock"]
						del _fsspec_registry["tmp"]


@pytest.fixture
def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  tmp_path_factory.mktemp("mockfs"		)
						return MockFileSystem(local_root_dir=snake_case_			,					auto_mkdir=snake_case_		)





@pytest.fixture
def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  tmp_path_factory.mktemp("tmpfs"		)
						with patch.object(snake_case_			,					"tmp_dir"			,					snake_case_		):
												yield TmpDirFileSystem()










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




