

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




    @property
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       1
          SCREAMING_SNAKE_CASE_      :			Dict     =       3
          SCREAMING_SNAKE_CASE_      :			str     =       (32, 32)

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




    @property
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          torch.manual_seed(0			)
          SCREAMING_SNAKE_CASE_      :			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   __lowerCamelCase      (					self			):






          """simple docstring"""






          torch.manual_seed(0			)
          SCREAMING_SNAKE_CASE_      :			Dict     =       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   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[Any]     =       "cpu"
          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       self.dummy_uncond_unet
          SCREAMING_SNAKE_CASE_      :			Any     =       DDIMScheduler()
          SCREAMING_SNAKE_CASE_      :			int     =       self.dummy_vq_model

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

          SCREAMING_SNAKE_CASE_      :			Any     =       self.dummy_image.to(lowercase__			)

          SCREAMING_SNAKE_CASE_      :			str     =       torch.Generator(device=lowercase__			).manual_seed(0			)
          SCREAMING_SNAKE_CASE_      :			str     =       ldm(image=lowercase__				,     generator=lowercase__				,     num_inference_steps=2				,     output_type="numpy"			).images

          SCREAMING_SNAKE_CASE_      :			Tuple     =       image[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)
          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150]			)

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







    @unittest.skipIf(torch_device != "cuda"				,     "This test requires a GPU"			)
    def   __lowerCamelCase      (					self			):






          """simple docstring"""






          SCREAMING_SNAKE_CASE_      :			Optional[int]     =       self.dummy_uncond_unet
          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       DDIMScheduler()
          SCREAMING_SNAKE_CASE_      :			List[str]     =       self.dummy_vq_model

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

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

          SCREAMING_SNAKE_CASE_      :			Tuple     =       self.dummy_image.to(lowercase__			)

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

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






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




    def   __lowerCamelCase      (					self			):






          """simple docstring"""






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

          SCREAMING_SNAKE_CASE_      :			List[Any]     =       LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution"				,     device_map="auto"			)
          ldm.set_progress_bar_config(disable=lowercase__			)

          SCREAMING_SNAKE_CASE_      :			Union[str, Any]     =       torch.manual_seed(0			)
          SCREAMING_SNAKE_CASE_      :			str     =       ldm(image=lowercase__				,     generator=lowercase__				,     num_inference_steps=20				,     output_type="numpy"			).images

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

          assert image.shape == (1, 256, 256, 3)
          SCREAMING_SNAKE_CASE_      :			Tuple     =       np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907]			)

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







from __future__ import annotations

from enum import Enum, unique
from typing import TypeVar

# Create a generic variable that can be 'Enum', or any subclass.
_lowerCamelCase			 =						TypeVar('T', bound='Enum')



@unique
class 			a     (   _A			):

     '''simple docstring'''





     lowerCAmelCase				:       Optional[Any]   		=  80
     lowerCAmelCase				:       List[str]   		=  70
     lowerCAmelCase				:       Optional[Any]   		=  60
     lowerCAmelCase				:       Optional[Any]   		=  50
     lowerCAmelCase				:       List[Any]   		=  40
     lowerCAmelCase				:       Dict   		=  30
     lowerCAmelCase				:       List[Any]   		=  20
     lowerCAmelCase				:       List[Any]   		=  10



@unique
class 			a     (   _A			):

     '''simple docstring'''





     lowerCAmelCase				:       Dict   		=  24
     lowerCAmelCase				:       Union[str, Any]   		=  21
     lowerCAmelCase				:       Tuple   		=  18
     lowerCAmelCase				:       str   		=  15
     lowerCAmelCase				:       Dict   		=  12
     lowerCAmelCase				:       Dict   		=  9
     lowerCAmelCase				:       Tuple   		=  6
     lowerCAmelCase				:       Any   		=  3
     lowerCAmelCase				:       List[str]   		=  2
     lowerCAmelCase				:       Any   		=  1
     lowerCAmelCase				:       str   		=  -1
     lowerCAmelCase				:       Any   		=  -2
     lowerCAmelCase				:       List[str]   		=  -3
     lowerCAmelCase				:       Dict   		=  -6
     lowerCAmelCase				:       int   		=  -9
     lowerCAmelCase				:       Optional[Any]   		=  -12
     lowerCAmelCase				:       List[str]   		=  -15
     lowerCAmelCase				:       Any   		=  -18
     lowerCAmelCase				:       int   		=  -21
     lowerCAmelCase				:       Union[str, Any]   		=  -24
     @classmethod
     def   lowerCamelCase_   (	cls   :			type[T]				):
         return {unit.name: unit.value for unit in cls if unit.value > 0}




     @classmethod
     def   lowerCamelCase_   (	cls   :			type[T]				):
         return {unit.name: unit.value for unit in cls if unit.value < 0}




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						float      )  ->							str:
    UpperCAmelCase_							 =     SIUnit.get_positive() if value > 0 else SIUnit.get_negative()
    for name_prefix, value_prefix in prefixes.items():
        UpperCAmelCase_							 =     value / (10**value_prefix)
        if numerical_part > 1:
            return f'{numerical_part!s} {name_prefix}'
    return str(__UpperCamelCase      )




def 					SCREAMING_SNAKE_CASE	(       __UpperCamelCase			:						float      )  ->							str:
    for prefix in BinaryUnit:
        UpperCAmelCase_							 =     value / (2**prefix.value)
        if numerical_part > 1:
            return f'{numerical_part!s} {prefix.name}'
    return str(__UpperCamelCase      )


if __name__ == "__main__":
 import doctest

 doctest.testmod()





