






import copy

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


__A          = logging.get_logger(__name__)

__A          = {
    '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   SCREAMING_SNAKE_CASE (       snake_case    ):





							"""simple docstring"""

							def __init__(     self:      Any						,   __A:      Any=2_56						,   __A:      List[str]=10_24						,   __A:      List[Any]=16						,   __A:      Optional[Any]=16						,   __A:      Optional[Any]=4						,   __A:      Optional[Any]="gelu"						,   __A:      List[str]=1e-6						,   **__A:      Any						,   )			-> str:
												super().__init__(**__A      )
												_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   SCREAMING_SNAKE_CASE (       snake_case    ):





							"""simple docstring"""

							def __init__(     self:      Union[str, Any]						,   __A:      Any=2_56						,   __A:      Optional[int]="relu"						,   __A:      Optional[int]=20_48						,   __A:      Tuple=2						,   __A:      List[str]=8						,   __A:      Optional[int]=2						,   __A:      Dict=3						,   __A:      List[str]=3						,   __A:      Tuple=2_56						,   __A:      Union[str, Any]=1e-6						,   **__A:      Optional[int]						,   )			-> Any:
												super().__init__(**__A      )
												_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   SCREAMING_SNAKE_CASE (       snake_case    ):





							"""simple docstring"""

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

												_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   SCREAMING_SNAKE_CASE (       snake_case    ):





							"""simple docstring"""

							A_       =    "sam"
							A_       =    True

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

												_A									=       SamVisionConfig(**__A      )
												_A									=       SamPromptEncoderConfig(**__A      )
												_A									=       SamMaskDecoderConfig(**__A      )
												_A									=       initializer_range



							def 					__A       (     self:      Tuple      )			-> List[str]:
												_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











import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor





class   SCREAMING_SNAKE_CASE (       unittest.TestCase    ):





							"""simple docstring"""

							def 					__A       (     self:      Optional[Any]						,   __A:      bool = False      )			-> Union[str, Any]:
												_A									=       10

												if only_cross_attention:
																	_A									=       12
												else:
																	# when only cross attention is not set, the cross attention dim must be the same as the query dim
																	_A									=       query_dim

												return {
												    "query_dim": query_dim,
												    "cross_attention_dim": cross_attention_dim,
												    "heads": 2,
												    "dim_head": 4,
												    "added_kv_proj_dim": 6,
												    "norm_num_groups": 1,
												    "only_cross_attention": only_cross_attention,
												    "processor": AttnAddedKVProcessor(),
												}

							def 					__A       (     self:      List[Any]						,   __A:      str						,   __A:      Union[str, Any]      )			-> Union[str, Any]:
												_A									=       2

												_A									=       torch.rand(__A						,   __A						,   3						,   2      )
												_A									=       torch.rand(__A						,   4						,   __A      )
												_A									=       None

												return {
												    "hidden_states": hidden_states,
												    "encoder_hidden_states": encoder_hidden_states,
												    "attention_mask": attention_mask,
												}



							def 					__A       (     self:      Optional[int]      )			-> Any:
												# self and cross attention

												torch.manual_seed(0      )

												_A									=       self.get_constructor_arguments(only_cross_attention=__A      )
												_A									=       Attention(**__A      )

												self.assertTrue(attn.to_k is not None      )
												self.assertTrue(attn.to_v is not None      )

												_A									=       self.get_forward_arguments(
												    query_dim=constructor_args['''query_dim''']						,   added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']      )

												_A									=       attn(**__A      )

												# only self attention

												torch.manual_seed(0      )

												_A									=       self.get_constructor_arguments(only_cross_attention=__A      )
												_A									=       Attention(**__A      )

												self.assertTrue(attn.to_k is None      )
												self.assertTrue(attn.to_v is None      )

												_A									=       self.get_forward_arguments(
												    query_dim=constructor_args['''query_dim''']						,   added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']      )

												_A									=       attn(**__A      )

												self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )





class   SCREAMING_SNAKE_CASE (       unittest.TestCase    ):





							"""simple docstring"""

							def 					__A       (     self:      Any      )			-> Dict:
												_A									=       DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''						,   safety_checker=__A      )

												_A									=       pipe(
												    '''foo'''						,   num_inference_steps=2						,   generator=torch.Generator('''cpu'''      ).manual_seed(0      )						,   output_type='''np'''						,   ).images

												# the initial conversion succeeds
												_A									=       DiffusionPipeline.from_pretrained(
												    '''hf-internal-testing/tiny-stable-diffusion-pipe'''						,   device_map='''sequential'''						,   safety_checker=__A      )

												_A									=       pipe(
												    '''foo'''						,   num_inference_steps=2						,   generator=torch.Generator('''cpu'''      ).manual_seed(0      )						,   output_type='''np'''						,   ).images

												with tempfile.TemporaryDirectory() as tmpdir:
																	# save the converted model
																	pipe.save_pretrained(__A      )

																	# can also load the converted weights
																	_A									=       DiffusionPipeline.from_pretrained(__A						,   device_map='''sequential'''						,   safety_checker=__A      )

												_A									=       pipe(
												    '''foo'''						,   num_inference_steps=2						,   generator=torch.Generator('''cpu'''      ).manual_seed(0      )						,   output_type='''np'''						,   ).images

												self.assertTrue(np.allclose(__A						,   __A						,   atol=1e-5      )      )
												self.assertTrue(np.allclose(__A						,   __A						,   atol=1e-5      )      )



