





'''simple docstring'''







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import MobileViTImageProcessor


class 					__a      (unittest.TestCase     ):

							def __init__(							self							:     List[str]       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[int]=7       ,     __magic_name__							:     Tuple=3       ,     __magic_name__							:     Optional[Any]=18       ,     __magic_name__							:     Tuple=30       ,     __magic_name__							:     List[str]=4_00       ,     __magic_name__							:     Union[str, Any]=True       ,     __magic_name__							:     Tuple=None       ,     __magic_name__							:     Union[str, Any]=True       ,     __magic_name__							:     Optional[int]=None       ,     __magic_name__							:     Tuple=True       ,     )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  size if size is not None else {'''shortest_edge''': 20}
													UpperCAmelCase_			:      Union[str, Any]     						=  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
													UpperCAmelCase_			:      str     						=  parent
													UpperCAmelCase_			:      int     						=  batch_size
													UpperCAmelCase_			:      Tuple     						=  num_channels
													UpperCAmelCase_			:      Union[str, Any]     						=  image_size
													UpperCAmelCase_			:      Tuple     						=  min_resolution
													UpperCAmelCase_			:      Tuple     						=  max_resolution
													UpperCAmelCase_			:      Any     						=  do_resize
													UpperCAmelCase_			:      str     						=  size
													UpperCAmelCase_			:      Any     						=  do_center_crop
													UpperCAmelCase_			:      Any     						=  crop_size
													UpperCAmelCase_			:      List[Any]     						=  do_flip_channel_order



							def     UpperCAmelCase__	(							self							:     Dict   )					->       str:







													"""simple docstring"""
													return {
													    "do_resize": self.do_resize,
													    "size": self.size,
													    "do_center_crop": self.do_center_crop,
													    "crop_size": self.crop_size,
													    "do_flip_channel_order": self.do_flip_channel_order,
													}





@require_torch
@require_vision
class 					__a      (lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   int									=       MobileViTImageProcessor if is_vision_available() else None

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  MobileViTImageProcessingTester(self   )

							@property
							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       int:







													"""simple docstring"""
													return self.image_processor_tester.prepare_image_processor_dict()

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.image_processing_class(**self.image_processor_dict   )
													self.assertTrue(hasattr(__magic_name__       ,     '''do_resize'''   )   )
													self.assertTrue(hasattr(__magic_name__       ,     '''size'''   )   )
													self.assertTrue(hasattr(__magic_name__       ,     '''do_center_crop'''   )   )
													self.assertTrue(hasattr(__magic_name__       ,     '''center_crop'''   )   )
													self.assertTrue(hasattr(__magic_name__       ,     '''do_flip_channel_order'''   )   )

							def     UpperCAmelCase__	(							self							:     str   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  self.image_processing_class.from_dict(self.image_processor_dict   )
													self.assertEqual(image_processor.size       ,     {'''shortest_edge''': 20}   )
													self.assertEqual(image_processor.crop_size       ,     {'''height''': 18, '''width''': 18}   )

													UpperCAmelCase_			:      Union[str, Any]     						=  self.image_processing_class.from_dict(self.image_processor_dict       ,     size=42       ,     crop_size=84   )
													self.assertEqual(image_processor.size       ,     {'''shortest_edge''': 42}   )
													self.assertEqual(image_processor.crop_size       ,     {'''height''': 84, '''width''': 84}   )

							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       Dict:







													"""simple docstring"""
													pass

							def     UpperCAmelCase__	(							self							:     Dict   )					->       Union[str, Any]:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_			:      Optional[int]     						=  self.image_processing_class(**self.image_processor_dict   )
													# create random PIL images
													UpperCAmelCase_			:      Tuple     						=  prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=__magic_name__   )
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__       ,     Image.Image   )

													# Test not batched input
													UpperCAmelCase_			:      Tuple     						=  image_processing(image_inputs[0]       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )

													# Test batched
													UpperCAmelCase_			:      List[Any]     						=  image_processing(__magic_name__       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )

							def     UpperCAmelCase__	(							self							:     str   )					->       List[Any]:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_			:      Optional[Any]     						=  self.image_processing_class(**self.image_processor_dict   )
													# create random numpy tensors
													UpperCAmelCase_			:      str     						=  prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=__magic_name__       ,     numpify=__magic_name__   )
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__       ,     np.ndarray   )

													# Test not batched input
													UpperCAmelCase_			:      int     						=  image_processing(image_inputs[0]       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )

													# Test batched
													UpperCAmelCase_			:      Dict     						=  image_processing(__magic_name__       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )



							def     UpperCAmelCase__	(							self							:     Any   )					->       Dict:







													"""simple docstring"""
													# Initialize image_processing
													UpperCAmelCase_			:      int     						=  self.image_processing_class(**self.image_processor_dict   )
													# create random PyTorch tensors
													UpperCAmelCase_			:      int     						=  prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=__magic_name__       ,     torchify=__magic_name__   )
													for image in image_inputs:
																			self.assertIsInstance(__magic_name__       ,     torch.Tensor   )

													# Test not batched input
													UpperCAmelCase_			:      Any     						=  image_processing(image_inputs[0]       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        1,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )

													# Test batched
													UpperCAmelCase_			:      int     						=  image_processing(__magic_name__       ,     return_tensors='''pt'''   ).pixel_values
													self.assertEqual(
													    encoded_images.shape       ,     (
													        self.image_processor_tester.batch_size,
													        self.image_processor_tester.num_channels,
													        self.image_processor_tester.crop_size['''height'''],
													        self.image_processor_tester.crop_size['''width'''],
													    )       ,     )












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


