




'''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_      :		Dict							      =       """CompVis/stable-diffusion-v1-1"""
a_      :		str							      =       """CompVis/stable-diffusion-v1-2"""
a_      :		Any							      =       """CompVis/stable-diffusion-v1-3"""
a_      :		str							      =       """CompVis/stable-diffusion-v1-4"""


class   __UpperCamelCase       ( lowerCamelCase__				):






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase = True,		):



              """simple docstring"""







              super()._init_()

              lowerCamelCase_							=StableDiffusionPipeline.from_pretrained(lowerCAmelCase  )
              lowerCamelCase_							=StableDiffusionPipeline.from_pretrained(lowerCAmelCase  )
              lowerCamelCase_							=StableDiffusionPipeline.from_pretrained(lowerCAmelCase  )
              lowerCamelCase_							=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      lowercase__     (							self  ):



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase = "auto"  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







              self.enable_attention_slicing(lowerCAmelCase  )






       @torch.no_grad()
       def      lowercase__     (							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,		):



              """simple docstring"""







              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      lowercase__     (							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,		):



              """simple docstring"""







              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      lowercase__     (							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,		):



              """simple docstring"""







              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      lowercase__     (							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,		):



              """simple docstring"""







              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      lowercase__     (							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,		):



              """simple docstring"""







              lowerCamelCase_							='''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
              lowerCamelCase_							=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
              lowerCamelCase_							=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
              lowerCamelCase_							=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
              lowerCamelCase_							=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]]  )





'''simple docstring'''




import math

import flax.linen as nn
import jax.numpy as jnp






def 				snake_case__							(			_A:					jnp.ndarray      ,   _A:					int      ,   _A:					float = 1      ,   _A:					float = 1      ,   _A:					float = 1.0e4      ,   _A:					bool = False      ,   _A:					float = 1.0      ,   )    ->	jnp.ndarray:

		'''simple docstring'''


		assert timesteps.ndim == 1, "Timesteps should be a 1d-array"
		assert embedding_dim % 2 == 0, f"Embedding dimension {embedding_dim} should be even"
		lowerCAmelCase   					=  float(embedding_dim // 2 )
		lowerCAmelCase   					=  math.log(max_timescale / min_timescale ) / (num_timescales - freq_shift)
		lowerCAmelCase   					=  min_timescale * jnp.exp(jnp.arange(_A      ,   dtype=jnp.floataa ) * -log_timescale_increment )
		lowerCAmelCase   					=  jnp.expand_dims(_A      ,   1 ) * jnp.expand_dims(_A      ,   0 )

		# scale embeddings
		lowerCAmelCase   					=  scale * emb

		if flip_sin_to_cos:
				lowerCAmelCase   					=  jnp.concatenate([jnp.cos(_A ), jnp.sin(_A )]      ,   axis=1 )
		else:
				lowerCAmelCase   					=  jnp.concatenate([jnp.sin(_A ), jnp.cos(_A )]      ,   axis=1 )
		lowerCAmelCase   					=  jnp.reshape(_A      ,   [jnp.shape(_A )[0], embedding_dim] )
		return signal






class 				a__(					nn.Module	):


			'''simple docstring'''






			UpperCAmelCase_			:     int      =       3_2
			UpperCAmelCase_			:     jnp.dtype      =       jnp.floataa



			@nn.compact
			def __call__(   self       ,    __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  nn.Dense(self.time_embed_dim       ,    dtype=self.dtype       ,    name="""linear_1""")(__lowerCAmelCase)
					lowerCAmelCase   					=  nn.silu(__lowerCAmelCase)
					lowerCAmelCase   					=  nn.Dense(self.time_embed_dim       ,    dtype=self.dtype       ,    name="""linear_2""")(__lowerCAmelCase)
					return temb







class 				a__(					nn.Module	):


			'''simple docstring'''






			UpperCAmelCase_			:     int      =       3_2
			UpperCAmelCase_			:     bool      =       False
			UpperCAmelCase_			:     float      =       1



			@nn.compact
			def __call__(   self       ,    __lowerCAmelCase):




					"""simple docstring"""


					return get_sinusoidal_embeddings(
					    __lowerCAmelCase       ,    embedding_dim=self.dim       ,    flip_sin_to_cos=self.flip_sin_to_cos       ,    freq_shift=self.freq_shift)




