

"""simple docstring"""






import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







class       snake_case__  ( lowerCAmelCase_					,  lowerCAmelCase_					,  unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						StableDiffusionSAGPipeline
			SCREAMING_SNAKE_CASE__     				=						TEXT_TO_IMAGE_PARAMS
			SCREAMING_SNAKE_CASE__     				=						TEXT_TO_IMAGE_BATCH_PARAMS
			SCREAMING_SNAKE_CASE__     				=						TEXT_TO_IMAGE_IMAGE_PARAMS
			SCREAMING_SNAKE_CASE__     				=						TEXT_TO_IMAGE_IMAGE_PARAMS
			SCREAMING_SNAKE_CASE__     				=						False







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								torch.manual_seed(0  )
								UpperCAmelCase			:   Optional[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			,  )
								UpperCAmelCase			:   Optional[Any]      =				DDIMScheduler(
								    beta_start=0.0_0_0_8_5			,  beta_end=0.0_1_2			,  beta_schedule="scaled_linear"			,  clip_sample=lowercase			,  set_alpha_to_one=lowercase			,  )
								torch.manual_seed(0  )
								UpperCAmelCase			:   str      =				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			,  )
								torch.manual_seed(0  )
								UpperCAmelCase			:   Optional[int]      =				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			,  )
								UpperCAmelCase			:   Union[str, Any]      =				CLIPTextModel(lowercase  )
								UpperCAmelCase			:   str      =				CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"  )

								UpperCAmelCase			:   Any      =				{
								    "unet": unet,
								    "scheduler": scheduler,
								    "vae": vae,
								    "text_encoder": text_encoder,
								    "tokenizer": tokenizer,
								    "safety_checker": None,
								    "feature_extractor": None,
								}
								return components







			def 			__lowerCAmelCase				(			self			: Any			,  lowercase			: str			,  lowercase			: List[str]=0  ):





								'''simple docstring'''



								if str(lowercase  ).startswith("mps"  ):
													UpperCAmelCase			:   Optional[Any]      =				torch.manual_seed(lowercase  )
								else:
													UpperCAmelCase			:   Optional[Any]      =				torch.Generator(device=lowercase  ).manual_seed(lowercase  )
								UpperCAmelCase			:   Any      =				{
								    "prompt": ".",
								    "generator": generator,
								    "num_inference_steps": 2,
								    "guidance_scale": 1.0,
								    "sag_scale": 1.0,
								    "output_type": "numpy",
								}
								return inputs







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								super().test_inference_batch_single_identical(expected_max_diff=3E-3  )







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







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Any      =				StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"  )
								UpperCAmelCase			:   List[str]      =				sag_pipe.to(lowercase  )
								sag_pipe.set_progress_bar_config(disable=lowercase  )

								UpperCAmelCase			:   Dict      =				"."
								UpperCAmelCase			:   Optional[int]      =				torch.manual_seed(0  )
								UpperCAmelCase			:   Optional[Any]      =				sag_pipe(
								    [prompt]			,  generator=lowercase			,  guidance_scale=7.5			,  sag_scale=1.0			,  num_inference_steps=20			,  output_type="np"  )

								UpperCAmelCase			:   Union[str, Any]      =				output.images

								UpperCAmelCase			:   str      =				image[0, -3:, -3:, -1]

								assert image.shape == (1, 5_12, 5_12, 3)
								UpperCAmelCase			:   Dict      =				np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9]  )

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







			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"  )
								UpperCAmelCase			:   List[str]      =				sag_pipe.to(lowercase  )
								sag_pipe.set_progress_bar_config(disable=lowercase  )

								UpperCAmelCase			:   List[Any]      =				"."
								UpperCAmelCase			:   Dict      =				torch.manual_seed(0  )
								UpperCAmelCase			:   str      =				sag_pipe(
								    [prompt]			,  generator=lowercase			,  guidance_scale=7.5			,  sag_scale=1.0			,  num_inference_steps=20			,  output_type="np"  )

								UpperCAmelCase			:   List[Any]      =				output.images

								UpperCAmelCase			:   Dict      =				image[0, -3:, -3:, -1]

								assert image.shape == (1, 5_12, 5_12, 3)
								UpperCAmelCase			:   Any      =				np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1]  )

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







			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"  )
								UpperCAmelCase			:   Optional[int]      =				sag_pipe.to(lowercase  )
								sag_pipe.set_progress_bar_config(disable=lowercase  )

								UpperCAmelCase			:   List[Any]      =				"."
								UpperCAmelCase			:   List[str]      =				torch.manual_seed(0  )
								UpperCAmelCase			:   List[str]      =				sag_pipe(
								    [prompt]			,  width=7_68			,  height=5_12			,  generator=lowercase			,  guidance_scale=7.5			,  sag_scale=1.0			,  num_inference_steps=20			,  output_type="np"			,  )

								UpperCAmelCase			:   Any      =				output.images

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





"""simple docstring"""






from functools import reduce

snake_case_   :   List[Any]        =       (
    """73167176531330624919225119674426574742355349194934"""
    """96983520312774506326239578318016984801869478851843"""
    """85861560789112949495459501737958331952853208805511"""
    """12540698747158523863050715693290963295227443043557"""
    """66896648950445244523161731856403098711121722383113"""
    """62229893423380308135336276614282806444486645238749"""
    """30358907296290491560440772390713810515859307960866"""
    """70172427121883998797908792274921901699720888093776"""
    """65727333001053367881220235421809751254540594752243"""
    """52584907711670556013604839586446706324415722155397"""
    """53697817977846174064955149290862569321978468622482"""
    """83972241375657056057490261407972968652414535100474"""
    """82166370484403199890008895243450658541227588666881"""
    """16427171479924442928230863465674813919123162824586"""
    """17866458359124566529476545682848912883142607690042"""
    """24219022671055626321111109370544217506941658960408"""
    """07198403850962455444362981230987879927244284909188"""
    """84580156166097919133875499200524063689912560717606"""
    """05886116467109405077541002256983155200055935729725"""
    """71636269561882670428252483600823257530420752963450"""
)



def   lowercase_	(    _lowercase       :		str = N	):




					'''simple docstring'''






					return max(
					    # mypy cannot properly interpret reduce
					    int(reduce(lambda _lowercase							,      _lowercase	: str(int(_lowercase	) * int(_lowercase	)	)							,      n[i : i + 13]	)	)
					    for i in range(len(_lowercase	) - 12	)	)


if __name__ == "__main__":
	print(f'''{solution() = }''')


