




"""simple docstring"""






import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class lowerCAmelCase__      (	lowercase,      unittest.TestCase		):






			'''simple docstring'''



			lowerCamelCase__ =     LDMTextToImagePipeline
			lowerCamelCase__ =     TEXT_TO_IMAGE_PARAMS - {
			    """negative_prompt""",
			    """negative_prompt_embeds""",
			    """cross_attention_kwargs""",
			    """prompt_embeds""",
			}
			lowerCamelCase__ =     PipelineTesterMixin.required_optional_params - {
			    """num_images_per_prompt""",
			    """callback""",
			    """callback_steps""",
			}
			lowerCamelCase__ =     TEXT_TO_IMAGE_BATCH_PARAMS
			lowerCamelCase__ =     False
			def    A_       (    self    ):
							torch.manual_seed(0    )
							_lowerCamelCase	:     Optional[Any]       =       UNetaDConditionModel(
							    block_out_channels=(32, 64)    , layers_per_block=2    , sample_size=32    , in_channels=4    , out_channels=4    , down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')    , up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')    , cross_attention_dim=32    , )
							_lowerCamelCase	:     Optional[Any]       =       DDIMScheduler(
							    beta_start=0.0_00_85    , beta_end=0.0_12    , beta_schedule='scaled_linear'    , clip_sample=lowercase    , set_alpha_to_one=lowercase    , )
							torch.manual_seed(0    )
							_lowerCamelCase	:     Optional[int]       =       AutoencoderKL(
							    block_out_channels=(32, 64)    , in_channels=3    , out_channels=3    , down_block_types=('DownEncoderBlock2D', 'DownEncoderBlock2D')    , up_block_types=('UpDecoderBlock2D', 'UpDecoderBlock2D')    , latent_channels=4    , )
							torch.manual_seed(0    )
							_lowerCamelCase	:     Optional[Any]       =       CLIPTextConfig(
							    bos_token_id=0    , eos_token_id=2    , hidden_size=32    , intermediate_size=37    , layer_norm_eps=1E-05    , num_attention_heads=4    , num_hidden_layers=5    , pad_token_id=1    , vocab_size=1000    , )
							_lowerCamelCase	:     Tuple       =       CLIPTextModel(lowercase    )
							_lowerCamelCase	:     str       =       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'    )

							_lowerCamelCase	:     Dict       =       {
							    'unet': unet,
							    'scheduler': scheduler,
							    'vqvae': vae,
							    'bert': text_encoder,
							    'tokenizer': tokenizer,
							}
							return components
			def    A_       (    self    , lowercase    , lowercase=0    ):
							if str(lowercase    ).startswith('mps'    ):
											_lowerCamelCase	:     Dict       =       torch.manual_seed(lowercase    )
							else:
											_lowerCamelCase	:     Tuple       =       torch.Generator(device=lowercase    ).manual_seed(lowercase    )
							_lowerCamelCase	:     Dict       =       {
							    'prompt': 'A painting of a squirrel eating a burger',
							    'generator': generator,
							    'num_inference_steps': 2,
							    'guidance_scale': 6.0,
							    'output_type': 'numpy',
							}
							return inputs






			def    A_       (    self    ):
							_lowerCamelCase	:     Optional[Any]       =       'cpu'  # ensure determinism for the device-dependent torch.Generator

							_lowerCamelCase	:     Union[str, Any]       =       self.get_dummy_components()
							_lowerCamelCase	:     Optional[Any]       =       LDMTextToImagePipeline(**lowercase    )
							pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							_lowerCamelCase	:     str       =       self.get_dummy_inputs(lowercase    )
							_lowerCamelCase	:     Optional[Any]       =       pipe(**lowercase    ).images
							_lowerCamelCase	:     List[str]       =       image[0, -3:, -3:, -1]

							assert image.shape == (1, 16, 16, 3)
							_lowerCamelCase	:     Optional[int]       =       np.array([0.61_01, 0.61_56, 0.56_22, 0.48_95, 0.66_61, 0.38_04, 0.57_48, 0.61_36, 0.50_14]    )

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






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






			'''simple docstring'''



			def    A_       (    self    ):
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
			def    A_       (    self    , lowercase    , lowercase=torch.floataa    , lowercase=0    ):
							_lowerCamelCase	:     Optional[int]       =       torch.manual_seed(lowercase    )
							_lowerCamelCase	:     Dict       =       np.random.RandomState(lowercase    ).standard_normal((1, 4, 32, 32)    )
							_lowerCamelCase	:     Any       =       torch.from_numpy(lowercase    ).to(device=lowercase    , dtype=lowercase    )
							_lowerCamelCase	:     Tuple       =       {
							    'prompt': 'A painting of a squirrel eating a burger',
							    'latents': latents,
							    'generator': generator,
							    'num_inference_steps': 3,
							    'guidance_scale': 6.0,
							    'output_type': 'numpy',
							}
							return inputs






			def    A_       (    self    ):
							_lowerCamelCase	:     Any       =       LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'    ).to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							_lowerCamelCase	:     Any       =       self.get_inputs(lowercase    )
							_lowerCamelCase	:     Optional[int]       =       pipe(**lowercase    ).images
							_lowerCamelCase	:     Optional[int]       =       image[0, -3:, -3:, -1].flatten()

							assert image.shape == (1, 256, 256, 3)
							_lowerCamelCase	:     Optional[int]       =       np.array([0.5_18_25, 0.5_28_50, 0.5_25_43, 0.5_42_58, 0.5_23_04, 0.5_25_69, 0.5_43_63, 0.5_52_76, 0.5_68_78]    )
							_lowerCamelCase	:     List[Any]       =       np.abs(expected_slice - image_slice    ).max()
							assert max_diff < 1E-3






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






			'''simple docstring'''



			def    A_       (    self    ):
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
			def    A_       (    self    , lowercase    , lowercase=torch.floataa    , lowercase=0    ):
							_lowerCamelCase	:     Dict       =       torch.manual_seed(lowercase    )
							_lowerCamelCase	:     str       =       np.random.RandomState(lowercase    ).standard_normal((1, 4, 32, 32)    )
							_lowerCamelCase	:     str       =       torch.from_numpy(lowercase    ).to(device=lowercase    , dtype=lowercase    )
							_lowerCamelCase	:     Optional[int]       =       {
							    'prompt': 'A painting of a squirrel eating a burger',
							    'latents': latents,
							    'generator': generator,
							    'num_inference_steps': 50,
							    'guidance_scale': 6.0,
							    'output_type': 'numpy',
							}
							return inputs






			def    A_       (    self    ):
							_lowerCamelCase	:     Union[str, Any]       =       LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'    ).to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							_lowerCamelCase	:     Any       =       self.get_inputs(lowercase    )
							_lowerCamelCase	:     Optional[int]       =       pipe(**lowercase    ).images[0]

							_lowerCamelCase	:     Union[str, Any]       =       load_numpy(
							    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'    )
							_lowerCamelCase	:     Dict       =       np.abs(expected_image - image    ).max()
							assert max_diff < 1E-3





"""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    A_       (    self    ):
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
			@property
			def    A_       (    self    ):
							torch.manual_seed(0    )
							_lowerCamelCase	:     Union[str, Any]       =       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    A_       (    self    ):
							torch.manual_seed(0    )
							_lowerCamelCase	:     Dict       =       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    A_       (    self    ):
							torch.manual_seed(0    )
							_lowerCamelCase	:     List[str]       =       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')    , )
							_lowerCamelCase	:     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    A_       (    self    ):
							_lowerCamelCase	:     str       =       'cpu'  # ensure determinism for the device-dependent torch.Generator
							_lowerCamelCase	:     Optional[int]       =       Mel(
							    x_res=self.dummy_unet.config.sample_size[1]    , y_res=self.dummy_unet.config.sample_size[0]    , )

							_lowerCamelCase	:     List[str]       =       DDPMScheduler()
							_lowerCamelCase	:     List[Any]       =       AudioDiffusionPipeline(vqvae=lowercase    , unet=self.dummy_unet    , mel=lowercase    , scheduler=lowercase    )
							_lowerCamelCase	:     List[Any]       =       pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							_lowerCamelCase	:     Union[str, Any]       =       torch.Generator(device=lowercase    ).manual_seed(42    )
							_lowerCamelCase	:     Union[str, Any]       =       pipe(generator=lowercase    , steps=4    )
							_lowerCamelCase	:     Optional[Any]       =       output.audios[0]
							_lowerCamelCase	:     int       =       output.images[0]

							_lowerCamelCase	:     Dict       =       torch.Generator(device=lowercase    ).manual_seed(42    )
							_lowerCamelCase	:     Dict       =       pipe(generator=lowercase    , steps=4    , return_dict=lowercase    )
							_lowerCamelCase	:     List[str]       =       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]
							)
							_lowerCamelCase	:     Optional[Any]       =       np.frombuffer(image.tobytes()    , dtype='uint8'    )[:10]
							_lowerCamelCase	:     Dict       =       np.frombuffer(image_from_tuple.tobytes()    , dtype='uint8'    )[:10]
							_lowerCamelCase	:     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

							_lowerCamelCase	:     List[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]    , )

							_lowerCamelCase	:     Dict       =       DDIMScheduler()
							_lowerCamelCase	:     Tuple       =       self.dummy_vqvae_and_unet
							_lowerCamelCase	:     List[str]       =       AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]    , unet=dummy_vqvae_and_unet[1]    , mel=lowercase    , scheduler=lowercase    )
							_lowerCamelCase	:     Tuple       =       pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

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

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

							_lowerCamelCase	:     Dict       =       self.dummy_unet_condition
							_lowerCamelCase	:     Optional[int]       =       AudioDiffusionPipeline(
							    vqvae=self.dummy_vqvae_and_unet[0]    , unet=lowercase    , mel=lowercase    , scheduler=lowercase    )
							_lowerCamelCase	:     Dict       =       pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

							np.random.seed(0    )
							_lowerCamelCase	:     Optional[int]       =       torch.rand((1, 1, 10)    )
							_lowerCamelCase	:     Optional[Any]       =       pipe(generator=lowercase    , encoding=lowercase    )
							_lowerCamelCase	:     Dict       =       output.images[0]
							_lowerCamelCase	:     Tuple       =       np.frombuffer(image.tobytes()    , dtype='uint8'    )[:10]
							_lowerCamelCase	:     List[str]       =       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    A_       (    self    ):
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()






			def    A_       (    self    ):
							_lowerCamelCase	:     Optional[Any]       =       torch_device

							_lowerCamelCase	:     Dict       =       DiffusionPipeline.from_pretrained('teticio/audio-diffusion-ddim-256'    )
							_lowerCamelCase	:     List[str]       =       pipe.to(lowercase    )
							pipe.set_progress_bar_config(disable=lowercase    )

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

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