

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


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





class    lowerCamelCase						(lowercase__			):

   def __init__(					self				:						int    ,				__UpperCAmelCase				:						str    ,				__UpperCAmelCase				:						Optional[Any]    ,				__UpperCAmelCase				:						List[Any]    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Optional[int]    ,				__UpperCAmelCase				:						str    ,				__UpperCAmelCase				:						Union[str, Any]    ,				__UpperCAmelCase				:						Optional[int] = True    ,				)   ->    Optional[Any]:
      super()._init_()

      SCREAMING_SNAKE_CASE__								=      StableDiffusionPipeline.from_pretrained(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      StableDiffusionPipeline.from_pretrained(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      StableDiffusionPipeline.from_pretrained(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      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 					SCREAMING_SNAKE_CASE				(					self				:						Union[str, Any]       )   ->    int:
      return {k: getattr(self    ,				__UpperCAmelCase       ) for k in self.config.keys() if not k.startswith("""_"""       )}

   def 					SCREAMING_SNAKE_CASE				(					self				:						Union[str, Any]    ,				__UpperCAmelCase				:						str = "auto"       )   ->    Tuple:
      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(__UpperCAmelCase       )

   def 					SCREAMING_SNAKE_CASE				(					self				:						int       )   ->    Optional[int]:
      self.enable_attention_slicing(__UpperCAmelCase       )

   @torch.no_grad()
   def 					SCREAMING_SNAKE_CASE				(					self				:						Dict    ,				__UpperCAmelCase				:						Optional[Any]    ,				__UpperCAmelCase				:						str = 5_1_2    ,				__UpperCAmelCase				:						Optional[int] = 5_1_2    ,				__UpperCAmelCase				:						Optional[int] = 5_0    ,				__UpperCAmelCase				:						Optional[Any] = 7.5    ,				__UpperCAmelCase				:						List[Any] = None    ,				__UpperCAmelCase				:						Any = 1    ,				__UpperCAmelCase				:						Union[str, Any] = 0.0    ,				__UpperCAmelCase				:						Tuple = None    ,				__UpperCAmelCase				:						List[str] = None    ,				__UpperCAmelCase				:						Any = "pil"    ,				__UpperCAmelCase				:						Optional[Any] = True    ,				__UpperCAmelCase				:						int = None    ,				__UpperCAmelCase				:						Dict = 1    ,				**__UpperCAmelCase				:						Optional[Any]    ,				)   ->    Union[str, Any]:
      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 					SCREAMING_SNAKE_CASE				(					self				:						int    ,				__UpperCAmelCase				:						Dict    ,				__UpperCAmelCase				:						int = 5_1_2    ,				__UpperCAmelCase				:						Optional[int] = 5_1_2    ,				__UpperCAmelCase				:						List[str] = 5_0    ,				__UpperCAmelCase				:						Dict = 7.5    ,				__UpperCAmelCase				:						List[Any] = None    ,				__UpperCAmelCase				:						List[Any] = 1    ,				__UpperCAmelCase				:						int = 0.0    ,				__UpperCAmelCase				:						Dict = None    ,				__UpperCAmelCase				:						Union[str, Any] = None    ,				__UpperCAmelCase				:						Optional[Any] = "pil"    ,				__UpperCAmelCase				:						Optional[int] = True    ,				__UpperCAmelCase				:						str = None    ,				__UpperCAmelCase				:						Optional[int] = 1    ,				**__UpperCAmelCase				:						Tuple    ,				)   ->    int:
      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 					SCREAMING_SNAKE_CASE				(					self				:						List[str]    ,				__UpperCAmelCase				:						Optional[Any]    ,				__UpperCAmelCase				:						Union[str, Any] = 5_1_2    ,				__UpperCAmelCase				:						Optional[Any] = 5_1_2    ,				__UpperCAmelCase				:						Optional[Any] = 5_0    ,				__UpperCAmelCase				:						str = 7.5    ,				__UpperCAmelCase				:						Optional[int] = None    ,				__UpperCAmelCase				:						Optional[int] = 1    ,				__UpperCAmelCase				:						Optional[Any] = 0.0    ,				__UpperCAmelCase				:						Tuple = None    ,				__UpperCAmelCase				:						int = None    ,				__UpperCAmelCase				:						Dict = "pil"    ,				__UpperCAmelCase				:						List[Any] = True    ,				__UpperCAmelCase				:						Tuple = None    ,				__UpperCAmelCase				:						List[str] = 1    ,				**__UpperCAmelCase				:						Tuple    ,				)   ->    Union[str, Any]:
      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 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]    ,				__UpperCAmelCase				:						Optional[int]    ,				__UpperCAmelCase				:						Optional[int] = 5_1_2    ,				__UpperCAmelCase				:						Optional[Any] = 5_1_2    ,				__UpperCAmelCase				:						Dict = 5_0    ,				__UpperCAmelCase				:						int = 7.5    ,				__UpperCAmelCase				:						Optional[Any] = None    ,				__UpperCAmelCase				:						str = 1    ,				__UpperCAmelCase				:						List[str] = 0.0    ,				__UpperCAmelCase				:						str = None    ,				__UpperCAmelCase				:						Tuple = None    ,				__UpperCAmelCase				:						str = "pil"    ,				__UpperCAmelCase				:						Optional[int] = True    ,				__UpperCAmelCase				:						Any = None    ,				__UpperCAmelCase				:						str = 1    ,				**__UpperCAmelCase				:						Dict    ,				)   ->    Union[str, Any]:
      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 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						List[str] = 5_1_2    ,				__UpperCAmelCase				:						Optional[int] = 5_1_2    ,				__UpperCAmelCase				:						List[Any] = 5_0    ,				__UpperCAmelCase				:						Any = 7.5    ,				__UpperCAmelCase				:						Optional[int] = None    ,				__UpperCAmelCase				:						Tuple = 1    ,				__UpperCAmelCase				:						Dict = 0.0    ,				__UpperCAmelCase				:						Optional[int] = None    ,				__UpperCAmelCase				:						Tuple = None    ,				__UpperCAmelCase				:						Any = "pil"    ,				__UpperCAmelCase				:						Union[str, Any] = True    ,				__UpperCAmelCase				:						Any = None    ,				__UpperCAmelCase				:						Union[str, Any] = 1    ,				**__UpperCAmelCase				:						str    ,				)   ->    List[Any]:
      SCREAMING_SNAKE_CASE__								=      """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
      SCREAMING_SNAKE_CASE__								=      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
      SCREAMING_SNAKE_CASE__								=      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
      SCREAMING_SNAKE_CASE__								=      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
      SCREAMING_SNAKE_CASE__								=      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"""




