"""simple docstring"""






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       A    (			self )    ->	int:
							a_							:			Tuple     		=					1
							a_							:			List[str]     		=					3
							a_							:			int     		=					(3_2, 3_2)

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







	@property
	def       A    (			self )    ->	Dict:
							torch.manual_seed(0 )
							a_							:			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 )    ->	List[Any]:
							torch.manual_seed(0 )
							a_							:			Union[str, Any]     		=					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 )    ->	Tuple:
							a_							:			str     		=					"cpu"
							a_							:			Tuple     		=					self.dummy_uncond_unet
							a_							:			Tuple     		=					DDIMScheduler()
							a_							:			Tuple     		=					self.dummy_vq_model

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

							a_							:			Tuple     		=					self.dummy_image.to(lowerCamelCase_ )

							a_							:			Optional[Any]     		=					torch.Generator(device=lowerCamelCase_ ).manual_seed(0 )
							a_							:			Optional[Any]     		=					ldm(image=lowerCamelCase_       ,     generator=lowerCamelCase_       ,     num_inference_steps=2       ,     output_type="numpy" ).images

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

							assert image.shape == (1, 6_4, 6_4, 3)
							a_							:			Union[str, Any]     		=					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 )    ->	Optional[int]:
							a_							:			int     		=					self.dummy_uncond_unet
							a_							:			List[str]     		=					DDIMScheduler()
							a_							:			Optional[Any]     		=					self.dummy_vq_model

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

							a_							:			Optional[int]     		=					LDMSuperResolutionPipeline(unet=lowerCamelCase_       ,     vqvae=lowerCamelCase_       ,     scheduler=lowerCamelCase_ )
							ldm.to(lowerCamelCase_ )
							ldm.set_progress_bar_config(disable=lowerCamelCase_ )

							a_							:			str     		=					self.dummy_image.to(lowerCamelCase_ )

							a_							:			List[str]     		=					ldm(lowerCamelCase_       ,     num_inference_steps=2       ,     output_type="numpy" ).images

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




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


	"""simple docstring"""







	def       A    (			self )    ->	str:
							a_							:			Dict     		=					load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
							    "/vq_diffusion/teddy_bear_pool.png" )
							a_							:			Any     		=					init_image.resize((6_4, 6_4)       ,     resample=PIL_INTERPOLATION["lanczos"] )

							a_							:			Dict     		=					LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution"       ,     device_map="auto" )
							ldm.set_progress_bar_config(disable=lowerCamelCase_ )

							a_							:			Union[str, Any]     		=					torch.manual_seed(0 )
							a_							:			Optional[Any]     		=					ldm(image=lowerCamelCase_       ,     generator=lowerCamelCase_       ,     num_inference_steps=2_0       ,     output_type="numpy" ).images

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

							assert image.shape == (1, 2_5_6, 2_5_6, 3)
							a_							:			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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCamelCase     					=     {'configuration_levit': ['LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LevitConfig', 'LevitOnnxConfig']}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     ['LevitFeatureExtractor']
				UpperCamelCase     					=     ['LevitImageProcessor']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     [
				    'LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'LevitForImageClassification',
				    'LevitForImageClassificationWithTeacher',
				    'LevitModel',
				    'LevitPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_levit import LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, LevitConfig, LevitOnnxConfig

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_levit import LevitFeatureExtractor
								from .image_processing_levit import LevitImageProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_levit import (
								    LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    LevitForImageClassification,
								    LevitForImageClassificationWithTeacher,
								    LevitModel,
								    LevitPreTrainedModel,
								)
else:
				import sys

				UpperCamelCase     					=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




