




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     snake_case__							(_UpperCamelCase    ,       unittest.TestCase   ):
				"""simple docstring"""



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





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     Dict:
							torch.manual_seed(0							)
							a    =			UNetaDModel(
							    block_out_channels=(32, 32, 64)					,      extra_in_channels=16					,      sample_size=5_12					,      sample_rate=1_60_00					,      in_channels=2					,      out_channels=2					,      flip_sin_to_cos=__lowerCamelCase					,      use_timestep_embedding=__lowerCamelCase					,      time_embedding_type="fourier"					,      mid_block_type="UNetMidBlock1D"					,      down_block_types=("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")					,      up_block_types=("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")					,      )
							a    =			IPNDMScheduler()

							a    =			{
							    "unet": unet,
							    "scheduler": scheduler,
							}
							return components





				def 							__UpperCAmelCase							(      self				:     Dict					,      __lowerCamelCase				:     Union[str, Any]					,      __lowerCamelCase				:     int=0							)					->     Optional[Any]:
							if str(__lowerCamelCase							).startswith("mps"							):
										a    =			torch.manual_seed(__lowerCamelCase							)
							else:
										a    =			torch.Generator(device=__lowerCamelCase							).manual_seed(__lowerCamelCase							)
							a    =			{
							    "batch_size": 1,
							    "generator": generator,
							    "num_inference_steps": 4,
							}
							return inputs





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Any:
							a    =			"cpu"  # ensure determinism for the device-dependent torch.Generator
							a    =			self.get_dummy_components()
							a    =			DanceDiffusionPipeline(**__lowerCamelCase							)
							a    =			pipe.to(__lowerCamelCase							)
							pipe.set_progress_bar_config(disable=__lowerCamelCase							)

							a    =			self.get_dummy_inputs(__lowerCamelCase							)
							a    =			pipe(**__lowerCamelCase							)
							a    =			output.audios

							a    =			audio[0, -3:, -3:]

							assert audio.shape == (1, 2, components["unet"].sample_size)
							a    =			np.array([-0.7_265, 1.0_000, -0.8_388, 0.1_175, 0.9_498, -1.0_000]							)
							assert np.abs(audio_slice.flatten() - expected_slice							).max() < 1e-2





				@skip_mps
				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     List[Any]:
							return super().test_save_load_local()





				@skip_mps
				def 							__UpperCAmelCase							(      self				:     str							)					->     Tuple:
							return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3							)





				@skip_mps
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     List[str]:
							return super().test_save_load_optional_components()





				@skip_mps
				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Union[str, Any]:
							return super().test_attention_slicing_forward_pass()





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     List[Any]:
							super().test_inference_batch_single_identical(expected_max_diff=3e-3							)




@slow
@require_torch_gpu
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Dict:
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     str:
							a    =			torch_device

							a    =			DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"							)
							a    =			pipe.to(__lowerCamelCase							)
							pipe.set_progress_bar_config(disable=__lowerCamelCase							)

							a    =			torch.manual_seed(0							)
							a    =			pipe(generator=__lowerCamelCase					,      num_inference_steps=1_00					,      audio_length_in_s=4.096							)
							a    =			output.audios

							a    =			audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							a    =			np.array([-0.0_192, -0.0_231, -0.0_318, -0.0_059, 0.0_002, -0.0_020]							)

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





				def 							__UpperCAmelCase							(      self				:     Dict							)					->     List[Any]:
							a    =			torch_device

							a    =			DanceDiffusionPipeline.from_pretrained("harmonai/maestro-150k"					,      torch_dtype=torch.floataa							)
							a    =			pipe.to(__lowerCamelCase							)
							pipe.set_progress_bar_config(disable=__lowerCamelCase							)

							a    =			torch.manual_seed(0							)
							a    =			pipe(generator=__lowerCamelCase					,      num_inference_steps=1_00					,      audio_length_in_s=4.096							)
							a    =			output.audios

							a    =			audio[0, -3:, -3:]

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							a    =			np.array([-0.0_367, -0.0_488, -0.0_771, -0.0_525, -0.0_444, -0.0_341]							)

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











