





'''simple docstring'''




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()


class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[str]						=			1
          __A		:					List[str]						=			3
          __A		:					str						=			(32, 32)

          __A		:					List[Any]						=			floats_tensor((batch_size, num_channels) + sizes		,      rng=random.Random(0)).to(_UpperCAmelCase)
          return image


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)
          __A		:					Union[str, Any]						=			UNetaDConditionModel(
              block_out_channels=(32, 32, 64)		,      layers_per_block=2		,      sample_size=32		,      in_channels=7		,      out_channels=4		,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D', 'CrossAttnDownBlock2D')		,      up_block_types=('CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'UpBlock2D')		,      cross_attention_dim=32		,      attention_head_dim=8		,      use_linear_projection=_UpperCAmelCase		,      only_cross_attention=(True, True, False)		,      num_class_embeds=100		,      )
          return model


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)
          __A		:					Optional[Any]						=			AutoencoderKL(
              block_out_channels=[32, 32, 64]		,      in_channels=3		,      out_channels=3		,      down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D']		,      up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D']		,      latent_channels=4		,      )
          return model


     @property
     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.manual_seed(0)
          __A		:					Tuple						=			CLIPTextConfig(
              bos_token_id=0		,      eos_token_id=2		,      hidden_size=32		,      intermediate_size=37		,      layer_norm_eps=1e-0_5		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1000		,      hidden_act='gelu'		,      projection_dim=512		,      )
          return CLIPTextModel(_UpperCAmelCase)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					Tuple						=			'cpu'  # ensure determinism for the device-dependent torch.Generator
          __A		:					Tuple						=			self.dummy_cond_unet_upscale
          __A		:					Optional[Any]						=			DDPMScheduler()
          __A		:					str						=			DDIMScheduler(prediction_type='v_prediction')
          __A		:					int						=			self.dummy_vae
          __A		:					Tuple						=			self.dummy_text_encoder
          __A		:					int						=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')

          __A		:					str						=			self.dummy_image.cpu().permute(0		,      2		,      3		,      1)[0]
          __A		:					Tuple						=			Image.fromarray(np.uinta(_UpperCAmelCase)).convert('RGB').resize((64, 64))

          # make sure here that pndm scheduler skips prk
          __A		:					Dict						=			StableDiffusionUpscalePipeline(
              unet=_UpperCAmelCase		,      low_res_scheduler=_UpperCAmelCase		,      scheduler=_UpperCAmelCase		,      vae=_UpperCAmelCase		,      text_encoder=_UpperCAmelCase		,      tokenizer=_UpperCAmelCase		,      max_noise_level=350		,      )
          __A		:					Dict						=			sd_pipe.to(_UpperCAmelCase)
          sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase)

          __A		:					Dict						=			'A painting of a squirrel eating a burger'
          __A		:					Optional[int]						=			torch.Generator(device=_UpperCAmelCase).manual_seed(0)
          __A		:					Dict						=			sd_pipe(
              [prompt]		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      guidance_scale=6.0		,      noise_level=20		,      num_inference_steps=2		,      output_type='np'		,      )

          __A		:					Tuple						=			output.images

          __A		:					Dict						=			torch.Generator(device=_UpperCAmelCase).manual_seed(0)
          __A		:					str						=			sd_pipe(
              [prompt]		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      guidance_scale=6.0		,      noise_level=20		,      num_inference_steps=2		,      output_type='np'		,      return_dict=_UpperCAmelCase		,      )[0]

          __A		:					Union[str, Any]						=			image[0, -3:, -3:, -1]
          __A		:					List[Any]						=			image_from_tuple[0, -3:, -3:, -1]

          __A		:					Dict						=			low_res_image.size[0] * 4
          assert image.shape == (1, expected_height_width, expected_height_width, 3)
          __A		:					Optional[Any]						=			np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661])

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


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			'cpu'  # ensure determinism for the device-dependent torch.Generator
          __A		:					Optional[Any]						=			self.dummy_cond_unet_upscale
          __A		:					Any						=			DDPMScheduler()
          __A		:					Union[str, Any]						=			DDIMScheduler(prediction_type='v_prediction')
          __A		:					List[str]						=			self.dummy_vae
          __A		:					Optional[Any]						=			self.dummy_text_encoder
          __A		:					Tuple						=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')

          __A		:					List[str]						=			self.dummy_image.cpu().permute(0		,      2		,      3		,      1)[0]
          __A		:					List[Any]						=			Image.fromarray(np.uinta(_UpperCAmelCase)).convert('RGB').resize((64, 64))

          # make sure here that pndm scheduler skips prk
          __A		:					Optional[int]						=			StableDiffusionUpscalePipeline(
              unet=_UpperCAmelCase		,      low_res_scheduler=_UpperCAmelCase		,      scheduler=_UpperCAmelCase		,      vae=_UpperCAmelCase		,      text_encoder=_UpperCAmelCase		,      tokenizer=_UpperCAmelCase		,      max_noise_level=350		,      )
          __A		:					Any						=			sd_pipe.to(_UpperCAmelCase)
          sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase)

          __A		:					Optional[Any]						=			'A painting of a squirrel eating a burger'
          __A		:					List[str]						=			sd_pipe(
              2 * [prompt]		,      image=2 * [low_res_image]		,      guidance_scale=6.0		,      noise_level=20		,      num_inference_steps=2		,      output_type='np'		,      )
          __A		:					int						=			output.images
          assert image.shape[0] == 2

          __A		:					int						=			torch.Generator(device=_UpperCAmelCase).manual_seed(0)
          __A		:					int						=			sd_pipe(
              [prompt]		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      num_images_per_prompt=2		,      guidance_scale=6.0		,      noise_level=20		,      num_inference_steps=2		,      output_type='np'		,      )
          __A		:					Optional[Any]						=			output.images
          assert image.shape[0] == 2


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

          '''simple docstring'''






          __A		:					Tuple						=			self.dummy_cond_unet_upscale
          __A		:					Any						=			DDPMScheduler()
          __A		:					Optional[Any]						=			DDIMScheduler(prediction_type='v_prediction')
          __A		:					Any						=			self.dummy_vae
          __A		:					Union[str, Any]						=			self.dummy_text_encoder
          __A		:					Optional[int]						=			CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')

          __A		:					Any						=			self.dummy_image.cpu().permute(0		,      2		,      3		,      1)[0]
          __A		:					Union[str, Any]						=			Image.fromarray(np.uinta(_UpperCAmelCase)).convert('RGB').resize((64, 64))

          # put models in fp16, except vae as it overflows in fp16
          __A		:					Optional[Any]						=			unet.half()
          __A		:					str						=			text_encoder.half()

          # make sure here that pndm scheduler skips prk
          __A		:					Union[str, Any]						=			StableDiffusionUpscalePipeline(
              unet=_UpperCAmelCase		,      low_res_scheduler=_UpperCAmelCase		,      scheduler=_UpperCAmelCase		,      vae=_UpperCAmelCase		,      text_encoder=_UpperCAmelCase		,      tokenizer=_UpperCAmelCase		,      max_noise_level=350		,      )
          __A		:					Any						=			sd_pipe.to(_UpperCAmelCase)
          sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase)

          __A		:					int						=			'A painting of a squirrel eating a burger'
          __A		:					int						=			torch.manual_seed(0)
          __A		:					Optional[Any]						=			sd_pipe(
              [prompt]		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      num_inference_steps=2		,      output_type='np'		,      ).images

          __A		:					List[str]						=			low_res_image.size[0] * 4
          assert image.shape == (1, expected_height_width, expected_height_width, 3)




