




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






	@property
	def  lowercase      (	self						)      ->   Any:




						"""simple docstring"""






						__magic_name__					:							Tuple		   =							1
						__magic_name__					:							Tuple		   =							3
						__magic_name__					:							Any		   =							(32, 32)

						__magic_name__					:							List[str]		   =							floats_tensor((batch_size, num_channels) + sizes				,     rng=random.Random(0						)						).to(snake_case__						)
						return image






	@property
	def  lowercase      (	self						)      ->   Optional[int]:




						"""simple docstring"""






						torch.manual_seed(0						)
						__magic_name__					:							Dict		   =							UNetaDModel(
						    block_out_channels=(32, 64)				,     layers_per_block=2				,     sample_size=32				,     in_channels=6				,     out_channels=3				,     down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')				,     up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')				,     )
						return model






	@property
	def  lowercase      (	self						)      ->   Dict:




						"""simple docstring"""






						torch.manual_seed(0						)
						__magic_name__					:							str		   =							VQModel(
						    block_out_channels=[32, 64]				,     in_channels=3				,     out_channels=3				,     down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']				,     up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']				,     latent_channels=3				,     )
						return model






	def  lowercase      (	self						)      ->   Any:




						"""simple docstring"""






						__magic_name__					:							List[str]		   =							"cpu"
						__magic_name__					:							List[Any]		   =							self.dummy_uncond_unet
						__magic_name__					:							int		   =							DDIMScheduler()
						__magic_name__					:							Dict		   =							self.dummy_vq_model

						__magic_name__					:							List[str]		   =							LDMSuperResolutionPipeline(unet=snake_case__				,     vqvae=snake_case__				,     scheduler=snake_case__						)
						ldm.to(snake_case__						)
						ldm.set_progress_bar_config(disable=snake_case__						)

						__magic_name__					:							Optional[int]		   =							self.dummy_image.to(snake_case__						)

						__magic_name__					:							List[str]		   =							torch.Generator(device=snake_case__						).manual_seed(0						)
						__magic_name__					:							Dict		   =							ldm(image=snake_case__				,     generator=snake_case__				,     num_inference_steps=2				,     output_type='''numpy'''						).images

						__magic_name__					:							Union[str, Any]		   =							image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						__magic_name__					:							Tuple		   =							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  lowercase      (	self						)      ->   List[Any]:




						"""simple docstring"""






						__magic_name__					:							str		   =							self.dummy_uncond_unet
						__magic_name__					:							str		   =							DDIMScheduler()
						__magic_name__					:							int		   =							self.dummy_vq_model

						# put models in fp16
						__magic_name__					:							List[Any]		   =							unet.half()
						__magic_name__					:							int		   =							vqvae.half()

						__magic_name__					:							List[Any]		   =							LDMSuperResolutionPipeline(unet=snake_case__				,     vqvae=snake_case__				,     scheduler=snake_case__						)
						ldm.to(snake_case__						)
						ldm.set_progress_bar_config(disable=snake_case__						)

						__magic_name__					:							Any		   =							self.dummy_image.to(snake_case__						)

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

						assert image.shape == (1, 64, 64, 3)





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






	def  lowercase      (	self						)      ->   List[Any]:




						"""simple docstring"""






						__magic_name__					:							Union[str, Any]		   =							load_image(
						    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
						    '''/vq_diffusion/teddy_bear_pool.png'''						)
						__magic_name__					:							List[Any]		   =							init_image.resize((64, 64)				,     resample=PIL_INTERPOLATION['''lanczos''']						)

						__magic_name__					:							List[str]		   =							LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''				,     device_map='''auto'''						)
						ldm.set_progress_bar_config(disable=snake_case__						)

						__magic_name__					:							Dict		   =							torch.manual_seed(0						)
						__magic_name__					:							int		   =							ldm(image=snake_case__				,     generator=snake_case__				,     num_inference_steps=20				,     output_type='''numpy'''						).images

						__magic_name__					:							int		   =							image[0, -3:, -3:, -1]

						assert image.shape == (1, 256, 256, 3)
						__magic_name__					:							Optional[Any]		   =							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"""







import importlib.metadata
import subprocess
import sys





def 						lowercase__(A = False						)				->List[str]:




						"""simple docstring"""



						lowercase__				:      Any=       False
						if "IPython" in sys.modules:
												lowercase__				:      Tuple=       "google.colab" in str(sys.modules["IPython"].get_ipython()						)

						if in_colab:
												if upgrade:
																		lowercase__				:      Optional[int]=       ["pip", "install", "-U", "torch"]
																		subprocess.run(A					,		check=A						)
												# get the current version of torch
												lowercase__				:      Tuple=       importlib.metadata.version("torch"						)
												lowercase__				:      Dict=       torch_version[: torch_version.rindex("."						)]
												lowercase__				:      Dict=       f'''https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl'''
												lowercase__				:      Union[str, Any]=       ["pip", "install", xla_wheel]
												subprocess.run(A					,		check=A						)
						else:
												raise RuntimeError("`install_xla` utility works only on google colab."						)






