



"""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


lowercase__		:    Dict				   =     """CompVis/stable-diffusion-v1-1"""
lowercase__		:    Any				   =     """CompVis/stable-diffusion-v1-2"""
lowercase__		:    str				   =     """CompVis/stable-diffusion-v1-3"""
lowercase__		:    List[Any]				   =     """CompVis/stable-diffusion-v1-4"""







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      def __init__(				self    :				Union[str, Any]   ,      SCREAMING_SNAKE_CASE_    :				AutoencoderKL   ,      SCREAMING_SNAKE_CASE_    :				CLIPTextModel   ,      SCREAMING_SNAKE_CASE_    :				CLIPTokenizer   ,      SCREAMING_SNAKE_CASE_    :				UNetaDConditionModel   ,      SCREAMING_SNAKE_CASE_    :				Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]   ,      SCREAMING_SNAKE_CASE_    :				StableDiffusionSafetyChecker   ,      SCREAMING_SNAKE_CASE_    :				CLIPImageProcessor   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      ):
        super()._init_()

        lowerCAmelCase_       :    Union[str, Any]								=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    int								=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Optional[int]								=			StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    int								=			StableDiffusionPipeline(
            vae=SCREAMING_SNAKE_CASE_   ,      text_encoder=SCREAMING_SNAKE_CASE_   ,      tokenizer=SCREAMING_SNAKE_CASE_   ,      unet=SCREAMING_SNAKE_CASE_   ,      scheduler=SCREAMING_SNAKE_CASE_   ,      safety_checker=SCREAMING_SNAKE_CASE_   ,      feature_extractor=SCREAMING_SNAKE_CASE_   ,      requires_safety_checker=SCREAMING_SNAKE_CASE_   ,      )

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



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        return {k: getattr(self   ,      SCREAMING_SNAKE_CASE_	) for k in self.config.keys() if not k.startswith('_'	)}



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, int]] = "auto"	):
        if slice_size == "auto":
          # half the attention head size is usually a good trade-off between
          # speed and memory
          lowerCAmelCase_       :    List[str]								=			self.unet.config.attention_head_dim // 2
        self.unet.set_attention_slice(SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        self.enable_attention_slicing(SCREAMING_SNAKE_CASE_	)



      @torch.no_grad()
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Union[str, List[str]]   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_0   ,      SCREAMING_SNAKE_CASE_    :				float = 7.5   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, List[str]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = 1   ,      SCREAMING_SNAKE_CASE_    :				float = 0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.Generator] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.FloatTensor] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[str] = "pil"   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 1   ,      **SCREAMING_SNAKE_CASE_    :				List[Any]   ,      ):
        return self.pipea(
            prompt=SCREAMING_SNAKE_CASE_   ,      height=SCREAMING_SNAKE_CASE_   ,      width=SCREAMING_SNAKE_CASE_   ,      num_inference_steps=SCREAMING_SNAKE_CASE_   ,      guidance_scale=SCREAMING_SNAKE_CASE_   ,      negative_prompt=SCREAMING_SNAKE_CASE_   ,      num_images_per_prompt=SCREAMING_SNAKE_CASE_   ,      eta=SCREAMING_SNAKE_CASE_   ,      generator=SCREAMING_SNAKE_CASE_   ,      latents=SCREAMING_SNAKE_CASE_   ,      output_type=SCREAMING_SNAKE_CASE_   ,      return_dict=SCREAMING_SNAKE_CASE_   ,      callback=SCREAMING_SNAKE_CASE_   ,      callback_steps=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )



      @torch.no_grad()
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Union[str, List[str]]   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_0   ,      SCREAMING_SNAKE_CASE_    :				float = 7.5   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, List[str]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = 1   ,      SCREAMING_SNAKE_CASE_    :				float = 0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.Generator] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.FloatTensor] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[str] = "pil"   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 1   ,      **SCREAMING_SNAKE_CASE_    :				Dict   ,      ):
        return self.pipea(
            prompt=SCREAMING_SNAKE_CASE_   ,      height=SCREAMING_SNAKE_CASE_   ,      width=SCREAMING_SNAKE_CASE_   ,      num_inference_steps=SCREAMING_SNAKE_CASE_   ,      guidance_scale=SCREAMING_SNAKE_CASE_   ,      negative_prompt=SCREAMING_SNAKE_CASE_   ,      num_images_per_prompt=SCREAMING_SNAKE_CASE_   ,      eta=SCREAMING_SNAKE_CASE_   ,      generator=SCREAMING_SNAKE_CASE_   ,      latents=SCREAMING_SNAKE_CASE_   ,      output_type=SCREAMING_SNAKE_CASE_   ,      return_dict=SCREAMING_SNAKE_CASE_   ,      callback=SCREAMING_SNAKE_CASE_   ,      callback_steps=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )



      @torch.no_grad()
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Union[str, List[str]]   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_0   ,      SCREAMING_SNAKE_CASE_    :				float = 7.5   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, List[str]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = 1   ,      SCREAMING_SNAKE_CASE_    :				float = 0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.Generator] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.FloatTensor] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[str] = "pil"   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 1   ,      **SCREAMING_SNAKE_CASE_    :				Tuple   ,      ):
        return self.pipea(
            prompt=SCREAMING_SNAKE_CASE_   ,      height=SCREAMING_SNAKE_CASE_   ,      width=SCREAMING_SNAKE_CASE_   ,      num_inference_steps=SCREAMING_SNAKE_CASE_   ,      guidance_scale=SCREAMING_SNAKE_CASE_   ,      negative_prompt=SCREAMING_SNAKE_CASE_   ,      num_images_per_prompt=SCREAMING_SNAKE_CASE_   ,      eta=SCREAMING_SNAKE_CASE_   ,      generator=SCREAMING_SNAKE_CASE_   ,      latents=SCREAMING_SNAKE_CASE_   ,      output_type=SCREAMING_SNAKE_CASE_   ,      return_dict=SCREAMING_SNAKE_CASE_   ,      callback=SCREAMING_SNAKE_CASE_   ,      callback_steps=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )



      @torch.no_grad()
      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				Union[str, List[str]]   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_0   ,      SCREAMING_SNAKE_CASE_    :				float = 7.5   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, List[str]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = 1   ,      SCREAMING_SNAKE_CASE_    :				float = 0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.Generator] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.FloatTensor] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[str] = "pil"   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 1   ,      **SCREAMING_SNAKE_CASE_    :				List[str]   ,      ):
        return self.pipea(
            prompt=SCREAMING_SNAKE_CASE_   ,      height=SCREAMING_SNAKE_CASE_   ,      width=SCREAMING_SNAKE_CASE_   ,      num_inference_steps=SCREAMING_SNAKE_CASE_   ,      guidance_scale=SCREAMING_SNAKE_CASE_   ,      negative_prompt=SCREAMING_SNAKE_CASE_   ,      num_images_per_prompt=SCREAMING_SNAKE_CASE_   ,      eta=SCREAMING_SNAKE_CASE_   ,      generator=SCREAMING_SNAKE_CASE_   ,      latents=SCREAMING_SNAKE_CASE_   ,      output_type=SCREAMING_SNAKE_CASE_   ,      return_dict=SCREAMING_SNAKE_CASE_   ,      callback=SCREAMING_SNAKE_CASE_   ,      callback_steps=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )





      @torch.no_grad()
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Union[str, List[str]]   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_1_2   ,      SCREAMING_SNAKE_CASE_    :				int = 5_0   ,      SCREAMING_SNAKE_CASE_    :				float = 7.5   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, List[str]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = 1   ,      SCREAMING_SNAKE_CASE_    :				float = 0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.Generator] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[torch.FloatTensor] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[str] = "pil"   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Callable[[int, int, torch.FloatTensor], None]] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 1   ,      **SCREAMING_SNAKE_CASE_    :				str   ,      ):
        lowerCAmelCase_       :    Tuple								=			'cuda' if torch.cuda.is_available() else 'cpu'
        self.to(SCREAMING_SNAKE_CASE_	)

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

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

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

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

        # 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"""







