






"""simple docstring"""





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       __A		(   SCREAMING_SNAKE_CASE_     ,unittest.TestCase			):
					UpperCAmelCase__								=				DanceDiffusionPipeline
					UpperCAmelCase__								=				UNCONDITIONAL_AUDIO_GENERATION_PARAMS
					UpperCAmelCase__								=				PipelineTesterMixin.required_optional_params - {
					    "callback",
					    "latents",
					    "callback_steps",
					    "output_type",
					    "num_images_per_prompt",
					}
					UpperCAmelCase__								=				UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
					UpperCAmelCase__								=				False
					UpperCAmelCase__								=				False

					def   lowerCamelCase__   (			self  :      Union[str, Any]					)			->							List[Any]:
											torch.manual_seed(0					)
											__magic_name__:	Dict							=       UNetaDModel(
											    block_out_channels=(3_2, 3_2, 6_4)     ,		extra_in_channels=1_6     ,		sample_size=5_1_2     ,		sample_rate=1_6_0_0_0     ,		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""")     ,		)
											__magic_name__:	Tuple							=       IPNDMScheduler()

											__magic_name__:	Union[str, Any]							=       {
											    """unet""": unet,
											    """scheduler""": scheduler,
											}
											return components

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

					def   lowerCamelCase__   (			self  :      str					)			->							Optional[int]:
											__magic_name__:	int							=       """cpu"""  # ensure determinism for the device-dependent torch.Generator
											__magic_name__:	List[Any]							=       self.get_dummy_components()
											__magic_name__:	Dict							=       DanceDiffusionPipeline(**__snake_case					)
											__magic_name__:	List[str]							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

											__magic_name__:	Union[str, Any]							=       self.get_dummy_inputs(__snake_case					)
											__magic_name__:	Any							=       pipe(**__snake_case					)
											__magic_name__:	int							=       output.audios

											__magic_name__:	Optional[int]							=       audio[0, -3:, -3:]

											assert audio.shape == (1, 2, components["unet"].sample_size)
											__magic_name__:	Optional[Any]							=       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  :      Dict					)			->							Optional[int]:
											return super().test_save_load_local()

					@skip_mps
					def   lowerCamelCase__   (			self  :      int					)			->							Union[str, Any]:
											return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3					)

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

					@skip_mps
					def   lowerCamelCase__   (			self  :      Tuple					)			->							Any:
											return super().test_attention_slicing_forward_pass()






					def   lowerCamelCase__   (			self  :      Dict					)			->							Optional[Any]:
											super().test_inference_batch_single_identical(expected_max_diff=3E-3					)



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

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

					def   lowerCamelCase__   (			self  :      Tuple					)			->							Optional[int]:
											__magic_name__:	Tuple							=       torch_device

											__magic_name__:	Union[str, Any]							=       DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""					)
											__magic_name__:	Optional[Any]							=       pipe.to(__snake_case					)
											pipe.set_progress_bar_config(disable=__snake_case					)

											__magic_name__:	List[str]							=       torch.manual_seed(0					)
											__magic_name__:	Any							=       pipe(generator=__snake_case     ,		num_inference_steps=1_0_0     ,		audio_length_in_s=4.096					)
											__magic_name__:	Union[str, Any]							=       output.audios

											__magic_name__:	Optional[int]							=       audio[0, -3:, -3:]

											assert audio.shape == (1, 2, pipe.unet.sample_size)
											__magic_name__:	int							=       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					)			->							int:
											__magic_name__:	int							=       torch_device

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

											__magic_name__:	Tuple							=       torch.manual_seed(0					)
											__magic_name__:	List[Any]							=       pipe(generator=__snake_case     ,		num_inference_steps=1_0_0     ,		audio_length_in_s=4.096					)
											__magic_name__:	str							=       output.audios

											__magic_name__:	Optional[Any]							=       audio[0, -3:, -3:]

											assert audio.shape == (1, 2, pipe.unet.sample_size)
											__magic_name__:	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











"""simple docstring"""



from __future__ import annotations

from collections.abc import Iterator
from typing import Any




class 			lowerCAmelCase__			:
   def __init__(		self      :	Any							,			snake_case__      :	Any							):


        '''simple docstring'''
        UpperCAmelCase__						:    Any							=							data
        UpperCAmelCase__						:    Node | None							=							None




class 			lowerCAmelCase__			:
   def __init__(		self      :	Dict							):


        '''simple docstring'''
        UpperCAmelCase__						:    Tuple							=							None
        UpperCAmelCase__						:    Optional[int]							=							None
   def __iter__(		self      :	Tuple							):


        '''simple docstring'''
        UpperCAmelCase__						:    Optional[int]							=							self.head
        while self.head:
             yield node.data
             UpperCAmelCase__						:    Optional[Any]							=							node.next
             if node == self.head:
                  break
   def __len__(		self      :	Tuple							):


        '''simple docstring'''
        return sum(1 for _ in self							)
   def __repr__(		self      :	str							):


        '''simple docstring'''
        return "->".join(str(snake_case__							) for item in iter(self							)							)
   def       __a							(		self      :	Optional[Any]							,			snake_case__      :	Any							):


        '''simple docstring'''
        self.insert_nth(len(self							)							,			snake_case__							)
   def       __a							(		self      :	Optional[Any]							,			snake_case__      :	Any							):


        '''simple docstring'''
        self.insert_nth(0							,			snake_case__							)
   def       __a							(		self      :	Any							,			snake_case__      :	int							,			snake_case__      :	Any							):


        '''simple docstring'''
        if index < 0 or index > len(self							):
             raise IndexError("list index out of range."							)
        UpperCAmelCase__						:    Any							=							Node(snake_case__							)
        if self.head is None:
             UpperCAmelCase__						:    Union[str, Any]							=							new_node  # first node points itself
             UpperCAmelCase__						:    List[str]							=							new_node
        elif index == 0:  # insert at head
             UpperCAmelCase__						:    Optional[Any]							=							self.head
             UpperCAmelCase__						:    List[Any]							=							new_node
        else:
             UpperCAmelCase__						:    Union[str, Any]							=							self.head
             for _ in range(index - 1							):
                  UpperCAmelCase__						:    Dict							=							temp.next
             UpperCAmelCase__						:    Tuple							=							temp.next
             UpperCAmelCase__						:    str							=							new_node
             if index == len(self							) - 1:  # insert at tail
                  UpperCAmelCase__						:    Union[str, Any]							=							new_node
   def       __a							(		self      :	List[Any]							):


        '''simple docstring'''
        return self.delete_nth(0							)
   def       __a							(		self      :	Optional[Any]							):


        '''simple docstring'''
        return self.delete_nth(len(self							) - 1							)
   def       __a							(		self      :	Tuple							,			snake_case__      :	int = 0							):


        '''simple docstring'''
        if not 0 <= index < len(self							):
             raise IndexError("list index out of range."							)
        UpperCAmelCase__						:    Union[str, Any]							=							self.head
        if self.head == self.tail:  # just one node
             UpperCAmelCase__						:    str							=							None
        elif index == 0:  # delete head node
             UpperCAmelCase__						:    List[Any]							=							self.tail.next.next
             UpperCAmelCase__						:    Dict							=							self.head.next
        else:
             UpperCAmelCase__						:    List[Any]							=							self.head
             for _ in range(index - 1							):
                  UpperCAmelCase__						:    Tuple							=							temp.next
             UpperCAmelCase__						:    Dict							=							temp.next
             UpperCAmelCase__						:    Dict							=							temp.next.next
             if index == len(self							) - 1:  # delete at tail
                  UpperCAmelCase__						:    int							=							temp
        return delete_node.data




   def       __a							(		self      :	Any							):


        '''simple docstring'''
        return len(self							) == 0




def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     UpperCAmelCase__						:    str							=							CircularLinkedList()
     assert len(snake_case				) == 0
     assert circular_linked_list.is_empty() is True
     assert str(snake_case				) == ""

     try:
          circular_linked_list.delete_front()
          raise AssertionError  # This should not happen
     except IndexError:
          assert True  # This should happen

     try:
          circular_linked_list.delete_tail()
          raise AssertionError  # This should not happen
     except IndexError:
          assert True  # This should happen

     try:
          circular_linked_list.delete_nth(-1				)
          raise AssertionError
     except IndexError:
          assert True

     try:
          circular_linked_list.delete_nth(0				)
          raise AssertionError
     except IndexError:
          assert True

     assert circular_linked_list.is_empty() is True
     for i in range(5				):
          assert len(snake_case				) == i
          circular_linked_list.insert_nth(snake_case			,					i + 1				)
     assert str(snake_case				) == "->".join(str(snake_case				) for i in range(1			,					6				)				)

     circular_linked_list.insert_tail(6				)
     assert str(snake_case				) == "->".join(str(snake_case				) for i in range(1			,					7				)				)
     circular_linked_list.insert_head(0				)
     assert str(snake_case				) == "->".join(str(snake_case				) for i in range(0			,					7				)				)

     assert circular_linked_list.delete_front() == 0
     assert circular_linked_list.delete_tail() == 6
     assert str(snake_case				) == "->".join(str(snake_case				) for i in range(1			,					6				)				)
     assert circular_linked_list.delete_nth(2				) == 3

     circular_linked_list.insert_nth(2			,					3				)
     assert str(snake_case				) == "->".join(str(snake_case				) for i in range(1			,					6				)				)

     assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
 import doctest

 doctest.testmod()


