



"""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     lowerCAmelCase__						(	unittest.TestCase ):


	'''simple docstring'''







	def 			_lowerCAmelCase     (      self				:       str    )						->	List[str]:





					"""simple docstring"""


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





	@property
	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	List[Any]:





					"""simple docstring"""


					torch.manual_seed(0    )
					SCREAMING_SNAKE_CASE						:							str							=     UNetaDModel(
					    sample_size=(32, 64)     ,       in_channels=1     ,       out_channels=1     ,       layers_per_block=2     ,       block_out_channels=(128, 128)     ,       down_block_types=('AttnDownBlock2D', 'DownBlock2D')     ,       up_block_types=('UpBlock2D', 'AttnUpBlock2D')     ,       )
					return model





	@property
	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	Tuple:





					"""simple docstring"""


					torch.manual_seed(0    )
					SCREAMING_SNAKE_CASE						:							int							=     UNetaDConditionModel(
					    sample_size=(64, 32)     ,       in_channels=1     ,       out_channels=1     ,       layers_per_block=2     ,       block_out_channels=(128, 128)     ,       down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')     ,       up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')     ,       cross_attention_dim=10     ,       )
					return model





	@property
	def 			_lowerCAmelCase     (      self				:       List[str]    )						->	int:





					"""simple docstring"""


					torch.manual_seed(0    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     AutoencoderKL(
					    sample_size=(128, 64)     ,       in_channels=1     ,       out_channels=1     ,       latent_channels=1     ,       layers_per_block=2     ,       block_out_channels=(128, 128)     ,       down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')     ,       up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')     ,       )
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     UNetaDModel(
					    sample_size=(64, 32)     ,       in_channels=1     ,       out_channels=1     ,       layers_per_block=2     ,       block_out_channels=(128, 128)     ,       down_block_types=('AttnDownBlock2D', 'DownBlock2D')     ,       up_block_types=('UpBlock2D', 'AttnUpBlock2D')     ,       )
					return vqvae, unet







	@slow
	def 			_lowerCAmelCase     (      self				:       List[Any]    )						->	Dict:





					"""simple docstring"""


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

					SCREAMING_SNAKE_CASE						:							int							=     DDPMScheduler()
					SCREAMING_SNAKE_CASE						:							Any							=     AudioDiffusionPipeline(vqvae=_SCREAMING_SNAKE_CASE     ,       unet=self.dummy_unet     ,       mel=_SCREAMING_SNAKE_CASE     ,       scheduler=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							str							=     pipe.to(_SCREAMING_SNAKE_CASE    )
					pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Dict							=     torch.Generator(device=_SCREAMING_SNAKE_CASE    ).manual_seed(42    )
					SCREAMING_SNAKE_CASE						:							List[Any]							=     pipe(generator=_SCREAMING_SNAKE_CASE     ,       steps=4    )
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     output.audios[0]
					SCREAMING_SNAKE_CASE						:							List[str]							=     output.images[0]

					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     torch.Generator(device=_SCREAMING_SNAKE_CASE    ).manual_seed(42    )
					SCREAMING_SNAKE_CASE						:							Any							=     pipe(generator=_SCREAMING_SNAKE_CASE     ,       steps=4     ,       return_dict=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							int							=     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]
					)
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     np.frombuffer(image.tobytes()     ,       dtype='uint8'    )[:10]
					SCREAMING_SNAKE_CASE						:							int							=     np.frombuffer(image_from_tuple.tobytes()     ,       dtype='uint8'    )[:10]
					SCREAMING_SNAKE_CASE						:							List[str]							=     np.array([69, 255, 255, 255, 0, 0, 77, 181, 12, 127]    )

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

					SCREAMING_SNAKE_CASE						:							Optional[int]							=     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]     ,       )

					SCREAMING_SNAKE_CASE						:							Any							=     DDIMScheduler()
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.dummy_vqvae_and_unet
					SCREAMING_SNAKE_CASE						:							Dict							=     AudioDiffusionPipeline(
					    vqvae=self.dummy_vqvae_and_unet[0]     ,       unet=dummy_vqvae_and_unet[1]     ,       mel=_SCREAMING_SNAKE_CASE     ,       scheduler=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							str							=     pipe.to(_SCREAMING_SNAKE_CASE    )
					pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE    )

					np.random.seed(0    )
					SCREAMING_SNAKE_CASE						:							Dict							=     np.random.uniform(-1     ,       1     ,       ((dummy_vqvae_and_unet[0].config.sample_size[1] - 1) * mel.hop_length,)    )
					SCREAMING_SNAKE_CASE						:							str							=     torch.Generator(device=_SCREAMING_SNAKE_CASE    ).manual_seed(42    )
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     pipe(raw_audio=_SCREAMING_SNAKE_CASE     ,       generator=_SCREAMING_SNAKE_CASE     ,       start_step=5     ,       steps=10    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     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]
					)
					SCREAMING_SNAKE_CASE						:							str							=     np.frombuffer(image.tobytes()     ,       dtype='uint8'    )[:10]
					SCREAMING_SNAKE_CASE						:							Dict							=     np.array([120, 117, 110, 109, 138, 167, 138, 148, 132, 121]    )

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

					SCREAMING_SNAKE_CASE						:							Tuple							=     self.dummy_unet_condition
					SCREAMING_SNAKE_CASE						:							Tuple							=     AudioDiffusionPipeline(
					    vqvae=self.dummy_vqvae_and_unet[0]     ,       unet=_SCREAMING_SNAKE_CASE     ,       mel=_SCREAMING_SNAKE_CASE     ,       scheduler=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							str							=     pipe.to(_SCREAMING_SNAKE_CASE    )
					pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE    )

					np.random.seed(0    )
					SCREAMING_SNAKE_CASE						:							Any							=     torch.rand((1, 1, 10)    )
					SCREAMING_SNAKE_CASE						:							int							=     pipe(generator=_SCREAMING_SNAKE_CASE     ,       encoding=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Dict							=     output.images[0]
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     np.frombuffer(image.tobytes()     ,       dtype='uint8'    )[:10]
					SCREAMING_SNAKE_CASE						:							List[Any]							=     np.array([107, 103, 120, 127, 142, 122, 113, 122, 97, 111]    )

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




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


	'''simple docstring'''







	def 			_lowerCAmelCase     (      self				:       Dict    )						->	List[Any]:





					"""simple docstring"""


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







	def 			_lowerCAmelCase     (      self				:       int    )						->	Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							int							=     torch_device

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

					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     torch.Generator(device=_SCREAMING_SNAKE_CASE    ).manual_seed(42    )
					SCREAMING_SNAKE_CASE						:							Tuple							=     pipe(generator=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     output.audios[0]
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     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]
					SCREAMING_SNAKE_CASE						:							int							=     np.frombuffer(image.tobytes()     ,       dtype='uint8'    )[:10]
					SCREAMING_SNAKE_CASE						:							List[str]							=     np.array([151, 167, 154, 144, 122, 134, 121, 105, 70, 26]    )

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










"""simple docstring"""

# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works

from ...utils import deprecate
from ..controlnet.pipeline_flax_controlnet import FlaxStableDiffusionControlNetPipeline  # noqa: F401


deprecate(
    'stable diffusion controlnet',
    '0.22.0',
    'Importing `FlaxStableDiffusionControlNetPipeline` from diffusers.pipelines.stable_diffusion.flax_pipeline_stable_diffusion_controlnet is deprecated. Please import `from diffusers import FlaxStableDiffusionControlNetPipeline` instead.',
    standard_warn=False,
    stacklevel=3,
)





