




'''simple docstring'''
import math
from typing import Optional

import numpy as np
import torch
from torch import nn

from .activations import get_activation
def 				snake_case_  (   a__		:	torch.Tensor    ,a__		:	int    ,a__		:	bool = False    ,a__		:	float = 1    ,a__		:	float = 1    ,a__		:	int = 1_00_00    ,):



    """simple docstring"""




    assert len(timesteps.shape			) == 1, "Timesteps should be a 1d-array"

    __lowercase							=						embedding_dim // 2
    __lowercase							=						-math.log(a__			) * torch.arange(
        start=0    ,end=a__    ,dtype=torch.floataa    ,device=timesteps.device			)
    __lowercase							=						exponent / (half_dim - downscale_freq_shift)

    __lowercase							=						torch.exp(a__			)
    __lowercase							=						timesteps[:, None].float() * emb[None, :]

    # scale embeddings
    __lowercase							=						scale * emb

    # concat sine and cosine embeddings
    __lowercase							=						torch.cat([torch.sin(a__			), torch.cos(a__			)]    ,dim=-1			)

    # flip sine and cosine embeddings
    if flip_sin_to_cos:
        __lowercase							=						torch.cat([emb[:, half_dim:], emb[:, :half_dim]]    ,dim=-1			)

    # zero pad
    if embedding_dim % 2 == 1:
        __lowercase							=						torch.nn.functional.pad(a__    ,(0, 1, 0, 0)			)
    return emb
def 				snake_case_  (   a__		:	int    ,a__		:	Union[str, Any]    ,a__		:	Optional[int]=False    ,a__		:	Optional[Any]=0			):



    """simple docstring"""




    __lowercase							=						np.arange(a__    ,dtype=np.floataa			)
    __lowercase							=						np.arange(a__    ,dtype=np.floataa			)
    __lowercase							=						np.meshgrid(a__    ,a__			)  # here w goes first
    __lowercase							=						np.stack(a__    ,axis=0			)

    __lowercase							=						grid.reshape([2, 1, grid_size, grid_size]			)
    __lowercase							=						get_ad_sincos_pos_embed_from_grid(a__    ,a__			)
    if cls_token and extra_tokens > 0:
        __lowercase							=						np.concatenate([np.zeros([extra_tokens, embed_dim]			), pos_embed]    ,axis=0			)
    return pos_embed
