






"""simple docstring"""



import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, slow, torch_device

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin

class 			a	(  a_,							a_,							unittest.TestCase      ):
    UpperCAmelCase_					:	Tuple      	=UNetaDModel
    UpperCAmelCase_					:	Optional[int]      	="sample"







    @property
    def      UpperCamelCase_ (     self			):
      lowercase				       =			4
      lowercase				       =			1_4
      lowercase				       =			1_6

      lowercase				       =			floats_tensor((batch_size, num_features, seq_len)			).to(_lowerCamelCase			)
      lowercase				       =			torch.tensor([1_0] * batch_size			).to(_lowerCamelCase			)

      return {"sample": noise, "timestep": time_step}







    @property
    def      UpperCamelCase_ (     self			):
      return (4, 1_4, 1_6)







    @property
    def      UpperCamelCase_ (     self			):
      return (4, 1_4, 1_6)







    def      UpperCamelCase_ (     self			):
      pass







    def      UpperCamelCase_ (     self			):
      pass







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_determinism()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_outputs_equivalence()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_from_save_pretrained()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_from_save_pretrained_variant()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_model_from_pretrained()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_output()







    def      UpperCamelCase_ (     self			):
      lowercase				       =			{
          'block_out_channels': (3_2, 6_4, 1_2_8, 2_5_6),
          'in_channels': 1_4,
          'out_channels': 1_4,
          'time_embedding_type': 'positional',
          'use_timestep_embedding': True,
          'flip_sin_to_cos': False,
          'freq_shift': 1.0,
          'out_block_type': 'OutConv1DBlock',
          'mid_block_type': 'MidResTemporalBlock1D',
          'down_block_types': ('DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'),
          'up_block_types': ('UpResnetBlock1D', 'UpResnetBlock1D', 'UpResnetBlock1D'),
          'act_fn': 'mish',
      }
      lowercase				       =			self.dummy_input
      return init_dict, inputs_dict







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      lowercase       ,	lowercase				       =			UNetaDModel.from_pretrained(
          'bglick13/hopper-medium-v2-value-function-hor32'				,						output_loading_info=_lowerCamelCase				,						subfolder='unet'			)
      self.assertIsNotNone(_lowerCamelCase			)
      self.assertEqual(len(loading_info['missing_keys']			)				,						0			)

      model.to(_lowerCamelCase			)
      lowercase				       =			model(**self.dummy_input			)

      assert image is not None, "Make sure output is not None"







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      lowercase				       =			UNetaDModel.from_pretrained('bglick13/hopper-medium-v2-value-function-hor32'				,						subfolder='unet'			)
      torch.manual_seed(0			)
      if torch.cuda.is_available():
        torch.cuda.manual_seed_all(0			)

      lowercase				       =			model.config.in_channels
      lowercase				       =			1_6
      lowercase				       =			torch.randn((1, seq_len, num_features)			).permute(
          0				,						2				,						1			)  # match original, we can update values and remove
      lowercase				       =			torch.full((num_features,)				,						0			)

      with torch.no_grad():
        lowercase				       =			model(_lowerCamelCase				,						_lowerCamelCase			).sample.permute(0				,						2				,						1			)

      lowercase				       =			output[0, -3:, -3:].flatten()
      # fmt: off
      lowercase				       =			torch.tensor([-2.1_3_7_1_7_2, 1.1_4_2_6_0_1_6, 0.3_6_8_8_6_8_7, -0.7_6_6_9_2_2, 0.7_3_0_3_1_4_6, 0.1_1_0_3_8_8_6_4, -0.4_7_6_0_6_3_3, 0.1_3_2_7_0_1_7_2, 0.0_2_5_9_1_3_4_8]			)
      # fmt: on
      self.assertTrue(torch.allclose(_lowerCamelCase				,						_lowerCamelCase				,						rtol=1e-3			)			)







    def      UpperCamelCase_ (     self			):
      # Not implemented yet for this UNet
      pass







    @slow
    def      UpperCamelCase_ (     self			):
      lowercase				       =			'harmonai/maestro-150k'
      lowercase				       =			UNetaDModel.from_pretrained(_lowerCamelCase				,						subfolder='unet'			)
      model.to(_lowerCamelCase			)

      lowercase				       =			6_5_5_3_6
      lowercase				       =			torch.sin(torch.arange(_lowerCamelCase			)[None, None, :].repeat(1				,						2				,						1			)			).to(_lowerCamelCase			)
      lowercase				       =			torch.tensor([1]			).to(_lowerCamelCase			)

      with torch.no_grad():
        lowercase				       =			model(_lowerCamelCase				,						_lowerCamelCase			).sample

      lowercase				       =			output.abs().sum()
      lowercase				       =			output.abs().max()

      assert (output_sum - 2_2_4.0_8_9_6).abs() < 0.5
      assert (output_max - 0.0_6_0_7).abs() < 4e-4


