



'''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    lowercase_	( self)			->   Any:



      """simple docstring"""







      a_									=1
      a_									=3
      a_									=(3_2, 3_2)

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




 @property
 def    lowercase_	( self)			->   Tuple:



      """simple docstring"""







      torch.manual_seed(0)
      a_									=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    lowercase_	( self)			->   Optional[Any]:



      """simple docstring"""







      torch.manual_seed(0)
      a_									=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    lowercase_	( self)			->   Union[str, Any]:



      """simple docstring"""







      a_									="cpu"
      a_									=self.dummy_uncond_unet
      a_									=DDIMScheduler()
      a_									=self.dummy_vq_model

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

      a_									=self.dummy_image.to(lowerCAmelCase_)

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

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

      assert image.shape == (1, 6_4, 6_4, 3)
      a_									=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    lowercase_	( self)			->   Optional[int]:



      """simple docstring"""







      a_									=self.dummy_uncond_unet
      a_									=DDIMScheduler()
      a_									=self.dummy_vq_model

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

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

      a_									=self.dummy_image.to(lowerCAmelCase_)

      a_									=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    lowercase_	( self)			->   Union[str, Any]:



      """simple docstring"""







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

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

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

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

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


import unittest
from dataclasses import dataclass
from typing import List, Union

import numpy as np
import PIL.Image

from diffusers.utils.outputs import BaseOutput







@dataclass
class 						UpperCAmelCase						(		__a):


 '''simple docstring'''




 __magic_name__       :			Union[List[PIL.Image.Image], np.ndarray]







class 						UpperCAmelCase						(		unittest.TestCase):


 '''simple docstring'''




 def    lowercase_	( self)			->   List[str]:



      """simple docstring"""







      a_									=CustomOutput(images=np.random.rand(1				,	3				,	4				,	4))

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	np.ndarray)
      assert outputs.images.shape == (1, 3, 4, 4)
      assert isinstance(outputs["images"]				,	np.ndarray)
      assert outputs["images"].shape == (1, 3, 4, 4)
      assert isinstance(outputs[0]				,	np.ndarray)
      assert outputs[0].shape == (1, 3, 4, 4)

      # test with a non-tensor attribute
      a_									=CustomOutput(images=[PIL.Image.new("RGB"				,	(4, 4))])

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	lowerCAmelCase_)
      assert isinstance(outputs.images[0]				,	PIL.Image.Image)
      assert isinstance(outputs["images"]				,	lowerCAmelCase_)
      assert isinstance(outputs["images"][0]				,	PIL.Image.Image)
      assert isinstance(outputs[0]				,	lowerCAmelCase_)
      assert isinstance(outputs[0][0]				,	PIL.Image.Image)




 def    lowercase_	( self)			->   Optional[int]:



      """simple docstring"""







      a_									=CustomOutput({"images": np.random.rand(1				,	3				,	4				,	4)})

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	np.ndarray)
      assert outputs.images.shape == (1, 3, 4, 4)
      assert isinstance(outputs["images"]				,	np.ndarray)
      assert outputs["images"].shape == (1, 3, 4, 4)
      assert isinstance(outputs[0]				,	np.ndarray)
      assert outputs[0].shape == (1, 3, 4, 4)

      # test with a non-tensor attribute
      a_									=CustomOutput({"images": [PIL.Image.new("RGB"				,	(4, 4))]})

      # check every way of getting the attribute
      assert isinstance(outputs.images				,	lowerCAmelCase_)
      assert isinstance(outputs.images[0]				,	PIL.Image.Image)
      assert isinstance(outputs["images"]				,	lowerCAmelCase_)
      assert isinstance(outputs["images"][0]				,	PIL.Image.Image)
      assert isinstance(outputs[0]				,	lowerCAmelCase_)
      assert isinstance(outputs[0][0]				,	PIL.Image.Image)


