






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


   '''simple docstring'''

   @property
   def     _SCREAMING_SNAKE_CASE    (     self			:					int  ):

     '''simple docstring'''
     __A						      =			1
     __A						      =			3
     __A						      =			(32, 32)

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


   @property
   def     _SCREAMING_SNAKE_CASE    (     self			:					List[Any]  ):

     '''simple docstring'''
     torch.manual_seed(0  )
     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					List[str]  ):

     '''simple docstring'''
     torch.manual_seed(0  )
     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					Optional[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, 64, 64, 3)
     __A						      =			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     _SCREAMING_SNAKE_CASE    (     self			:					Union[str, Any]  ):

     '''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, 64, 64, 3)





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


   '''simple docstring'''

   def     _SCREAMING_SNAKE_CASE    (     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((64, 64),	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=20,	output_type='''numpy'''  ).images

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

     assert image.shape == (1, 2_56, 2_56, 3)
     __A						      =			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"""


def  lowerCAmelCase    (   __UpperCamelCase			):




  """simple docstring"""







  if not isinstance(__UpperCamelCase		,	__UpperCamelCase			):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 8]'''			)
  if len(__UpperCamelCase			) == 0:
    raise ValueError('''Input list must be a non empty list'''			)
  if len(__UpperCamelCase			) == 1:
    return True
  try:
    __A						      =			series[1] / series[0]
    for index in range(len(__UpperCamelCase			) - 1			):
      if series[index + 1] / series[index] != common_ratio:
        return False
  except ZeroDivisionError:
    return False
  return True



def  lowerCAmelCase    (   __UpperCamelCase			):




  """simple docstring"""







  if not isinstance(__UpperCamelCase		,	__UpperCamelCase			):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 8]'''			)
  if len(__UpperCamelCase			) == 0:
    raise ValueError('''Input list must be a non empty list'''			)
  __A						      =			1
  for value in series:
    answer *= value
  return pow(__UpperCamelCase		,	1 / len(__UpperCamelCase			)			)


if __name__ == "__main__":
       import doctest

       doctest.testmod()




