





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	(  __SCREAMING_SNAKE_CASE			,     unittest.TestCase    ):


    '''simple docstring'''







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



    def  lowercase_					(       self     ):





        '''simple docstring'''

        torch.manual_seed(0     )
        SCREAMING_SNAKE_CASE__											=			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=A_   ,				use_timestep_embedding=A_   ,				time_embedding_type='''fourier'''   ,				mid_block_type='''UNetMidBlock1D'''   ,				down_block_types=('''DownBlock1DNoSkip''', '''DownBlock1D''', '''AttnDownBlock1D''')   ,				up_block_types=('''AttnUpBlock1D''', '''UpBlock1D''', '''UpBlock1DNoSkip''')   ,				)
        SCREAMING_SNAKE_CASE__											=			IPNDMScheduler()

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



    def  lowercase_					(       self   ,				A_   ,				A_=0     ):





        '''simple docstring'''

        if str(A_     ).startswith('''mps'''     ):
            SCREAMING_SNAKE_CASE__											=			torch.manual_seed(A_     )
        else:
            SCREAMING_SNAKE_CASE__											=			torch.Generator(device=A_     ).manual_seed(A_     )
        SCREAMING_SNAKE_CASE__											=			{
            '''batch_size''': 1,
            '''generator''': generator,
            '''num_inference_steps''': 4,
        }
        return inputs



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        SCREAMING_SNAKE_CASE__											=			self.get_dummy_components()
        SCREAMING_SNAKE_CASE__											=			DanceDiffusionPipeline(**A_     )
        SCREAMING_SNAKE_CASE__											=			pipe.to(A_     )
        pipe.set_progress_bar_config(disable=A_     )

        SCREAMING_SNAKE_CASE__											=			self.get_dummy_inputs(A_     )
        SCREAMING_SNAKE_CASE__											=			pipe(**A_     )
        SCREAMING_SNAKE_CASE__											=			output.audios

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

        assert audio.shape == (1, 2, components["unet"].sample_size)
        SCREAMING_SNAKE_CASE__											=			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  lowercase_					(       self     ):





        '''simple docstring'''

        return super().test_save_load_local()



    @skip_mps
    def  lowercase_					(       self     ):





        '''simple docstring'''

        return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3     )



    @skip_mps
    def  lowercase_					(       self     ):





        '''simple docstring'''

        return super().test_save_load_optional_components()



    @skip_mps
    def  lowercase_					(       self     ):





        '''simple docstring'''

        return super().test_attention_slicing_forward_pass()




    def  lowercase_					(       self     ):





        '''simple docstring'''

        super().test_inference_batch_single_identical(expected_max_diff=3E-3     )





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


    '''simple docstring'''







    def  lowercase_					(       self     ):





        '''simple docstring'''

        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()



    def  lowercase_					(       self     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			torch_device

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

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

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

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





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			torch_device

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

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

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

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


    '''simple docstring'''







    def __init__(       self   ,				A_   ,				A_   ,				A_     ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__											=			name
        SCREAMING_SNAKE_CASE__											=			value
        SCREAMING_SNAKE_CASE__											=			weight



    def __repr__(       self     ):





        '''simple docstring'''

        return f'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})'''



    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self.value



    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self.name



    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self.weight




    def  lowercase_					(       self     ):





        '''simple docstring'''

        return self.value / self.weight





def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_ ,							lowerCAmelCase_	)				->						Any:
    SCREAMING_SNAKE_CASE__											=			[]
    for i in range(len(lowerCAmelCase_	)	):
        menu.append(Things(name[i] ,							value[i] ,							weight[i]	)	)
    return menu


def 							__snake_case							(    lowerCAmelCase_ ,							lowerCAmelCase_ ,							lowerCAmelCase_	)				->						Dict:
    SCREAMING_SNAKE_CASE__											=			sorted(lowerCAmelCase_ ,							key=lowerCAmelCase_ ,							reverse=lowerCAmelCase_	)
    SCREAMING_SNAKE_CASE__											=			[]
    SCREAMING_SNAKE_CASE__				,		SCREAMING_SNAKE_CASE__											=			0.0, 0.0
    for i in range(len(lowerCAmelCase_	)	):
        if (total_cost + items_copy[i].get_weight()) <= max_cost:
            result.append(items_copy[i]	)
            total_cost += items_copy[i].get_weight()
            total_value += items_copy[i].get_value()
    return (result, total_value)


def 							__snake_case							(    )				->						str:
    pass


if __name__ == "__main__":
  import doctest

  doctest.testmod()