@slow
@require_torch_gpu
class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					str						=			load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/sd2-upscale/low_res_cat.png')
          __A		:					Union[str, Any]						=			load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
              '/upsampled_cat.npy')

          __A		:					Optional[Any]						=			'stabilityai/stable-diffusion-x4-upscaler'
          __A		:					Any						=			StableDiffusionUpscalePipeline.from_pretrained(_UpperCAmelCase)
          pipe.to(_UpperCAmelCase)
          pipe.set_progress_bar_config(disable=_UpperCAmelCase)
          pipe.enable_attention_slicing()

          __A		:					List[Any]						=			'a cat sitting on a park bench'

          __A		:					Optional[int]						=			torch.manual_seed(0)
          __A		:					Any						=			pipe(
              prompt=_UpperCAmelCase		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      output_type='np'		,      )
          __A		:					Tuple						=			output.images[0]

          assert image.shape == (512, 512, 3)
          assert np.abs(expected_image - image).max() < 1e-3


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[str]						=			load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/sd2-upscale/low_res_cat.png')
          __A		:					Union[str, Any]						=			load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
              '/upsampled_cat_fp16.npy')

          __A		:					Any						=			'stabilityai/stable-diffusion-x4-upscaler'
          __A		:					Tuple						=			StableDiffusionUpscalePipeline.from_pretrained(
              _UpperCAmelCase		,      torch_dtype=torch.floataa		,      )
          pipe.to(_UpperCAmelCase)
          pipe.set_progress_bar_config(disable=_UpperCAmelCase)
          pipe.enable_attention_slicing()

          __A		:					List[str]						=			'a cat sitting on a park bench'

          __A		:					Tuple						=			torch.manual_seed(0)
          __A		:					int						=			pipe(
              prompt=_UpperCAmelCase		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      output_type='np'		,      )
          __A		:					List[str]						=			output.images[0]

          assert image.shape == (512, 512, 3)
          assert np.abs(expected_image - image).max() < 5e-1


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          torch.cuda.empty_cache()
          torch.cuda.reset_max_memory_allocated()
          torch.cuda.reset_peak_memory_stats()

          __A		:					Tuple						=			load_image(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/sd2-upscale/low_res_cat.png')

          __A		:					Any						=			'stabilityai/stable-diffusion-x4-upscaler'
          __A		:					Dict						=			StableDiffusionUpscalePipeline.from_pretrained(
              _UpperCAmelCase		,      torch_dtype=torch.floataa		,      )
          pipe.to(_UpperCAmelCase)
          pipe.set_progress_bar_config(disable=_UpperCAmelCase)
          pipe.enable_attention_slicing(1)
          pipe.enable_sequential_cpu_offload()

          __A		:					Optional[int]						=			'a cat sitting on a park bench'

          __A		:					Dict						=			torch.manual_seed(0)
          __A		:					Tuple						=			pipe(
              prompt=_UpperCAmelCase		,      image=_UpperCAmelCase		,      generator=_UpperCAmelCase		,      num_inference_steps=5		,      output_type='np'		,      )

          __A		:					Any						=			torch.cuda.max_memory_allocated()
          # make sure that less than 2.9 GB is allocated
          assert mem_bytes < 2.9 * 10**9






'''simple docstring'''




import mpmath  # for roots of unity
import numpy as np


class 					SCREAMING_SNAKE_CASE       :


     def __init__(		self		,      _UpperCAmelCase=None		,      _UpperCAmelCase=None):

          '''simple docstring'''






          __A		:					List[Any]						=			list(poly_a or [0])[:]
          __A		:					Optional[int]						=			list(poly_b or [0])[:]

          # Remove leading zero coefficients
          while self.polyA[-1] == 0:
               self.polyA.pop()
          __A		:					Union[str, Any]						=			len(self.polyA)

          while self.polyB[-1] == 0:
               self.polyB.pop()
          __A		:					Optional[int]						=			len(self.polyB)

          # Add 0 to make lengths equal a power of 2
          __A		:					Optional[Any]						=			int(
              2 ** np.ceil(np.loga(len(self.polyA) + len(self.polyB) - 1)))

          while len(self.polyA) < self.c_max_length:
               self.polyA.append(0)
          while len(self.polyB) < self.c_max_length:
               self.polyB.append(0)
          # A complex root used for the fourier transform
          __A		:					str						=			complex(mpmath.root(x=1		,      n=self.c_max_length		,      k=1))

          # The product
          __A		:					Tuple						=			self.__multiply()


     def 				SCREAMING_SNAKE_CASE    (		self		,      _UpperCAmelCase):

          '''simple docstring'''






          __A		:					Optional[int]						=			[[x] for x in self.polyA] if which == 'A' else [[x] for x in self.polyB]
          # Corner case
          if len(_UpperCAmelCase) <= 1:
               return dft[0]
          #
          __A		:					Dict						=			self.c_max_length // 2
          while next_ncol > 0:
               __A		:					Optional[Any]						=			[[] for i in range(_UpperCAmelCase)]
               __A		:					Tuple						=			self.root**next_ncol

               # First half of next step
               __A		:					Optional[Any]						=			1
               for j in range(self.c_max_length // (next_ncol * 2)):
                    for i in range(_UpperCAmelCase):
                         new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j])
                    current_root *= root
               # Second half of next step
               __A		:					List[str]						=			1
               for j in range(self.c_max_length // (next_ncol * 2)):
                    for i in range(_UpperCAmelCase):
                         new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j])
                    current_root *= root
               # Update
               __A		:					Optional[int]						=			new_dft
               __A		:					Tuple						=			next_ncol // 2
          return dft[0]


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					int						=			self.__dft('A')
          __A		:					Optional[Any]						=			self.__dft('B')
          __A		:					str						=			[[dft_a[i] * dft_b[i] for i in range(self.c_max_length)]]
          del dft_a
          del dft_b

          # Corner Case
          if len(inverce_c[0]) <= 1:
               return inverce_c[0]
          # Inverse DFT
          __A		:					Dict						=			2
          while next_ncol <= self.c_max_length:
               __A		:					Optional[int]						=			[[] for i in range(_UpperCAmelCase)]
               __A		:					Any						=			self.root ** (next_ncol // 2)
               __A		:					Tuple						=			1
               # First half of next step
               for j in range(self.c_max_length // next_ncol):
                    for i in range(next_ncol // 2):
                         # Even positions
                         new_inverse_c[i].append(
                             (
                                 inverce_c[i][j]
                                 + inverce_c[i][j + self.c_max_length // next_ncol]
                             )
                             / 2)
                         # Odd positions
                         new_inverse_c[i + next_ncol // 2].append(
                             (
                                 inverce_c[i][j]
                                 - inverce_c[i][j + self.c_max_length // next_ncol]
                             )
                             / (2 * current_root))
                    current_root *= root
               # Update
               __A		:					int						=			new_inverse_c
               next_ncol *= 2
          # Unpack
          __A		:					Optional[int]						=			[round(x[0].real		,      8) + round(x[0].imag		,      8) * 1j for x in inverce_c]

          # Remove leading 0's
          while inverce_c[-1] == 0:
               inverce_c.pop()
          return inverce_c


     def __str__(		self):

          '''simple docstring'''






          __A		:					int						=			'A = ' + ' + '.join(
              F'{coef}*x^{i}' for coef, i in enumerate(self.polyA[: self.len_A]))
          __A		:					Optional[Any]						=			'B = ' + ' + '.join(
              F'{coef}*x^{i}' for coef, i in enumerate(self.polyB[: self.len_B]))
          __A		:					str						=			'A*B = ' + ' + '.join(
              F'{coef}*x^{i}' for coef, i in enumerate(self.product))

          return F'{a}\n{b}\n{c}'


# Unit tests
if __name__ == "__main__":
  import doctest

  doctest.testmod()