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   lowerCAmelCase    (       _SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		unittest.TestCase      ):
     _SCREAMING_SNAKE_CASE  :  Union[str, Any]		=UNetaDModel
     _SCREAMING_SNAKE_CASE  :  Any		="""sample"""



     @property
     def       a__	(       self							):
            _A=						4
            _A=						14
            _A=						16

            _A=						floats_tensor((batch_size, num_features, seq_len)							).to(_SCREAMING_SNAKE_CASE							)
            _A=						torch.tensor([10] * batch_size							).to(_SCREAMING_SNAKE_CASE							)

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



     @property
     def       a__	(       self							):
            return (4, 14, 16)



     @property
     def       a__	(       self							):
            return (4, 14, 16)



     def       a__	(       self							):
            pass



     def       a__	(       self							):
            pass



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



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



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



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



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



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



     def       a__	(       self							):
            _A=						{
                'block_out_channels': (32, 64, 128, 256),
                'in_channels': 14,
                'out_channels': 14,
                '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',
            }
            _A=						self.dummy_input
            return init_dict, inputs_dict



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

            model.to(_SCREAMING_SNAKE_CASE							)
            _A=						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       a__	(       self							):
            _A=						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							)

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

            with torch.no_grad():
                   _A=						model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE							).sample.permute(0					,   2					,   1							)

            _A=						output[0, -3:, -3:].flatten()
            # fmt: off
            _A=						torch.tensor([-2.137172, 1.1426016, 0.3688687, -0.766922, 0.7303146, 0.11038864, -0.4760633, 0.13270172, 0.02591348]							)
            # fmt: on
            self.assertTrue(torch.allclose(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   rtol=1E-3							)							)



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





     @slow
     def       a__	(       self							):
            _A=						'harmonai/maestro-150k'
            _A=						UNetaDModel.from_pretrained(_SCREAMING_SNAKE_CASE					,   subfolder='unet'							)
            model.to(_SCREAMING_SNAKE_CASE							)

            _A=						65536
            _A=						torch.sin(torch.arange(_SCREAMING_SNAKE_CASE							)[None, None, :].repeat(1					,   2					,   1							)							).to(_SCREAMING_SNAKE_CASE							)
            _A=						torch.tensor([1]							).to(_SCREAMING_SNAKE_CASE							)

            with torch.no_grad():
                   _A=						model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE							).sample

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

            assert (output_sum - 224.0896).abs() < 0.5
            assert (output_max - 0.0607).abs() < 4E-4





class   lowerCAmelCase    (       _SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		unittest.TestCase      ):
     _SCREAMING_SNAKE_CASE  :  Optional[Any]		=UNetaDModel
     _SCREAMING_SNAKE_CASE  :  Dict		="""sample"""



     @property
     def       a__	(       self							):
            _A=						4
            _A=						14
            _A=						16

            _A=						floats_tensor((batch_size, num_features, seq_len)							).to(_SCREAMING_SNAKE_CASE							)
            _A=						torch.tensor([10] * batch_size							).to(_SCREAMING_SNAKE_CASE							)

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



     @property
     def       a__	(       self							):
            return (4, 14, 16)



     @property
     def       a__	(       self							):
            return (4, 14, 1)



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



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



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



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



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



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

            with torch.no_grad():
                   _A=						model(**_SCREAMING_SNAKE_CASE							)

                   if isinstance(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE							):
                          _A=						output.sample

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



     def       a__	(       self							):
            pass



     def       a__	(       self							):
            pass



     def       a__	(       self							):
            _A=						{
                'in_channels': 14,
                'out_channels': 14,
                'down_block_types': ['DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D', 'DownResnetBlock1D'],
                'up_block_types': [],
                'out_block_type': 'ValueFunction',
                'mid_block_type': 'ValueFunctionMidBlock1D',
                'block_out_channels': [32, 64, 128, 256],
                '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',
            }
            _A=						self.dummy_input
            return init_dict, inputs_dict



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

            value_function.to(_SCREAMING_SNAKE_CASE							)
            _A=						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       a__	(       self							):
            _A,      _A=						UNetaDModel.from_pretrained(
                'bglick13/hopper-medium-v2-value-function-hor32'					,   output_loading_info=_SCREAMING_SNAKE_CASE					,   subfolder='value_function'							)
            torch.manual_seed(0							)
            if torch.cuda.is_available():
                   torch.cuda.manual_seed_all(0							)

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

            with torch.no_grad():
                   _A=						value_function(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE							).sample

            # fmt: off
            _A=						torch.tensor([165.25] * seq_len							)
            # fmt: on
            self.assertTrue(torch.allclose(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   rtol=1E-3							)							)





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



class 			lowerCamelCase_					:





							def __init__(							self    ):
								a_     			=   [0, 1]





							def   __magic_name__      (							self			,       _SCREAMING_SNAKE_CASE    ):
								if (difference := index - (len(self.sequence    ) - 2)) >= 1:
									for _ in range(_SCREAMING_SNAKE_CASE    ):
										self.sequence.append(self.sequence[-1] + self.sequence[-2]    )
								return self.sequence[:index]







def 			__SCREAMING_SNAKE_CASE		(		)     ->	None:



	"""simple docstring"""






	print(
	    """Fibonacci Series Using Dynamic Programming\n"""     ,					"""Enter the index of the Fibonacci number you want to calculate """     ,					"""in the prompt below. (To exit enter exit or Ctrl-C)\n"""     ,					sep=""""""     ,					)
	a_     			=   Fibonacci()

	while True:
		a_     			=   input(""">> """	)
		if prompt in {"exit", "quit"}:
			break

		try:
			a_     			=   int(UpperCamelCase	)
		except ValueError:
			print("""Enter a number or 'exit'"""	)
			continue

		print(fibonacci.get(UpperCamelCase	)	)


if __name__ == "__main__":
			main()