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



      """simple docstring"""







      @property
      def 						snake_case_     ( self						:    Dict			):
          __lowercase						:     Optional[int]		      =    1
          __lowercase						:     List[str]		      =    3
          __lowercase						:     Tuple		      =    (32, 32)

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




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

          __lowercase						:     Tuple		      =    LDMSuperResolutionPipeline(unet=_snake_case			,    vqvae=_snake_case			,    scheduler=_snake_case			)
          ldm.to(_snake_case			)
          ldm.set_progress_bar_config(disable=_snake_case			)

          __lowercase						:     Tuple		      =    self.dummy_image.to(_snake_case			)

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

          __lowercase						:     Optional[int]		      =    image[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)
          __lowercase						:     Dict		      =    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 						snake_case_     ( self						:    Optional[int]			):
          __lowercase						:     Optional[Any]		      =    self.dummy_uncond_unet
          __lowercase						:     str		      =    DDIMScheduler()
          __lowercase						:     str		      =    self.dummy_vq_model

          # put models in fp16
          __lowercase						:     Union[str, Any]		      =    unet.half()
          __lowercase						:     Any		      =    vqvae.half()

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

          __lowercase						:     Any		      =    self.dummy_image.to(_snake_case			)

          __lowercase						:     int		      =    ldm(_snake_case			,    num_inference_steps=2			,    output_type='''numpy'''			).images

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






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



      """simple docstring"""







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

          __lowercase						:     Optional[Any]		      =    LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''			,    device_map='''auto'''			)
          ldm.set_progress_bar_config(disable=_snake_case			)

          __lowercase						:     str		      =    torch.manual_seed(0			)
          __lowercase						:     List[str]		      =    ldm(image=_snake_case			,    generator=_snake_case			,    num_inference_steps=20			,    output_type='''numpy'''			).images

          __lowercase						:     List[str]		      =    image[0, -3:, -3:, -1]

          assert image.shape == (1, 256, 256, 3)
          __lowercase						:     List[str]		      =    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




__lowerCAmelCase   :     List[Any]      =						"\n# Transformers installation\n! pip install transformers datasets\n# To install from source instead of the last release, comment the command above and uncomment the following one.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

__lowerCAmelCase   :     Optional[Any]      =						[{"type": "code", "content": INSTALL_CONTENT}]
__lowerCAmelCase   :     List[str]      =						{
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}


