


"""simple docstring"""






import copy

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


__snake_case												=							logging.get_logger(__name__)

__snake_case												=							{
    '''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	(       a__					):

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase=256     ,					__UpperCAmelCase=1024     ,					__UpperCAmelCase=16     ,					__UpperCAmelCase=16     ,					__UpperCAmelCase=4     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=1e-6     ,					**__UpperCAmelCase     ,					)							->				Optional[int]:
          super().__init__(**__UpperCAmelCase				)
          _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      __lowerCamelCase	(       a__					):

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase=256     ,					__UpperCAmelCase="relu"     ,					__UpperCAmelCase=2048     ,					__UpperCAmelCase=2     ,					__UpperCAmelCase=8     ,					__UpperCAmelCase=2     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=256     ,					__UpperCAmelCase=1e-6     ,					**__UpperCAmelCase     ,					)							->				Any:
          super().__init__(**__UpperCAmelCase				)
          _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      __lowerCamelCase	(       a__					):

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase=768     ,					__UpperCAmelCase=256     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=1024     ,					__UpperCAmelCase=16     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=1e-0_6     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=1e-1_0     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=4.0     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=14     ,					__UpperCAmelCase=[2, 5, 8, 11]     ,					__UpperCAmelCase=128     ,					__UpperCAmelCase=None     ,					**__UpperCAmelCase     ,					)							->				Optional[Any]:
          super().__init__(**__UpperCAmelCase				)

          _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      __lowerCamelCase	(       a__					):

     '''simple docstring'''
     A_							:						List[Any]     =		'sam'
     A_							:						Union[str, Any]     =		True


     def __init__(   self     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=0.02     ,					**__UpperCAmelCase     ,					)							->				str:
          super().__init__(**__UpperCAmelCase				)
          _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(__UpperCAmelCase     ,					__UpperCAmelCase				):
               _a     						=					vision_config.to_dict()
          if isinstance(__UpperCAmelCase     ,					__UpperCAmelCase				):
               _a     						=					prompt_encoder_config.to_dict()
          if isinstance(__UpperCAmelCase     ,					__UpperCAmelCase				):
               _a     						=					mask_decoder_config.to_dict()

          _a     						=					SamVisionConfig(**__UpperCAmelCase				)
          _a     						=					SamPromptEncoderConfig(**__UpperCAmelCase				)
          _a     						=					SamMaskDecoderConfig(**__UpperCAmelCase				)
          _a     						=					initializer_range





     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          _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



"""simple docstring"""






import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


__snake_case												=							False


@nightly
@require_torch_gpu
class      __lowerCamelCase	(       unittest.TestCase					):

     '''simple docstring'''


     def 				_UpperCAmelCase   (   self				)							->				Tuple:
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()


     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          _a     						=					VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''				)
          # remove text_unet
          pipe.remove_unused_weights()
          pipe.to(__UpperCAmelCase				)
          pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

          _a     						=					load_image(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)

          _a     						=					torch.manual_seed(0				)
          _a     						=					pipe(
              prompt='''first prompt'''     ,					image=__UpperCAmelCase     ,					text_to_image_strength=0.75     ,					generator=__UpperCAmelCase     ,					guidance_scale=7.5     ,					num_inference_steps=2     ,					output_type='''numpy'''     ,					).images

          with tempfile.TemporaryDirectory() as tmpdirname:
               pipe.save_pretrained(__UpperCAmelCase				)
               _a     						=					VersatileDiffusionDualGuidedPipeline.from_pretrained(__UpperCAmelCase				)

          pipe.to(__UpperCAmelCase				)
          pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

          _a     						=					generator.manual_seed(0				)
          _a     						=					pipe(
              prompt='''first prompt'''     ,					image=__UpperCAmelCase     ,					text_to_image_strength=0.75     ,					generator=__UpperCAmelCase     ,					guidance_scale=7.5     ,					num_inference_steps=2     ,					output_type='''numpy'''     ,					).images

          assert np.abs(image - new_image				).sum() < 1e-5, "Models don't have the same forward pass"





     def 				_UpperCAmelCase   (   self				)							->				Dict:
          _a     						=					VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''				)
          pipe.remove_unused_weights()
          pipe.to(__UpperCAmelCase				)
          pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

          _a     						=					'''cyberpunk 2077'''
          _a     						=					load_image(
              '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)
          _a     						=					torch.manual_seed(0				)
          _a     						=					pipe(
              prompt=__UpperCAmelCase     ,					image=__UpperCAmelCase     ,					text_to_image_strength=0.75     ,					generator=__UpperCAmelCase     ,					guidance_scale=7.5     ,					num_inference_steps=50     ,					output_type='''numpy'''     ,					).images

          _a     						=					image[0, 253:256, 253:256, -1]

          assert image.shape == (1, 512, 512, 3)
          _a     						=					np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]				)

          assert np.abs(image_slice.flatten() - expected_slice				).max() < 1e-2