'''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 	__SCREAMING_SNAKE_CASE		(			lowerCamelCase_		,      lowerCamelCase_		,      unittest.TestCase			):







  '''simple docstring'''






  lowerCamelCase_						:Optional[int]		       =						UNetaDModel
  lowerCamelCase_						:int		       =						'''sample'''


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Any  = 4
       UpperCAmelCase_				:  Any  = 1_4
       UpperCAmelCase_				:  Dict  = 1_6

       UpperCAmelCase_				:  Union[str, Any]  = floats_tensor((batch_size, num_features, seq_len)		).to(snake_case_		)
       UpperCAmelCase_				:  Union[str, Any]  = torch.tensor([1_0] * batch_size		).to(snake_case_		)

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


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       return (4, 1_4, 1_6)


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       return (4, 1_4, 1_6)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass


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




       '''simple docstring'''

       super().test_determinism()


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




       '''simple docstring'''

       super().test_outputs_equivalence()


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




       '''simple docstring'''

       super().test_from_save_pretrained()


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




       '''simple docstring'''

       super().test_from_save_pretrained_variant()


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




       '''simple docstring'''

       super().test_model_from_pretrained()


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




       '''simple docstring'''

       super().test_output()


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[int]  = {
           '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',
       }
       UpperCAmelCase_				:  List[str]  = self.dummy_input
       return init_dict, inputs_dict


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




       '''simple docstring'''

       UpperCAmelCase_  ,     UpperCAmelCase_				:  Any  = UNetaDModel.from_pretrained(
           'bglick13/hopper-medium-v2-value-function-hor32' ,						output_loading_info=snake_case_ ,						subfolder='unet'		)
       self.assertIsNotNone(snake_case_		)
       self.assertEqual(len(loading_info['missing_keys']		) ,						0		)

       model.to(snake_case_		)
       UpperCAmelCase_				:  Union[str, Any]  = 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		):




       '''simple docstring'''

       UpperCAmelCase_				:  str  = 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		)

       UpperCAmelCase_				:  Optional[Any]  = model.config.in_channels
       UpperCAmelCase_				:  List[Any]  = 1_6
       UpperCAmelCase_				:  int  = torch.randn((1, seq_len, num_features)		).permute(
           0 ,						2 ,						1		)  # match original, we can update values and remove
       UpperCAmelCase_				:  List[str]  = torch.full((num_features,) ,						0		)

       with torch.no_grad():
            UpperCAmelCase_				:  Union[str, Any]  = model(snake_case_ ,						snake_case_		).sample.permute(0 ,						2 ,						1		)

       UpperCAmelCase_				:  Tuple  = output[0, -3:, -3:].flatten()
       # fmt: off
       UpperCAmelCase_				:  Any  = 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(snake_case_ ,						snake_case_ ,						rtol=1E-3		)		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass






  @slow
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  str  = 'harmonai/maestro-150k'
       UpperCAmelCase_				:  Optional[Any]  = UNetaDModel.from_pretrained(snake_case_ ,						subfolder='unet'		)
       model.to(snake_case_		)

       UpperCAmelCase_				:  Optional[int]  = 6_5_5_3_6
       UpperCAmelCase_				:  Optional[Any]  = torch.sin(torch.arange(snake_case_		)[None, None, :].repeat(1 ,						2 ,						1		)		).to(snake_case_		)
       UpperCAmelCase_				:  Dict  = torch.tensor([1]		).to(snake_case_		)

       with torch.no_grad():
            UpperCAmelCase_				:  str  = model(snake_case_ ,						snake_case_		).sample

       UpperCAmelCase_				:  Any  = output.abs().sum()
       UpperCAmelCase_				:  Tuple  = output.abs().max()

       assert (output_sum - 2_24.08_96).abs() < 0.5
       assert (output_max - 0.06_07).abs() < 4E-4







class 	__SCREAMING_SNAKE_CASE		(			lowerCamelCase_		,      lowerCamelCase_		,      unittest.TestCase			):







  '''simple docstring'''






  lowerCamelCase_						:Tuple		       =						UNetaDModel
  lowerCamelCase_						:Any		       =						'''sample'''


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Dict  = 4
       UpperCAmelCase_				:  int  = 1_4
       UpperCAmelCase_				:  Optional[Any]  = 1_6

       UpperCAmelCase_				:  Dict  = floats_tensor((batch_size, num_features, seq_len)		).to(snake_case_		)
       UpperCAmelCase_				:  Any  = torch.tensor([1_0] * batch_size		).to(snake_case_		)

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


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       return (4, 1_4, 1_6)


  @property
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       return (4, 1_4, 1)


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




       '''simple docstring'''

       super().test_determinism()


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




       '''simple docstring'''

       super().test_outputs_equivalence()


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




       '''simple docstring'''

       super().test_from_save_pretrained()


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




       '''simple docstring'''

       super().test_from_save_pretrained_variant()


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




       '''simple docstring'''

       super().test_model_from_pretrained()


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




       '''simple docstring'''

       UpperCAmelCase_  ,     UpperCAmelCase_				:  Dict  = self.prepare_init_args_and_inputs_for_common()
       UpperCAmelCase_				:  Optional[Any]  = self.model_class(**snake_case_		)
       model.to(snake_case_		)
       model.eval()

       with torch.no_grad():
            UpperCAmelCase_				:  Optional[Any]  = model(**snake_case_		)

            if isinstance(snake_case_ ,						snake_case_		):
                 UpperCAmelCase_				:  Any  = output.sample

       self.assertIsNotNone(snake_case_		)
       UpperCAmelCase_				:  Optional[Any]  = torch.Size((inputs_dict['sample'].shape[0], 1)		)
       self.assertEqual(output.shape ,						snake_case_ ,						'Input and output shapes do not match'		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Dict  = {
           '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',
       }
       UpperCAmelCase_				:  Optional[Any]  = self.dummy_input
       return init_dict, inputs_dict


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




       '''simple docstring'''

       UpperCAmelCase_  ,     UpperCAmelCase_				:  int  = UNetaDModel.from_pretrained(
           'bglick13/hopper-medium-v2-value-function-hor32' ,						output_loading_info=snake_case_ ,						subfolder='value_function'		)
       self.assertIsNotNone(snake_case_		)
       self.assertEqual(len(vf_loading_info['missing_keys']		) ,						0		)

       value_function.to(snake_case_		)
       UpperCAmelCase_				:  List[str]  = 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		):




       '''simple docstring'''

       UpperCAmelCase_  ,     UpperCAmelCase_				:  Union[str, Any]  = UNetaDModel.from_pretrained(
           'bglick13/hopper-medium-v2-value-function-hor32' ,						output_loading_info=snake_case_ ,						subfolder='value_function'		)
       torch.manual_seed(0		)
       if torch.cuda.is_available():
            torch.cuda.manual_seed_all(0		)

       UpperCAmelCase_				:  List[Any]  = value_function.config.in_channels
       UpperCAmelCase_				:  Tuple  = 1_4
       UpperCAmelCase_				:  Any  = torch.randn((1, seq_len, num_features)		).permute(
           0 ,						2 ,						1		)  # match original, we can update values and remove
       UpperCAmelCase_				:  Union[str, Any]  = torch.full((num_features,) ,						0		)

       with torch.no_grad():
            UpperCAmelCase_				:  Tuple  = value_function(snake_case_ ,						snake_case_		).sample

       # fmt: off
       UpperCAmelCase_				:  Tuple  = torch.tensor([1_65.25] * seq_len		)
       # fmt: on
       self.assertTrue(torch.allclose(snake_case_ ,						snake_case_ ,						rtol=1E-3		)		)






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       pass














"""simple docstring"""


import json

import requests

from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info




def      snake_case    (A_ :Union[str, Any] ):







  '''simple docstring'''

  class 		snake_case    :





      def __init__(				self      :   Tuple							,						A      :   Dict     ):


        '''simple docstring'''




        assert isinstance(A							,						(bytes, str)     )
        a	:     Union[str, Any]							=  content





      def 							lowerCamelCase__ (				self      :   str     ):


        '''simple docstring'''




        return json.loads(self.content     )

  def mock_response(*A_ :Union[str, Any]      ,     **A_ :Tuple ):
    assert args[0] == AUTHENTICATED_USER_ENDPOINT
    assert "Authorization" in kwargs["headers"]
    assert kwargs["headers"]["Authorization"].startswith('token ' )
    assert "Accept" in kwargs["headers"]
    return FakeResponse(B'{"login":"test","id":1}' )

  monkeypatch.setattr(A_      ,     'get'      ,     A_ )
  a	:     List[str]							=  fetch_github_info('token' )
  assert result["login"] == "test"
  assert result["id"] == 1