class 			a	(  a_,							a_,							unittest.TestCase      ):
    UpperCAmelCase_					:	Dict      	=UNetaDModel
    UpperCAmelCase_					:	str      	="sample"







    @property
    def      UpperCamelCase_ (     self			):
      lowercase				       =			4
      lowercase				       =			1_4
      lowercase				       =			1_6

      lowercase				       =			floats_tensor((batch_size, num_features, seq_len)			).to(_lowerCamelCase			)
      lowercase				       =			torch.tensor([1_0] * batch_size			).to(_lowerCamelCase			)

      return {"sample": noise, "timestep": time_step}







    @property
    def      UpperCamelCase_ (     self			):
      return (4, 1_4, 1_6)







    @property
    def      UpperCamelCase_ (     self			):
      return (4, 1_4, 1)







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_determinism()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_outputs_equivalence()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_from_save_pretrained()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_from_save_pretrained_variant()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      super().test_model_from_pretrained()







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      # UNetRL is a value-function is different output shape
      lowercase       ,	lowercase				       =			self.prepare_init_args_and_inputs_for_common()
      lowercase				       =			self.model_class(**_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()

      with torch.no_grad():
        lowercase				       =			model(**_lowerCamelCase			)

        if isinstance(_lowerCamelCase				,						_lowerCamelCase			):
          lowercase				       =			output.sample

      self.assertIsNotNone(_lowerCamelCase			)
      lowercase				       =			torch.Size((inputs_dict['sample'].shape[0], 1)			)
      self.assertEqual(output.shape				,						_lowerCamelCase				,						'Input and output shapes do not match'			)







    def      UpperCamelCase_ (     self			):
      pass







    def      UpperCamelCase_ (     self			):
      pass







    def      UpperCamelCase_ (     self			):
      lowercase				       =			{
          'in_channels': 1_4,
          'out_channels': 1_4,
          'down_block_types': ['DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'],
          'up_block_types': [],
          'out_block_type': 'ValueFunction',
          'mid_block_type': 'ValueFunctionMidBlock1D',
          'block_out_channels': [3_2, 6_4, 1_2_8, 2_5_6],
          'layers_per_block': 1,
          'downsample_each_block': True,
          'use_timestep_embedding': True,
          'freq_shift': 1.0,
          'flip_sin_to_cos': False,
          'time_embedding_type': 'positional',
          'act_fn': 'mish',
      }
      lowercase				       =			self.dummy_input
      return init_dict, inputs_dict







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      lowercase       ,	lowercase				       =			UNetaDModel.from_pretrained(
          'bglick13/hopper-medium-v2-value-function-hor32'				,						output_loading_info=_lowerCamelCase				,						subfolder='value_function'			)
      self.assertIsNotNone(_lowerCamelCase			)
      self.assertEqual(len(vf_loading_info['missing_keys']			)				,						0			)

      value_function.to(_lowerCamelCase			)
      lowercase				       =			value_function(**self.dummy_input			)

      assert image is not None, "Make sure output is not None"







    @unittest.skipIf(torch_device == 'mps'				,						'mish op not supported in MPS'			)
    def      UpperCamelCase_ (     self			):
      lowercase       ,	lowercase				       =			UNetaDModel.from_pretrained(
          'bglick13/hopper-medium-v2-value-function-hor32'				,						output_loading_info=_lowerCamelCase				,						subfolder='value_function'			)
      torch.manual_seed(0			)
      if torch.cuda.is_available():
        torch.cuda.manual_seed_all(0			)

      lowercase				       =			value_function.config.in_channels
      lowercase				       =			1_4
      lowercase				       =			torch.randn((1, seq_len, num_features)			).permute(
          0				,						2				,						1			)  # match original, we can update values and remove
      lowercase				       =			torch.full((num_features,)				,						0			)

      with torch.no_grad():
        lowercase				       =			value_function(_lowerCamelCase				,						_lowerCamelCase			).sample

      # fmt: off
      lowercase				       =			torch.tensor([1_6_5.2_5] * seq_len			)
      # fmt: on
      self.assertTrue(torch.allclose(_lowerCamelCase				,						_lowerCamelCase				,						rtol=1e-3			)			)







    def      UpperCamelCase_ (     self			):
      # Not implemented yet for this UNet
      pass









import copy

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


SCREAMING_SNAKE_CASE_		  =			logging.get_logger(__name__)

SCREAMING_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 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     def __init__(				self		:       List[str]		,lowerCamelCase__		:       int=256		,lowerCamelCase__		:       List[Any]=1024		,lowerCamelCase__		:       Any=16		,lowerCamelCase__		:       Union[str, Any]=16		,lowerCamelCase__		:       int=4		,lowerCamelCase__		:       Tuple="gelu"		,lowerCamelCase__		:       Dict=1e-6		,**lowerCamelCase__		:       int		,)	->						List[str]:




           '''simple docstring'''


           super().__init__(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		hidden_size
           SCREAMING_SNAKE_CASE						=		image_size
           SCREAMING_SNAKE_CASE						=		patch_size
           SCREAMING_SNAKE_CASE						=		image_size // patch_size
           SCREAMING_SNAKE_CASE						=		mask_input_channels
           SCREAMING_SNAKE_CASE						=		num_point_embeddings
           SCREAMING_SNAKE_CASE						=		hidden_act
           SCREAMING_SNAKE_CASE						=		layer_norm_eps

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

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




           '''simple docstring'''


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

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

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




           '''simple docstring'''


           super().__init__(**lowerCamelCase__			)

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

class 						UpperCamelCase__					( lowerCAmelCase_  ):



     '''simple docstring'''

     __snake_case       :      Any						      =     "sam"
     __snake_case       :      Optional[Any]						      =     True






     def __init__(				self		:       Union[str, Any]		,lowerCamelCase__		:       List[Any]=None		,lowerCamelCase__		:       int=None		,lowerCamelCase__		:       int=None		,lowerCamelCase__		:       int=0.02		,**lowerCamelCase__		:       int		,)	->						str:




           '''simple docstring'''


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

           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		vision_config.to_dict()
           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		prompt_encoder_config.to_dict()
           if isinstance(lowerCamelCase__		,lowerCamelCase__			):
                 SCREAMING_SNAKE_CASE						=		mask_decoder_config.to_dict()

           SCREAMING_SNAKE_CASE						=		SamVisionConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		SamPromptEncoderConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		SamMaskDecoderConfig(**lowerCamelCase__			)
           SCREAMING_SNAKE_CASE						=		initializer_range






     def 							SCREAMING_SNAKE_CASE__    (				self		:       Union[str, Any]			)	->						Optional[Any]:




           '''simple docstring'''


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


