"""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   ):
    @property
    def 		_lowerCamelCase      (	self			:       Dict		)						->    Union[str, Any]:
       _lowercase							     =       1
       _lowercase							     =       3
       _lowercase							     =       (3_2, 3_2)

       _lowercase							     =       floats_tensor((batch_size, num_channels) + sizes   ,						rng=random.Random(0		)		).to(_lowercase		)
       return image
    @property
    def 		_lowerCamelCase      (	self			:       Any		)						->    List[Any]:
       torch.manual_seed(0		)
       _lowercase							     =       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 		_lowerCamelCase      (	self			:       List[str]		)						->    Any:
       torch.manual_seed(0		)
       _lowercase							     =       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 		_lowerCamelCase      (	self			:       List[str]		)						->    str:
       _lowercase							     =       "cpu"
       _lowercase							     =       self.dummy_uncond_unet
       _lowercase							     =       DDIMScheduler()
       _lowercase							     =       self.dummy_vq_model

       _lowercase							     =       LDMSuperResolutionPipeline(unet=_lowercase   ,						vqvae=_lowercase   ,						scheduler=_lowercase		)
       ldm.to(_lowercase		)
       ldm.set_progress_bar_config(disable=_lowercase		)

       _lowercase							     =       self.dummy_image.to(_lowercase		)

       _lowercase							     =       torch.Generator(device=_lowercase		).manual_seed(0		)
       _lowercase							     =       ldm(image=_lowercase   ,						generator=_lowercase   ,						num_inference_steps=2   ,						output_type="numpy"		).images

       _lowercase							     =       image[0, -3:, -3:, -1]

       assert image.shape == (1, 6_4, 6_4, 3)
       _lowercase							     =       np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]		)

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




    @unittest.skipIf(torch_device != "cuda"   ,						"This test requires a GPU"		)
    def 		_lowerCamelCase      (	self			:       List[str]		)						->    Optional[int]:
       _lowercase							     =       self.dummy_uncond_unet
       _lowercase							     =       DDIMScheduler()
       _lowercase							     =       self.dummy_vq_model

       # put models in fp16
       _lowercase							     =       unet.half()
       _lowercase							     =       vqvae.half()

       _lowercase							     =       LDMSuperResolutionPipeline(unet=_lowercase   ,						vqvae=_lowercase   ,						scheduler=_lowercase		)
       ldm.to(_lowercase		)
       ldm.set_progress_bar_config(disable=_lowercase		)

       _lowercase							     =       self.dummy_image.to(_lowercase		)

       _lowercase							     =       ldm(_lowercase   ,						num_inference_steps=2   ,						output_type="numpy"		).images

       assert image.shape == (1, 6_4, 6_4, 3)
@slow
@require_torch
class   UpperCAmelCase_	(				unittest.TestCase   ):
    def 		_lowerCamelCase      (	self			:       Optional[int]		)						->    Tuple:
       _lowercase							     =       load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/vq_diffusion/teddy_bear_pool.png"		)
       _lowercase							     =       init_image.resize((6_4, 6_4)   ,						resample=PIL_INTERPOLATION["lanczos"]		)

       _lowercase							     =       LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution"   ,						device_map="auto"		)
       ldm.set_progress_bar_config(disable=_lowercase		)

       _lowercase							     =       torch.manual_seed(0		)
       _lowercase							     =       ldm(image=_lowercase   ,						generator=_lowercase   ,						num_inference_steps=2_0   ,						output_type="numpy"		).images

       _lowercase							     =       image[0, -3:, -3:, -1]

       assert image.shape == (1, 2_5_6, 2_5_6, 3)
       _lowercase							     =       np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]		)

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


from ..utils import DummyObject, requires_backends
class   UpperCAmelCase_	(				metaclass=lowercase__   ):
    snake_case_											=      ["""torch""", """transformers""", """k_diffusion"""]
    def __init__(	self			:       Optional[Any]   ,						*_lowercase			:       Optional[int]   ,						**_lowercase			:       List[str]		)						->    Dict:
       requires_backends(self   ,						["torch", "transformers", "k_diffusion"]		)
    @classmethod
    def 		_lowerCamelCase      (	cls			:       Union[str, Any]   ,						*_lowercase			:       Any   ,						**_lowercase			:       List[Any]		)						->    str:
       requires_backends(cls   ,						["torch", "transformers", "k_diffusion"]		)




    @classmethod
    def 		_lowerCamelCase      (	cls			:       Optional[Any]   ,						*_lowercase			:       List[Any]   ,						**_lowercase			:       Any		)						->    Any:
       requires_backends(cls   ,						["torch", "transformers", "k_diffusion"]		)