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







	'''simple docstring'''





	@property
	def 	__snake_case					(      self		):
					A__	:						Union[str, Any]								=				1
					A__	:						int								=				3
					A__	:						Tuple								=				(32, 32)

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





	@property
	def 	__snake_case					(      self		):
					torch.manual_seed(0		)
					A__	:						Optional[Any]								=				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 	__snake_case					(      self		):
					torch.manual_seed(0		)
					A__	:						int								=				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 	__snake_case					(      self		):
					A__	:						Dict								=				'''cpu'''
					A__	:						List[Any]								=				self.dummy_uncond_unet
					A__	:						Tuple								=				DDIMScheduler()
					A__	:						int								=				self.dummy_vq_model

					A__	:						Optional[Any]								=				LDMSuperResolutionPipeline(unet=UpperCamelCase__					,							vqvae=UpperCamelCase__					,							scheduler=UpperCamelCase__		)
					ldm.to(UpperCamelCase__		)
					ldm.set_progress_bar_config(disable=UpperCamelCase__		)

					A__	:						int								=				self.dummy_image.to(UpperCamelCase__		)

					A__	:						Optional[int]								=				torch.Generator(device=UpperCamelCase__		).manual_seed(0		)
					A__	:						str								=				ldm(image=UpperCamelCase__					,							generator=UpperCamelCase__					,							num_inference_steps=2					,							output_type='''numpy'''		).images

					A__	:						Optional[Any]								=				image[0, -3:, -3:, -1]

					assert image.shape == (1, 64, 64, 3)
					A__	:						List[str]								=				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 	__snake_case					(      self		):
					A__	:						Dict								=				self.dummy_uncond_unet
					A__	:						Union[str, Any]								=				DDIMScheduler()
					A__	:						Tuple								=				self.dummy_vq_model

					# put models in fp16
					A__	:						int								=				unet.half()
					A__	:						Optional[int]								=				vqvae.half()

					A__	:						Union[str, Any]								=				LDMSuperResolutionPipeline(unet=UpperCamelCase__					,							vqvae=UpperCamelCase__					,							scheduler=UpperCamelCase__		)
					ldm.to(UpperCamelCase__		)
					ldm.set_progress_bar_config(disable=UpperCamelCase__		)

					A__	:						Any								=				self.dummy_image.to(UpperCamelCase__		)

					A__	:						List[str]								=				ldm(UpperCamelCase__					,							num_inference_steps=2					,							output_type='''numpy'''		).images

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







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







	'''simple docstring'''





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

					A__	:						List[Any]								=				LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''					,							device_map='''auto'''		)
					ldm.set_progress_bar_config(disable=UpperCamelCase__		)

					A__	:						Optional[Any]								=				torch.manual_seed(0		)
					A__	:						List[Any]								=				ldm(image=UpperCamelCase__					,							generator=UpperCamelCase__					,							num_inference_steps=20					,							output_type='''numpy'''		).images

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

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

_SCREAMING_SNAKE_CASE     :   dict[str, float]							 =      {
    "kilogram": 1,
    "gram": pow(1_0, 3),
    "milligram": pow(1_0, 6),
    "metric-ton": pow(1_0, -3),
    "long-ton": 0.00_0984_2073,
    "short-ton": 0.00_1102_3122,
    "pound": 2.20_4624_4202,
    "stone": 0.15_7473_1728,
    "ounce": 35.2_7399_0723,
    "carrat": 5_0_0_0,
    "atomic-mass-unit": 6.022136652E26,
}

_SCREAMING_SNAKE_CASE     :   dict[str, float]							 =      {
    "kilogram": 1,
    "gram": pow(1_0, -3),
    "milligram": pow(1_0, -6),
    "metric-ton": pow(1_0, 3),
    "long-ton": 1016.0_4608,
    "short-ton": 907.184,
    "pound": 0.45_3592,
    "stone": 6.3_5029,
    "ounce": 0.028_3495,
    "carrat": 0.0002,
    "atomic-mass-unit": 1.660540199E-27,
}

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    str				,    __UpperCamelCase			:    str				,    __UpperCamelCase			:    float							)			->							float:

				"""simple docstring"""







				if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:
								A__	:						int								=				(
								    F"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
								    F"Supported values are: {', '.join(__UpperCamelCase							)}"
								)
								raise ValueError(__UpperCamelCase							)
				return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type]


if __name__ == "__main__":
					import doctest

					doctest.testmod()