snake_case_     :       Union[str, Any]						=  "CompVis/stable-diffusion-v1-1"
snake_case_     :       Dict						=  "CompVis/stable-diffusion-v1-2"
snake_case_     :       Any						=  "CompVis/stable-diffusion-v1-3"
snake_case_     :       str						=  "CompVis/stable-diffusion-v1-4"


class 					__a      (lowerCamelCase     ):

							def __init__(							self							:     Any       ,     __magic_name__							:     AutoencoderKL       ,     __magic_name__							:     CLIPTextModel       ,     __magic_name__							:     CLIPTokenizer       ,     __magic_name__							:     UNetaDConditionModel       ,     __magic_name__							:     Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]       ,     __magic_name__							:     StableDiffusionSafetyChecker       ,     __magic_name__							:     CLIPImageProcessor       ,     __magic_name__							:     bool = True       ,     )					->       str:







													"""simple docstring"""
													super()._init_()

													UpperCAmelCase_			:      Any     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      Dict     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      Tuple     						=  StableDiffusionPipeline(
													    vae=__magic_name__       ,     text_encoder=__magic_name__       ,     tokenizer=__magic_name__       ,     unet=__magic_name__       ,     scheduler=__magic_name__       ,     safety_checker=__magic_name__       ,     feature_extractor=__magic_name__       ,     requires_safety_checker=__magic_name__       ,     )

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

							@property
							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Dict[str, Any]:







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

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Optional[Union[str, int]] = "auto"   )					->       int:







													"""simple docstring"""
													if slice_size == "auto":
																			# half the attention head size is usually a good trade-off between
																			# speed and memory
																			UpperCAmelCase_			:      List[str]     						=  self.unet.config.attention_head_dim // 2
													self.unet.set_attention_slice(__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       List[str]:







													"""simple docstring"""
													self.enable_attention_slicing(__magic_name__   )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Tuple       ,     )					->       Optional[int]:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Any       ,     )					->       Any:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Dict       ,     )					->       List[str]:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Optional[int]       ,     )					->       str:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )



							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Optional[int]       ,     )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  '''cuda''' if torch.cuda.is_available() else '''cpu'''
													self.to(__magic_name__   )

													# 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
													UpperCAmelCase_			:      Optional[int]     						=  self.textaimg_sda_a(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

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

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

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

													# 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]]   )