import numpy as np

class 		UpperCAmelCase__			:




			'''simple docstring'''






			def __init__(   self							: List[Any] ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[Any]													=	(0, 0)
					__UpperCAmelCase    :     List[str]													=	None
					__UpperCAmelCase    :     int													=	0
					__UpperCAmelCase    :     Union[str, Any]													=	0
					__UpperCAmelCase    :     List[Any]													=	0




			def __eq__(   self							: Optional[int]       ,       a_							: List[Any] ):
					'''simple docstring'''





					return self.position == cell.position




			def  snake_case__     (   self							: List[Any] ):
					'''simple docstring'''





					print(self.position )

class 		UpperCAmelCase__			:




			'''simple docstring'''






			def __init__(   self							: str       ,       a_							: Dict=(5, 5) ):
					'''simple docstring'''





					__UpperCAmelCase    :     str													=	np.zeros(a_ )
					__UpperCAmelCase    :     List[Any]													=	world_size[0]
					__UpperCAmelCase    :     List[str]													=	world_size[1]




			def  snake_case__     (   self							: Tuple ):
					'''simple docstring'''





					print(self.w )




			def  snake_case__     (   self							: Union[str, Any]       ,       a_							: Tuple ):
					'''simple docstring'''





					__UpperCAmelCase    :     Optional[Any]													=	[
					    (-1, -1),
					    (-1, 0),
					    (-1, 1),
					    (0, -1),
					    (0, 1),
					    (1, -1),
					    (1, 0),
					    (1, 1),
					]
					__UpperCAmelCase    :     str													=	cell.position[0]
					__UpperCAmelCase    :     List[Any]													=	cell.position[1]
					__UpperCAmelCase    :     str													=	[]
					for n in neughbour_cord:
							__UpperCAmelCase    :     Optional[Any]													=	current_x + n[0]
							__UpperCAmelCase    :     Any													=	current_y + n[1]
							if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit:
									__UpperCAmelCase    :     int													=	Cell()
									__UpperCAmelCase    :     List[str]													=	(x, y)
									__UpperCAmelCase    :     List[Any]													=	cell
									neighbours.append(a_ )
					return neighbours

def        a						(      _UpperCAmelCase							:					List[Any] ,   _UpperCAmelCase							:					Any ,   _UpperCAmelCase							:					int						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[Any]													=	[]
		__UpperCAmelCase    :     Optional[Any]													=	[]
		_open.append(_UpperCAmelCase						)

		while _open:
				__UpperCAmelCase    :     str													=	np.argmin([n.f for n in _open]						)
				__UpperCAmelCase    :     Optional[Any]													=	_open[min_f]
				_closed.append(_open.pop(_UpperCAmelCase						)						)
				if current == goal:
						break
				for n in world.get_neigbours(_UpperCAmelCase						):
						for c in _closed:
								if c == n:
										continue
						__UpperCAmelCase    :     Optional[Any]													=	current.g + 1
						__UpperCAmelCase							,					__UpperCAmelCase    :     Union[str, Any]													=	n.position
						__UpperCAmelCase							,					__UpperCAmelCase    :     Tuple													=	goal.position
						__UpperCAmelCase    :     Union[str, Any]													=	(ya - ya) ** 2 + (xa - xa) ** 2
						__UpperCAmelCase    :     Optional[Any]													=	n.h + n.g

						for c in _open:
								if c == n and c.f < n.f:
										continue
						_open.append(_UpperCAmelCase						)
		__UpperCAmelCase    :     str													=	[]
		while current.parent is not None:
				path.append(current.position						)
				__UpperCAmelCase    :     int													=	current.parent
		path.append(current.position						)
		return path[::-1]


if __name__ == "__main__":
				__A   			=Gridworld()
				# Start position and goal
				__A   			=Cell()
				__A   			=(0, 0)
				__A   			=Cell()
				__A   			=(4, 4)
				print(f'''path from {start.position} to {goal.position}''')
				__A   			=astar(world, start, goal)
				#   Just for visual reasons.
				for i in s:
								__A   			=1
				print(world.w)