def 				snake_case_  (   a__		:	int    ,a__		:	Optional[int]			):



    """simple docstring"""




    if embed_dim % 2 != 0:
        raise ValueError("""embed_dim must be divisible by 2"""			)

    # use half of dimensions to encode grid_h
    __lowercase							=						get_ad_sincos_pos_embed_from_grid(embed_dim // 2    ,grid[0]			)  # (H*W, D/2)
    __lowercase							=						get_ad_sincos_pos_embed_from_grid(embed_dim // 2    ,grid[1]			)  # (H*W, D/2)

    __lowercase							=						np.concatenate([emb_h, emb_w]    ,axis=1			)  # (H*W, D)
    return emb
def 				snake_case_  (   a__		:	int    ,a__		:	List[str]			):



    """simple docstring"""




    if embed_dim % 2 != 0:
        raise ValueError("""embed_dim must be divisible by 2"""			)

    __lowercase							=						np.arange(embed_dim // 2    ,dtype=np.floataa			)
    omega /= embed_dim / 2.0
    __lowercase							=						1.0 / 1_00_00**omega  # (D/2,)

    __lowercase							=						pos.reshape(-1			)  # (M,)
    __lowercase							=						np.einsum("""m,d->md"""    ,a__    ,a__			)  # (M, D/2), outer product

    __lowercase							=						np.sin(a__			)  # (M, D/2)
    __lowercase							=						np.cos(a__			)  # (M, D/2)

    __lowercase							=						np.concatenate([emb_sin, emb_cos]    ,axis=1			)  # (M, D)
    return emb






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__=224					,		lowerCamelCase__=224					,		lowerCamelCase__=16					,		lowerCamelCase__=3					,		lowerCamelCase__=768					,		lowerCamelCase__=False					,		lowerCamelCase__=True					,		lowerCamelCase__=True					,		)    ->					int:

           """simple docstring"""



           super().__init__()

           __lowercase							=						(height // patch_size) * (width // patch_size)
           __lowercase							=						flatten
           __lowercase							=						layer_norm

           __lowercase							=						nn.Convad(
               lowerCamelCase__					,		lowerCamelCase__					,		kernel_size=(patch_size, patch_size)					,		stride=lowerCamelCase__					,		bias=lowerCamelCase__     )
           if layer_norm:
               __lowercase							=						nn.LayerNorm(lowerCamelCase__					,		elementwise_affine=lowerCamelCase__					,		eps=1E-6     )
           else:
               __lowercase							=						None

           __lowercase							=						get_ad_sincos_pos_embed(lowerCamelCase__					,		int(num_patches**0.5     )     )
           self.register_buffer("""pos_embed"""					,		torch.from_numpy(lowerCamelCase__     ).float().unsqueeze(0     )					,		persistent=lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					List[Any]:

           """simple docstring"""



           __lowercase							=						self.proj(lowerCamelCase__     )
           if self.flatten:
               __lowercase							=						latent.flatten(2     ).transpose(1					,		2     )  # BCHW -> BNC
           if self.layer_norm:
               __lowercase							=						self.norm(lowerCamelCase__     )
           return latent + self.pos_embed






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__ = "silu"					,		lowerCamelCase__ = None					,		lowerCamelCase__ = None					,		lowerCamelCase__=None					,		)    ->					List[str]:

           """simple docstring"""



           super().__init__()

           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )

           if cond_proj_dim is not None:
               __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__					,		bias=lowerCamelCase__     )
           else:
               __lowercase							=						None

           __lowercase							=						get_activation(lowerCamelCase__     )

           if out_dim is not None:
               __lowercase							=						out_dim
           else:
               __lowercase							=						time_embed_dim
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )

           if post_act_fn is None:
               __lowercase							=						None
           else:
               __lowercase							=						get_activation(lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__=None     )    ->					List[Any]:

           """simple docstring"""



           if condition is not None:
               __lowercase							=						sample + self.cond_proj(lowerCamelCase__     )
           __lowercase							=						self.linear_a(lowerCamelCase__     )

           if self.act is not None:
               __lowercase							=						self.act(lowerCamelCase__     )

           __lowercase							=						self.linear_a(lowerCamelCase__     )

           if self.post_act is not None:
               __lowercase							=						self.post_act(lowerCamelCase__     )
           return sample






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Optional[int]:

           """simple docstring"""



           super().__init__()
           __lowercase							=						num_channels
           __lowercase							=						flip_sin_to_cos
           __lowercase							=						downscale_freq_shift




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					Tuple:

           """simple docstring"""



           __lowercase							=						get_timestep_embedding(
               lowerCamelCase__					,		self.num_channels					,		flip_sin_to_cos=self.flip_sin_to_cos					,		downscale_freq_shift=self.downscale_freq_shift					,		)
           return t_emb






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 256					,		lowerCamelCase__ = 1.0					,		lowerCamelCase__=True					,		lowerCamelCase__=True					,		lowerCamelCase__=False     )    ->					Tuple:

           """simple docstring"""



           super().__init__()
           __lowercase							=						nn.Parameter(torch.randn(lowerCamelCase__     ) * scale					,		requires_grad=lowerCamelCase__     )
           __lowercase							=						log
           __lowercase							=						flip_sin_to_cos

           if set_W_to_weight:
               # to delete later
               __lowercase							=						nn.Parameter(torch.randn(lowerCamelCase__     ) * scale					,		requires_grad=lowerCamelCase__     )

               __lowercase							=						self.W




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					Dict:

           """simple docstring"""



           if self.log:
               __lowercase							=						torch.log(lowerCamelCase__     )

           __lowercase							=						x[:, None] * self.weight[None, :] * 2 * np.pi

           if self.flip_sin_to_cos:
               __lowercase							=						torch.cat([torch.cos(lowerCamelCase__     ), torch.sin(lowerCamelCase__     )]					,		dim=-1     )
           else:
               __lowercase							=						torch.cat([torch.sin(lowerCamelCase__     ), torch.cos(lowerCamelCase__     )]					,		dim=-1     )
           return out






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		)    ->					str:

           """simple docstring"""



           super().__init__()

           __lowercase							=						height
           __lowercase							=						width
           __lowercase							=						num_embed
           __lowercase							=						embed_dim

           __lowercase							=						nn.Embedding(self.num_embed					,		lowerCamelCase__     )
           __lowercase							=						nn.Embedding(self.height					,		lowerCamelCase__     )
           __lowercase							=						nn.Embedding(self.width					,		lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					Dict:

           """simple docstring"""



           __lowercase							=						self.emb(lowerCamelCase__     )

           __lowercase							=						self.height_emb(torch.arange(self.height					,		device=index.device     ).view(1					,		self.height     )     )

           # 1 x H x D -> 1 x H x 1 x D
           __lowercase							=						height_emb.unsqueeze(2     )

           __lowercase							=						self.width_emb(torch.arange(self.width					,		device=index.device     ).view(1					,		self.width     )     )

           # 1 x W x D -> 1 x 1 x W x D
           __lowercase							=						width_emb.unsqueeze(1     )

           __lowercase							=						height_emb + width_emb

           # 1 x H x W x D -> 1 x L xD
           __lowercase							=						pos_emb.view(1					,		self.height * self.width					,		-1     )

           __lowercase							=						emb + pos_emb[:, : emb.shape[1], :]

           return emb






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					int:

           """simple docstring"""



           super().__init__()
           __lowercase							=						dropout_prob > 0
           __lowercase							=						nn.Embedding(num_classes + use_cfg_embedding					,		lowerCamelCase__     )
           __lowercase							=						num_classes
           __lowercase							=						dropout_prob

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__=None     )    ->					Union[str, Any]:

           """simple docstring"""



           if force_drop_ids is None:
               __lowercase							=						torch.rand(labels.shape[0]					,		device=labels.device     ) < self.dropout_prob
           else:
               __lowercase							=						torch.tensor(force_drop_ids == 1     )
           __lowercase							=						torch.where(lowerCamelCase__					,		self.num_classes					,		lowerCamelCase__     )
           return labels




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__=None     )    ->					Optional[int]:

           """simple docstring"""



           __lowercase							=						self.dropout_prob > 0
           if (self.training and use_dropout) or (force_drop_ids is not None):
               __lowercase							=						self.token_drop(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						self.embedding_table(lowerCamelCase__     )
           return embeddings






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 1024					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 10					,		)    ->					int:

           """simple docstring"""



           super().__init__()

           __lowercase							=						num_image_text_embeds
           __lowercase							=						nn.Linear(lowerCamelCase__					,		self.num_image_text_embeds * cross_attention_dim     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Dict:

           """simple docstring"""



           __lowercase							=						text_embeds.shape[0]

           # image
           __lowercase							=						self.image_embeds(lowerCamelCase__     )
           __lowercase							=						image_text_embeds.reshape(lowerCamelCase__					,		self.num_image_text_embeds					,		-1     )

           # text
           __lowercase							=						self.text_proj(lowerCamelCase__     )

           return torch.cat([image_text_embeds, text_embeds]					,		dim=1     )






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 32					,		)    ->					Tuple:

           """simple docstring"""



           super().__init__()

           __lowercase							=						num_image_text_embeds
           __lowercase							=						nn.Linear(lowerCamelCase__					,		self.num_image_text_embeds * cross_attention_dim     )
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					Optional[int]:

           """simple docstring"""



           __lowercase							=						image_embeds.shape[0]

           # image
           __lowercase							=						self.image_embeds(lowerCamelCase__     )
           __lowercase							=						image_embeds.reshape(lowerCamelCase__					,		self.num_image_text_embeds					,		-1     )
           __lowercase							=						self.norm(lowerCamelCase__     )
           return image_embeds






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__=0.1     )    ->					Any:

           """simple docstring"""



           super().__init__()

           __lowercase							=						Timesteps(num_channels=256					,		flip_sin_to_cos=lowerCamelCase__					,		downscale_freq_shift=1     )
           __lowercase							=						TimestepEmbedding(in_channels=256					,		time_embed_dim=lowerCamelCase__     )
           __lowercase							=						LabelEmbedding(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__=None     )    ->					Union[str, Any]:

           """simple docstring"""



           __lowercase							=						self.time_proj(lowerCamelCase__     )
           __lowercase							=						self.timestep_embedder(timesteps_proj.to(dtype=lowerCamelCase__     )     )  # (N, D)

           __lowercase							=						self.class_embedder(lowerCamelCase__     )  # (N, D)

           __lowercase							=						timesteps_emb + class_labels  # (N, D)

           return conditioning






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__ = 64     )    ->					str:

           """simple docstring"""



           super().__init__()
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )
           __lowercase							=						AttentionPooling(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					List[str]:

           """simple docstring"""



           __lowercase							=						self.norma(lowerCamelCase__     )
           __lowercase							=						self.pool(lowerCamelCase__     )
           __lowercase							=						self.proj(lowerCamelCase__     )
           __lowercase							=						self.norma(lowerCamelCase__     )
           return hidden_states






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 1536     )    ->					List[Any]:

           """simple docstring"""



           super().__init__()
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					List[str]:

           """simple docstring"""



           __lowercase							=						self.text_proj(lowerCamelCase__     )
           __lowercase							=						self.text_norm(lowerCamelCase__     )

           # image
           __lowercase							=						self.image_proj(lowerCamelCase__     )

           return time_image_embeds + time_text_embeds






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 1536     )    ->					Optional[Any]:

           """simple docstring"""



           super().__init__()
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					str:

           """simple docstring"""



           __lowercase							=						self.image_proj(lowerCamelCase__     )
           __lowercase							=						self.image_norm(lowerCamelCase__     )
           return time_image_embeds






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__ = 768					,		lowerCamelCase__ = 1536     )    ->					str:

           """simple docstring"""



           super().__init__()
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__     )
           __lowercase							=						nn.LayerNorm(lowerCamelCase__     )
           __lowercase							=						nn.Sequential(
               nn.Convad(3					,		16					,		3					,		padding=1     )					,		nn.SiLU()					,		nn.Convad(16					,		16					,		3					,		padding=1     )					,		nn.SiLU()					,		nn.Convad(16					,		32					,		3					,		padding=1					,		stride=2     )					,		nn.SiLU()					,		nn.Convad(32					,		32					,		3					,		padding=1     )					,		nn.SiLU()					,		nn.Convad(32					,		96					,		3					,		padding=1					,		stride=2     )					,		nn.SiLU()					,		nn.Convad(96					,		96					,		3					,		padding=1     )					,		nn.SiLU()					,		nn.Convad(96					,		256					,		3					,		padding=1					,		stride=2     )					,		nn.SiLU()					,		nn.Convad(256					,		4					,		3					,		padding=1     )					,		)




       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Any:

           """simple docstring"""



           __lowercase							=						self.image_proj(lowerCamelCase__     )
           __lowercase							=						self.image_norm(lowerCamelCase__     )
           __lowercase							=						self.input_hint_block(lowerCamelCase__     )
           return time_image_embeds, hint






