
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  : List[Any]						   =							"""CompVis/stable-diffusion-v1-1"""
lowercase  : List[Any]						   =							"""CompVis/stable-diffusion-v1-2"""
lowercase  : Optional[int]						   =							"""CompVis/stable-diffusion-v1-3"""
lowercase  : Tuple						   =							"""CompVis/stable-diffusion-v1-4"""

class __snake_case		(				lowerCAmelCase					):
    def __init__(  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case = True		,):



           '''simple docstring'''







           super()._init_()

           lowercase							:					Dict	     =				StableDiffusionPipeline.from_pretrained(snake_case				)
           lowercase							:					List[str]	     =				StableDiffusionPipeline.from_pretrained(snake_case				)
           lowercase							:					Optional[int]	     =				StableDiffusionPipeline.from_pretrained(snake_case				)
           lowercase							:					str	     =				StableDiffusionPipeline(
               vae=snake_case		,text_encoder=snake_case		,tokenizer=snake_case		,unet=snake_case		,scheduler=snake_case		,safety_checker=snake_case		,feature_extractor=snake_case		,requires_safety_checker=snake_case		,)

           self.register_modules(pipelinea=self.pipea		,pipelinea=self.pipea		,pipelinea=self.pipea		,pipelinea=self.pipea				)
    @property
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return {k: getattr(self		,snake_case				) for k in self.config.keys() if not k.startswith("""_"""				)}
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case = "auto"				):



           '''simple docstring'''







           if slice_size == "auto":
                  # half the attention head size is usually a good trade-off between
                  # speed and memory
                  lowercase							:					List[Any]	     =				self.unet.config.attention_head_dim // 2
           self.unet.set_attention_slice(snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           self.enable_attention_slicing(snake_case				)
    @torch.no_grad()
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case = 512		,snake_case = 512		,snake_case = 50		,snake_case = 7.5		,snake_case = None		,snake_case = 1		,snake_case = 0.0		,snake_case = None		,snake_case = None		,snake_case = "pil"		,snake_case = True		,snake_case = None		,snake_case = 1		,**snake_case		,):



           '''simple docstring'''







           return self.pipea(
               prompt=snake_case		,height=snake_case		,width=snake_case		,num_inference_steps=snake_case		,guidance_scale=snake_case		,negative_prompt=snake_case		,num_images_per_prompt=snake_case		,eta=snake_case		,generator=snake_case		,latents=snake_case		,output_type=snake_case		,return_dict=snake_case		,callback=snake_case		,callback_steps=snake_case		,**snake_case		,)
    @torch.no_grad()
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case = 512		,snake_case = 512		,snake_case = 50		,snake_case = 7.5		,snake_case = None		,snake_case = 1		,snake_case = 0.0		,snake_case = None		,snake_case = None		,snake_case = "pil"		,snake_case = True		,snake_case = None		,snake_case = 1		,**snake_case		,):



           '''simple docstring'''







           return self.pipea(
               prompt=snake_case		,height=snake_case		,width=snake_case		,num_inference_steps=snake_case		,guidance_scale=snake_case		,negative_prompt=snake_case		,num_images_per_prompt=snake_case		,eta=snake_case		,generator=snake_case		,latents=snake_case		,output_type=snake_case		,return_dict=snake_case		,callback=snake_case		,callback_steps=snake_case		,**snake_case		,)
    @torch.no_grad()
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case = 512		,snake_case = 512		,snake_case = 50		,snake_case = 7.5		,snake_case = None		,snake_case = 1		,snake_case = 0.0		,snake_case = None		,snake_case = None		,snake_case = "pil"		,snake_case = True		,snake_case = None		,snake_case = 1		,**snake_case		,):



           '''simple docstring'''







           return self.pipea(
               prompt=snake_case		,height=snake_case		,width=snake_case		,num_inference_steps=snake_case		,guidance_scale=snake_case		,negative_prompt=snake_case		,num_images_per_prompt=snake_case		,eta=snake_case		,generator=snake_case		,latents=snake_case		,output_type=snake_case		,return_dict=snake_case		,callback=snake_case		,callback_steps=snake_case		,**snake_case		,)
    @torch.no_grad()
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case = 512		,snake_case = 512		,snake_case = 50		,snake_case = 7.5		,snake_case = None		,snake_case = 1		,snake_case = 0.0		,snake_case = None		,snake_case = None		,snake_case = "pil"		,snake_case = True		,snake_case = None		,snake_case = 1		,**snake_case		,):



           '''simple docstring'''







           return self.pipea(
               prompt=snake_case		,height=snake_case		,width=snake_case		,num_inference_steps=snake_case		,guidance_scale=snake_case		,negative_prompt=snake_case		,num_images_per_prompt=snake_case		,eta=snake_case		,generator=snake_case		,latents=snake_case		,output_type=snake_case		,return_dict=snake_case		,callback=snake_case		,callback_steps=snake_case		,**snake_case		,)
    @torch.no_grad()
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case = 512		,snake_case = 512		,snake_case = 50		,snake_case = 7.5		,snake_case = None		,snake_case = 1		,snake_case = 0.0		,snake_case = None		,snake_case = None		,snake_case = "pil"		,snake_case = True		,snake_case = None		,snake_case = 1		,**snake_case		,):



           '''simple docstring'''







           lowercase							:					List[Any]	     =				"""cuda""" if torch.cuda.is_available() else """cpu"""
           self.to(snake_case				)

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

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

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

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

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


