



"""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 (					lowerCAmelCase			,							unittest.TestCase):
			"""simple docstring"""
			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  lowercase__						(		self     :    List[Any]   )->Tuple:
							torch.manual_seed(0   )
							_UpperCAmelCase					=    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=__UpperCamelCase				,   use_timestep_embedding=__UpperCamelCase				,   time_embedding_type='''fourier'''				,   mid_block_type='''UNetMidBlock1D'''				,   down_block_types=('''DownBlock1DNoSkip''', '''DownBlock1D''', '''AttnDownBlock1D''')				,   up_block_types=('''AttnUpBlock1D''', '''UpBlock1D''', '''UpBlock1DNoSkip''')				,   )
							_UpperCAmelCase					=    IPNDMScheduler()

							_UpperCAmelCase					=    {
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							}
							return components



			def  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Optional[int]=0   )->Union[str, Any]:
							if str(__UpperCamelCase   ).startswith('''mps'''   ):
											_UpperCAmelCase					=    torch.manual_seed(__UpperCamelCase   )
							else:
											_UpperCAmelCase					=    torch.Generator(device=__UpperCamelCase   ).manual_seed(__UpperCamelCase   )
							_UpperCAmelCase					=    {
							    '''batch_size''': 1,
							    '''generator''': generator,
							    '''num_inference_steps''': 4,
							}
							return inputs



			def  lowercase__						(		self     :    Tuple   )->Union[str, Any]:
							_UpperCAmelCase					=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							_UpperCAmelCase					=    self.get_dummy_components()
							_UpperCAmelCase					=    DanceDiffusionPipeline(**__UpperCamelCase   )
							_UpperCAmelCase					=    pipe.to(__UpperCamelCase   )
							pipe.set_progress_bar_config(disable=__UpperCamelCase   )

							_UpperCAmelCase					=    self.get_dummy_inputs(__UpperCamelCase   )
							_UpperCAmelCase					=    pipe(**__UpperCamelCase   )
							_UpperCAmelCase					=    output.audios

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

							assert audio.shape == (1, 2, components["unet"].sample_size)
							_UpperCAmelCase					=    np.array([-0.7_2_6_5, 1.0_0_0_0, -0.8_3_8_8, 0.1_1_7_5, 0.9_4_9_8, -1.0_0_0_0]   )
							assert np.abs(audio_slice.flatten() - expected_slice   ).max() < 1e-2



			@skip_mps
			def  lowercase__						(		self     :    Tuple   )->Tuple:
							return super().test_save_load_local()



			@skip_mps
			def  lowercase__						(		self     :    Any   )->List[str]:
							return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3   )



			@skip_mps
			def  lowercase__						(		self     :    Dict   )->Dict:
							return super().test_save_load_optional_components()



			@skip_mps
			def  lowercase__						(		self     :    List[Any]   )->Any:
							return super().test_attention_slicing_forward_pass()



			def  lowercase__						(		self     :    Optional[Any]   )->List[str]:
							super().test_inference_batch_single_identical(expected_max_diff=3e-3   )



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



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



			def  lowercase__						(		self     :    Tuple   )->List[Any]:
							_UpperCAmelCase					=    torch_device

							_UpperCAmelCase					=    DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''   )
							_UpperCAmelCase					=    pipe.to(__UpperCamelCase   )
							pipe.set_progress_bar_config(disable=__UpperCamelCase   )

							_UpperCAmelCase					=    torch.manual_seed(0   )
							_UpperCAmelCase					=    pipe(generator=__UpperCamelCase				,   num_inference_steps=1_0_0				,   audio_length_in_s=4.0_9_6   )
							_UpperCAmelCase					=    output.audios

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

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCAmelCase					=    np.array([-0.0_1_9_2, -0.0_2_3_1, -0.0_3_1_8, -0.0_0_5_9, 0.0_0_0_2, -0.0_0_2_0]   )

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



			def  lowercase__						(		self     :    List[Any]   )->str:
							_UpperCAmelCase					=    torch_device

							_UpperCAmelCase					=    DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''				,   torch_dtype=torch.floataa   )
							_UpperCAmelCase					=    pipe.to(__UpperCamelCase   )
							pipe.set_progress_bar_config(disable=__UpperCamelCase   )

							_UpperCAmelCase					=    torch.manual_seed(0   )
							_UpperCAmelCase					=    pipe(generator=__UpperCamelCase				,   num_inference_steps=1_0_0				,   audio_length_in_s=4.0_9_6   )
							_UpperCAmelCase					=    output.audios

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

							assert audio.shape == (1, 2, pipe.unet.sample_size)
							_UpperCAmelCase					=    np.array([-0.0_3_6_7, -0.0_4_8_8, -0.0_7_7_1, -0.0_5_2_5, -0.0_4_4_4, -0.0_3_4_1]   )

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








"""simple docstring"""


import operator







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						list					,     _SCREAMING_SNAKE_CASE   :						bool = False					,     _SCREAMING_SNAKE_CASE   :						list | None = None				):






				'''simple docstring'''



				_UpperCAmelCase					=    operator.lt if reverse else operator.gt
				_UpperCAmelCase					=    solution or []

				if not arr:
								return solution

				_UpperCAmelCase					=    [arr.pop(0				)]
				for i, item in enumerate(_SCREAMING_SNAKE_CASE				):
								if _operator(_SCREAMING_SNAKE_CASE					,     sublist[-1]				):
												sublist.append(_SCREAMING_SNAKE_CASE				)
												arr.pop(_SCREAMING_SNAKE_CASE				)

    #  merging sublist into solution list
				if not solution:
								solution.extend(_SCREAMING_SNAKE_CASE				)
				else:
								while sublist:
												_UpperCAmelCase					=    sublist.pop(0				)
												for i, xx in enumerate(_SCREAMING_SNAKE_CASE				):
																if not _operator(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				):
																				solution.insert(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
																				break
												else:
																solution.append(_SCREAMING_SNAKE_CASE				)

				strand_sort(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
				return solution


if __name__ == "__main__":
					assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5]
					assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]