class        SCREAMING_SNAKE_CASE(				nn.Module     ):

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__=None     )    ->					int:

           """simple docstring"""



           super().__init__()
           __lowercase							=						dtype
           __lowercase							=						nn.Parameter(torch.randn(1					,		lowerCamelCase__     ) / embed_dim**0.5     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__					,		dtype=self.dtype     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__					,		dtype=self.dtype     )
           __lowercase							=						nn.Linear(lowerCamelCase__					,		lowerCamelCase__					,		dtype=self.dtype     )
           __lowercase							=						num_heads
           __lowercase							=						embed_dim // self.num_heads




       def     snake_case__			(    self					,		lowerCamelCase__     )    ->					int:

           """simple docstring"""



           __lowercase				,__lowercase				,__lowercase							=						x.size()

           def shape(lowerCamelCase__     ):
               # (bs, length, width) --> (bs, length, n_heads, dim_per_head)
               __lowercase							=						x.view(lowerCamelCase__					,		-1					,		self.num_heads					,		self.dim_per_head     )
               # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
               __lowercase							=						x.transpose(1					,		2     )
               # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
               __lowercase							=						x.reshape(bs * self.num_heads					,		-1					,		self.dim_per_head     )
               # (bs*n_heads, length, dim_per_head) --> (bs*n_heads, dim_per_head, length)
               __lowercase							=						x.transpose(1					,		2     )
               return x

           __lowercase							=						x.mean(dim=1					,		keepdim=lowerCamelCase__     ) + self.positional_embedding.to(x.dtype     )
           __lowercase							=						torch.cat([class_token, x]					,		dim=1     )  # (bs, length+1, width)

           # (bs*n_heads, class_token_length, dim_per_head)
           __lowercase							=						shape(self.q_proj(lowerCamelCase__     )     )
           # (bs*n_heads, length+class_token_length, dim_per_head)
           __lowercase							=						shape(self.k_proj(lowerCamelCase__     )     )
           __lowercase							=						shape(self.v_proj(lowerCamelCase__     )     )

           # (bs*n_heads, class_token_length, length+class_token_length):
           __lowercase							=						1 / math.sqrt(math.sqrt(self.dim_per_head     )     )
           __lowercase							=						torch.einsum("""bct,bcs->bts"""					,		q * scale					,		k * scale     )  # More stable with f16 than dividing afterwards
           __lowercase							=						torch.softmax(weight.float()					,		dim=-1     ).type(weight.dtype     )

           # (bs*n_heads, dim_per_head, class_token_length)
           __lowercase							=						torch.einsum("""bts,bcs->bct"""					,		lowerCamelCase__					,		lowerCamelCase__     )

           # (bs, length+1, width)
           __lowercase							=						a.reshape(lowerCamelCase__					,		-1					,		1     ).transpose(1					,		2     )

           return a[:, 0, :]  # cls_token










