





'''simple docstring'''







import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


class     __a					(unittest.TestCase   ):

							def 			UpperCAmelCase__		(							self							:   Any      )    ->				int:







													"""simple docstring"""
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

							@property
							def 			UpperCAmelCase__		(							self							:   int      )    ->				Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_	:					Dict     =     1
													UpperCAmelCase_	:					Any     =     3
													UpperCAmelCase_	:					List[str]     =     (32, 32)

													UpperCAmelCase_	:					List[str]     =     floats_tensor((batch_size, num_channels) + sizes				,   rng=random.Random(0      )      ).to(_lowerCAmelCase      )
													return image

							@property
							def 			UpperCAmelCase__		(							self							:   List[str]      )    ->				Optional[Any]:







													"""simple docstring"""
													torch.manual_seed(0      )
													UpperCAmelCase_	:					int     =     UNetaDConditionModel(
													    block_out_channels=(32, 64)				,   layers_per_block=2				,   sample_size=32				,   in_channels=4				,   out_channels=4				,   down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')				,   up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')				,   cross_attention_dim=32				,   )
													return model

							@property
							def 			UpperCAmelCase__		(							self							:   List[str]      )    ->				Dict:







													"""simple docstring"""
													torch.manual_seed(0      )
													UpperCAmelCase_	:					Tuple     =     AutoencoderKL(
													    block_out_channels=[32, 64]				,   in_channels=3				,   out_channels=3				,   down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']				,   up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']				,   latent_channels=4				,   )
													return model

							@property
							def 			UpperCAmelCase__		(							self							:   Union[str, Any]      )    ->				Any:







													"""simple docstring"""
													torch.manual_seed(0      )
													UpperCAmelCase_	:					Any     =     CLIPTextConfig(
													    bos_token_id=0				,   eos_token_id=2				,   hidden_size=32				,   intermediate_size=37				,   layer_norm_eps=1E-05				,   num_attention_heads=4				,   num_hidden_layers=5				,   pad_token_id=1				,   vocab_size=10_00				,   )
													return CLIPTextModel(_lowerCAmelCase      )

							@property
							def 			UpperCAmelCase__		(							self							:   int      )    ->				Dict:







													"""simple docstring"""
													def extract(*__magic_name__							:   Optional[int]				,   **__magic_name__							:   Any      ):


																			class     __a					:

																										def __init__(							self							:   Any      )    ->				int:







																																"""simple docstring"""
																																UpperCAmelCase_	:					Tuple     =     torch.ones([0]      )



																										def 			UpperCAmelCase__		(							self							:   Optional[int]				,   __magic_name__							:   str      )    ->				Any:







																																"""simple docstring"""
																																self.pixel_values.to(_lowerCAmelCase      )
																																return self





																			return Out()

													return extract

							def 			UpperCAmelCase__		(							self							:   Union[str, Any]      )    ->				Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[Any]     =     "cpu"  # ensure determinism for the device-dependent torch.Generator
													UpperCAmelCase_	:					Optional[int]     =     self.dummy_cond_unet
													UpperCAmelCase_	:					Union[str, Any]     =     DDIMScheduler(
													    beta_start=0.0_0_0_8_5				,   beta_end=0.0_1_2				,   beta_schedule='''scaled_linear'''				,   clip_sample=_lowerCAmelCase				,   set_alpha_to_one=_lowerCAmelCase				,   )

													UpperCAmelCase_	:					List[str]     =     self.dummy_vae
													UpperCAmelCase_	:					int     =     self.dummy_text_encoder
													UpperCAmelCase_	:					str     =     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

													# make sure here that pndm scheduler skips prk
													UpperCAmelCase_	:					List[str]     =     StableDiffusionPipeline(
													    unet=_lowerCAmelCase				,   scheduler=_lowerCAmelCase				,   vae=_lowerCAmelCase				,   text_encoder=_lowerCAmelCase				,   tokenizer=_lowerCAmelCase				,   safety_checker=_lowerCAmelCase				,   feature_extractor=self.dummy_extractor				,   )
													UpperCAmelCase_	:					str     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					Any     =     "A painting of a squirrel eating a burger"

													UpperCAmelCase_	:					Union[str, Any]     =     torch.Generator(device=_lowerCAmelCase      ).manual_seed(0      )
													UpperCAmelCase_	:					Union[str, Any]     =     sd_pipe([prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=6.0				,   num_inference_steps=2				,   output_type='''np'''      )
													UpperCAmelCase_	:					Optional[int]     =     output.images

													UpperCAmelCase_	:					Dict     =     torch.Generator(device=_lowerCAmelCase      ).manual_seed(0      )
													UpperCAmelCase_	:					Dict     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=6.0				,   num_inference_steps=2				,   output_type='''np'''				,   return_dict=_lowerCAmelCase				,   )[0]

													UpperCAmelCase_	:					int     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					List[str]     =     image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													UpperCAmelCase_	:					List[str]     =     np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4]      )

													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 			UpperCAmelCase__		(							self							:   Any      )    ->				Dict:







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[Any]     =     "cpu"  # ensure determinism for the device-dependent torch.Generator
													UpperCAmelCase_	:					Optional[int]     =     self.dummy_cond_unet
													UpperCAmelCase_	:					List[Any]     =     PNDMScheduler(skip_prk_steps=_lowerCAmelCase      )
													UpperCAmelCase_	:					Union[str, Any]     =     self.dummy_vae
													UpperCAmelCase_	:					Union[str, Any]     =     self.dummy_text_encoder
													UpperCAmelCase_	:					List[str]     =     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

													# make sure here that pndm scheduler skips prk
													UpperCAmelCase_	:					List[str]     =     StableDiffusionPipeline(
													    unet=_lowerCAmelCase				,   scheduler=_lowerCAmelCase				,   vae=_lowerCAmelCase				,   text_encoder=_lowerCAmelCase				,   tokenizer=_lowerCAmelCase				,   safety_checker=_lowerCAmelCase				,   feature_extractor=self.dummy_extractor				,   )
													UpperCAmelCase_	:					str     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					Any     =     "A painting of a squirrel eating a burger"
													UpperCAmelCase_	:					Any     =     torch.Generator(device=_lowerCAmelCase      ).manual_seed(0      )
													UpperCAmelCase_	:					Any     =     sd_pipe([prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=6.0				,   num_inference_steps=2				,   output_type='''np'''      )

													UpperCAmelCase_	:					Optional[int]     =     output.images

													UpperCAmelCase_	:					List[str]     =     torch.Generator(device=_lowerCAmelCase      ).manual_seed(0      )
													UpperCAmelCase_	:					Optional[int]     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=6.0				,   num_inference_steps=2				,   output_type='''np'''				,   return_dict=_lowerCAmelCase				,   )[0]

													UpperCAmelCase_	:					List[str]     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					Optional[int]     =     image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													UpperCAmelCase_	:					Union[str, Any]     =     np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3]      )

													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 			UpperCAmelCase__		(							self							:   str      )    ->				Tuple:







													"""simple docstring"""
													UpperCAmelCase_	:					int     =     StableDiffusionPipeline.from_pretrained(
													    '''hf-internal-testing/tiny-stable-diffusion-lms-pipe'''				,   safety_checker=_lowerCAmelCase      )
													assert isinstance(_lowerCAmelCase				,   _lowerCAmelCase      )
													assert isinstance(pipe.scheduler				,   _lowerCAmelCase      )
													assert pipe.safety_checker is None

													UpperCAmelCase_	:					List[str]     =     pipe('''example prompt'''				,   num_inference_steps=2      ).images[0]
													assert image is not None

													# check that there's no error when saving a pipeline with one of the models being None
													with tempfile.TemporaryDirectory() as tmpdirname:
																			pipe.save_pretrained(_lowerCAmelCase      )
																			UpperCAmelCase_	:					List[Any]     =     StableDiffusionPipeline.from_pretrained(_lowerCAmelCase      )

													# sanity check that the pipeline still works
													assert pipe.safety_checker is None
													UpperCAmelCase_	:					List[Any]     =     pipe('''example prompt'''				,   num_inference_steps=2      ).images[0]
													assert image is not None



							@unittest.skipIf(torch_device != '''cuda'''				,   '''This test requires a GPU'''      )
							def 			UpperCAmelCase__		(							self							:   str      )    ->				Any:







													"""simple docstring"""
													UpperCAmelCase_	:					List[Any]     =     self.dummy_cond_unet
													UpperCAmelCase_	:					Any     =     PNDMScheduler(skip_prk_steps=_lowerCAmelCase      )
													UpperCAmelCase_	:					str     =     self.dummy_vae
													UpperCAmelCase_	:					Any     =     self.dummy_text_encoder
													UpperCAmelCase_	:					List[str]     =     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

													# put models in fp16
													UpperCAmelCase_	:					Optional[int]     =     unet.half()
													UpperCAmelCase_	:					Optional[int]     =     vae.half()
													UpperCAmelCase_	:					List[str]     =     bert.half()

													# make sure here that pndm scheduler skips prk
													UpperCAmelCase_	:					int     =     StableDiffusionPipeline(
													    unet=_lowerCAmelCase				,   scheduler=_lowerCAmelCase				,   vae=_lowerCAmelCase				,   text_encoder=_lowerCAmelCase				,   tokenizer=_lowerCAmelCase				,   safety_checker=_lowerCAmelCase				,   feature_extractor=self.dummy_extractor				,   )
													UpperCAmelCase_	:					Optional[Any]     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					List[str]     =     "A painting of a squirrel eating a burger"
													UpperCAmelCase_	:					Optional[int]     =     sd_pipe([prompt]				,   num_inference_steps=2				,   output_type='''np'''      ).images

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