import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
						import onnxruntime as ort





@nightly
@require_onnxruntime
@require_torch_gpu
class    lowerCamelCase						(unittest.TestCase			):

			@property
			def 					SCREAMING_SNAKE_CASE				(					self				:						Dict       )   ->    str:
						return (
						    "CUDAExecutionProvider",
						    {
						        "gpu_mem_limit": "15000000000",  # 15GB
						        "arena_extend_strategy": "kSameAsRequested",
						    },
						)

			@property
			def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple       )   ->    Optional[Any]:
						SCREAMING_SNAKE_CASE__								=      ort.SessionOptions()
						SCREAMING_SNAKE_CASE__								=      False
						return options





			def 					SCREAMING_SNAKE_CASE				(					self				:						Dict       )   ->    Any:
						SCREAMING_SNAKE_CASE__								=      load_image(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
						    """/in_paint/overture-creations-5sI6fQgYIuo.png"""       )
						SCREAMING_SNAKE_CASE__								=      load_image(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
						    """/in_paint/overture-creations-5sI6fQgYIuo_mask.png"""       )
						SCREAMING_SNAKE_CASE__								=      load_numpy(
						    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
						    """/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy"""       )

						# using the PNDM scheduler by default
						SCREAMING_SNAKE_CASE__								=      OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
						    """CompVis/stable-diffusion-v1-4"""    ,				revision="""onnx"""    ,				safety_checker=__UpperCAmelCase    ,				feature_extractor=__UpperCAmelCase    ,				provider=self.gpu_provider    ,				sess_options=self.gpu_options    ,				)
						pipe.set_progress_bar_config(disable=__UpperCAmelCase       )

						SCREAMING_SNAKE_CASE__								=      """A red cat sitting on a park bench"""

						SCREAMING_SNAKE_CASE__								=      np.random.RandomState(0       )
						SCREAMING_SNAKE_CASE__								=      pipe(
						    prompt=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				mask_image=__UpperCAmelCase    ,				strength=0.75    ,				guidance_scale=7.5    ,				num_inference_steps=1_5    ,				generator=__UpperCAmelCase    ,				output_type="""np"""    ,				)

						SCREAMING_SNAKE_CASE__								=      output.images[0]

						assert image.shape == (5_1_2, 5_1_2, 3)
						assert np.abs(expected_image - image       ).max() < 1e-2


