



import copy

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


UpperCamelCase_	       =   logging.get_logger(__name__)

UpperCamelCase_	       =   {
    "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_  (		_snake_case				):

						def __init__(    self	:Tuple   ,						_lowercase	:Union[str, Any]=256   ,						_lowercase	:Optional[int]=1024   ,						_lowercase	:Union[str, Any]=16   ,						_lowercase	:Dict=16   ,						_lowercase	:Union[str, Any]=4   ,						_lowercase	:str="gelu"   ,						_lowercase	:List[str]=1E-6   ,						**_lowercase	:List[str]   ,						)		->					int:
												super().__init__(**_lowercase)
												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 							a_  (		_snake_case				):

						def __init__(    self	:Optional[Any]   ,						_lowercase	:List[Any]=256   ,						_lowercase	:List[str]="relu"   ,						_lowercase	:Optional[Any]=2048   ,						_lowercase	:Optional[int]=2   ,						_lowercase	:Union[str, Any]=8   ,						_lowercase	:List[str]=2   ,						_lowercase	:Optional[Any]=3   ,						_lowercase	:List[Any]=3   ,						_lowercase	:Optional[int]=256   ,						_lowercase	:Dict=1E-6   ,						**_lowercase	:int   ,						)		->					Optional[int]:
												super().__init__(**_lowercase)
												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 							a_  (		_snake_case				):

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

												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 							a_  (		_snake_case				):
						UpperCamelCase__      :						Optional[Any]				       ="sam"
						UpperCamelCase__      :						str				       =True

						def __init__(    self	:Dict   ,						_lowercase	:Tuple=None   ,						_lowercase	:str=None   ,						_lowercase	:str=None   ,						_lowercase	:str=0.02   ,						**_lowercase	:Tuple   ,						)		->					Union[str, Any]:
												super().__init__(**_lowercase)
												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(_lowercase   ,						_lowercase):
																		UpperCAmelCase_     							=     vision_config.to_dict()
												if isinstance(_lowercase   ,						_lowercase):
																		UpperCAmelCase_     							=     prompt_encoder_config.to_dict()
												if isinstance(_lowercase   ,						_lowercase):
																		UpperCAmelCase_     							=     mask_decoder_config.to_dict()

												UpperCAmelCase_     							=     SamVisionConfig(**_lowercase)
												UpperCAmelCase_     							=     SamPromptEncoderConfig(**_lowercase)
												UpperCAmelCase_     							=     SamMaskDecoderConfig(**_lowercase)
												UpperCAmelCase_     							=     initializer_range




						def 	__a       (    self	:List[str])		->					Any:
												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










import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
							from PIL import Image







class 							a_  (		unittest.TestCase					, _snake_case				):

						def 	__a       (    self	:Union[str, Any])		->					Optional[Any]:
												UpperCAmelCase_     							=     load_tool('''image-question-answering''')
												self.tool.setup()
												UpperCAmelCase_     							=     load_tool('''image-question-answering'''   ,						remote=_lowercase)

						def 	__a       (    self	:int)		->					Union[str, Any]:
												UpperCAmelCase_     							=     Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''')) / '''000000039769.png''')
												UpperCAmelCase_     							=     self.tool(_lowercase   ,						'''How many cats are sleeping on the couch?''')
												self.assertEqual(_lowercase   ,						'''2''')

						def 	__a       (    self	:int)		->					Dict:
												UpperCAmelCase_     							=     Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''')) / '''000000039769.png''')
												UpperCAmelCase_     							=     self.remote_tool(_lowercase   ,						'''How many cats are sleeping on the couch?''')
												self.assertEqual(_lowercase   ,						'''2''')

						def 	__a       (    self	:Optional[int])		->					int:
												UpperCAmelCase_     							=     Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''')) / '''000000039769.png''')
												UpperCAmelCase_     							=     self.tool(image=_lowercase   ,						question='''How many cats are sleeping on the couch?''')
												self.assertEqual(_lowercase   ,						'''2''')




						def 	__a       (    self	:Optional[Any])		->					List[str]:
												UpperCAmelCase_     							=     Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO''')) / '''000000039769.png''')
												UpperCAmelCase_     							=     self.remote_tool(image=_lowercase   ,						question='''How many cats are sleeping on the couch?''')
												self.assertEqual(_lowercase   ,						'''2''')





