
'''simple docstring'''







from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch







@require_torch
@require_tf
class UpperCAmelCase__	( a_	):





  """simple docstring"""

  def   __lowercase		(		self      :     Optional[int]       ):


     '''simple docstring'''






     _a				:	Optional[Any]			=   SMALL_MODEL_IDENTIFIER
     _a				:	Any			=   "pt"
     _a				:	Optional[int]			=   "tf"


  def   __lowercase		(		self      :     Optional[Any]							,_a      :     List[Any]       ):


     '''simple docstring'''






     _a				:	Optional[int]			=   AutoModel.from_pretrained(self.test_model       )
     model_pt.save_pretrained(_a       )


  def   __lowercase		(		self      :     Optional[Any]							,_a      :     Dict       ):


     '''simple docstring'''






     _a				:	Union[str, Any]			=   TFAutoModel.from_pretrained(self.test_model							,from_pt=_a       )
     model_tf.save_pretrained(_a       )


  def   __lowercase		(		self      :     str       ):


     '''simple docstring'''






     _a				:	int			=   "mock_framework"

     # Framework provided - return whatever the user provides
     _a				:	Optional[int]			=   FeaturesManager.determine_framework(self.test_model							,_a       )
     self.assertEqual(_a							,_a       )

     # Local checkpoint and framework provided - return provided framework
     # PyTorch checkpoint
     with TemporaryDirectory() as local_pt_ckpt:
        self._setup_pt_ckpt(_a       )
        _a				:	List[Any]			=   FeaturesManager.determine_framework(_a							,_a       )
        self.assertEqual(_a							,_a       )

     # TensorFlow checkpoint
     with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(_a       )
        _a				:	str			=   FeaturesManager.determine_framework(_a							,_a       )
        self.assertEqual(_a							,_a       )


  def   __lowercase		(		self      :     List[str]       ):


     '''simple docstring'''






     with TemporaryDirectory() as local_pt_ckpt:
        self._setup_pt_ckpt(_a       )
        _a				:	Optional[Any]			=   FeaturesManager.determine_framework(_a       )
        self.assertEqual(_a							,self.framework_pt       )

     # TensorFlow checkpoint
     with TemporaryDirectory() as local_tf_ckpt:
        self._setup_tf_ckpt(_a       )
        _a				:	List[Any]			=   FeaturesManager.determine_framework(_a       )
        self.assertEqual(_a							,self.framework_tf       )

     # Invalid local checkpoint
     with TemporaryDirectory() as local_invalid_ckpt:
        with self.assertRaises(_a       ):
           _a				:	Tuple			=   FeaturesManager.determine_framework(_a       )


  def   __lowercase		(		self      :     Tuple       ):


     '''simple docstring'''






     _a				:	Dict			=   MagicMock(return_value=_a       )
     with patch('transformers.onnx.features.is_tf_available'							,_a       ):
        _a				:	Optional[int]			=   FeaturesManager.determine_framework(self.test_model       )
        self.assertEqual(_a							,self.framework_pt       )

     # PyTorch not in environment -> use TensorFlow
     _a				:	Optional[int]			=   MagicMock(return_value=_a       )
     with patch('transformers.onnx.features.is_torch_available'							,_a       ):
        _a				:	int			=   FeaturesManager.determine_framework(self.test_model       )
        self.assertEqual(_a							,self.framework_tf       )

     # Both in environment -> use PyTorch
     _a				:	int			=   MagicMock(return_value=_a       )
     _a				:	List[str]			=   MagicMock(return_value=_a       )
     with patch('transformers.onnx.features.is_tf_available'							,_a       ), patch(
         'transformers.onnx.features.is_torch_available'							,_a       ):
        _a				:	Any			=   FeaturesManager.determine_framework(self.test_model       )
        self.assertEqual(_a							,self.framework_pt       )

     # Both not in environment -> raise error
     _a				:	List[str]			=   MagicMock(return_value=_a       )
     _a				:	Dict			=   MagicMock(return_value=_a       )
     with patch('transformers.onnx.features.is_tf_available'							,_a       ), patch(
         'transformers.onnx.features.is_torch_available'							,_a       ):
        with self.assertRaises(_a       ):
           _a				:	Optional[Any]			=   FeaturesManager.determine_framework(self.test_model       )





"""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			:       int   		=       """CompVis/stable-diffusion-v1-1"""
_A			:       Any   		=       """CompVis/stable-diffusion-v1-2"""
_A			:       Optional[int]   		=       """CompVis/stable-diffusion-v1-3"""
_A			:       Union[str, Any]   		=       """CompVis/stable-diffusion-v1-4"""

class 		a__						(      a_						):
   def __init__(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a = True	,    ):
          super()._init_()

          lowercase    :				Optional[Any]       		=							StableDiffusionPipeline.from_pretrained(_a				)
          lowercase    :				str       		=							StableDiffusionPipeline.from_pretrained(_a				)
          lowercase    :				Dict       		=							StableDiffusionPipeline.from_pretrained(_a				)
          lowercase    :				Union[str, 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 			__magic_name__			(			self				):
          return {k: getattr(self	,    _a				) for k in self.config.keys() if not k.startswith("_"				)}
   def 			__magic_name__			(			self	,    _a = "auto"				):
          if slice_size == "auto":
                 # half the attention head size is usually a good trade-off between
                 # speed and memory
                 lowercase    :				str       		=							self.unet.config.attention_head_dim // 2
          self.unet.set_attention_slice(_a				)
   def 			__magic_name__			(			self				):
          self.enable_attention_slicing(_a				)
   @torch.no_grad()
   def 			__magic_name__			(			self	,    _a	,    _a = 512	,    _a = 512	,    _a = 50	,    _a = 7.5	,    _a = None	,    _a = 1	,    _a = 0.0	,    _a = None	,    _a = None	,    _a = "pil"	,    _a = True	,    _a = None	,    _a = 1	,    **_a	,    ):
          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 			__magic_name__			(			self	,    _a	,    _a = 512	,    _a = 512	,    _a = 50	,    _a = 7.5	,    _a = None	,    _a = 1	,    _a = 0.0	,    _a = None	,    _a = None	,    _a = "pil"	,    _a = True	,    _a = None	,    _a = 1	,    **_a	,    ):
          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 			__magic_name__			(			self	,    _a	,    _a = 512	,    _a = 512	,    _a = 50	,    _a = 7.5	,    _a = None	,    _a = 1	,    _a = 0.0	,    _a = None	,    _a = None	,    _a = "pil"	,    _a = True	,    _a = None	,    _a = 1	,    **_a	,    ):
          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 			__magic_name__			(			self	,    _a	,    _a = 512	,    _a = 512	,    _a = 50	,    _a = 7.5	,    _a = None	,    _a = 1	,    _a = 0.0	,    _a = None	,    _a = None	,    _a = "pil"	,    _a = True	,    _a = None	,    _a = 1	,    **_a	,    ):
          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 			__magic_name__			(			self	,    _a	,    _a = 512	,    _a = 512	,    _a = 50	,    _a = 7.5	,    _a = None	,    _a = 1	,    _a = 0.0	,    _a = None	,    _a = None	,    _a = "pil"	,    _a = True	,    _a = None	,    _a = 1	,    **_a	,    ):
          lowercase    :				List[Any]       		=							"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
          lowercase    :				List[Any]       		=							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
          lowercase    :				Any       		=							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
          lowercase    :				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.4
          lowercase    :				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]]				)