import logging

from transformers.configuration_utils import PretrainedConfig


lowercase__		:    List[Any]				   =     logging.getLogger(__name__)







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""masked_bert"""



      def __init__(				self    :				Any   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=3_0_5_2_2   ,      SCREAMING_SNAKE_CASE_    :				int=7_6_8   ,      SCREAMING_SNAKE_CASE_    :				Tuple=1_2   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=1_2   ,      SCREAMING_SNAKE_CASE_    :				Tuple=3_0_7_2   ,      SCREAMING_SNAKE_CASE_    :				List[str]="gelu"   ,      SCREAMING_SNAKE_CASE_    :				Tuple=0.1   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=0.1   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=5_1_2   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2   ,      SCREAMING_SNAKE_CASE_    :				Dict=0.02   ,      SCREAMING_SNAKE_CASE_    :				Any=1E-12   ,      SCREAMING_SNAKE_CASE_    :				List[str]=0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]="topK"   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]="constant"   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=0.0   ,      **SCREAMING_SNAKE_CASE_    :				Any   ,      ):
        super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)

        lowerCAmelCase_       :    List[Any]								=			vocab_size
        lowerCAmelCase_       :    str								=			hidden_size
        lowerCAmelCase_       :    Optional[int]								=			num_hidden_layers
        lowerCAmelCase_       :    Dict								=			num_attention_heads
        lowerCAmelCase_       :    List[str]								=			hidden_act
        lowerCAmelCase_       :    List[Any]								=			intermediate_size
        lowerCAmelCase_       :    Any								=			hidden_dropout_prob
        lowerCAmelCase_       :    str								=			attention_probs_dropout_prob
        lowerCAmelCase_       :    Any								=			max_position_embeddings
        lowerCAmelCase_       :    Dict								=			type_vocab_size
        lowerCAmelCase_       :    Tuple								=			initializer_range
        lowerCAmelCase_       :    List[Any]								=			layer_norm_eps
        lowerCAmelCase_       :    str								=			pruning_method
        lowerCAmelCase_       :    Optional[Any]								=			mask_init
        lowerCAmelCase_       :    int								=			mask_scale





