


'''simple docstring'''
from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


__snake_case							   =  '''CompVis/stable-diffusion-v1-1'''
__snake_case							   =  '''CompVis/stable-diffusion-v1-2'''
__snake_case							   =  '''CompVis/stable-diffusion-v1-3'''
__snake_case							   =  '''CompVis/stable-diffusion-v1-4'''
class      lowercase			(							A__      ):

		"""simple docstring"""

		def __init__(	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = True					,     ):

							'''simple docstring'''


							super()._init_()

							UpperCamelCase__     :List[str]							      =				StableDiffusionPipeline.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__     :str							      =				StableDiffusionPipeline.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__     :Tuple							      =				StableDiffusionPipeline.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				StableDiffusionPipeline(
							    vae=UpperCamelCase_					,     text_encoder=UpperCamelCase_					,     tokenizer=UpperCamelCase_					,     unet=UpperCamelCase_					,     scheduler=UpperCamelCase_					,     safety_checker=UpperCamelCase_					,     feature_extractor=UpperCamelCase_					,     requires_safety_checker=UpperCamelCase_					,     )

							self.register_modules(pipelinea=self.pipea					,     pipelinea=self.pipea					,     pipelinea=self.pipea					,     pipelinea=self.pipea							)




		@property
		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							return {k: getattr(self					,     UpperCamelCase_							) for k in self.config.keys() if not k.startswith('''_'''							)}




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_ = "auto"							):

							'''simple docstring'''


							if slice_size == "auto":
												# half the attention head size is usually a good trade-off between
												# speed and memory
												UpperCamelCase__     :int							      =				self.unet.config.attention_head_dim // 2
							self.unet.set_attention_slice(UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							self.enable_attention_slicing(UpperCamelCase_							)




		@torch.no_grad()
		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 50					,     UpperCamelCase_ = 7.5					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     UpperCamelCase_ = 0.0					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = "pil"					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return self.pipea(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )




		@torch.no_grad()
		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 50					,     UpperCamelCase_ = 7.5					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     UpperCamelCase_ = 0.0					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = "pil"					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return self.pipea(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )




		@torch.no_grad()
		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 50					,     UpperCamelCase_ = 7.5					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     UpperCamelCase_ = 0.0					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = "pil"					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return self.pipea(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )




		@torch.no_grad()
		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 50					,     UpperCamelCase_ = 7.5					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     UpperCamelCase_ = 0.0					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = "pil"					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							return self.pipea(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )




		@torch.no_grad()
		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 512					,     UpperCamelCase_ = 50					,     UpperCamelCase_ = 7.5					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     UpperCamelCase_ = 0.0					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = "pil"					,     UpperCamelCase_ = True					,     UpperCamelCase_ = None					,     UpperCamelCase_ = 1					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				'''cuda''' if torch.cuda.is_available() else '''cpu'''
							self.to(UpperCamelCase_							)

							# Checks if the height and width are divisible by 8 or not
							if height % 8 != 0 or width % 8 != 0:
												raise ValueError(F'''`height` and `width` must be divisible by 8 but are {height} and {width}.'''							)

							# Get first result from Stable Diffusion Checkpoint v1.1
							UpperCamelCase__     :List[Any]							      =				self.textaimg_sda_a(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )

							# Get first result from Stable Diffusion Checkpoint v1.2
							UpperCamelCase__     :Optional[int]							      =				self.textaimg_sda_a(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )

							# Get first result from Stable Diffusion Checkpoint v1.3
							UpperCamelCase__     :Dict							      =				self.textaimg_sda_a(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )

							# Get first result from Stable Diffusion Checkpoint v1.4
							UpperCamelCase__     :Union[str, Any]							      =				self.textaimg_sda_a(
							    prompt=UpperCamelCase_					,     height=UpperCamelCase_					,     width=UpperCamelCase_					,     num_inference_steps=UpperCamelCase_					,     guidance_scale=UpperCamelCase_					,     negative_prompt=UpperCamelCase_					,     num_images_per_prompt=UpperCamelCase_					,     eta=UpperCamelCase_					,     generator=UpperCamelCase_					,     latents=UpperCamelCase_					,     output_type=UpperCamelCase_					,     return_dict=UpperCamelCase_					,     callback=UpperCamelCase_					,     callback_steps=UpperCamelCase_					,     **UpperCamelCase_					,     )

							# Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
							return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]							)



'''simple docstring'''
from .integrations import (
    is_optuna_available,
    is_ray_available,
    is_sigopt_available,
    is_wandb_available,
    run_hp_search_optuna,
    run_hp_search_ray,
    run_hp_search_sigopt,
    run_hp_search_wandb,
)
from .trainer_utils import (
    HPSearchBackend,
    default_hp_space_optuna,
    default_hp_space_ray,
    default_hp_space_sigopt,
    default_hp_space_wandb,
)
from .utils import logging


__snake_case							   =  logging.get_logger(__name__)
class      lowercase			:

		"""simple docstring"""

		_a									=       42
		_a									=       None




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							raise NotImplementedError




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							if not self.is_available():
												raise RuntimeError(
												    F'''You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}.'''							)




		@classmethod
		def 					lowerCAmelCase__       (	cls							):

							'''simple docstring'''


							return F'''`pip install {cls.pip_package or cls.name}`'''



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'optuna'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_optuna_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_optuna(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_optuna(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'ray'
		_a									=       '\'ray[tune]\''




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_ray_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_ray(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_ray(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'sigopt'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_sigopt_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_sigopt(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_sigopt(UpperCamelCase_							)



class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'wandb'




		@staticmethod
		def 					lowerCAmelCase__       (	):

							'''simple docstring'''


							return is_wandb_available()




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return run_hp_search_wandb(UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_							):

							'''simple docstring'''


							return default_hp_space_wandb(UpperCamelCase_							)



__snake_case							   =  {
    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
}


def     a   (    )						->			str:



					'''simple docstring'''






					UpperCamelCase__     :List[Any]							      =				[backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
					if len(__a     ) > 0:
										UpperCamelCase__     :Tuple							      =				available_backends[0].name
										if len(__a     ) > 1:
															logger.info(
															    f'''{len(__a     )} hyperparameter search backends available. Using {name} as the default.'''     )
										return name
					raise RuntimeError(
					    '''No hyperparameter search backend available.\n'''
					    + '''\n'''.join(
					        f''' - To install {backend.name} run {backend.pip_install()}'''
					        for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()     )     )