





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


_lowerCamelCase   				=			'CompVis/stable-diffusion-v1-1'
_lowerCamelCase   				=			'CompVis/stable-diffusion-v1-2'
_lowerCamelCase   				=			'CompVis/stable-diffusion-v1-3'
_lowerCamelCase   				=			'CompVis/stable-diffusion-v1-4'




class   UpperCamelCase_       (     UpperCamelCase__    ):


							def __init__(							self    :Union[str, Any]							,				__A    :AutoencoderKL							,				__A    :CLIPTextModel							,				__A    :CLIPTokenizer							,				__A    :UNetaDConditionModel							,				__A    :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]							,				__A    :StableDiffusionSafetyChecker							,				__A    :CLIPImageProcessor							,				__A    :bool = True							,				)   ->   Dict:



									"""simple docstring"""



									super()._init_()

									SCREAMING_SNAKE_CASE__			=      StableDiffusionPipeline.from_pretrained(__A		)
									SCREAMING_SNAKE_CASE__			=      StableDiffusionPipeline.from_pretrained(__A		)
									SCREAMING_SNAKE_CASE__			=      StableDiffusionPipeline.from_pretrained(__A		)
									SCREAMING_SNAKE_CASE__			=      StableDiffusionPipeline(
									    vae=__A							,				text_encoder=__A							,				tokenizer=__A							,				unet=__A							,				scheduler=__A							,				safety_checker=__A							,				feature_extractor=__A							,				requires_safety_checker=__A							,				)

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


							@property
							def      _snake_case  (							self    :Optional[int]		)   ->   Dict[str, Any]:



									"""simple docstring"""



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


							def      _snake_case  (							self    :str							,				__A    :Optional[Union[str, int]] = "auto"		)   ->   Dict:



									"""simple docstring"""



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


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



									"""simple docstring"""



									self.enable_attention_slicing(__A		)


							@torch.no_grad()
							def      _snake_case  (							self    :Dict							,				__A    :Union[str, List[str]]							,				__A    :int = 512							,				__A    :int = 512							,				__A    :int = 50							,				__A    :float = 7.5							,				__A    :Optional[Union[str, List[str]]] = None							,				__A    :Optional[int] = 1							,				__A    :float = 0.0							,				__A    :Optional[torch.Generator] = None							,				__A    :Optional[torch.FloatTensor] = None							,				__A    :Optional[str] = "pil"							,				__A    :bool = True							,				__A    :Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,				__A    :int = 1							,				**__A    :Tuple							,				)   ->   str:



									"""simple docstring"""



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


							@torch.no_grad()
							def      _snake_case  (							self    :int							,				__A    :Union[str, List[str]]							,				__A    :int = 512							,				__A    :int = 512							,				__A    :int = 50							,				__A    :float = 7.5							,				__A    :Optional[Union[str, List[str]]] = None							,				__A    :Optional[int] = 1							,				__A    :float = 0.0							,				__A    :Optional[torch.Generator] = None							,				__A    :Optional[torch.FloatTensor] = None							,				__A    :Optional[str] = "pil"							,				__A    :bool = True							,				__A    :Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,				__A    :int = 1							,				**__A    :Optional[Any]							,				)   ->   Any:



									"""simple docstring"""



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


							@torch.no_grad()
							def      _snake_case  (							self    :List[Any]							,				__A    :Union[str, List[str]]							,				__A    :int = 512							,				__A    :int = 512							,				__A    :int = 50							,				__A    :float = 7.5							,				__A    :Optional[Union[str, List[str]]] = None							,				__A    :Optional[int] = 1							,				__A    :float = 0.0							,				__A    :Optional[torch.Generator] = None							,				__A    :Optional[torch.FloatTensor] = None							,				__A    :Optional[str] = "pil"							,				__A    :bool = True							,				__A    :Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,				__A    :int = 1							,				**__A    :Union[str, Any]							,				)   ->   Any:



									"""simple docstring"""



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


							@torch.no_grad()
							def      _snake_case  (							self    :Union[str, Any]							,				__A    :Union[str, List[str]]							,				__A    :int = 512							,				__A    :int = 512							,				__A    :int = 50							,				__A    :float = 7.5							,				__A    :Optional[Union[str, List[str]]] = None							,				__A    :Optional[int] = 1							,				__A    :float = 0.0							,				__A    :Optional[torch.Generator] = None							,				__A    :Optional[torch.FloatTensor] = None							,				__A    :Optional[str] = "pil"							,				__A    :bool = True							,				__A    :Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,				__A    :int = 1							,				**__A    :List[str]							,				)   ->   Any:



									"""simple docstring"""



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


							@torch.no_grad()
							def      _snake_case  (							self    :List[Any]							,				__A    :Union[str, List[str]]							,				__A    :int = 512							,				__A    :int = 512							,				__A    :int = 50							,				__A    :float = 7.5							,				__A    :Optional[Union[str, List[str]]] = None							,				__A    :Optional[int] = 1							,				__A    :float = 0.0							,				__A    :Optional[torch.Generator] = None							,				__A    :Optional[torch.FloatTensor] = None							,				__A    :Optional[str] = "pil"							,				__A    :bool = True							,				__A    :Optional[Callable[[int, int, torch.FloatTensor], None]] = None							,				__A    :int = 1							,				**__A    :str							,				)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      """cuda""" if torch.cuda.is_available() else """cpu"""
									self.to(__A		)

									# 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
									SCREAMING_SNAKE_CASE__			=      self.textaimg_sda_a(
									    prompt=__A							,				height=__A							,				width=__A							,				num_inference_steps=__A							,				guidance_scale=__A							,				negative_prompt=__A							,				num_images_per_prompt=__A							,				eta=__A							,				generator=__A							,				latents=__A							,				output_type=__A							,				return_dict=__A							,				callback=__A							,				callback_steps=__A							,				**__A							,				)

									# Get first result from Stable Diffusion Checkpoint v1.2
									SCREAMING_SNAKE_CASE__			=      self.textaimg_sda_a(
									    prompt=__A							,				height=__A							,				width=__A							,				num_inference_steps=__A							,				guidance_scale=__A							,				negative_prompt=__A							,				num_images_per_prompt=__A							,				eta=__A							,				generator=__A							,				latents=__A							,				output_type=__A							,				return_dict=__A							,				callback=__A							,				callback_steps=__A							,				**__A							,				)

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

									# Get first result from Stable Diffusion Checkpoint v1.4
									SCREAMING_SNAKE_CASE__			=      self.textaimg_sda_a(
									    prompt=__A							,				height=__A							,				width=__A							,				num_inference_steps=__A							,				guidance_scale=__A							,				negative_prompt=__A							,				num_images_per_prompt=__A							,				eta=__A							,				generator=__A							,				latents=__A							,				output_type=__A							,				return_dict=__A							,				callback=__A							,				callback_steps=__A							,				**__A							,				)

									# 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]]		)






from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase   				=			logging.get_logger(__name__)




class   UpperCamelCase_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					"timm_backbone"


							def __init__(							self    :Union[str, Any]							,				__A    :str=None							,				__A    :Union[str, Any]=3							,				__A    :str=True							,				__A    :Any=True							,				__A    :Optional[Any]=None							,				**__A    :List[str]							,				)   ->   Tuple:



									"""simple docstring"""



									super().__init__(**__A		)
									SCREAMING_SNAKE_CASE__			=      backbone
									SCREAMING_SNAKE_CASE__			=      num_channels
									SCREAMING_SNAKE_CASE__			=      features_only
									SCREAMING_SNAKE_CASE__			=      use_pretrained_backbone
									SCREAMING_SNAKE_CASE__			=      True
									SCREAMING_SNAKE_CASE__			=      out_indices if out_indices is not None else (-1,)