'''simple docstring'''
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class        SCREAMING_SNAKE_CASE(				__A   ,      __A   ,      unittest.TestCase     ):
       snake_case_ :     str    =					StableDiffusionParadigmsPipeline
       snake_case_ :     Optional[Any]    =					TEXT_TO_IMAGE_PARAMS
       snake_case_ :     Tuple    =					TEXT_TO_IMAGE_BATCH_PARAMS
       snake_case_ :     Optional[Any]    =					TEXT_TO_IMAGE_IMAGE_PARAMS
       snake_case_ :     str    =					TEXT_TO_IMAGE_IMAGE_PARAMS

       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           torch.manual_seed(0     )
           __lowercase							=						UNetaDConditionModel(
               block_out_channels=(32, 64)					,		layers_per_block=2					,		sample_size=32					,		in_channels=4					,		out_channels=4					,		down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")					,		up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")					,		cross_attention_dim=32					,		attention_head_dim=(2, 4)					,		use_linear_projection=lowerCamelCase__					,		)
           __lowercase							=						DDIMParallelScheduler(
               beta_start=0.0_00_85					,		beta_end=0.0_12					,		beta_schedule="""scaled_linear"""					,		clip_sample=lowerCamelCase__					,		set_alpha_to_one=lowerCamelCase__					,		)
           torch.manual_seed(0     )
           __lowercase							=						AutoencoderKL(
               block_out_channels=[32, 64]					,		in_channels=3					,		out_channels=3					,		down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]					,		up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]					,		latent_channels=4					,		sample_size=128					,		)
           torch.manual_seed(0     )
           __lowercase							=						CLIPTextConfig(
               bos_token_id=0					,		eos_token_id=2					,		hidden_size=32					,		intermediate_size=37					,		layer_norm_eps=1E-0_5					,		num_attention_heads=4					,		num_hidden_layers=5					,		pad_token_id=1					,		vocab_size=1000					,		hidden_act="""gelu"""					,		projection_dim=512					,		)
           __lowercase							=						CLIPTextModel(lowerCamelCase__     )
           __lowercase							=						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     )

           __lowercase							=						{
               """unet""": unet,
               """scheduler""": scheduler,
               """vae""": vae,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
               """safety_checker""": None,
               """feature_extractor""": None,
           }
           return components

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__=0     )    ->					Any:

           """simple docstring"""



           if str(lowerCamelCase__     ).startswith("""mps"""     ):
               __lowercase							=						torch.manual_seed(lowerCamelCase__     )
           else:
               __lowercase							=						torch.Generator(device=lowerCamelCase__     ).manual_seed(lowerCamelCase__     )
           __lowercase							=						{
               """prompt""": """a photograph of an astronaut riding a horse""",
               """generator""": generator,
               """num_inference_steps""": 10,
               """guidance_scale""": 6.0,
               """output_type""": """numpy""",
               """parallel""": 3,
               """debug""": True,
           }
           return inputs

       def     snake_case__			(    self     )    ->					List[str]:

           """simple docstring"""



           __lowercase							=						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           __lowercase							=						self.get_dummy_components()
           __lowercase							=						StableDiffusionParadigmsPipeline(**lowerCamelCase__     )
           __lowercase							=						sd_pipe.to(lowerCamelCase__     )
           sd_pipe.set_progress_bar_config(disable=lowerCamelCase__     )

           __lowercase							=						self.get_dummy_inputs(lowerCamelCase__     )
           __lowercase							=						sd_pipe(**lowerCamelCase__     ).images
           __lowercase							=						image[0, -3:, -3:, -1]
           assert image.shape == (1, 64, 64, 3)

           __lowercase							=						np.array([0.47_73, 0.54_17, 0.47_23, 0.49_25, 0.56_31, 0.47_52, 0.52_40, 0.49_35, 0.50_23]     )

           assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           __lowercase							=						self.get_dummy_components()
           torch.manual_seed(0     )
           __lowercase							=						DDPMParallelScheduler()
           torch.manual_seed(0     )
           __lowercase							=						StableDiffusionParadigmsPipeline(**lowerCamelCase__     )
           __lowercase							=						sd_pipe.to(lowerCamelCase__     )
           sd_pipe.set_progress_bar_config(disable=lowerCamelCase__     )

           __lowercase							=						self.get_dummy_inputs(lowerCamelCase__     )
           __lowercase							=						sd_pipe(**lowerCamelCase__     ).images
           __lowercase							=						image[0, -3:, -3:, -1]
           assert image.shape == (1, 64, 64, 3)

           __lowercase							=						np.array([0.35_73, 0.44_20, 0.49_60, 0.47_99, 0.37_96, 0.38_79, 0.48_19, 0.43_65, 0.44_68]     )

           assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           super().test_inference_batch_consistent(batch_sizes=[1, 2]     )

       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           super().test_inference_batch_single_identical(batch_size=2					,		expected_max_diff=3E-3     )




       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           __lowercase							=						self.get_dummy_components()
           __lowercase							=						StableDiffusionParadigmsPipeline(**lowerCamelCase__     )
           __lowercase							=						sd_pipe.to(lowerCamelCase__     )
           sd_pipe.set_progress_bar_config(disable=lowerCamelCase__     )

           __lowercase							=						self.get_dummy_inputs(lowerCamelCase__     )
           __lowercase							=						"""french fries"""
           __lowercase							=						sd_pipe(**lowerCamelCase__					,		negative_prompt=lowerCamelCase__     )
           __lowercase							=						output.images
           __lowercase							=						image[0, -3:, -3:, -1]

           assert image.shape == (1, 64, 64, 3)

           __lowercase							=						np.array([0.47_71, 0.54_20, 0.46_83, 0.49_18, 0.56_36, 0.47_25, 0.52_30, 0.49_23, 0.50_15]     )

           assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1E-2






