






'''simple docstring'''







import gc
import random
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()





class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






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






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Optional[Any]:
							a_		:  Optional[int]											=      1
							a_		:  List[str]											=      3
							a_		:  List[Any]											=      (32, 32)

							a_		:  int											=      floats_tensor((batch_size, num_channels) + sizes  ,						rng=random.Random(0					)					).to(__SCREAMING_SNAKE_CASE					)
							return image






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			List[str]:
							torch.manual_seed(0					)
							a_		:  Optional[int]											=      UNetaDConditionModel(
							    block_out_channels=(32, 32, 64)  ,						layers_per_block=2  ,						sample_size=32  ,						in_channels=7  ,						out_channels=4  ,						down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''', '''CrossAttnDownBlock2D''')  ,						up_block_types=('''CrossAttnUpBlock2D''', '''CrossAttnUpBlock2D''', '''UpBlock2D''')  ,						cross_attention_dim=32  ,						attention_head_dim=8  ,						use_linear_projection=__SCREAMING_SNAKE_CASE  ,						only_cross_attention=(True, True, False)  ,						num_class_embeds=100  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			str:
							torch.manual_seed(0					)
							a_		:  Dict											=      AutoencoderKL(
							    block_out_channels=[32, 32, 64]  ,						in_channels=3  ,						out_channels=3  ,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''']  ,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,						latent_channels=4  ,						)
							return model






			@property
			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			List[str]:
							torch.manual_seed(0					)
							a_		:  str											=      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  ,						hidden_act='''gelu'''  ,						projection_dim=512  ,						)
							return CLIPTextModel(__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Union[str, Any]:
							a_		:  str											=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							a_		:  Union[str, Any]											=      self.dummy_cond_unet_upscale
							a_		:  Any											=      DDPMScheduler()
							a_		:  int											=      DDIMScheduler(prediction_type='''v_prediction'''					)
							a_		:  int											=      self.dummy_vae
							a_		:  List[Any]											=      self.dummy_text_encoder
							a_		:  Dict											=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

							a_		:  Union[str, Any]											=      self.dummy_image.cpu().permute(0  ,						2  ,						3  ,						1					)[0]
							a_		:  Dict											=      Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE					)					).convert('''RGB'''					).resize((64, 64)					)

							# make sure here that pndm scheduler skips prk
							a_		:  Dict											=      StableDiffusionUpscalePipeline(
							    unet=__SCREAMING_SNAKE_CASE  ,						low_res_scheduler=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE  ,						vae=__SCREAMING_SNAKE_CASE  ,						text_encoder=__SCREAMING_SNAKE_CASE  ,						tokenizer=__SCREAMING_SNAKE_CASE  ,						max_noise_level=350  ,						)
							a_		:  List[str]											=      sd_pipe.to(__SCREAMING_SNAKE_CASE					)
							sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[int]											=      '''A painting of a squirrel eating a burger'''
							a_		:  Union[str, Any]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(0					)
							a_		:  List[Any]											=      sd_pipe(
							    [prompt]  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						guidance_scale=6.0  ,						noise_level=20  ,						num_inference_steps=2  ,						output_type='''np'''  ,						)

							a_		:  Tuple											=      output.images

							a_		:  Union[str, Any]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(0					)
							a_		:  List[str]											=      sd_pipe(
							    [prompt]  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						guidance_scale=6.0  ,						noise_level=20  ,						num_inference_steps=2  ,						output_type='''np'''  ,						return_dict=__SCREAMING_SNAKE_CASE  ,						)[0]

							a_		:  List[str]											=      image[0, -3:, -3:, -1]
							a_		:  Optional[int]											=      image_from_tuple[0, -3:, -3:, -1]

							a_		:  int											=      low_res_image.size[0] * 4
							assert image.shape == (1, expected_height_width, expected_height_width, 3)
							a_		:  Optional[int]											=      np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661]					)

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			str:
							a_		:  Dict											=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
							a_		:  int											=      self.dummy_cond_unet_upscale
							a_		:  Any											=      DDPMScheduler()
							a_		:  Optional[int]											=      DDIMScheduler(prediction_type='''v_prediction'''					)
							a_		:  Optional[int]											=      self.dummy_vae
							a_		:  int											=      self.dummy_text_encoder
							a_		:  Union[str, Any]											=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

							a_		:  Optional[int]											=      self.dummy_image.cpu().permute(0  ,						2  ,						3  ,						1					)[0]
							a_		:  Tuple											=      Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE					)					).convert('''RGB'''					).resize((64, 64)					)

							# make sure here that pndm scheduler skips prk
							a_		:  Tuple											=      StableDiffusionUpscalePipeline(
							    unet=__SCREAMING_SNAKE_CASE  ,						low_res_scheduler=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE  ,						vae=__SCREAMING_SNAKE_CASE  ,						text_encoder=__SCREAMING_SNAKE_CASE  ,						tokenizer=__SCREAMING_SNAKE_CASE  ,						max_noise_level=350  ,						)
							a_		:  List[Any]											=      sd_pipe.to(__SCREAMING_SNAKE_CASE					)
							sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[int]											=      '''A painting of a squirrel eating a burger'''
							a_		:  List[Any]											=      sd_pipe(
							    2 * [prompt]  ,						image=2 * [low_res_image]  ,						guidance_scale=6.0  ,						noise_level=20  ,						num_inference_steps=2  ,						output_type='''np'''  ,						)
							a_		:  Optional[Any]											=      output.images
							assert image.shape[0] == 2

							a_		:  Any											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(0					)
							a_		:  Optional[int]											=      sd_pipe(
							    [prompt]  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						num_images_per_prompt=2  ,						guidance_scale=6.0  ,						noise_level=20  ,						num_inference_steps=2  ,						output_type='''np'''  ,						)
							a_		:  int											=      output.images
							assert image.shape[0] == 2







			@unittest.skipIf(torch_device != '''cuda'''  ,						'''This test requires a GPU'''					)
			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Any:
							a_		:  Union[str, Any]											=      self.dummy_cond_unet_upscale
							a_		:  List[Any]											=      DDPMScheduler()
							a_		:  Tuple											=      DDIMScheduler(prediction_type='''v_prediction'''					)
							a_		:  List[Any]											=      self.dummy_vae
							a_		:  Optional[int]											=      self.dummy_text_encoder
							a_		:  Tuple											=      CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

							a_		:  Tuple											=      self.dummy_image.cpu().permute(0  ,						2  ,						3  ,						1					)[0]
							a_		:  List[str]											=      Image.fromarray(np.uinta(__SCREAMING_SNAKE_CASE					)					).convert('''RGB'''					).resize((64, 64)					)

							# put models in fp16, except vae as it overflows in fp16
							a_		:  Optional[Any]											=      unet.half()
							a_		:  Optional[Any]											=      text_encoder.half()

							# make sure here that pndm scheduler skips prk
							a_		:  List[Any]											=      StableDiffusionUpscalePipeline(
							    unet=__SCREAMING_SNAKE_CASE  ,						low_res_scheduler=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE  ,						vae=__SCREAMING_SNAKE_CASE  ,						text_encoder=__SCREAMING_SNAKE_CASE  ,						tokenizer=__SCREAMING_SNAKE_CASE  ,						max_noise_level=350  ,						)
							a_		:  List[str]											=      sd_pipe.to(__SCREAMING_SNAKE_CASE					)
							sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[int]											=      '''A painting of a squirrel eating a burger'''
							a_		:  Dict											=      torch.manual_seed(0					)
							a_		:  List[str]											=      sd_pipe(
							    [prompt]  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						num_inference_steps=2  ,						output_type='''np'''  ,						).images

							a_		:  Union[str, Any]											=      low_res_image.size[0] * 4
							assert image.shape == (1, expected_height_width, expected_height_width, 3)





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






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Optional[Any]:
							a_		:  int											=      load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
							    '''/sd2-upscale/low_res_cat.png'''					)
							a_		:  Tuple											=      load_numpy(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
							    '''/upsampled_cat.npy'''					)

							a_		:  int											=      '''stabilityai/stable-diffusion-x4-upscaler'''
							a_		:  Tuple											=      StableDiffusionUpscalePipeline.from_pretrained(__SCREAMING_SNAKE_CASE					)
							pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)
							pipe.enable_attention_slicing()

							a_		:  Any											=      '''a cat sitting on a park bench'''

							a_		:  Optional[int]											=      torch.manual_seed(0					)
							a_		:  Tuple											=      pipe(
							    prompt=__SCREAMING_SNAKE_CASE  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						output_type='''np'''  ,						)
							a_		:  Optional[Any]											=      output.images[0]

							assert image.shape == (512, 512, 3)
							assert np.abs(expected_image - image					).max() < 1e-3






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[int]:
							a_		:  str											=      load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
							    '''/sd2-upscale/low_res_cat.png'''					)
							a_		:  Any											=      load_numpy(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
							    '''/upsampled_cat_fp16.npy'''					)

							a_		:  int											=      '''stabilityai/stable-diffusion-x4-upscaler'''
							a_		:  List[Any]											=      StableDiffusionUpscalePipeline.from_pretrained(
							    __SCREAMING_SNAKE_CASE  ,						torch_dtype=torch.floataa  ,						)
							pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)
							pipe.enable_attention_slicing()

							a_		:  Optional[int]											=      '''a cat sitting on a park bench'''

							a_		:  str											=      torch.manual_seed(0					)
							a_		:  List[str]											=      pipe(
							    prompt=__SCREAMING_SNAKE_CASE  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						output_type='''np'''  ,						)
							a_		:  int											=      output.images[0]

							assert image.shape == (512, 512, 3)
							assert np.abs(expected_image - image					).max() < 5e-1







			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			List[str]:
							torch.cuda.empty_cache()
							torch.cuda.reset_max_memory_allocated()
							torch.cuda.reset_peak_memory_stats()

							a_		:  Dict											=      load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
							    '''/sd2-upscale/low_res_cat.png'''					)

							a_		:  int											=      '''stabilityai/stable-diffusion-x4-upscaler'''
							a_		:  Any											=      StableDiffusionUpscalePipeline.from_pretrained(
							    __SCREAMING_SNAKE_CASE  ,						torch_dtype=torch.floataa  ,						)
							pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)
							pipe.enable_attention_slicing(1					)
							pipe.enable_sequential_cpu_offload()

							a_		:  Tuple											=      '''a cat sitting on a park bench'''

							a_		:  List[str]											=      torch.manual_seed(0					)
							a_		:  List[Any]											=      pipe(
							    prompt=__SCREAMING_SNAKE_CASE  ,						image=__SCREAMING_SNAKE_CASE  ,						generator=__SCREAMING_SNAKE_CASE  ,						num_inference_steps=5  ,						output_type='''np'''  ,						)

							a_		:  Dict											=      torch.cuda.max_memory_allocated()
							# make sure that less than 2.9 GB is allocated
							assert mem_bytes < 2.9 * 10**9












