



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







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


						'''simple docstring'''







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







		@property
		def 					a_  (    self  :   str					)       ->		Tuple:


						'''simple docstring'''







						_UpperCAmelCase						:  List[str]     					=	1
						_UpperCAmelCase						:  Tuple     					=	3
						_UpperCAmelCase						:  Union[str, Any]     					=	(32, 32)

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







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


						'''simple docstring'''







						torch.manual_seed(0					)
						_UpperCAmelCase						:  Optional[Any]     					=	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 					a_  (    self  :   Optional[Any]					)       ->		Any:


						'''simple docstring'''







						torch.manual_seed(0					)
						_UpperCAmelCase						:  Union[str, Any]     					=	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 					a_  (    self  :   Optional[Any]					)       ->		List[str]:


						'''simple docstring'''







						torch.manual_seed(0					)
						_UpperCAmelCase						:  str     					=	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=1000       ,		)
						return CLIPTextModel(UpperCAmelCase_					)







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


						'''simple docstring'''







						def extract(*UpperCAmelCase_  :   int       ,		**UpperCAmelCase_  :   int					):





										class 							lowerCAmelCase_		:







												def __init__(    self  :   Tuple					)       ->		List[Any]:


																'''simple docstring'''







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







												def 					a_  (    self  :   Any       ,		UpperCAmelCase_  :   str					)       ->		List[str]:


																'''simple docstring'''







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

						return extract







		def 					a_  (    self  :   List[str]					)       ->		Tuple:


						'''simple docstring'''







						_UpperCAmelCase						:  int     					=	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase						:  Tuple     					=	self.dummy_cond_unet
						_UpperCAmelCase						:  str     					=	DDIMScheduler(
						    beta_start=0.00_085       ,		beta_end=0.012       ,		beta_schedule='''scaled_linear'''       ,		clip_sample=UpperCAmelCase_       ,		set_alpha_to_one=UpperCAmelCase_       ,		)

						_UpperCAmelCase						:  Optional[int]     					=	self.dummy_vae
						_UpperCAmelCase						:  int     					=	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						:  Any     					=	StableDiffusionPipeline(
						    unet=UpperCAmelCase_       ,		scheduler=UpperCAmelCase_       ,		vae=UpperCAmelCase_       ,		text_encoder=UpperCAmelCase_       ,		tokenizer=UpperCAmelCase_       ,		safety_checker=UpperCAmelCase_       ,		feature_extractor=self.dummy_extractor       ,		)
						_UpperCAmelCase						:  Optional[Any]     					=	sd_pipe.to(UpperCAmelCase_					)
						sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

						_UpperCAmelCase						:  List[str]     					=	'''A painting of a squirrel eating a burger'''

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

						_UpperCAmelCase						:  List[str]     					=	torch.Generator(device=UpperCAmelCase_					).manual_seed(0					)
						_UpperCAmelCase						:  List[Any]     					=	sd_pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=6.0       ,		num_inference_steps=2       ,		output_type='''np'''       ,		return_dict=UpperCAmelCase_       ,		)[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						:  List[str]     					=	np.array([0.5_753, 0.6_114, 0.5_001, 0.5_034, 0.5_470, 0.4_729, 0.4_971, 0.4_867, 0.4_867]					)

						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 					a_  (    self  :   List[str]					)       ->		Union[str, Any]:


						'''simple docstring'''







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

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

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

						_UpperCAmelCase						:  Dict     					=	output.images

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

						_UpperCAmelCase						:  Tuple     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  List[str]     					=	image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)
						_UpperCAmelCase						:  Optional[int]     					=	np.array([0.5_122, 0.5_712, 0.4_825, 0.5_053, 0.5_646, 0.4_769, 0.5_179, 0.4_894, 0.4_994]					)

						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 					a_  (    self  :   List[Any]					)       ->		str:


						'''simple docstring'''







						_UpperCAmelCase						:  str     					=	StableDiffusionPipeline.from_pretrained(
						    '''hf-internal-testing/tiny-stable-diffusion-lms-pipe'''       ,		safety_checker=UpperCAmelCase_					)
						assert isinstance(UpperCAmelCase_       ,		UpperCAmelCase_					)
						assert isinstance(pipe.scheduler       ,		UpperCAmelCase_					)
						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(UpperCAmelCase_					)
										_UpperCAmelCase						:  Union[str, Any]     					=	StableDiffusionPipeline.from_pretrained(UpperCAmelCase_					)

						# sanity check that the pipeline still works
						assert pipe.safety_checker is None
						_UpperCAmelCase						:  List[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 					a_  (    self  :   Optional[Any]					)       ->		Dict:


						'''simple docstring'''







						_UpperCAmelCase						:  Optional[Any]     					=	self.dummy_cond_unet
						_UpperCAmelCase						:  int     					=	PNDMScheduler(skip_prk_steps=UpperCAmelCase_					)
						_UpperCAmelCase						:  Tuple     					=	self.dummy_vae
						_UpperCAmelCase						:  Dict     					=	self.dummy_text_encoder
						_UpperCAmelCase						:  List[str]     					=	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

						# put models in fp16
						_UpperCAmelCase						:  Tuple     					=	unet.half()
						_UpperCAmelCase						:  Union[str, Any]     					=	vae.half()
						_UpperCAmelCase						:  Optional[Any]     					=	bert.half()

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

						_UpperCAmelCase						:  int     					=	'''A painting of a squirrel eating a burger'''
						_UpperCAmelCase						:  Dict     					=	sd_pipe([prompt]       ,		num_inference_steps=2       ,		output_type='''np'''					).images

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





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







		def 					a_  (    self  :   Tuple					)       ->		int:


						'''simple docstring'''







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







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


						'''simple docstring'''







						_UpperCAmelCase						:  List[str]     					=	'''cuda'''
						_UpperCAmelCase						:  Union[str, Any]     					=	StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''					)
						_UpperCAmelCase						:  Optional[int]     					=	pipe.to(UpperCAmelCase_					)
						pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

						_UpperCAmelCase						:  Optional[int]     					=	'''a photo of a cat'''
						_UpperCAmelCase						:  Union[str, Any]     					=	{
						    '''editing_prompt''': ['''sunglasses'''],
						    '''reverse_editing_direction''': [False],
						    '''edit_warmup_steps''': 10,
						    '''edit_guidance_scale''': 6,
						    '''edit_threshold''': 0.95,
						    '''edit_momentum_scale''': 0.5,
						    '''edit_mom_beta''': 0.6,
						}

						_UpperCAmelCase						:  Tuple     					=	3
						_UpperCAmelCase						:  Optional[Any]     					=	7

						# no sega enabled
						_UpperCAmelCase						:  List[Any]     					=	torch.Generator(UpperCAmelCase_					)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  List[Any]     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		)

						_UpperCAmelCase						:  Optional[int]     					=	output.images
						_UpperCAmelCase						:  Optional[Any]     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  Optional[int]     					=	[
						    0.34_673_113,
						    0.38_492_733,
						    0.37_597_352,
						    0.34_086_335,
						    0.35_650_748,
						    0.35_579_205,
						    0.3_384_763,
						    0.34_340_236,
						    0.3_573_271,
						]

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

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

						# with sega enabled
						# generator = torch.manual_seed(seed)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  List[Any]     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		**UpperCAmelCase_       ,		)

						_UpperCAmelCase						:  List[Any]     					=	output.images
						_UpperCAmelCase						:  str     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  int     					=	[
						    0.41_887_826,
						    0.37_728_766,
						    0.30_138_272,
						    0.41_416_335,
						    0.41_664_985,
						    0.36_283_392,
						    0.36_191_246,
						    0.43_364_465,
						    0.43_001_732,
						]

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

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







		def 					a_  (    self  :   Any					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  Tuple     					=	'''cuda'''
						_UpperCAmelCase						:  List[str]     					=	StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''					)
						_UpperCAmelCase						:  List[Any]     					=	pipe.to(UpperCAmelCase_					)
						pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

						_UpperCAmelCase						:  Optional[int]     					=	'''an image of a crowded boulevard, realistic, 4k'''
						_UpperCAmelCase						:  Union[str, Any]     					=	{
						    '''editing_prompt''': '''crowd, crowded, people''',
						    '''reverse_editing_direction''': True,
						    '''edit_warmup_steps''': 10,
						    '''edit_guidance_scale''': 8.3,
						    '''edit_threshold''': 0.9,
						    '''edit_momentum_scale''': 0.5,
						    '''edit_mom_beta''': 0.6,
						}

						_UpperCAmelCase						:  List[str]     					=	9
						_UpperCAmelCase						:  Any     					=	7

						# no sega enabled
						_UpperCAmelCase						:  str     					=	torch.Generator(UpperCAmelCase_					)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  Tuple     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		)

						_UpperCAmelCase						:  str     					=	output.images
						_UpperCAmelCase						:  Dict     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  List[str]     					=	[
						    0.43_497_998,
						    0.91_814_065,
						    0.7_540_739,
						    0.55_580_205,
						    0.8_467_265,
						    0.5_389_691,
						    0.62_574_506,
						    0.58_897_763,
						    0.50_926_757,
						]

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

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

						# with sega enabled
						# generator = torch.manual_seed(seed)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  str     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		**UpperCAmelCase_       ,		)

						_UpperCAmelCase						:  Dict     					=	output.images
						_UpperCAmelCase						:  Optional[int]     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  Any     					=	[
						    0.3_089_719,
						    0.30_500_144,
						    0.29_016_042,
						    0.30_630_964,
						    0.325_687,
						    0.29_419_225,
						    0.2_908_091,
						    0.28_723_598,
						    0.27_696_294,
						]

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

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







		def 					a_  (    self  :   Optional[int]					)       ->		List[Any]:


						'''simple docstring'''







						_UpperCAmelCase						:  Tuple     					=	'''cuda'''
						_UpperCAmelCase						:  Tuple     					=	StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''					)
						_UpperCAmelCase						:  Tuple     					=	pipe.to(UpperCAmelCase_					)
						pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

						_UpperCAmelCase						:  Union[str, Any]     					=	'''a castle next to a river'''
						_UpperCAmelCase						:  Union[str, Any]     					=	{
						    '''editing_prompt''': ['''boat on a river, boat''', '''monet, impression, sunrise'''],
						    '''reverse_editing_direction''': False,
						    '''edit_warmup_steps''': [15, 18],
						    '''edit_guidance_scale''': 6,
						    '''edit_threshold''': [0.9, 0.8],
						    '''edit_momentum_scale''': 0.5,
						    '''edit_mom_beta''': 0.6,
						}

						_UpperCAmelCase						:  Optional[Any]     					=	48
						_UpperCAmelCase						:  Tuple     					=	7

						# no sega enabled
						_UpperCAmelCase						:  Optional[Any]     					=	torch.Generator(UpperCAmelCase_					)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  Optional[Any]     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		)

						_UpperCAmelCase						:  int     					=	output.images
						_UpperCAmelCase						:  Optional[Any]     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  Dict     					=	[
						    0.75_163_555,
						    0.76_037_145,
						    0.61_785,
						    0.9_189_673,
						    0.8_627_701,
						    0.85_189_694,
						    0.8_512_813,
						    0.87_012_076,
						    0.8_312_857,
						]

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

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

						# with sega enabled
						# generator = torch.manual_seed(seed)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  Optional[Any]     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		**UpperCAmelCase_       ,		)

						_UpperCAmelCase						:  str     					=	output.images
						_UpperCAmelCase						:  Any     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  Dict     					=	[
						    0.73_553_365,
						    0.7_537_271,
						    0.74_341_905,
						    0.66_480_356,
						    0.6_472_925,
						    0.63_039_416,
						    0.64_812_905,
						    0.6_749_717,
						    0.6_517_102,
						]

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

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







		def 					a_  (    self  :   Optional[Any]					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  List[str]     					=	'''cuda'''
						_UpperCAmelCase						:  str     					=	StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5'''       ,		torch_dtype=torch.floataa					)
						_UpperCAmelCase						:  Dict     					=	pipe.to(UpperCAmelCase_					)
						pipe.set_progress_bar_config(disable=UpperCAmelCase_					)

						_UpperCAmelCase						:  Union[str, Any]     					=	'''a photo of a cat'''
						_UpperCAmelCase						:  str     					=	{
						    '''editing_prompt''': ['''sunglasses'''],
						    '''reverse_editing_direction''': [False],
						    '''edit_warmup_steps''': 10,
						    '''edit_guidance_scale''': 6,
						    '''edit_threshold''': 0.95,
						    '''edit_momentum_scale''': 0.5,
						    '''edit_mom_beta''': 0.6,
						}

						_UpperCAmelCase						:  Dict     					=	3
						_UpperCAmelCase						:  int     					=	7

						# no sega enabled
						_UpperCAmelCase						:  str     					=	torch.Generator(UpperCAmelCase_					)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  str     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		)

						_UpperCAmelCase						:  str     					=	output.images
						_UpperCAmelCase						:  str     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  str     					=	[
						    0.34_887_695,
						    0.3_876_953,
						    0.375,
						    0.34_423_828,
						    0.3_581_543,
						    0.35_717_773,
						    0.3_383_789,
						    0.34_570_312,
						    0.359_375,
						]

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

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

						# with sega enabled
						# generator = torch.manual_seed(seed)
						generator.manual_seed(UpperCAmelCase_					)
						_UpperCAmelCase						:  Union[str, Any]     					=	pipe(
						    [prompt]       ,		generator=UpperCAmelCase_       ,		guidance_scale=UpperCAmelCase_       ,		num_inference_steps=50       ,		output_type='''np'''       ,		width=512       ,		height=512       ,		**UpperCAmelCase_       ,		)

						_UpperCAmelCase						:  str     					=	output.images
						_UpperCAmelCase						:  Dict     					=	image[0, -3:, -3:, -1]
						_UpperCAmelCase						:  List[str]     					=	[
						    0.42_285_156,
						    0.36_914_062,
						    0.29_077_148,
						    0.42_041_016,
						    0.41_918_945,
						    0.35_498_047,
						    0.3_618_164,
						    0.4_423_828,
						    0.43_115_234,
						]

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

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


'''simple docstring'''







from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
    import torch

lowerCAmelCase_      =   logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
    from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):







    def   lowerCAmelCase(  cls    :  Dict      ):







           '''simple docstring'''






           return cls







def   lowerCAmelCase(  a__    :  Union[Tuple, List]      , a__    :  Optional[Union[List["torch.Generator"], "torch.Generator"]] = None      , a__    :  Optional["torch.device"] = None      , a__    :  Optional["torch.dtype"] = None      , a__    :  Optional["torch.layout"] = None      , ):







       '''simple docstring'''






       lowerCamelCase__				   =      device
       lowerCamelCase__				   =      shape[0]

       lowerCamelCase__				   =      layout or torch.strided
       lowerCamelCase__				   =      device or torch.device("cpu"      )

       if generator is not None:
              lowerCamelCase__				   =      generator.device.type if not isinstance(a__      , a__      ) else generator[0].device.type
              if gen_device_type != device.type and gen_device_type == "cpu":
                     lowerCamelCase__				   =      "cpu"
                     if device != "mps":
                            logger.info(
                                f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
                                f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
                                f""" slighly speed up this function by passing a generator that was created on the {device} device."""      )
              elif gen_device_type != device.type and gen_device_type == "cuda":
                     raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}."""      )

       if isinstance(a__      , a__      ):
              lowerCamelCase__				   =      (1,) + shape[1:]
              lowerCamelCase__				   =      [
                  torch.randn(a__      , generator=generator[i]      , device=a__      , dtype=a__      , layout=a__      )
                  for i in range(a__      )
              ]
              lowerCamelCase__				   =      torch.cat(a__      , dim=0      ).to(a__      )
       else:
              lowerCamelCase__				   =      torch.randn(a__      , generator=a__      , device=a__      , dtype=a__      , layout=a__      ).to(a__      )

       return latents







def   lowerCAmelCase(  a__    :  Union[str, Any]      ):







       '''simple docstring'''






       if is_torch_version("<"      , "2.0.0"      ) or not hasattr(a__      , "_dynamo"      ):
              return False
       return isinstance(a__      , torch._dynamo.eval_frame.OptimizedModule      )

