





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


   @property
   def        snake_case					(			self       ):
       A							:							int				=    1
       A							:							Tuple				=    3
       A							:							Dict				=    (32, 32)

       A							:							Optional[int]				=    floats_tensor((batch_size, num_channels) + sizes     , rng=random.Random(0       )       ).to(_lowercase       )
       return image


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

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

       A							:							Any				=    self.dummy_image.to(_lowercase       )

       A							:							Optional[Any]				=    torch.Generator(device=_lowercase       ).manual_seed(0       )
       A							:							Optional[int]				=    ldm(image=_lowercase     , generator=_lowercase     , num_inference_steps=2     , output_type='''numpy'''       ).images

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

       assert image.shape == (1, 64, 64, 3)
       A							:							List[str]				=    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        snake_case					(			self       ):
       A							:							List[Any]				=    self.dummy_uncond_unet
       A							:							List[Any]				=    DDIMScheduler()
       A							:							Union[str, Any]				=    self.dummy_vq_model

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

       A							:							Optional[int]				=    LDMSuperResolutionPipeline(unet=_lowercase     , vqvae=_lowercase     , scheduler=_lowercase       )
       ldm.to(_lowercase       )
       ldm.set_progress_bar_config(disable=_lowercase       )

       A							:							Union[str, Any]				=    self.dummy_image.to(_lowercase       )

       A							:							Tuple				=    ldm(_lowercase     , num_inference_steps=2     , output_type='''numpy'''       ).images

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







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


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

       A							:							Tuple				=    LDMSuperResolutionPipeline.from_pretrained('''duongna/ldm-super-resolution'''     , device_map='''auto'''       )
       ldm.set_progress_bar_config(disable=_lowercase       )

       A							:							List[Any]				=    torch.manual_seed(0       )
       A							:							int				=    ldm(image=_lowercase     , generator=_lowercase     , num_inference_steps=20     , output_type='''numpy'''       ).images

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

       assert image.shape == (1, 256, 256, 3)
       A							:							Optional[Any]				=    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

"""simple docstring"""


from .number_theory.prime_numbers import next_prime
class   UpperCAmelCase_	:
    def __init__(	self			:       str   ,						_lowercase			:       int   ,						_lowercase			:       int | None = None   ,						_lowercase			:       float | None = None   ,						)						->    None:
       _lowercase							     =       size_table
       _lowercase							     =       [None] * self.size_table
       _lowercase							     =       0.75 if lim_charge is None else lim_charge
       _lowercase							     =       1 if charge_factor is None else charge_factor
       _lowercase							     =       []
       _lowercase							     =       {}
    def 		_lowerCamelCase      (	self			:       Any		)						->    Optional[Any]:
       return self._keys
    def 		_lowerCamelCase      (	self			:       Dict		)						->    Dict:
       return sum(1 for slot in self.values if slot is not None		) / (
           self.size_table * self.charge_factor
       )
    def 		_lowerCamelCase      (	self			:       Union[str, Any]   ,						_lowercase			:       List[str]		)						->    str:
       return key % self.size_table
    def 		_lowerCamelCase      (	self			:       Any   ,						_lowercase			:       Union[str, Any]		)						->    Optional[Any]:
       print(f"""step {step_ord}"""		)
       print(list(range(len(self.values		)		)		)		)
       print(self.values		)
    def 		_lowerCamelCase      (	self			:       str   ,						_lowercase			:       List[Any]		)						->    List[Any]:
       _lowercase							     =       1
       _lowercase							     =       values
       for value in values:
          self.insert_data(_lowercase		)
          self._step_by_step(_lowercase		)
          i += 1
    def 		_lowerCamelCase      (	self			:       Union[str, Any]   ,						_lowercase			:       Optional[int]   ,						_lowercase			:       Union[str, Any]		)						->    List[Any]:
       _lowercase							     =       data
       _lowercase							     =       data
    def 		_lowerCamelCase      (	self			:       int   ,						_lowercase			:       Optional[Any]   ,						_lowercase			:       Tuple=None		)						->    Any:
       _lowercase							     =       self.hash_function(key + 1		)

       while self.values[new_key] is not None and self.values[new_key] != key:
          if self.values.count(_lowercase		) > 0:
             _lowercase							     =       self.hash_function(new_key + 1		)
          else:
             _lowercase							     =       None
             break

       return new_key
    def 		_lowerCamelCase      (	self			:       str		)						->    Any:
       _lowercase							     =       [value for value in self.values if value is not None]
       _lowercase							     =       next_prime(self.size_table   ,						factor=2		)
       self._keys.clear()
       _lowercase							     =       [None] * self.size_table  # hell's pointers D: don't DRY ;/
       for value in survivor_values:
          self.insert_data(_lowercase		)




    def 		_lowerCamelCase      (	self			:       Union[str, Any]   ,						_lowercase			:       Tuple		)						->    Optional[int]:
       _lowercase							     =       self.hash_function(_lowercase		)

       if self.values[key] is None:
          self._set_value(_lowercase   ,						_lowercase		)

       elif self.values[key] == data:
          pass

       else:
          _lowercase							     =       self._collision_resolution(_lowercase   ,						_lowercase		)
          if collision_resolution is not None:
             self._set_value(_lowercase   ,						_lowercase		)
          else:
             self.rehashing()
             self.insert_data(_lowercase		)