
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()


class 						__magic_name__      (  unittest.TestCase			):





		@property
		def 					_A							(							self			:		Optional[Any] ):
							lowerCAmelCase						:    int     		=   1
							lowerCAmelCase						:    List[str]     		=   3
							lowerCAmelCase						:    List[Any]     		=   (3_2, 3_2)

							lowerCAmelCase						:    Tuple     		=   floats_tensor((batch_size, num_channels) + sizes  ,     rng=random.Random(0 ) ).to(_snake_case )
							return image





		@property
		def 					_A							(							self			:		List[str] ):
							torch.manual_seed(0 )
							lowerCAmelCase						:    Optional[int]     		=   UNetaDModel(
							    block_out_channels=(3_2, 6_4)  ,     layers_per_block=2  ,     sample_size=3_2  ,     in_channels=6  ,     out_channels=3  ,     down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')  ,     up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')  ,     )
							return model





		@property
		def 					_A							(							self			:		int ):
							torch.manual_seed(0 )
							lowerCAmelCase						:    Dict     		=   VQModel(
							    block_out_channels=[3_2, 6_4]  ,     in_channels=3  ,     out_channels=3  ,     down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']  ,     up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']  ,     latent_channels=3  ,     )
							return model





		def 					_A							(							self			:		Dict ):
							lowerCAmelCase						:    int     		=   "cpu"
							lowerCAmelCase						:    Optional[int]     		=   self.dummy_uncond_unet
							lowerCAmelCase						:    Any     		=   DDIMScheduler()
							lowerCAmelCase						:    List[Any]     		=   self.dummy_vq_model

							lowerCAmelCase						:    str     		=   LDMSuperResolutionPipeline(unet=_snake_case  ,     vqvae=_snake_case  ,     scheduler=_snake_case )
							ldm.to(_snake_case )
							ldm.set_progress_bar_config(disable=_snake_case )

							lowerCAmelCase						:    Dict     		=   self.dummy_image.to(_snake_case )

							lowerCAmelCase						:    List[str]     		=   torch.Generator(device=_snake_case ).manual_seed(0 )
							lowerCAmelCase						:    Optional[Any]     		=   ldm(image=_snake_case  ,     generator=_snake_case  ,     num_inference_steps=2  ,     output_type='''numpy''' ).images

							lowerCAmelCase						:    Dict     		=   image[0, -3:, -3:, -1]

							assert image.shape == (1, 6_4, 6_4, 3)
							lowerCAmelCase						:    int     		=   np.array([0.8_6_7_8, 0.8_2_4_5, 0.6_3_8_1, 0.6_8_3_0, 0.4_3_8_5, 0.5_5_9_9, 0.4_6_4_1, 0.6_2_0_1, 0.5_1_5_0] )

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





		@unittest.skipIf(torch_device != '''cuda'''  ,     '''This test requires a GPU''' )
		def 					_A							(							self			:		Any ):
							lowerCAmelCase						:    Optional[Any]     		=   self.dummy_uncond_unet
							lowerCAmelCase						:    List[str]     		=   DDIMScheduler()
							lowerCAmelCase						:    Union[str, Any]     		=   self.dummy_vq_model

							# put models in fp16
							lowerCAmelCase						:    Any     		=   unet.half()
							lowerCAmelCase						:    int     		=   vqvae.half()

							lowerCAmelCase						:    Dict     		=   LDMSuperResolutionPipeline(unet=_snake_case  ,     vqvae=_snake_case  ,     scheduler=_snake_case )
							ldm.to(_snake_case )
							ldm.set_progress_bar_config(disable=_snake_case )

							lowerCAmelCase						:    Optional[int]     		=   self.dummy_image.to(_snake_case )

							lowerCAmelCase						:    Optional[Any]     		=   ldm(_snake_case  ,     num_inference_steps=2  ,     output_type='''numpy''' ).images

							assert image.shape == (1, 6_4, 6_4, 3)



@slow
@require_torch
class 						__magic_name__      (  unittest.TestCase			):





		def 					_A							(							self			:		Optional[int] ):
							lowerCAmelCase						:    str     		=   load_image(
							    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
							    '''/vq_diffusion/teddy_bear_pool.png''' )
							lowerCAmelCase						:    Dict     		=   init_image.resize((6_4, 6_4)  ,     resample=PIL_INTERPOLATION['''lanczos'''] )

							lowerCAmelCase						:    Optional[int]     		=   LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''  ,     device_map='''auto''' )
							ldm.set_progress_bar_config(disable=_snake_case )

							lowerCAmelCase						:    Any     		=   torch.manual_seed(0 )
							lowerCAmelCase						:    str     		=   ldm(image=_snake_case  ,     generator=_snake_case  ,     num_inference_steps=2_0  ,     output_type='''numpy''' ).images

							lowerCAmelCase						:    str     		=   image[0, -3:, -3:, -1]

							assert image.shape == (1, 2_5_6, 2_5_6, 3)
							lowerCAmelCase						:    List[str]     		=   np.array([0.7_6_4_4, 0.7_6_7_9, 0.7_6_4_2, 0.7_6_3_3, 0.7_6_6_6, 0.7_5_6_0, 0.7_4_2_5, 0.7_2_5_7, 0.6_9_0_7] )

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



'''simple docstring'''
from transformers import PretrainedConfig
class  _snake_case						(__SCREAMING_SNAKE_CASE):
			__A   :     Optional[Any]          ="custom"






			def __init__(   self       ,_snake_case=1       ,**_snake_case			):
				UpperCAmelCase_			:	Optional[int]      =   attribute
				super().__init__(**_snake_case			)




class  _snake_case						(__SCREAMING_SNAKE_CASE):
			__A   :     Dict          ="custom"






			def __init__(   self       ,_snake_case=1       ,**_snake_case			):
				UpperCAmelCase_			:	Optional[int]      =   attribute




