





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


UpperCAmelCase					:		Dict		=							'CompVis/stable-diffusion-v1-1'
UpperCAmelCase					:		int		=							'CompVis/stable-diffusion-v1-2'
UpperCAmelCase					:		List[str]		=							'CompVis/stable-diffusion-v1-3'
UpperCAmelCase					:		List[Any]		=							'CompVis/stable-diffusion-v1-4'





class lowerCamelCase						(__a					):







  def __init__(		self     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__     ,     lowercase__ = True     ,     )		->						Optional[Any]:






        """simple docstring"""





        super()._init_()

        _snake_case		:	Tuple					     =     StableDiffusionPipeline.from_pretrained(a_		)
        _snake_case		:	int					     =     StableDiffusionPipeline.from_pretrained(a_		)
        _snake_case		:	Any					     =     StableDiffusionPipeline.from_pretrained(a_		)
        _snake_case		:	Any					     =     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 				UpperCAmelCase_				(		self		)		->						Union[str, Any]:






        """simple docstring"""





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







  def 				UpperCAmelCase_				(		self     ,     lowercase__ = "auto"		)		->						Any:






        """simple docstring"""





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







  def 				UpperCAmelCase_				(		self		)		->						int:






        """simple docstring"""





        self.enable_attention_slicing(a_		)







  @torch.no_grad()
  def 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = 512     ,     lowercase__ = 512     ,     lowercase__ = 50     ,     lowercase__ = 7.5     ,     lowercase__ = None     ,     lowercase__ = 1     ,     lowercase__ = 0.0     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = "pil"     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = 1     ,     **lowercase__     ,     )		->						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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = 512     ,     lowercase__ = 512     ,     lowercase__ = 50     ,     lowercase__ = 7.5     ,     lowercase__ = None     ,     lowercase__ = 1     ,     lowercase__ = 0.0     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = "pil"     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = 1     ,     **lowercase__     ,     )		->						List[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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = 512     ,     lowercase__ = 512     ,     lowercase__ = 50     ,     lowercase__ = 7.5     ,     lowercase__ = None     ,     lowercase__ = 1     ,     lowercase__ = 0.0     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = "pil"     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = 1     ,     **lowercase__     ,     )		->						List[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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = 512     ,     lowercase__ = 512     ,     lowercase__ = 50     ,     lowercase__ = 7.5     ,     lowercase__ = None     ,     lowercase__ = 1     ,     lowercase__ = 0.0     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = "pil"     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = 1     ,     **lowercase__     ,     )		->						Optional[int]:






        """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 				UpperCAmelCase_				(		self     ,     lowercase__     ,     lowercase__ = 512     ,     lowercase__ = 512     ,     lowercase__ = 50     ,     lowercase__ = 7.5     ,     lowercase__ = None     ,     lowercase__ = 1     ,     lowercase__ = 0.0     ,     lowercase__ = None     ,     lowercase__ = None     ,     lowercase__ = "pil"     ,     lowercase__ = True     ,     lowercase__ = None     ,     lowercase__ = 1     ,     **lowercase__     ,     )		->						List[Any]:






        """simple docstring"""





        _snake_case		:	int					     =     """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
        _snake_case		:	Dict					     =     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
        _snake_case		:	str					     =     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
        _snake_case		:	Optional[int]					     =     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
        _snake_case		:	Optional[int]					     =     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]]		)









'''simple docstring'''

from __future__ import annotations




def    _a    (							lowerCAmelCase_			,      lowerCAmelCase_ = None			,      lowerCAmelCase_ = None	):





      """simple docstring"""



      if start is None:
            _snake_case		:	Optional[Any]					     =     0

      if end is None:
            _snake_case		:	Any					     =     len(lowerCAmelCase_	) - 1

      if start >= end:
            return

      _snake_case		:	Optional[Any]					     =     (start + end) // 2

      slowsort(lowerCAmelCase_			,      lowerCAmelCase_			,      lowerCAmelCase_	)
      slowsort(lowerCAmelCase_			,      mid + 1			,      lowerCAmelCase_	)

      if sequence[end] < sequence[mid]:
            _snake_case     ,							_snake_case		:	int					     =     sequence[mid], sequence[end]

      slowsort(lowerCAmelCase_			,      lowerCAmelCase_			,      end - 1	)


if __name__ == "__main__":
   from doctest import testmod

   testmod()


