


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







class 						_snake_case    ( _snake_case				):




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase = True   ,    ):
       super()._init_()

       a		:Dict									=			StableDiffusionPipeline.from_pretrained(_lowerCamelCase       )
       a		:Tuple									=			StableDiffusionPipeline.from_pretrained(_lowerCamelCase       )
       a		:Dict									=			StableDiffusionPipeline.from_pretrained(_lowerCamelCase       )
       a		:int									=			StableDiffusionPipeline(
           vae=_lowerCamelCase   ,    text_encoder=_lowerCamelCase   ,    tokenizer=_lowerCamelCase   ,    unet=_lowerCamelCase   ,    scheduler=_lowerCamelCase   ,    safety_checker=_lowerCamelCase   ,    feature_extractor=_lowerCamelCase   ,    requires_safety_checker=_lowerCamelCase   ,    )

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




 @property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return {k: getattr(self   ,    _lowerCamelCase       ) for k in self.config.keys() if not k.startswith('''_'''       )}




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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.enable_attention_slicing(_lowerCamelCase       )




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




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




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




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




 @torch.no_grad()
 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = 512   ,    _lowerCamelCase = 512   ,    _lowerCamelCase = 50   ,    _lowerCamelCase = 7.5   ,    _lowerCamelCase = None   ,    _lowerCamelCase = 1   ,    _lowerCamelCase = 0.0   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = "pil"   ,    _lowerCamelCase = True   ,    _lowerCamelCase = None   ,    _lowerCamelCase = 1   ,    **_lowerCamelCase   ,    ):
       a		:Union[str, Any]									=			'''cuda''' if torch.cuda.is_available() else '''cpu'''
       self.to(_lowerCamelCase       )

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

       # Get first result from Stable Diffusion Checkpoint v1.2
       a		:List[Any]									=			self.textaimg_sda_a(
           prompt=_lowerCamelCase   ,    height=_lowerCamelCase   ,    width=_lowerCamelCase   ,    num_inference_steps=_lowerCamelCase   ,    guidance_scale=_lowerCamelCase   ,    negative_prompt=_lowerCamelCase   ,    num_images_per_prompt=_lowerCamelCase   ,    eta=_lowerCamelCase   ,    generator=_lowerCamelCase   ,    latents=_lowerCamelCase   ,    output_type=_lowerCamelCase   ,    return_dict=_lowerCamelCase   ,    callback=_lowerCamelCase   ,    callback_steps=_lowerCamelCase   ,    **_lowerCamelCase   ,    )

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

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

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









from dataclasses import dataclass
from typing import Optional, Tuple

import torch
from torch import nn
from transformers import RobertaPreTrainedModel, XLMRobertaConfig, XLMRobertaModel
from transformers.utils import ModelOutput







@dataclass
class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			None
 SCREAMING_SNAKE_CASE__ =			None
 SCREAMING_SNAKE_CASE__ =			None
 SCREAMING_SNAKE_CASE__ =			None







class 						_snake_case    ( _snake_case				):




 def __init__(						self   ,    _lowerCamelCase=1   ,    _lowerCamelCase=0   ,    _lowerCamelCase=2   ,    _lowerCamelCase=512   ,    _lowerCamelCase="cls"   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    **_lowerCamelCase   ,    ):
       super().__init__(pad_token_id=_lowerCamelCase   ,    bos_token_id=_lowerCamelCase   ,    eos_token_id=_lowerCamelCase   ,    **_lowerCamelCase       )
       a		:Tuple									=			project_dim
       a		:Optional[int]									=			pooler_fn
       a		:int									=			learn_encoder
       a		:int									=			use_attention_mask







class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			[r'pooler', r'logit_scale']
 SCREAMING_SNAKE_CASE__ =			[r'position_ids', r'predictions.decoder.bias']
 SCREAMING_SNAKE_CASE__ =			'roberta'
 SCREAMING_SNAKE_CASE__ =			RobertaSeriesConfig




 def __init__(						self   ,    _lowerCamelCase       ):
       super().__init__(_lowerCamelCase       )
       a		:Tuple									=			XLMRobertaModel(_lowerCamelCase       )
       a		:Optional[Any]									=			nn.Linear(config.hidden_size   ,    config.project_dim       )
       a		:Optional[int]									=			getattr(_lowerCamelCase   ,    '''has_pre_transformation'''   ,    _lowerCamelCase       )
       if self.has_pre_transformation:
             a		:Tuple									=			nn.Linear(config.hidden_size   ,    config.project_dim       )
             a		:Union[str, Any]									=			nn.LayerNorm(config.hidden_size   ,    eps=config.layer_norm_eps       )
       self.post_init()




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    _lowerCamelCase = None   ,    ):
       a		:Optional[int]									=			return_dict if return_dict is not None else self.config.use_return_dict

       a		:int									=			self.base_model(
           input_ids=_lowerCamelCase   ,    attention_mask=_lowerCamelCase   ,    token_type_ids=_lowerCamelCase   ,    position_ids=_lowerCamelCase   ,    head_mask=_lowerCamelCase   ,    inputs_embeds=_lowerCamelCase   ,    encoder_hidden_states=_lowerCamelCase   ,    encoder_attention_mask=_lowerCamelCase   ,    output_attentions=_lowerCamelCase   ,    output_hidden_states=True if self.has_pre_transformation else output_hidden_states   ,    return_dict=_lowerCamelCase   ,    )

       if self.has_pre_transformation:
             a		:Optional[int]									=			outputs['''hidden_states'''][-2]
             a		:List[Any]									=			self.pre_LN(_lowerCamelCase       )
             a		:Optional[Any]									=			self.transformation_pre(_lowerCamelCase       )

             return TransformationModelOutput(
                 projection_state=_lowerCamelCase   ,    last_hidden_state=outputs.last_hidden_state   ,    hidden_states=outputs.hidden_states   ,    attentions=outputs.attentions   ,    )
       else:
             a		:List[str]									=			self.transformation(outputs.last_hidden_state       )
             return TransformationModelOutput(
                 projection_state=_lowerCamelCase   ,    last_hidden_state=outputs.last_hidden_state   ,    hidden_states=outputs.hidden_states   ,    attentions=outputs.attentions   ,    )