@slow
@require_torch_gpu
class        SCREAMING_SNAKE_CASE(				unittest.TestCase     ):

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

       def     snake_case__			(    self					,		lowerCamelCase__=0     )    ->					int:

           """simple docstring"""



           __lowercase							=						torch.Generator(device=lowerCamelCase__     ).manual_seed(lowerCamelCase__     )
           __lowercase							=						{
               """prompt""": """a photograph of an astronaut riding a horse""",
               """generator""": generator,
               """num_inference_steps""": 10,
               """guidance_scale""": 7.5,
               """output_type""": """numpy""",
               """parallel""": 3,
               """debug""": True,
           }
           return inputs




       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           __lowercase							=						"""stabilityai/stable-diffusion-2-base"""
           __lowercase							=						DDIMParallelScheduler.from_pretrained(lowerCamelCase__					,		subfolder="""scheduler"""     )
           __lowercase							=						StableDiffusionParadigmsPipeline.from_pretrained(lowerCamelCase__					,		scheduler=lowerCamelCase__					,		safety_checker=lowerCamelCase__     )
           pipe.to(lowerCamelCase__     )
           pipe.set_progress_bar_config(disable=lowerCamelCase__     )
           pipe.enable_attention_slicing()

           __lowercase							=						self.get_inputs()
           __lowercase							=						pipe(**lowerCamelCase__     ).images
           __lowercase							=						image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)

           __lowercase							=						np.array([0.96_22, 0.96_02, 0.97_48, 0.95_91, 0.96_30, 0.96_91, 0.96_61, 0.96_31, 0.97_41]     )

           assert np.abs(expected_slice - image_slice     ).max() < 1E-2




