


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):
	_a  :  List[str]				   =							DanceDiffusionPipeline
	_a  :  Tuple				   =							UNCONDITIONAL_AUDIO_GENERATION_PARAMS
	_a  :  Tuple				   =							PipelineTesterMixin.required_optional_params - {
	    '''callback''',
	    '''latents''',
	    '''callback_steps''',
	    '''output_type''',
	    '''num_images_per_prompt''',
	}
	_a  :  Optional[Any]				   =							UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
	_a  :  Optional[int]				   =							False
	_a  :  Union[str, Any]				   =							False




	def 							UpperCAmelCase__( self						:		Tuple		)->	Optional[Any]:
			torch.manual_seed(0		)
			lowerCAmelCase__       :      Dict   					= 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=SCREAMING_SNAKE_CASE_		, use_timestep_embedding=SCREAMING_SNAKE_CASE_		, time_embedding_type='''fourier'''		, mid_block_type='''UNetMidBlock1D'''		, down_block_types=('''DownBlock1DNoSkip''', '''DownBlock1D''', '''AttnDownBlock1D''')		, up_block_types=('''AttnUpBlock1D''', '''UpBlock1D''', '''UpBlock1DNoSkip''')		, )
			lowerCAmelCase__       :      Any   					= IPNDMScheduler()

			lowerCAmelCase__       :      List[str]   					= {
			    """unet""": unet,
			    """scheduler""": scheduler,
			}
			return components




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




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

			lowerCAmelCase__       :      int   					= self.get_dummy_inputs(SCREAMING_SNAKE_CASE_		)
			lowerCAmelCase__       :      Optional[Any]   					= pipe(**SCREAMING_SNAKE_CASE_		)
			lowerCAmelCase__       :      int   					= output.audios

			lowerCAmelCase__       :      List[str]   					= audio[0, -3:, -3:]

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




	@skip_mps
	def 							UpperCAmelCase__( self						:		str		)->	Any:
			return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3		)




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




	@skip_mps
	def 							UpperCAmelCase__( self						:		int		)->	Dict:
			return super().test_attention_slicing_forward_pass()




	def 							UpperCAmelCase__( self						:		int		)->	Optional[int]:
			super().test_inference_batch_single_identical(expected_max_diff=3E-3		)





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




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




	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	List[Any]:
			lowerCAmelCase__       :      Any   					= torch_device

			lowerCAmelCase__       :      Tuple   					= DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''		)
			lowerCAmelCase__       :      int   					= pipe.to(SCREAMING_SNAKE_CASE_		)
			pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)

			lowerCAmelCase__       :      Dict   					= torch.manual_seed(0		)
			lowerCAmelCase__       :      Union[str, Any]   					= pipe(generator=SCREAMING_SNAKE_CASE_		, num_inference_steps=100		, audio_length_in_s=4.096		)
			lowerCAmelCase__       :      Optional[Any]   					= output.audios

			lowerCAmelCase__       :      List[Any]   					= audio[0, -3:, -3:]

			assert audio.shape == (1, 2, pipe.unet.sample_size)
			lowerCAmelCase__       :      Optional[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 							UpperCAmelCase__( self						:		Any		)->	Union[str, Any]:
			lowerCAmelCase__       :      Dict   					= torch_device

			lowerCAmelCase__       :      Optional[Any]   					= DanceDiffusionPipeline.from_pretrained('''harmonai/maestro-150k'''		, torch_dtype=torch.floataa		)
			lowerCAmelCase__       :      Dict   					= pipe.to(SCREAMING_SNAKE_CASE_		)
			pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_		)

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

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

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




from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase						=		logging.get_logger(__name__)

lowerCamelCase						=		{
    '''facebook/dpr-ctx_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-ctx_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json'''
    ),
}





class  _a							(    _lowercase):
	_a  :  List[Any]				   =							'''dpr'''




	def __init__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		List[str]=3_0522		, _SCREAMING_SNAKE_CASE						:		Optional[int]=768		, _SCREAMING_SNAKE_CASE						:		List[str]=12		, _SCREAMING_SNAKE_CASE						:		Tuple=12		, _SCREAMING_SNAKE_CASE						:		str=3072		, _SCREAMING_SNAKE_CASE						:		Optional[Any]="gelu"		, _SCREAMING_SNAKE_CASE						:		Dict=0.1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0.1		, _SCREAMING_SNAKE_CASE						:		List[str]=512		, _SCREAMING_SNAKE_CASE						:		int=2		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=0.02		, _SCREAMING_SNAKE_CASE						:		Tuple=1E-12		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=0		, _SCREAMING_SNAKE_CASE						:		List[str]="absolute"		, _SCREAMING_SNAKE_CASE						:		int = 0		, **_SCREAMING_SNAKE_CASE						:		Optional[Any]		, )->	Optional[int]:
			super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		)

			lowerCAmelCase__       :      Union[str, Any]   					= vocab_size
			lowerCAmelCase__       :      Tuple   					= hidden_size
			lowerCAmelCase__       :      Union[str, Any]   					= num_hidden_layers
			lowerCAmelCase__       :      List[Any]   					= num_attention_heads
			lowerCAmelCase__       :      Optional[int]   					= hidden_act
			lowerCAmelCase__       :      Optional[Any]   					= intermediate_size
			lowerCAmelCase__       :      Optional[Any]   					= hidden_dropout_prob
			lowerCAmelCase__       :      int   					= attention_probs_dropout_prob
			lowerCAmelCase__       :      str   					= max_position_embeddings
			lowerCAmelCase__       :      List[Any]   					= type_vocab_size
			lowerCAmelCase__       :      Optional[int]   					= initializer_range
			lowerCAmelCase__       :      List[str]   					= layer_norm_eps
			lowerCAmelCase__       :      Dict   					= projection_dim
			lowerCAmelCase__       :      int   					= position_embedding_type




