



'''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.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






class 						_UpperCamelCase  (							unittest.TestCase   ):


			'''simple docstring'''

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





					"""simple docstring"""






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

			@property
			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		1
					__SCREAMING_SNAKE_CASE	:		Dict   	=		3
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		(3_2, 3_2)

					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		floats_tensor((batch_size, num_channels) + sizes							,					rng=random.Random(0						)						).to(lowerCAmelCase__						)
					return image

			@property
			def UpperCamelCase__ (							self					:			Tuple						):





					"""simple docstring"""






					torch.manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		int   	=		UNetaDConditionModel(
					    block_out_channels=(3_2, 6_4)							,					layers_per_block=2							,					sample_size=3_2							,					in_channels=4							,					out_channels=4							,					down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")							,					up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")							,					cross_attention_dim=3_2							,					)
					return model

			@property
			def UpperCamelCase__ (							self					:			Tuple						):





					"""simple docstring"""






					torch.manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		int   	=		AutoencoderKL(
					    block_out_channels=[3_2, 6_4]							,					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]						):





					"""simple docstring"""






					torch.manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		CLIPTextConfig(
					    bos_token_id=0							,					eos_token_id=2							,					hidden_size=3_2							,					intermediate_size=3_7							,					layer_norm_eps=1E-05							,					num_attention_heads=4							,					num_hidden_layers=5							,					pad_token_id=1							,					vocab_size=1_0_0_0							,					)
					return CLIPTextModel(lowerCAmelCase__						)

			@property
			def UpperCamelCase__ (							self					:			Optional[Any]						):





					"""simple docstring"""






					def extract(*lowerCAmelCase__					:			List[Any]							,					**lowerCAmelCase__					:			int						):






							class 						_UpperCamelCase  :


										'''simple docstring'''

										def __init__(							self					:			List[str]						):





												"""simple docstring"""






												__SCREAMING_SNAKE_CASE	:		int   	=		torch.ones([0]						)



										def UpperCamelCase__ (							self					:			List[Any]							,					lowerCAmelCase__					:			Dict						):





												"""simple docstring"""






												self.pixel_values.to(lowerCAmelCase__						)
												return self
							return Out()

					return extract

			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		self.dummy_cond_unet
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		DDIMScheduler(
					    beta_start=0.0_00_85							,					beta_end=0.0_12							,					beta_schedule="""scaled_linear"""							,					clip_sample=lowerCAmelCase__							,					set_alpha_to_one=lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		int   	=		self.dummy_vae
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.dummy_text_encoder
					__SCREAMING_SNAKE_CASE	:		int   	=		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""						)

					# make sure here that pndm scheduler skips prk
					__SCREAMING_SNAKE_CASE	:		Dict   	=		StableDiffusionPipeline(
					    unet=lowerCAmelCase__							,					scheduler=lowerCAmelCase__							,					vae=lowerCAmelCase__							,					text_encoder=lowerCAmelCase__							,					tokenizer=lowerCAmelCase__							,					safety_checker=lowerCAmelCase__							,					feature_extractor=self.dummy_extractor							,					)
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		sd_pipe.to(lowerCAmelCase__						)
					sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		str   	=		"""A painting of a squirrel eating a burger"""

					__SCREAMING_SNAKE_CASE	:		Tuple   	=		torch.Generator(device=lowerCAmelCase__						).manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		int   	=		sd_pipe([prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=6.0							,					num_inference_steps=2							,					output_type="""np"""						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		output.images

					__SCREAMING_SNAKE_CASE	:		Any   	=		torch.Generator(device=lowerCAmelCase__						).manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		sd_pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=6.0							,					num_inference_steps=2							,					output_type="""np"""							,					return_dict=lowerCAmelCase__							,					)[0]

					__SCREAMING_SNAKE_CASE	:		int   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Any   	=		image_from_tuple[0, -3:, -3:, -1]

					assert image.shape == (1, 6_4, 6_4, 3)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		np.array([0.57_53, 0.61_14, 0.50_01, 0.50_34, 0.54_70, 0.47_29, 0.49_71, 0.48_67, 0.48_67]						)

					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						):





					"""simple docstring"""






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

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

					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		"""A painting of a squirrel eating a burger"""
					__SCREAMING_SNAKE_CASE	:		int   	=		torch.Generator(device=lowerCAmelCase__						).manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		sd_pipe([prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=6.0							,					num_inference_steps=2							,					output_type="""np"""						)

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		output.images

					__SCREAMING_SNAKE_CASE	:		int   	=		torch.Generator(device=lowerCAmelCase__						).manual_seed(0						)
					__SCREAMING_SNAKE_CASE	:		str   	=		sd_pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=6.0							,					num_inference_steps=2							,					output_type="""np"""							,					return_dict=lowerCAmelCase__							,					)[0]

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

					assert image.shape == (1, 6_4, 6_4, 3)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		np.array([0.51_22, 0.57_12, 0.48_25, 0.50_53, 0.56_46, 0.47_69, 0.51_79, 0.48_94, 0.49_94]						)

					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					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Dict   	=		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

					__SCREAMING_SNAKE_CASE	:		Tuple   	=		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__						)
							__SCREAMING_SNAKE_CASE	:		List[Any]   	=		StableDiffusionPipeline.from_pretrained(lowerCAmelCase__						)

					# sanity check that the pipeline still works
					assert pipe.safety_checker is None
					__SCREAMING_SNAKE_CASE	:		str   	=		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						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		self.dummy_cond_unet
					__SCREAMING_SNAKE_CASE	:		int   	=		PNDMScheduler(skip_prk_steps=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		self.dummy_vae
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		self.dummy_text_encoder
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""						)

					# put models in fp16
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		unet.half()
					__SCREAMING_SNAKE_CASE	:		Any   	=		vae.half()
					__SCREAMING_SNAKE_CASE	:		Any   	=		bert.half()

					# make sure here that pndm scheduler skips prk
					__SCREAMING_SNAKE_CASE	:		Dict   	=		StableDiffusionPipeline(
					    unet=lowerCAmelCase__							,					scheduler=lowerCAmelCase__							,					vae=lowerCAmelCase__							,					text_encoder=lowerCAmelCase__							,					tokenizer=lowerCAmelCase__							,					safety_checker=lowerCAmelCase__							,					feature_extractor=self.dummy_extractor							,					)
					__SCREAMING_SNAKE_CASE	:		Dict   	=		sd_pipe.to(lowerCAmelCase__						)
					sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		"""A painting of a squirrel eating a burger"""
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		sd_pipe([prompt]							,					num_inference_steps=2							,					output_type="""np"""						).images

					assert image.shape == (1, 6_4, 6_4, 3)






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


			'''simple docstring'''

			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






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

			def UpperCamelCase__ (							self					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		"""cuda"""
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""						)
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		pipe.to(lowerCAmelCase__						)
					pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		"""a photo of a cat"""
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		{
					    """editing_prompt""": ["""sunglasses"""],
					    """reverse_editing_direction""": [False],
					    """edit_warmup_steps""": 1_0,
					    """edit_guidance_scale""": 6,
					    """edit_threshold""": 0.95,
					    """edit_momentum_scale""": 0.5,
					    """edit_mom_beta""": 0.6,
					}

					__SCREAMING_SNAKE_CASE	:		Any   	=		3
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		7

					# no sega enabled
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		torch.Generator(lowerCAmelCase__						)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					)

					__SCREAMING_SNAKE_CASE	:		List[str]   	=		output.images
					__SCREAMING_SNAKE_CASE	:		str   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		[
					    0.34_67_31_13,
					    0.38_49_27_33,
					    0.37_59_73_52,
					    0.34_08_63_35,
					    0.35_65_07_48,
					    0.35_57_92_05,
					    0.3_38_47_63,
					    0.34_34_02_36,
					    0.3_57_32_71,
					]

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

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

					# with sega enabled
					# generator = torch.manual_seed(seed)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Dict   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					**lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		int   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		[
					    0.41_88_78_26,
					    0.37_72_87_66,
					    0.30_13_82_72,
					    0.41_41_63_35,
					    0.41_66_49_85,
					    0.36_28_33_92,
					    0.36_19_12_46,
					    0.43_36_44_65,
					    0.43_00_17_32,
					]

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

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

			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		"""cuda"""
					__SCREAMING_SNAKE_CASE	:		Any   	=		StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		pipe.to(lowerCAmelCase__						)
					pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		Any   	=		"""an image of a crowded boulevard, realistic, 4k"""
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		{
					    """editing_prompt""": """crowd, crowded, people""",
					    """reverse_editing_direction""": True,
					    """edit_warmup_steps""": 1_0,
					    """edit_guidance_scale""": 8.3,
					    """edit_threshold""": 0.9,
					    """edit_momentum_scale""": 0.5,
					    """edit_mom_beta""": 0.6,
					}

					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		9
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		7

					# no sega enabled
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		torch.Generator(lowerCAmelCase__						)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					)

					__SCREAMING_SNAKE_CASE	:		List[str]   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		[
					    0.43_49_79_98,
					    0.91_81_40_65,
					    0.7_54_07_39,
					    0.55_58_02_05,
					    0.8_46_72_65,
					    0.5_38_96_91,
					    0.62_57_45_06,
					    0.58_89_77_63,
					    0.50_92_67_57,
					]

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

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

					# with sega enabled
					# generator = torch.manual_seed(seed)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					**lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		Tuple   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		[
					    0.3_08_97_19,
					    0.30_50_01_44,
					    0.29_01_60_42,
					    0.30_63_09_64,
					    0.32_56_87,
					    0.29_41_92_25,
					    0.2_90_80_91,
					    0.28_72_35_98,
					    0.27_69_62_94,
					]

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

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

			def UpperCamelCase__ (							self					:			Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Dict   	=		"""cuda"""
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""						)
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		pipe.to(lowerCAmelCase__						)
					pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		Dict   	=		"""a castle next to a river"""
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{
					    """editing_prompt""": ["""boat on a river, boat""", """monet, impression, sunrise"""],
					    """reverse_editing_direction""": False,
					    """edit_warmup_steps""": [1_5, 1_8],
					    """edit_guidance_scale""": 6,
					    """edit_threshold""": [0.9, 0.8],
					    """edit_momentum_scale""": 0.5,
					    """edit_mom_beta""": 0.6,
					}

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		4_8
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		7

					# no sega enabled
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		torch.Generator(lowerCAmelCase__						)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					)

					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Dict   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Any   	=		[
					    0.75_16_35_55,
					    0.76_03_71_45,
					    0.6_17_85,
					    0.9_18_96_73,
					    0.8_62_77_01,
					    0.85_18_96_94,
					    0.8_51_28_13,
					    0.87_01_20_76,
					    0.8_31_28_57,
					]

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

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

					# with sega enabled
					# generator = torch.manual_seed(seed)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Dict   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					**lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		[
					    0.73_55_33_65,
					    0.7_53_72_71,
					    0.74_34_19_05,
					    0.66_48_03_56,
					    0.6_47_29_25,
					    0.63_03_94_16,
					    0.64_81_29_05,
					    0.6_74_97_17,
					    0.6_51_71_02,
					]

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

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



			def UpperCamelCase__ (							self					:			Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		int   	=		"""cuda"""
					__SCREAMING_SNAKE_CASE	:		str   	=		StableDiffusionPipeline.from_pretrained("""runwayml/stable-diffusion-v1-5"""							,					torch_dtype=torch.floataa						)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		pipe.to(lowerCAmelCase__						)
					pipe.set_progress_bar_config(disable=lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		str   	=		"""a photo of a cat"""
					__SCREAMING_SNAKE_CASE	:		Dict   	=		{
					    """editing_prompt""": ["""sunglasses"""],
					    """reverse_editing_direction""": [False],
					    """edit_warmup_steps""": 1_0,
					    """edit_guidance_scale""": 6,
					    """edit_threshold""": 0.95,
					    """edit_momentum_scale""": 0.5,
					    """edit_mom_beta""": 0.6,
					}

					__SCREAMING_SNAKE_CASE	:		Tuple   	=		3
					__SCREAMING_SNAKE_CASE	:		int   	=		7

					# no sega enabled
					__SCREAMING_SNAKE_CASE	:		str   	=		torch.Generator(lowerCAmelCase__						)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					)

					__SCREAMING_SNAKE_CASE	:		int   	=		output.images
					__SCREAMING_SNAKE_CASE	:		int   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		[
					    0.34_88_76_95,
					    0.3_87_69_53,
					    0.3_75,
					    0.34_42_38_28,
					    0.3_58_15_43,
					    0.35_71_77_73,
					    0.3_38_37_89,
					    0.34_57_03_12,
					    0.35_93_75,
					]

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

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

					# with sega enabled
					# generator = torch.manual_seed(seed)
					generator.manual_seed(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Any   	=		pipe(
					    [prompt]							,					generator=lowerCAmelCase__							,					guidance_scale=lowerCAmelCase__							,					num_inference_steps=5_0							,					output_type="""np"""							,					width=5_1_2							,					height=5_1_2							,					**lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		Any   	=		output.images
					__SCREAMING_SNAKE_CASE	:		Dict   	=		image[0, -3:, -3:, -1]
					__SCREAMING_SNAKE_CASE	:		str   	=		[
					    0.42_28_51_56,
					    0.36_91_40_62,
					    0.29_07_71_48,
					    0.42_04_10_16,
					    0.41_91_89_45,
					    0.35_49_80_47,
					    0.3_61_81_64,
					    0.4_42_38_28,
					    0.43_11_52_34,
					]

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

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




'''simple docstring'''




from __future__ import annotations

import random
from collections.abc import Iterable
from typing import Any, Generic, TypeVar

UpperCamelCase__	:     str    =			TypeVar('''T''', bound=bool)






class 						_UpperCamelCase  (							Generic[T]   ):


			'''simple docstring'''

			def __init__(							self					:			str							,					lowerCAmelCase__					:			T						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		T   	=		value
					__SCREAMING_SNAKE_CASE	:		RandomizedHeapNode[T] | None   	=		None
					__SCREAMING_SNAKE_CASE	:		RandomizedHeapNode[T] | None   	=		None

			@property
			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					return self._value



			@staticmethod
			def UpperCamelCase__ (							lowerCAmelCase__					:			RandomizedHeapNode[T] | None							,					lowerCAmelCase__					:			RandomizedHeapNode[T] | None						):





					"""simple docstring"""






					if not roota:
							return roota

					if not roota:
							return roota

					if roota.value > roota.value:
							__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		List[str]   	=		roota, roota

					if random.choice([True, False]						):
							__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Tuple   	=		roota.right, roota.left

					__SCREAMING_SNAKE_CASE	:		Tuple   	=		RandomizedHeapNode.merge(roota.left							,					lowerCAmelCase__						)

					return roota






class 						_UpperCamelCase  (							Generic[T]   ):


			'''simple docstring'''

			def __init__(							self					:			Optional[int]							,					lowerCAmelCase__					:			Iterable[T] | None = ()						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		RandomizedHeapNode[T] | None   	=		None

					if data:
							for item in data:
									self.insert(lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			T						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Dict   	=		RandomizedHeapNode.merge(self._root							,					RandomizedHeapNode(lowerCAmelCase__						)						)

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





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.top()

					if self._root is None:
							return None

					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		RandomizedHeapNode.merge(self._root.left							,					self._root.right						)

					return result

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





					"""simple docstring"""






					if not self._root:
							raise IndexError("""Can't get top element for the empty heap."""						)
					return self._root.value

			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		None

			def UpperCamelCase__ (							self					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		int   	=		[]
					while self:
							result.append(self.pop()						)

					return result



			def __bool__(							self					:			List[Any]						):





					"""simple docstring"""






					return self._root is not None


if __name__ == "__main__":
						import doctest

						doctest.testmod()