





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







													"""simple docstring"""
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

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







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

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

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







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      List[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     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       str:







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      List[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     UpperCAmelCase__	(							self							:     List[str]   )					->       Dict:







													"""simple docstring"""
													torch.manual_seed(0   )
													UpperCAmelCase_			:      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       ,     )
													return CLIPTextModel(__magic_name__   )

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







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


																			class 					__a      :

																										def __init__(							self							:     Any   )					->       Any:







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



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







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





																			return Out()

													return extract

							def     UpperCAmelCase__	(							self							:     str   )					->       int:







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

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

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

													UpperCAmelCase_			:      Optional[Any]     						=  '''A painting of a squirrel eating a burger'''

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

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

													UpperCAmelCase_			:      Optional[Any]     						=  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_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							:     Optional[Any]   )					->       int:







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

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

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

													UpperCAmelCase_			:      int     						=  output.images

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

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

													assert image.shape == (1, 64, 64, 3)
													UpperCAmelCase_			:      str     						=  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							:     Dict   )					->       Union[str, Any]:







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

													UpperCAmelCase_			:      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(__magic_name__   )
																			UpperCAmelCase_			:      Dict     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )

													# sanity check that the pipeline still works
													assert pipe.safety_checker is None
													UpperCAmelCase_			:      int     						=  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							:     List[Any]   )					->       int:







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

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

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

													UpperCAmelCase_			:      Union[str, Any]     						=  '''A painting of a squirrel eating a burger'''
													UpperCAmelCase_			:      Any     						=  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							:     Dict   )					->       str:







													"""simple docstring"""
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()

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







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

													UpperCAmelCase_			:      Tuple     						=  (
													    '''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_			:      Dict     						=  40_03_66_03_46
													UpperCAmelCase_			:      Optional[int]     						=  7

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

													UpperCAmelCase_			:      Optional[int]     						=  output.images
													UpperCAmelCase_			:      Union[str, Any]     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      Dict     						=  [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_			:      Union[str, Any]     						=  torch.manual_seed(__magic_name__   )
													UpperCAmelCase_			:      Any     						=  sd_pipe(
													    [prompt]       ,     generator=__magic_name__       ,     guidance_scale=__magic_name__       ,     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_			:      str     						=  output.images
													UpperCAmelCase_			:      Optional[int]     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      List[Any]     						=  [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							:     int   )					->       List[str]:







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

													UpperCAmelCase_			:      Any     						=  '''padme amidala taking a bath artwork, safe for work, no nudity'''
													UpperCAmelCase_			:      Union[str, Any]     						=  27_34_97_17_55
													UpperCAmelCase_			:      str     						=  7

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

													UpperCAmelCase_			:      str     						=  output.images
													UpperCAmelCase_			:      int     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      Dict     						=  [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_			:      Optional[Any]     						=  torch.manual_seed(__magic_name__   )
													UpperCAmelCase_			:      int     						=  sd_pipe(
													    [prompt]       ,     generator=__magic_name__       ,     guidance_scale=__magic_name__       ,     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[int]     						=  output.images
													UpperCAmelCase_			:      Union[str, Any]     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      Optional[Any]     						=  [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							:     Optional[int]   )					->       Optional[int]:







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

													UpperCAmelCase_			:      str     						=  (
													    '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.'''
													    ''' leyendecker'''
													)
													UpperCAmelCase_			:      Any     						=  10_44_35_52_34
													UpperCAmelCase_			:      Dict     						=  12

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

													UpperCAmelCase_			:      Union[str, Any]     						=  output.images
													UpperCAmelCase_			:      Optional[int]     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      List[Any]     						=  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_			:      Union[str, Any]     						=  torch.manual_seed(__magic_name__   )
													UpperCAmelCase_			:      Any     						=  sd_pipe(
													    [prompt]       ,     generator=__magic_name__       ,     guidance_scale=__magic_name__       ,     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_			:      Tuple     						=  output.images
													UpperCAmelCase_			:      Any     						=  image[0, -3:, -3:, -1]
													UpperCAmelCase_			:      Optional[int]     						=  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












'''simple docstring'''







def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			list[int]   ) ->	list[list[int]]:
						UpperCAmelCase_			:      int     						=  []
						if len(SCREAMING_SNAKE_CASE__   ) == 1:
												return [nums.copy()]
						for _ in range(len(SCREAMING_SNAKE_CASE__   )   ):
												UpperCAmelCase_			:      List[Any]     						=  nums.pop(0   )
												UpperCAmelCase_			:      Optional[Any]     						=  permute(SCREAMING_SNAKE_CASE__   )
												for perm in permutations:
																		perm.append(SCREAMING_SNAKE_CASE__   )
												result.extend(SCREAMING_SNAKE_CASE__   )
												nums.append(SCREAMING_SNAKE_CASE__   )
						return result





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			List[str]   ) ->	Any:

						def backtrack(SCREAMING_SNAKE_CASE__      :			Union[str, Any]   ):
												if start == len(SCREAMING_SNAKE_CASE__   ) - 1:
																		output.append(nums[:]   )
												else:
																		for i in range(SCREAMING_SNAKE_CASE__,			len(SCREAMING_SNAKE_CASE__   )   ):
																								UpperCAmelCase_						,       UpperCAmelCase_			:      Tuple     						=  nums[i], nums[start]
																								backtrack(start + 1   )
																								UpperCAmelCase_						,       UpperCAmelCase_			:      int     						=  nums[i], nums[start]  # backtrack

						UpperCAmelCase_			:      Optional[int]     						=  []
						backtrack(0   )
						return output


if __name__ == "__main__":
					import doctest

					# use res to print the data in permute2 function
					snake_case_     :       Tuple						=  permutea([1, 2, 3])
					print(res)
					doctest.testmod()





