





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

			"""simple docstring"""
			__lowerCAmelCase     :				List[Any]										=  DanceDiffusionPipeline
			__lowerCAmelCase     :				Any										=  UNCONDITIONAL_AUDIO_GENERATION_PARAMS
			__lowerCAmelCase     :				List[str]										=  PipelineTesterMixin.required_optional_params - {
			    """callback""",
			    """latents""",
			    """callback_steps""",
			    """output_type""",
			    """num_images_per_prompt""",
			}
			__lowerCAmelCase     :				List[str]										=  UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
			__lowerCAmelCase     :				int										=  False
			__lowerCAmelCase     :				List[Any]										=  False







			def   _a      (      self    ):






									'''simple docstring'''







									torch.manual_seed(0    )
									UpperCamelCase				:		List[str]				   =			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=_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""")						,   )
									UpperCamelCase				:		Tuple				   =			IPNDMScheduler()

									UpperCamelCase				:		int				   =			{
									    """unet""": unet,
									    """scheduler""": scheduler,
									}
									return components







			def   _a      (      self						,   _A						,   _A=0    ):






									'''simple docstring'''







									if str(_A    ).startswith("""mps"""    ):
															UpperCamelCase				:		Tuple				   =			torch.manual_seed(_A    )
									else:
															UpperCamelCase				:		Union[str, Any]				   =			torch.Generator(device=_A    ).manual_seed(_A    )
									UpperCamelCase				:		List[Any]				   =			{
									    """batch_size""": 1,
									    """generator""": generator,
									    """num_inference_steps""": 4,
									}
									return inputs







			def   _a      (      self    ):






									'''simple docstring'''







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

									UpperCamelCase				:		str				   =			self.get_dummy_inputs(_A    )
									UpperCamelCase				:		Any				   =			pipe(**_A    )
									UpperCamelCase				:		List[str]				   =			output.audios

									UpperCamelCase				:		Dict				   =			audio[0, -3:, -3:]

									assert audio.shape == (1, 2, components["unet"].sample_size)
									UpperCamelCase				:		Any				   =			np.array([-0.72_65, 1.00_00, -0.83_88, 0.11_75, 0.94_98, -1.00_00]    )
									assert np.abs(audio_slice.flatten() - expected_slice    ).max() < 1e-2







			@skip_mps
			def   _a      (      self    ):






									'''simple docstring'''







									return super().test_save_load_local()







			@skip_mps
			def   _a      (      self    ):






									'''simple docstring'''







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







			@skip_mps
			def   _a      (      self    ):






									'''simple docstring'''







									return super().test_save_load_optional_components()







			@skip_mps
			def   _a      (      self    ):






									'''simple docstring'''







									return super().test_attention_slicing_forward_pass()







			def   _a      (      self    ):






									'''simple docstring'''







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






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

			"""simple docstring"""







			def   _a      (      self    ):






									'''simple docstring'''







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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			torch_device

									UpperCamelCase				:		str				   =			DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""    )
									UpperCamelCase				:		Optional[int]				   =			pipe.to(_A    )
									pipe.set_progress_bar_config(disable=_A    )

									UpperCamelCase				:		List[str]				   =			torch.manual_seed(0    )
									UpperCamelCase				:		Optional[Any]				   =			pipe(generator=_A						,   num_inference_steps=1_0_0						,   audio_length_in_s=4.0_96    )
									UpperCamelCase				:		Union[str, Any]				   =			output.audios

									UpperCamelCase				:		Dict				   =			audio[0, -3:, -3:]

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									UpperCamelCase				:		Any				   =			np.array([-0.01_92, -0.02_31, -0.03_18, -0.00_59, 0.00_02, -0.00_20]    )

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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[Any]				   =			torch_device

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

									UpperCamelCase				:		Any				   =			torch.manual_seed(0    )
									UpperCamelCase				:		Union[str, Any]				   =			pipe(generator=_A						,   num_inference_steps=1_0_0						,   audio_length_in_s=4.0_96    )
									UpperCamelCase				:		int				   =			output.audios

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

									assert audio.shape == (1, 2, pipe.unet.sample_size)
									UpperCamelCase				:		Dict				   =			np.array([-0.03_67, -0.04_88, -0.07_71, -0.05_25, -0.04_44, -0.03_41]    )

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








import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


__snake_case     :str			       =False



class        lowerCAmelCase__					(  unittest.TestCase       ):
				pass





@nightly
@require_torch_gpu
class        lowerCAmelCase__					(  unittest.TestCase       ):


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


				def __UpperCamelCase						(							self      :  Union[str, Any]       )		->     List[Any]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )

											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt='first prompt'						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=2						, output_type='numpy'						, ).images

											with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(__UpperCamelCase       )
																		A      						=    VersatileDiffusionPipeline.from_pretrained(__UpperCamelCase						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    generator.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt='first prompt'						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=2						, output_type='numpy'						, ).images

											assert np.abs(image - new_image       ).sum() < 1e-5, "Models don't have the same forward pass"


				def __UpperCamelCase						(							self      :  Tuple       )		->     List[str]:
											A      						=    VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'						, torch_dtype=torch.floataa       )
											pipe.to(__UpperCamelCase       )
											pipe.set_progress_bar_config(disable=__UpperCamelCase       )

											A      						=    'cyberpunk 2077'
											A      						=    load_image(
											    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'       )
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.dual_guided(
											    prompt=__UpperCamelCase						, image=__UpperCamelCase						, text_to_image_strength=0.7_5						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=50						, output_type='numpy'						, ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.1_4_4_8, 0.1_6_1_9, 0.1_7_4_1, 0.1_0_8_6, 0.1_1_4_7, 0.1_1_2_8, 0.1_1_9_9, 0.1_1_6_5, 0.1_0_0_1]       )

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

											A      						=    'A painting of a squirrel eating a burger '
											A      						=    torch.manual_seed(0       )
											A      						=    pipe.text_to_image(
											    prompt=__UpperCamelCase						, generator=__UpperCamelCase						, guidance_scale=7.5						, num_inference_steps=50						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8]       )

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

											A      						=    pipe.image_variation(__UpperCamelCase						, generator=__UpperCamelCase						, output_type='numpy'       ).images

											A      						=    image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											A      						=    np.array([0.3_0_7_6, 0.3_1_2_3, 0.3_2_8_4, 0.3_7_8_2, 0.3_7_7_0, 0.3_8_9_4, 0.4_2_9_7, 0.4_3_3_1, 0.4_4_5_6]       )

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