"""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   UpperCAmelCase_	(				_lowercase,    _lowercase,    unittest.TestCase   ):
    snake_case_											=      UNetaDModel
    snake_case_											=      """sample"""
    @property
    def 		_lowerCamelCase      (	self			:       int		)						->    Any:
       _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 		_lowerCamelCase      (	self			:       Optional[Any]		)						->    Optional[int]:
       return (4, 1_4, 1_6)
    @property
    def 		_lowerCamelCase      (	self			:       str		)						->    Any:
       return (4, 1_4, 1_6)
    def 		_lowerCamelCase      (	self			:       Optional[int]		)						->    Any:
       pass
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    Tuple:
       pass
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Dict		)						->    Any:
       super().test_determinism()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Union[str, Any]		)						->    int:
       super().test_outputs_equivalence()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    Optional[Any]:
       super().test_from_save_pretrained()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       List[str]		)						->    Union[str, Any]:
       super().test_from_save_pretrained_variant()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    Optional[int]:
       super().test_model_from_pretrained()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Optional[Any]		)						->    Dict:
       super().test_output()
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    int:
       _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 		_lowerCamelCase      (	self			:       Optional[int]		)						->    List[Any]:
       _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 		_lowerCamelCase      (	self			:       int		)						->    Optional[Any]:
       _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.13_71_72, 1.1_42_60_16, 0.3_68_86_87, -0.76_69_22, 0.7_30_31_46, 0.11_03_88_64, -0.4_76_06_33, 0.13_27_01_72, 0.02_59_13_48]		)
       # fmt: on
       self.assertTrue(torch.allclose(__lowerCAmelCase   ,						__lowerCAmelCase   ,						rtol=1e-3		)		)
    def 		_lowerCamelCase      (	self			:       List[Any]		)						->    int:
       # Not implemented yet for this UNet
       pass




    @slow
    def 		_lowerCamelCase      (	self			:       int		)						->    Tuple:
       _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.06_07).abs() < 4e-4
class   UpperCAmelCase_	(				_lowercase,    _lowercase,    unittest.TestCase   ):
    snake_case_											=      UNetaDModel
    snake_case_											=      """sample"""
    @property
    def 		_lowerCamelCase      (	self			:       str		)						->    Any:
       _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 		_lowerCamelCase      (	self			:       Optional[int]		)						->    Union[str, Any]:
       return (4, 1_4, 1_6)
    @property
    def 		_lowerCamelCase      (	self			:       Optional[int]		)						->    List[Any]:
       return (4, 1_4, 1)
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       List[str]		)						->    Dict:
       super().test_determinism()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Dict		)						->    Optional[int]:
       super().test_outputs_equivalence()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Union[str, Any]		)						->    Optional[int]:
       super().test_from_save_pretrained()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Tuple		)						->    Optional[Any]:
       super().test_from_save_pretrained_variant()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       Tuple		)						->    Optional[Any]:
       super().test_model_from_pretrained()
    @unittest.skipIf(torch_device == "mps"   ,						"mish op not supported in MPS"		)
    def 		_lowerCamelCase      (	self			:       int		)						->    List[str]:
       # 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 		_lowerCamelCase      (	self			:       Union[str, Any]		)						->    Optional[int]:
       pass
    def 		_lowerCamelCase      (	self			:       Tuple		)						->    Union[str, Any]:
       pass
    def 		_lowerCamelCase      (	self			:       Dict		)						->    Dict:
       _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 		_lowerCamelCase      (	self			:       List[Any]		)						->    Tuple:
       _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 		_lowerCamelCase      (	self			:       List[Any]		)						->    List[Any]:
       _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 		_lowerCamelCase      (	self			:       List[str]		)						->    Union[str, Any]:
       # Not implemented yet for this UNet
       pass






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


__lowerCAmelCase 	=      logging.get_logger(__name__)

__lowerCAmelCase 	=      {
    "MCG-NJU/videomae-base": "https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json",
}




class 	_UpperCAmelCase						( _lowercase       ):
					__A        = "videomae"




					def __init__(    self     ,   __lowerCAmelCase=224     ,   __lowerCAmelCase=16     ,   __lowerCAmelCase=3     ,   __lowerCAmelCase=16     ,   __lowerCAmelCase=2     ,   __lowerCAmelCase=768     ,   __lowerCAmelCase=12     ,   __lowerCAmelCase=12     ,   __lowerCAmelCase=3_072     ,   __lowerCAmelCase="gelu"     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=0.0     ,   __lowerCAmelCase=0.0_2     ,   __lowerCAmelCase=1E-1_2     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase=True     ,   __lowerCAmelCase=6     ,   __lowerCAmelCase=384     ,   __lowerCAmelCase=4     ,   __lowerCAmelCase=1_536     ,   __lowerCAmelCase=True     ,   **__lowerCAmelCase     ,   )    ->	Any:
							super().__init__(**__lowerCAmelCase							)

							UpperCamelCase__								=						image_size
							UpperCamelCase__								=						patch_size
							UpperCamelCase__								=						num_channels
							UpperCamelCase__								=						num_frames
							UpperCamelCase__								=						tubelet_size

							UpperCamelCase__								=						hidden_size
							UpperCamelCase__								=						num_hidden_layers
							UpperCamelCase__								=						num_attention_heads
							UpperCamelCase__								=						intermediate_size
							UpperCamelCase__								=						hidden_act
							UpperCamelCase__								=						hidden_dropout_prob
							UpperCamelCase__								=						attention_probs_dropout_prob
							UpperCamelCase__								=						initializer_range
							UpperCamelCase__								=						layer_norm_eps
							UpperCamelCase__								=						qkv_bias
							UpperCamelCase__								=						use_mean_pooling

							UpperCamelCase__								=						decoder_num_attention_heads
							UpperCamelCase__								=						decoder_hidden_size
							UpperCamelCase__								=						decoder_num_hidden_layers
							UpperCamelCase__								=						decoder_intermediate_size
							UpperCamelCase__								=						norm_pix_loss





