






import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
 import jax.numpy as jnp

if is_tf_available():
 import tensorflow as tf

if is_torch_available():
 import torch


class     _a							(	unittest.TestCase     ):






  """simple docstring"""





  def  _UpperCAmelCase  (  self			)      ->			str:
     UpperCamelCase_						=  {
         'task_specific_params': {
             'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4},
             'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4},
             'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6},
         }
     }
     UpperCamelCase_						=  {
         'task_specific_params.summarization.length_penalty': 1.0,
         'task_specific_params.summarization.max_length': 128,
         'task_specific_params.summarization.min_length': 12,
         'task_specific_params.summarization.num_beams': 4,
         'task_specific_params.summarization_cnn.length_penalty': 2.0,
         'task_specific_params.summarization_cnn.max_length': 142,
         'task_specific_params.summarization_cnn.min_length': 56,
         'task_specific_params.summarization_cnn.num_beams': 4,
         'task_specific_params.summarization_xsum.length_penalty': 1.0,
         'task_specific_params.summarization_xsum.max_length': 62,
         'task_specific_params.summarization_xsum.min_length': 11,
         'task_specific_params.summarization_xsum.num_beams': 6,
     }

     self.assertEqual(flatten_dict(_UpperCAmelCase			)		,		_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase			)		,		x.transpose()			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			)		,		x.transpose((1, 2, 0)			)			)			)




  @require_torch
  def  _UpperCAmelCase  (  self			)      ->			int:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase			)		,		transpose(_UpperCAmelCase			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			)		,		transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			).numpy()			)			)




  @require_tf
  def  _UpperCAmelCase  (  self			)      ->			Optional[int]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase			)		,		transpose(_UpperCAmelCase			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			)		,		transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			).numpy()			)			)




  @require_flax
  def  _UpperCAmelCase  (  self			)      ->			Any:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase			)		,		np.asarray(transpose(_UpperCAmelCase			)			)			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			)		,		np.asarray(transpose(_UpperCAmelCase		,		axes=(1, 2, 0)			)			)			)			)




  def  _UpperCAmelCase  (  self			)      ->			Tuple:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(4, 3)			)		,		np.reshape(_UpperCAmelCase		,		(4, 3)			)			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(12, 5)			)		,		np.reshape(_UpperCAmelCase		,		(12, 5)			)			)			)




  @require_torch
  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(4, 3)			)		,		reshape(_UpperCAmelCase		,		(4, 3)			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(12, 5)			)		,		reshape(_UpperCAmelCase		,		(12, 5)			).numpy()			)			)




  @require_tf
  def  _UpperCAmelCase  (  self			)      ->			List[Any]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(4, 3)			)		,		reshape(_UpperCAmelCase		,		(4, 3)			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(12, 5)			)		,		reshape(_UpperCAmelCase		,		(12, 5)			).numpy()			)			)




  @require_flax
  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(4, 3)			)		,		np.asarray(reshape(_UpperCAmelCase		,		(4, 3)			)			)			)			)

     UpperCamelCase_						=  np.random.randn(3		,		4		,		5			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(reshape(_UpperCAmelCase		,		(12, 5)			)		,		np.asarray(reshape(_UpperCAmelCase		,		(12, 5)			)			)			)			)




  def  _UpperCAmelCase  (  self			)      ->			int:
     UpperCamelCase_						=  np.random.randn(1		,		3		,		4			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase			)		,		np.squeeze(_UpperCAmelCase			)			)			)

     UpperCamelCase_						=  np.random.randn(1		,		4		,		1		,		5			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase		,		axis=2			)		,		np.squeeze(_UpperCAmelCase		,		axis=2			)			)			)




  @require_torch
  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     UpperCamelCase_						=  np.random.randn(1		,		3		,		4			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase			)		,		squeeze(_UpperCAmelCase			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(1		,		4		,		1		,		5			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase		,		axis=2			)		,		squeeze(_UpperCAmelCase		,		axis=2			).numpy()			)			)




  @require_tf
  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_						=  np.random.randn(1		,		3		,		4			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase			)		,		squeeze(_UpperCAmelCase			).numpy()			)			)

     UpperCamelCase_						=  np.random.randn(1		,		4		,		1		,		5			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase		,		axis=2			)		,		squeeze(_UpperCAmelCase		,		axis=2			).numpy()			)			)




  @require_flax
  def  _UpperCAmelCase  (  self			)      ->			int:
     UpperCamelCase_						=  np.random.randn(1		,		3		,		4			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase			)		,		np.asarray(squeeze(_UpperCAmelCase			)			)			)			)

     UpperCamelCase_						=  np.random.randn(1		,		4		,		1		,		5			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(squeeze(_UpperCAmelCase		,		axis=2			)		,		np.asarray(squeeze(_UpperCAmelCase		,		axis=2			)			)			)			)




  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase		,		axis=1			)		,		np.expand_dims(_UpperCAmelCase		,		axis=1			)			)			)




  @require_torch
  def  _UpperCAmelCase  (  self			)      ->			List[str]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  torch.tensor(_UpperCAmelCase			)
     self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase		,		axis=1			)		,		expand_dims(_UpperCAmelCase		,		axis=1			).numpy()			)			)




  @require_tf
  def  _UpperCAmelCase  (  self			)      ->			Optional[int]:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  tf.constant(_UpperCAmelCase			)
     self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase		,		axis=1			)		,		expand_dims(_UpperCAmelCase		,		axis=1			).numpy()			)			)




  @require_flax
  def  _UpperCAmelCase  (  self			)      ->			Any:
     UpperCamelCase_						=  np.random.randn(3		,		4			)
     UpperCamelCase_						=  jnp.array(_UpperCAmelCase			)
     self.assertTrue(np.allclose(expand_dims(_UpperCAmelCase		,		axis=1			)		,		np.asarray(expand_dims(_UpperCAmelCase		,		axis=1			)			)			)			)













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    				=   "CompVis/stable-diffusion-v1-1"
lowercase    				=   "CompVis/stable-diffusion-v1-2"
lowercase    				=   "CompVis/stable-diffusion-v1-3"
lowercase    				=   "CompVis/stable-diffusion-v1-4"




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	def __init__(			self			,     a			,     a			,     a			,     a			,     a			,     a			,     a			,     a = True			,     )			->       int:
								super()._init_()

								snake_case_           =				StableDiffusionPipeline.from_pretrained(a   )
								snake_case_           =				StableDiffusionPipeline.from_pretrained(a   )
								snake_case_           =				StableDiffusionPipeline.from_pretrained(a   )
								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 				_UpperCamelCase     (			self   )			->       Dict[str, Any]:
								return {k: getattr(self			,     a   ) for k in self.config.keys() if not k.startswith('_'   )}




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




	def 				_UpperCamelCase     (			self   )			->       List[Any]:
								self.enable_attention_slicing(a   )




	@torch.no_grad()
	def 				_UpperCamelCase     (			self			,     a			,     a = 5_12			,     a = 5_12			,     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			,     )			->       Optional[Any]:
								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			,     a			,     a = 5_12			,     a = 5_12			,     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			,     )			->       int:
								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			,     a			,     a = 5_12			,     a = 5_12			,     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			,     )			->       Optional[int]:
								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			,     a			,     a = 5_12			,     a = 5_12			,     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			,     )			->       Any:
								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			,     a			,     a = 5_12			,     a = 5_12			,     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			,     )			->       Optional[Any]:
								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
								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
								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
								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
								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]]   )





