
import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()

class        _UpperCamelCase				(					_A     ,       unittest.TestCase  ):






			'''simple docstring'''

			__UpperCamelCase      :						str					      = DanceDiffusionPipeline
			__UpperCamelCase      :						Optional[Any]					      = UNCONDITIONAL_AUDIO_GENERATION_PARAMS
			__UpperCamelCase      :						int					      = PipelineTesterMixin.required_optional_params - {
			    """callback""",
			    """latents""",
			    """callback_steps""",
			    """output_type""",
			    """num_images_per_prompt""",
			}
			__UpperCamelCase      :						Optional[int]					      = UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
			__UpperCamelCase      :						Union[str, Any]					      = False
			__UpperCamelCase      :						Optional[Any]					      = False

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							torch.manual_seed(0      )
							UpperCamelCase_:							Optional[int]	=						UNetaDModel(
							    block_out_channels=(32, 32, 64)						,	extra_in_channels=16						,	sample_size=512						,	sample_rate=1_6000						,	in_channels=2						,	out_channels=2						,	flip_sin_to_cos=snake_case_						,	use_timestep_embedding=snake_case_						,	time_embedding_type="""fourier"""						,	mid_block_type="""UNetMidBlock1D"""						,	down_block_types=("""DownBlock1DNoSkip""", """DownBlock1D""", """AttnDownBlock1D""")						,	up_block_types=("""AttnUpBlock1D""", """UpBlock1D""", """UpBlock1DNoSkip""")						,	)
							UpperCamelCase_:							Optional[int]	=						IPNDMScheduler()

							UpperCamelCase_:							Optional[Any]	=						{
							    """unet""": unet,
							    """scheduler""": scheduler,
							}
							return components

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       List[str]						,	snake_case_ :       int=0      ):
							if str(snake_case_      ).startswith("""mps"""      ):
											UpperCamelCase_:							List[Any]	=						torch.manual_seed(snake_case_      )
							else:
											UpperCamelCase_:							List[Any]	=						torch.Generator(device=snake_case_      ).manual_seed(snake_case_      )
							UpperCamelCase_:							Tuple	=						{
							    """batch_size""": 1,
							    """generator""": generator,
							    """num_inference_steps""": 4,
							}
							return inputs

			def       lowerCAmelCase__	(       self :       List[str]      ):
							UpperCamelCase_:							List[Any]	=						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							UpperCamelCase_:							Any	=						self.get_dummy_components()
							UpperCamelCase_:							Optional[Any]	=						DanceDiffusionPipeline(**snake_case_      )
							UpperCamelCase_:							str	=						pipe.to(snake_case_      )
							pipe.set_progress_bar_config(disable=snake_case_      )

							UpperCamelCase_:							Union[str, Any]	=						self.get_dummy_inputs(snake_case_      )
							UpperCamelCase_:							Union[str, Any]	=						pipe(**snake_case_      )
							UpperCamelCase_:							Optional[int]	=						output.audios

							UpperCamelCase_:							str	=						audio[0, -3:, -3:]

							assert audio.shape == (1, 2, components["unet"].sample_size)
							UpperCamelCase_:							Tuple	=						np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000]      )
							assert np.abs(audio_slice.flatten() - expected_slice      ).max() < 1e-2

			@skip_mps
			def       lowerCAmelCase__	(       self :       Any      ):
							return super().test_save_load_local()

			@skip_mps
			def       lowerCAmelCase__	(       self :       Any      ):
							return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3      )

			@skip_mps
			def       lowerCAmelCase__	(       self :       str      ):
							return super().test_save_load_optional_components()

			@skip_mps
			def       lowerCAmelCase__	(       self :       str      ):
							return super().test_attention_slicing_forward_pass()




			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							super().test_inference_batch_single_identical(expected_max_diff=3e-3      )




@slow
@require_torch_gpu
class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							UpperCamelCase_:							Optional[int]	=						torch_device

							UpperCamelCase_:							Tuple	=						DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""      )
							UpperCamelCase_:							List[Any]	=						pipe.to(snake_case_      )
							pipe.set_progress_bar_config(disable=snake_case_      )

							UpperCamelCase_:							List[Any]	=						torch.manual_seed(0      )
							UpperCamelCase_:							int	=						pipe(generator=snake_case_						,	num_inference_steps=100						,	audio_length_in_s=4.096      )
							UpperCamelCase_:							Optional[int]	=						output.audios

							UpperCamelCase_:							Tuple	=						audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							UpperCamelCase_:							List[Any]	=						np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020]      )

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




			def       lowerCAmelCase__	(       self :       int      ):
							UpperCamelCase_:							Tuple	=						torch_device

							UpperCamelCase_:							Union[str, Any]	=						DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""						,	torch_dtype=torch.floataa      )
							UpperCamelCase_:							List[Any]	=						pipe.to(snake_case_      )
							pipe.set_progress_bar_config(disable=snake_case_      )

							UpperCamelCase_:							Optional[int]	=						torch.manual_seed(0      )
							UpperCamelCase_:							Union[str, Any]	=						pipe(generator=snake_case_						,	num_inference_steps=100						,	audio_length_in_s=4.096      )
							UpperCamelCase_:							Optional[Any]	=						output.audios

							UpperCamelCase_:							Union[str, Any]	=						audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							UpperCamelCase_:							List[Any]	=						np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341]      )

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