'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			snake_case__  =				DDIMPipeline
			snake_case__  =				UNCONDITIONAL_IMAGE_GENERATION_PARAMS
			snake_case__  =				PipelineTesterMixin.required_optional_params - {
			    "num_images_per_prompt",
			    "latents",
			    "callback",
			    "callback_steps",
			}
			snake_case__  =				UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
			snake_case__  =				False






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[Any]:
							torch.manual_seed(0					)
							a_		:  int											=      UNetaDModel(
							    block_out_channels=(32, 64)  ,						layers_per_block=2  ,						sample_size=32  ,						in_channels=3  ,						out_channels=3  ,						down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')  ,						up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')  ,						)
							a_		:  str											=      DDIMScheduler()
							a_		:  Union[str, Any]											=      {'''unet''': unet, '''scheduler''': scheduler}
							return components






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Tuple=0					)			->			str:
							if str(__SCREAMING_SNAKE_CASE					).startswith('''mps'''					):
											a_		:  Dict											=      torch.manual_seed(__SCREAMING_SNAKE_CASE					)
							else:
											a_		:  Union[str, Any]											=      torch.Generator(device=__SCREAMING_SNAKE_CASE					).manual_seed(__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      {
							    '''batch_size''': 1,
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''output_type''': '''numpy''',
							}
							return inputs






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union[str, Any]:
							a_		:  Dict											=      '''cpu'''

							a_		:  List[Any]											=      self.get_dummy_components()
							a_		:  List[str]											=      self.pipeline_class(**__SCREAMING_SNAKE_CASE					)
							pipe.to(__SCREAMING_SNAKE_CASE					)
							pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

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

							self.assertEqual(image.shape  ,						(1, 32, 32, 3)					)
							a_		:  int											=      np.array(
							    [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]					)
							a_		:  Union[str, Any]											=      np.abs(image_slice.flatten() - expected_slice					).max()
							self.assertLessEqual(__SCREAMING_SNAKE_CASE  ,						1e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			str:
							super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[int]:
							super().test_save_load_local(expected_max_difference=3e-3					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			List[str]:
							super().test_save_load_optional_components(expected_max_difference=3e-3					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[int]:
							super().test_inference_batch_single_identical(expected_max_diff=3e-3					)





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






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Any:
							a_		:  Optional[Any]											=      '''google/ddpm-cifar10-32'''

							a_		:  Optional[Any]											=      UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
							a_		:  Dict											=      DDIMScheduler()

							a_		:  List[str]											=      DDIMPipeline(unet=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							ddim.to(__SCREAMING_SNAKE_CASE					)
							ddim.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      torch.manual_seed(0					)
							a_		:  Tuple											=      ddim(generator=__SCREAMING_SNAKE_CASE  ,						eta=0.0  ,						output_type='''numpy'''					).images

							a_		:  List[Any]											=      image[0, -3:, -3:, -1]

							assert image.shape == (1, 32, 32, 3)
							a_		:  List[str]											=      np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]					)

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







			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			str:
							a_		:  int											=      '''google/ddpm-ema-bedroom-256'''

							a_		:  str											=      UNetaDModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
							a_		:  Tuple											=      DDIMScheduler.from_pretrained(__SCREAMING_SNAKE_CASE					)

							a_		:  Any											=      DDIMPipeline(unet=__SCREAMING_SNAKE_CASE  ,						scheduler=__SCREAMING_SNAKE_CASE					)
							ddpm.to(__SCREAMING_SNAKE_CASE					)
							ddpm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE					)

							a_		:  Tuple											=      torch.manual_seed(0					)
							a_		:  List[Any]											=      ddpm(generator=__SCREAMING_SNAKE_CASE  ,						output_type='''numpy'''					).images

							a_		:  Optional[int]											=      image[0, -3:, -3:, -1]

							assert image.shape == (1, 256, 256, 3)
							a_		:  Optional[Any]											=      np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]					)

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