import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class __snake_case		(				lowerCAmelCase ,     unittest.TestCase					):
    _a		:						Optional[int]=    None
    _a		:						Optional[Any]=    BloomTokenizerFast
    _a		:						Tuple=    BloomTokenizerFast
    _a		:						str=    True
    _a		:						Optional[int]=    False
    _a		:						List[Any]=    "tokenizer_file"
    _a		:						List[Any]=    {"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>"}
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           super().setUp()
           lowercase							:					Optional[Any]	     =				BloomTokenizerFast.from_pretrained("""bigscience/tokenizer"""				)
           tokenizer.save_pretrained(self.tmpdirname				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,**snake_case				):



           '''simple docstring'''







           kwargs.update(self.special_tokens_map				)
           return BloomTokenizerFast.from_pretrained(self.tmpdirname		,**snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					Tuple	     =				self.get_rust_tokenizer()

           lowercase							:					List[str]	     =				["""The quick brown fox</s>""", """jumps over the lazy dog</s>"""]
           lowercase							:					Optional[int]	     =				[[2175, 23714, 73173, 144252, 2], [77, 132619, 3478, 368, 109586, 35433, 2]]

           lowercase							:					Any	     =				tokenizer.batch_encode_plus(snake_case				)["""input_ids"""]
           self.assertListEqual(snake_case		,snake_case				)

           lowercase							:					Optional[int]	     =				tokenizer.batch_decode(snake_case				)
           self.assertListEqual(snake_case		,snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case=6				):



           '''simple docstring'''







           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                  with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"				):
                         lowercase							:					Dict	     =				self.rust_tokenizer_class.from_pretrained(snake_case		,**snake_case				)
                         # tokenizer_r.pad_token = None # Hotfixing padding = None
                         # Simple input
                         lowercase							:					Dict	     =				"""This is a simple input"""
                         lowercase							:					Tuple	     =				["""This is a simple input 1""", """This is a simple input 2"""]
                         lowercase							:					Dict	     =				("""This is a simple input""", """This is a pair""")
                         lowercase							:					Optional[Any]	     =				[
                             ("""This is a simple input 1""", """This is a simple input 2"""),
                             ("""This is a simple pair 1""", """This is a simple pair 2"""),
                         ]

                         # Simple input tests
                         try:
                                tokenizer_r.encode(snake_case		,max_length=snake_case				)
                                tokenizer_r.encode_plus(snake_case		,max_length=snake_case				)

                                tokenizer_r.batch_encode_plus(snake_case		,max_length=snake_case				)
                                tokenizer_r.encode(snake_case		,max_length=snake_case				)
                                tokenizer_r.batch_encode_plus(snake_case		,max_length=snake_case				)
                         except ValueError:
                                self.fail("""Bloom Tokenizer should be able to deal with padding"""				)

                         lowercase							:					Optional[int]	     =				None  # Hotfixing padding = None
                         self.assertRaises(snake_case		,tokenizer_r.encode		,snake_case		,max_length=snake_case		,padding="""max_length"""				)

                         # Simple input
                         self.assertRaises(snake_case		,tokenizer_r.encode_plus		,snake_case		,max_length=snake_case		,padding="""max_length"""				)

                         # Simple input
                         self.assertRaises(
                             snake_case		,tokenizer_r.batch_encode_plus		,snake_case		,max_length=snake_case		,padding="""max_length"""		,)

                         # Pair input
                         self.assertRaises(snake_case		,tokenizer_r.encode		,snake_case		,max_length=snake_case		,padding="""max_length"""				)

                         # Pair input
                         self.assertRaises(snake_case		,tokenizer_r.encode_plus		,snake_case		,max_length=snake_case		,padding="""max_length"""				)

                         # Pair input
                         self.assertRaises(
                             snake_case		,tokenizer_r.batch_encode_plus		,snake_case		,max_length=snake_case		,padding="""max_length"""		,)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					List[Any]	     =				self.get_rust_tokenizer()
           lowercase							:					List[str]	     =				load_dataset("""xnli"""		,"""all_languages"""		,split="""test"""		,streaming=snake_case				)

           lowercase							:					Optional[Any]	     =				next(iter(snake_case				)				)["""premise"""]  # pick up one data
           lowercase							:					str	     =				list(sample_data.values()				)

           lowercase							:					Optional[int]	     =				list(map(tokenizer.encode		,snake_case				)				)
           lowercase							:					Dict	     =				[tokenizer.decode(snake_case		,clean_up_tokenization_spaces=snake_case				) for x in output_tokens]
           self.assertListEqual(snake_case		,snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map				)		,1				)
           self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()				)[0]				)		,1				)