class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       List[str]						,	snake_case_ :       int						,	snake_case_ :       Optional[Any]=None						,	snake_case_ :       List[str]=None      ):
							UpperCamelCase_:							List[Any]	=						data
							UpperCamelCase_:							List[Any]	=						previous
							UpperCamelCase_:							Tuple	=						next_node

			def __str__(       self :       Dict      ):
							return f'''{self.data}'''

			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.data

			def       lowerCAmelCase__	(       self :       Any      ):
							return self.next




			def       lowerCAmelCase__	(       self :       List[str]      ):
							return self.previous




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       Optional[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						head

			def __iter__(       self :       Union[str, Any]      ):
							return self




			def       lowerCAmelCase__	(       self :       Union[str, Any]      ):
							if not self.current:
											raise StopIteration
							else:
											UpperCamelCase_:							Dict	=						self.current.get_data()
											UpperCamelCase_:							Tuple	=						self.current.get_next()
											return value




class        _UpperCamelCase				:






			'''simple docstring'''

			def __init__(       self :       int      ):
							UpperCamelCase_:							Optional[int]	=						None  # First node in list
							UpperCamelCase_:							Dict	=						None  # Last node in list

			def __str__(       self :       Tuple      ):
							UpperCamelCase_:							int	=						self.head
							UpperCamelCase_:							Tuple	=						[]
							while current is not None:
											nodes.append(current.get_data()      )
											UpperCamelCase_:							List[str]	=						current.get_next()
							return " ".join(str(snake_case_      ) for node in nodes      )

			def __contains__(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Optional[Any]	=						self.head
							while current:
											if current.get_data() == value:
															return True
											UpperCamelCase_:							Any	=						current.get_next()
							return False

			def __iter__(       self :       Any      ):
							return LinkedListIterator(self.head      )

			def       lowerCAmelCase__	(       self :       Tuple      ):
							if self.head:
											return self.head.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							if self.tail:
											return self.tail.get_data()
							return None

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       Node      ):
							if self.head is None:
											UpperCamelCase_:							Tuple	=						node
											UpperCamelCase_:							Optional[int]	=						node
							else:
											self.insert_before_node(self.head						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node      ):
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       int      ):
							UpperCamelCase_:							Any	=						Node(snake_case_      )
							if self.head is None:
											self.set_head(snake_case_      )
							else:
											self.set_tail(snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							str	=						node
							UpperCamelCase_:							int	=						node.previous

							if node.get_previous() is None:
											UpperCamelCase_:							int	=						node_to_insert
							else:
											UpperCamelCase_:							Dict	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Dict						,	snake_case_ :       Node						,	snake_case_ :       Node      ):
							UpperCamelCase_:							Tuple	=						node
							UpperCamelCase_:							Dict	=						node.next

							if node.get_next() is None:
											UpperCamelCase_:							Union[str, Any]	=						node_to_insert
							else:
											UpperCamelCase_:							str	=						node_to_insert

							UpperCamelCase_:							int	=						node_to_insert

			def       lowerCAmelCase__	(       self :       Tuple						,	snake_case_ :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						1
							UpperCamelCase_:							List[str]	=						Node(snake_case_      )
							UpperCamelCase_:							Optional[Any]	=						self.head
							while node:
											if current_position == position:
															self.insert_before_node(snake_case_						,	snake_case_      )
															return
											current_position += 1
											UpperCamelCase_:							Dict	=						node.next
							self.insert_after_node(self.tail						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       int						,	snake_case_ :       int      ):
							UpperCamelCase_:							Union[str, Any]	=						self.head
							while node:
											if node.get_data() == item:
															return node
											UpperCamelCase_:							List[Any]	=						node.get_next()
							raise Exception("""Node not found"""      )

			def       lowerCAmelCase__	(       self :       List[Any]						,	snake_case_ :       List[str]      ):
							if (node := self.get_node(snake_case_      )) is not None:
											if node == self.head:
															UpperCamelCase_:							Optional[int]	=						self.head.get_next()

											if node == self.tail:
															UpperCamelCase_:							Union[str, Any]	=						self.tail.get_previous()

											self.remove_node_pointers(snake_case_      )

			@staticmethod
			def       lowerCAmelCase__	(       snake_case_ :       Node      ):
							if node.get_next():
											UpperCamelCase_:							str	=						node.previous

							if node.get_previous():
											UpperCamelCase_:							int	=						node.next

							UpperCamelCase_:							List[str]	=						None
							UpperCamelCase_:							int	=						None




			def       lowerCAmelCase__	(       self :       str      ):
							return self.head is None







def        A__					(						)  ->	None:
				pass


if __name__ == "__main__":
							import doctest

							doctest.testmod()