@nightly
@require_torch_gpu
class     __a					(unittest.TestCase   ):

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







													"""simple docstring"""
													# clean up the VRAM after each test
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

							def 			UpperCAmelCase__		(							self							:   Dict      )    ->				List[Any]:







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[Any]     =     StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''				,   safety_checker=_lowerCAmelCase      )
													UpperCAmelCase_	:					Dict     =     LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config      )
													UpperCAmelCase_	:					Tuple     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					List[str]     =     (
													    "portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle"
													    " coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with"
													    " anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and"
													    " children from bahnhof zoo, detailed "
													)
													UpperCAmelCase_	:					str     =     40_03_66_03_46
													UpperCAmelCase_	:					str     =     7

													# without safety guidance (sld_guidance_scale = 0)
													UpperCAmelCase_	:					Tuple     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					Dict     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=0				,   )

													UpperCAmelCase_	:					Any     =     output.images
													UpperCAmelCase_	:					Any     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					List[str]     =     [0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6]

													assert image.shape == (1, 5_12, 5_12, 3)

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

													# without safety guidance (strong configuration)
													UpperCAmelCase_	:					Any     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					List[Any]     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=20_00				,   sld_warmup_steps=7				,   sld_threshold=0.0_2_5				,   sld_momentum_scale=0.5				,   sld_mom_beta=0.7				,   )

													UpperCAmelCase_	:					Optional[Any]     =     output.images
													UpperCAmelCase_	:					str     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					str     =     [0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9]

													assert image.shape == (1, 5_12, 5_12, 3)

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

							def 			UpperCAmelCase__		(							self							:   Dict      )    ->				List[Any]:







													"""simple docstring"""
													UpperCAmelCase_	:					List[str]     =     StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''				,   safety_checker=_lowerCAmelCase      )
													UpperCAmelCase_	:					int     =     LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config      )
													UpperCAmelCase_	:					Union[str, Any]     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					int     =     "padme amidala taking a bath artwork, safe for work, no nudity"
													UpperCAmelCase_	:					Optional[int]     =     27_34_97_17_55
													UpperCAmelCase_	:					str     =     7

													UpperCAmelCase_	:					Optional[int]     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					List[str]     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=0				,   )

													UpperCAmelCase_	:					Tuple     =     output.images
													UpperCAmelCase_	:					Optional[Any]     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					Optional[Any]     =     [0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7]

													assert image.shape == (1, 5_12, 5_12, 3)

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

													UpperCAmelCase_	:					int     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					Any     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=20_00				,   sld_warmup_steps=7				,   sld_threshold=0.0_2_5				,   sld_momentum_scale=0.5				,   sld_mom_beta=0.7				,   )

													UpperCAmelCase_	:					List[Any]     =     output.images
													UpperCAmelCase_	:					Any     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					Dict     =     [0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3]

													assert image.shape == (1, 5_12, 5_12, 3)

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



							def 			UpperCAmelCase__		(							self							:   Dict      )    ->				List[str]:







													"""simple docstring"""
													UpperCAmelCase_	:					Optional[Any]     =     StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''      )
													UpperCAmelCase_	:					List[Any]     =     sd_pipe.to(_lowerCAmelCase      )
													sd_pipe.set_progress_bar_config(disable=_lowerCAmelCase      )

													UpperCAmelCase_	:					List[Any]     =     (
													    "the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c."
													    " leyendecker"
													)
													UpperCAmelCase_	:					Optional[int]     =     10_44_35_52_34
													UpperCAmelCase_	:					Optional[int]     =     12

													UpperCAmelCase_	:					Tuple     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					Tuple     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=0				,   )

													UpperCAmelCase_	:					Optional[int]     =     output.images
													UpperCAmelCase_	:					Dict     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					Optional[int]     =     np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]      )

													assert image.shape == (1, 5_12, 5_12, 3)

													assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-7

													UpperCAmelCase_	:					Optional[int]     =     torch.manual_seed(_lowerCAmelCase      )
													UpperCAmelCase_	:					Optional[int]     =     sd_pipe(
													    [prompt]				,   generator=_lowerCAmelCase				,   guidance_scale=_lowerCAmelCase				,   num_inference_steps=50				,   output_type='''np'''				,   width=5_12				,   height=5_12				,   sld_guidance_scale=20_00				,   sld_warmup_steps=7				,   sld_threshold=0.0_2_5				,   sld_momentum_scale=0.5				,   sld_mom_beta=0.7				,   )

													UpperCAmelCase_	:					Union[str, Any]     =     output.images
													UpperCAmelCase_	:					Optional[int]     =     image[0, -3:, -3:, -1]
													UpperCAmelCase_	:					Dict     =     np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1]      )
													assert image.shape == (1, 5_12, 5_12, 3)

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











import math
import sys



def 					UpperCAmelCase__     (   lowerCamelCase      ):
  if number != int(lowerCamelCase      ):
    raise ValueError("the value of input must be a natural number"      )
  if number < 0:
    raise ValueError("the value of input must not be a negative number"      )
  if number == 0:
    return 1
  lowercase			:Dict							=  [-1] * (number + 1)
  lowercase			:Optional[Any]							=  0
  for i in range(1,	number + 1      ):
    lowercase			:Optional[int]							=  sys.maxsize
    lowercase			:Any							=  int(math.sqrt(lowerCamelCase      )      )
    for j in range(1,	root + 1      ):
      lowercase			:Any							=  1 + answers[i - (j**2)]
      lowercase			:Dict							=  min(lowerCamelCase,	lowerCamelCase      )
    lowercase			:Union[str, Any]							=  answer
  return answers[number]


if __name__ == "__main__":
     import doctest

     doctest.testmod()



