

'''simple docstring'''


import gc
import unittest

import numpy as np
import torch

from diffusers import (
    AudioDiffusionPipeline,
    AutoencoderKL,
    DDIMScheduler,
    DDPMScheduler,
    DiffusionPipeline,
    Mel,
    UNetaDConditionModel,
    UNetaDModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




class     a			(				unittest.TestCase       ):






						"""simple docstring"""




						def       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



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







						@property
						def       __magic_name__							(							self					:      Any  ):



								'''simple docstring'''



								torch.manual_seed(0  )
								snake_case__						:       Optional[Any]       						=  UNetaDModel(
								    sample_size=(3_2, 6_4)			,			in_channels=1			,			out_channels=1			,			layers_per_block=2			,			block_out_channels=(1_2_8, 1_2_8)			,			down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')			,			up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')			,			)
								return model







						@property
						def       __magic_name__							(							self					:      Dict  ):



								'''simple docstring'''



								torch.manual_seed(0  )
								snake_case__						:       Union[str, Any]       						=  UNetaDConditionModel(
								    sample_size=(6_4, 3_2)			,			in_channels=1			,			out_channels=1			,			layers_per_block=2			,			block_out_channels=(1_2_8, 1_2_8)			,			down_block_types=('''CrossAttnDownBlock2D''', '''DownBlock2D''')			,			up_block_types=('''UpBlock2D''', '''CrossAttnUpBlock2D''')			,			cross_attention_dim=1_0			,			)
								return model







						@property
						def       __magic_name__							(							self					:      int  ):



								'''simple docstring'''



								torch.manual_seed(0  )
								snake_case__						:       Union[str, Any]       						=  AutoencoderKL(
								    sample_size=(1_2_8, 6_4)			,			in_channels=1			,			out_channels=1			,			latent_channels=1			,			layers_per_block=2			,			block_out_channels=(1_2_8, 1_2_8)			,			down_block_types=('''DownEncoderBlock2D''', '''DownEncoderBlock2D''')			,			up_block_types=('''UpDecoderBlock2D''', '''UpDecoderBlock2D''')			,			)
								snake_case__						:       Union[str, Any]       						=  UNetaDModel(
								    sample_size=(6_4, 3_2)			,			in_channels=1			,			out_channels=1			,			layers_per_block=2			,			block_out_channels=(1_2_8, 1_2_8)			,			down_block_types=('''AttnDownBlock2D''', '''DownBlock2D''')			,			up_block_types=('''UpBlock2D''', '''AttnUpBlock2D''')			,			)
								return vqvae, unet







						@slow
						def       __magic_name__							(							self					:      List[Any]  ):



								'''simple docstring'''



								snake_case__						:       int       						=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
								snake_case__						:       Optional[int]       						=  Mel(
								    x_res=self.dummy_unet.config.sample_size[1]			,			y_res=self.dummy_unet.config.sample_size[0]			,			)

								snake_case__						:       Tuple       						=  DDPMScheduler()
								snake_case__						:       Tuple       						=  AudioDiffusionPipeline(vqvae=snake_case_			,			unet=self.dummy_unet			,			mel=snake_case_			,			scheduler=snake_case_  )
								snake_case__						:       int       						=  pipe.to(snake_case_  )
								pipe.set_progress_bar_config(disable=snake_case_  )

								snake_case__						:       List[Any]       						=  torch.Generator(device=snake_case_  ).manual_seed(4_2  )
								snake_case__						:       Optional[Any]       						=  pipe(generator=snake_case_			,			steps=4  )
								snake_case__						:       Tuple       						=  output.audios[0]
								snake_case__						:       Dict       						=  output.images[0]

								snake_case__						:       str       						=  torch.Generator(device=snake_case_  ).manual_seed(4_2  )
								snake_case__						:       Optional[int]       						=  pipe(generator=snake_case_			,			steps=4			,			return_dict=snake_case_  )
								snake_case__						:       Union[str, Any]       						=  output[0][0]

								assert audio.shape == (1, (self.dummy_unet.config.sample_size[1] - 1) * mel.hop_length)
								assert (
								    image.height == self.dummy_unet.config.sample_size[0]
								    and image.width == self.dummy_unet.config.sample_size[1]
								)
								snake_case__						:       List[Any]       						=  np.frombuffer(image.tobytes()			,			dtype='''uint8'''  )[:1_0]
								snake_case__						:       Dict       						=  np.frombuffer(image_from_tuple.tobytes()			,			dtype='''uint8'''  )[:1_0]
								snake_case__						:       int       						=  np.array([6_9, 2_5_5, 2_5_5, 2_5_5, 0, 0, 7_7, 1_8_1, 1_2, 1_2_7]  )

								assert np.abs(image_slice.flatten() - expected_slice  ).max() == 0
								assert np.abs(image_from_tuple_slice.flatten() - expected_slice  ).max() == 0

								snake_case__						:       Any       						=  Mel(
								    x_res=self.dummy_vqvae_and_unet[0].config.sample_size[1]			,			y_res=self.dummy_vqvae_and_unet[0].config.sample_size[0]			,			)

								snake_case__						:       Union[str, Any]       						=  DDIMScheduler()
								snake_case__						:       List[Any]       						=  self.dummy_vqvae_and_unet
								snake_case__						:       Union[str, Any]       						=  AudioDiffusionPipeline(
								    vqvae=self.dummy_vqvae_and_unet[0]			,			unet=dummy_vqvae_and_unet[1]			,			mel=snake_case_			,			scheduler=snake_case_  )
								snake_case__						:       Any       						=  pipe.to(snake_case_  )
								pipe.set_progress_bar_config(disable=snake_case_  )

								np.random.seed(0  )
								snake_case__						:       str       						=  np.random.uniform(-1			,			1			,			((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)  )
								snake_case__						:       Tuple       						=  torch.Generator(device=snake_case_  ).manual_seed(4_2  )
								snake_case__						:       Optional[int]       						=  pipe(raw_audio=snake_case_			,			generator=snake_case_			,			start_step=5			,			steps=1_0  )
								snake_case__						:       int       						=  output.images[0]

								assert (
								    image.height == self.dummy_vqvae_and_unet[0].config.sample_size[0]
								    and image.width == self.dummy_vqvae_and_unet[0].config.sample_size[1]
								)
								snake_case__						:       Union[str, Any]       						=  np.frombuffer(image.tobytes()			,			dtype='''uint8'''  )[:1_0]
								snake_case__						:       Optional[Any]       						=  np.array([1_2_0, 1_1_7, 1_1_0, 1_0_9, 1_3_8, 1_6_7, 1_3_8, 1_4_8, 1_3_2, 1_2_1]  )

								assert np.abs(image_slice.flatten() - expected_slice  ).max() == 0

								snake_case__						:       Dict       						=  self.dummy_unet_condition
								snake_case__						:       Dict       						=  AudioDiffusionPipeline(
								    vqvae=self.dummy_vqvae_and_unet[0]			,			unet=snake_case_			,			mel=snake_case_			,			scheduler=snake_case_  )
								snake_case__						:       Any       						=  pipe.to(snake_case_  )
								pipe.set_progress_bar_config(disable=snake_case_  )

								np.random.seed(0  )
								snake_case__						:       Union[str, Any]       						=  torch.rand((1, 1, 1_0)  )
								snake_case__						:       str       						=  pipe(generator=snake_case_			,			encoding=snake_case_  )
								snake_case__						:       Any       						=  output.images[0]
								snake_case__						:       Tuple       						=  np.frombuffer(image.tobytes()			,			dtype='''uint8'''  )[:1_0]
								snake_case__						:       Any       						=  np.array([1_0_7, 1_0_3, 1_2_0, 1_2_7, 1_4_2, 1_2_2, 1_1_3, 1_2_2, 9_7, 1_1_1]  )

								assert np.abs(image_slice.flatten() - expected_slice  ).max() == 0





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






						"""simple docstring"""




						def       __magic_name__							(							self					:      Tuple  ):



								'''simple docstring'''



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







						def       __magic_name__							(							self					:      List[Any]  ):



								'''simple docstring'''



								snake_case__						:       Optional[Any]       						=  torch_device

								snake_case__						:       Optional[int]       						=  DiffusionPipeline.from_pretrained('''teticio/audio-diffusion-ddim-256'''  )
								snake_case__						:       List[Any]       						=  pipe.to(snake_case_  )
								pipe.set_progress_bar_config(disable=snake_case_  )

								snake_case__						:       List[str]       						=  torch.Generator(device=snake_case_  ).manual_seed(4_2  )
								snake_case__						:       int       						=  pipe(generator=snake_case_  )
								snake_case__						:       List[str]       						=  output.audios[0]
								snake_case__						:       Dict       						=  output.images[0]

								assert audio.shape == (1, (pipe.unet.config.sample_size[1] - 1) * pipe.mel.hop_length)
								assert image.height == pipe.unet.config.sample_size[0] and image.width == pipe.unet.config.sample_size[1]
								snake_case__						:       Union[str, Any]       						=  np.frombuffer(image.tobytes()			,			dtype='''uint8'''  )[:1_0]
								snake_case__						:       Tuple       						=  np.array([1_5_1, 1_6_7, 1_5_4, 1_4_4, 1_2_2, 1_3_4, 1_2_1, 1_0_5, 7_0, 2_6]  )

								assert np.abs(image_slice.flatten() - expected_slice  ).max() == 0






'''simple docstring'''


import warnings

from diffusers import StableDiffusionImgaImgPipeline  # noqa F401


warnings.warn(
    """The `image_to_image.py` script is outdated. Please use directly `from diffusers import"""
    """ StableDiffusionImg2ImgPipeline` instead."""
)



