code: 
  
  
  import math
  from typing import Optional
  
  import numpy as np
  import torch
  from torch import nn
  
  from .activations import get_activation
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_							,UpperCamelCase_ = False							,UpperCamelCase_ = 1							,UpperCamelCase_ = 1							,UpperCamelCase_ = 1_00_00							,):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       assert len(timesteps.shape       ) == 1, "Timesteps should be a 1d-array"
  
       snake_case					=       embedding_dim // 2
       snake_case					=       -math.log(UpperCamelCase_       ) * torch.arange(
           start=0							,end=UpperCamelCase_							,dtype=torch.floataa							,device=timesteps.device       )
       snake_case					=       exponent / (half_dim - downscale_freq_shift)
  
       snake_case					=       torch.exp(UpperCamelCase_       )
       snake_case					=       timesteps[:, None].float() * emb[None, :]
  
       # scale embeddings
       snake_case					=       scale * emb
  
       # concat sine and cosine embeddings
       snake_case					=       torch.cat([torch.sin(UpperCamelCase_       ), torch.cos(UpperCamelCase_       )]							,dim=-1       )
  
       # flip sine and cosine embeddings
       if flip_sin_to_cos:
            snake_case					=       torch.cat([emb[:, half_dim:], emb[:, :half_dim]]							,dim=-1       )
  
       # zero pad
       if embedding_dim % 2 == 1:
            snake_case					=       torch.nn.functional.pad(UpperCamelCase_							,(0, 1, 0, 0)       )
       return emb
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_							,UpperCamelCase_=False							,UpperCamelCase_=0       ):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       snake_case					=       np.arange(UpperCamelCase_							,dtype=np.floataa       )
       snake_case					=       np.arange(UpperCamelCase_							,dtype=np.floataa       )
       snake_case					=       np.meshgrid(UpperCamelCase_							,UpperCamelCase_       )  # here w goes first
       snake_case					=       np.stack(UpperCamelCase_							,axis=0       )
  
       snake_case					=       grid.reshape([2, 1, grid_size, grid_size]       )
       snake_case					=       get_ad_sincos_pos_embed_from_grid(UpperCamelCase_							,UpperCamelCase_       )
       if cls_token and extra_tokens > 0:
            snake_case					=       np.concatenate([np.zeros([extra_tokens, embed_dim]       ), pos_embed]							,axis=0       )
       return pos_embed
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_       ):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       if embed_dim % 2 != 0:
            raise ValueError('''embed_dim must be divisible by 2'''       )
  
       # use half of dimensions to encode grid_h
       snake_case					=       get_ad_sincos_pos_embed_from_grid(embed_dim // 2							,grid[0]       )  # (H*W, D/2)
       snake_case					=       get_ad_sincos_pos_embed_from_grid(embed_dim // 2							,grid[1]       )  # (H*W, D/2)
  
       snake_case					=       np.concatenate([emb_h, emb_w]							,axis=1       )  # (H*W, D)
       return emb
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_       ):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       if embed_dim % 2 != 0:
            raise ValueError('''embed_dim must be divisible by 2'''       )
  
       snake_case					=       np.arange(embed_dim // 2							,dtype=np.floataa       )
       omega /= embed_dim / 2.0
       snake_case					=       1.0 / 1_00_00**omega  # (D/2,)
  
       snake_case					=       pos.reshape(-1       )  # (M,)
       snake_case					=       np.einsum('''m,d->md'''							,UpperCamelCase_							,UpperCamelCase_       )  # (M, D/2), outer product
  
       snake_case					=       np.sin(UpperCamelCase_       )  # (M, D/2)
       snake_case					=       np.cos(UpperCamelCase_       )  # (M, D/2)
  
       snake_case					=       np.concatenate([emb_sin, emb_cos]							,axis=1       )  # (M, D)
       return emb
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case=2_2_4    ,      __snake_case=2_2_4    ,      __snake_case=1_6    ,      __snake_case=3    ,      __snake_case=7_6_8    ,      __snake_case=False    ,      __snake_case=True    ,      __snake_case=True    ,      ):
           super().__init__()
  
           snake_case					=       (height // patch_size) * (width // patch_size)
           snake_case					=       flatten
           snake_case					=       layer_norm
  
           snake_case					=       nn.Convad(
               __snake_case    ,      __snake_case    ,      kernel_size=(patch_size, patch_size)    ,      stride=__snake_case    ,      bias=__snake_case					)
           if layer_norm:
                snake_case					=       nn.LayerNorm(__snake_case    ,      elementwise_affine=__snake_case    ,      eps=1E-6					)
           else:
                snake_case					=       None
  
           snake_case					=       get_ad_sincos_pos_embed(__snake_case    ,      int(num_patches**0.5					)					)
           self.register_buffer('''pos_embed'''    ,      torch.from_numpy(__snake_case					).float().unsqueeze(0					)    ,      persistent=__snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case					=       self.proj(__snake_case					)
           if self.flatten:
                snake_case					=       latent.flatten(2					).transpose(1    ,      2					)  # BCHW -> BNC
           if self.layer_norm:
                snake_case					=       self.norm(__snake_case					)
           return latent + self.pos_embed
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case = "silu"    ,      __snake_case = None    ,      __snake_case = None    ,      __snake_case=None    ,      ):
           super().__init__()
  
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
  
           if cond_proj_dim is not None:
                snake_case					=       nn.Linear(__snake_case    ,      __snake_case    ,      bias=__snake_case					)
           else:
                snake_case					=       None
  
           snake_case					=       get_activation(__snake_case					)
  
           if out_dim is not None:
                snake_case					=       out_dim
           else:
                snake_case					=       time_embed_dim
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
  
           if post_act_fn is None:
                snake_case					=       None
           else:
                snake_case					=       get_activation(__snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case=None					):
           if condition is not None:
                snake_case					=       sample + self.cond_proj(__snake_case					)
           snake_case					=       self.linear_a(__snake_case					)
  
           if self.act is not None:
                snake_case					=       self.act(__snake_case					)
  
           snake_case					=       self.linear_a(__snake_case					)
  
           if self.post_act is not None:
                snake_case					=       self.post_act(__snake_case					)
           return sample
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
           super().__init__()
           snake_case					=       num_channels
           snake_case					=       flip_sin_to_cos
           snake_case					=       downscale_freq_shift
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case					=       get_timestep_embedding(
               __snake_case    ,      self.num_channels    ,      flip_sin_to_cos=self.flip_sin_to_cos    ,      downscale_freq_shift=self.downscale_freq_shift    ,      )
           return t_emb
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 2_5_6    ,      __snake_case = 1.0    ,      __snake_case=True    ,      __snake_case=True    ,      __snake_case=False					):
           super().__init__()
           snake_case					=       nn.Parameter(torch.randn(__snake_case					) * scale    ,      requires_grad=__snake_case					)
           snake_case					=       log
           snake_case					=       flip_sin_to_cos
  
           if set_W_to_weight:
                # to delete later
                snake_case					=       nn.Parameter(torch.randn(__snake_case					) * scale    ,      requires_grad=__snake_case					)
  
                snake_case					=       self.W
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           if self.log:
                snake_case					=       torch.log(__snake_case					)
  
           snake_case					=       x[:, None] * self.weight[None, :] * 2 * np.pi
  
           if self.flip_sin_to_cos:
                snake_case					=       torch.cat([torch.cos(__snake_case					), torch.sin(__snake_case					)]    ,      dim=-1					)
           else:
                snake_case					=       torch.cat([torch.sin(__snake_case					), torch.cos(__snake_case					)]    ,      dim=-1					)
           return out
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      ):
           super().__init__()
  
           snake_case					=       height
           snake_case					=       width
           snake_case					=       num_embed
           snake_case					=       embed_dim
  
           snake_case					=       nn.Embedding(self.num_embed    ,      __snake_case					)
           snake_case					=       nn.Embedding(self.height    ,      __snake_case					)
           snake_case					=       nn.Embedding(self.width    ,      __snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case					=       self.emb(__snake_case					)
  
           snake_case					=       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
           snake_case					=       height_emb.unsqueeze(2					)
  
           snake_case					=       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
           snake_case					=       width_emb.unsqueeze(1					)
  
           snake_case					=       height_emb + width_emb
  
           # 1 x H x W x D -> 1 x L xD
           snake_case					=       pos_emb.view(1    ,      self.height * self.width    ,      -1					)
  
           snake_case					=       emb + pos_emb[:, : emb.shape[1], :]
  
           return emb
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
           super().__init__()
           snake_case					=       dropout_prob > 0
           snake_case					=       nn.Embedding(num_classes + use_cfg_embedding    ,      __snake_case					)
           snake_case					=       num_classes
           snake_case					=       dropout_prob
      def 	a_    ( self    ,      __snake_case    ,      __snake_case=None					):
           if force_drop_ids is None:
                snake_case					=       torch.rand(labels.shape[0]    ,      device=labels.device					) < self.dropout_prob
           else:
                snake_case					=       torch.tensor(force_drop_ids == 1					)
           snake_case					=       torch.where(__snake_case    ,      self.num_classes    ,      __snake_case					)
           return labels
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case=None					):
           snake_case					=       self.dropout_prob > 0
           if (self.training and use_dropout) or (force_drop_ids is not None):
                snake_case					=       self.token_drop(__snake_case    ,      __snake_case					)
           snake_case					=       self.embedding_table(__snake_case					)
           return embeddings
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 1_0_2_4    ,      __snake_case = 7_6_8    ,      __snake_case = 7_6_8    ,      __snake_case = 1_0    ,      ):
           super().__init__()
  
           snake_case					=       num_image_text_embeds
           snake_case					=       nn.Linear(__snake_case    ,      self.num_image_text_embeds * cross_attention_dim					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case					):
           snake_case					=       text_embeds.shape[0]
  
           # image
           snake_case					=       self.image_embeds(__snake_case					)
           snake_case					=       image_text_embeds.reshape(__snake_case    ,      self.num_image_text_embeds    ,      -1					)
  
           # text
           snake_case					=       self.text_proj(__snake_case					)
  
           return torch.cat([image_text_embeds, text_embeds]    ,      dim=1					)
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 7_6_8    ,      __snake_case = 7_6_8    ,      __snake_case = 3_2    ,      ):
           super().__init__()
  
           snake_case					=       num_image_text_embeds
           snake_case					=       nn.Linear(__snake_case    ,      self.num_image_text_embeds * cross_attention_dim					)
           snake_case					=       nn.LayerNorm(__snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case					=       image_embeds.shape[0]
  
           # image
           snake_case					=       self.image_embeds(__snake_case					)
           snake_case					=       image_embeds.reshape(__snake_case    ,      self.num_image_text_embeds    ,      -1					)
           snake_case					=       self.norm(__snake_case					)
           return image_embeds
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case=0.1					):
           super().__init__()
  
           snake_case					=       Timesteps(num_channels=2_5_6    ,      flip_sin_to_cos=__snake_case    ,      downscale_freq_shift=1					)
           snake_case					=       TimestepEmbedding(in_channels=2_5_6    ,      time_embed_dim=__snake_case					)
           snake_case					=       LabelEmbedding(__snake_case    ,      __snake_case    ,      __snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case=None					):
           snake_case					=       self.time_proj(__snake_case					)
           snake_case					=       self.timestep_embedder(timesteps_proj.to(dtype=__snake_case					)					)  # (N, D)
  
           snake_case					=       self.class_embedder(__snake_case					)  # (N, D)
  
           snake_case					=       timesteps_emb + class_labels  # (N, D)
  
           return conditioning
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case = 6_4					):
           super().__init__()
           snake_case					=       nn.LayerNorm(__snake_case					)
           snake_case					=       AttentionPooling(__snake_case    ,      __snake_case					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
           snake_case					=       nn.LayerNorm(__snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case					=       self.norma(__snake_case					)
           snake_case					=       self.pool(__snake_case					)
           snake_case					=       self.proj(__snake_case					)
           snake_case					=       self.norma(__snake_case					)
           return hidden_states
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 7_6_8    ,      __snake_case = 7_6_8    ,      __snake_case = 1_5_3_6					):
           super().__init__()
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
           snake_case					=       nn.LayerNorm(__snake_case					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case					):
           # text
           snake_case					=       self.text_proj(__snake_case					)
           snake_case					=       self.text_norm(__snake_case					)
  
           # image
           snake_case					=       self.image_proj(__snake_case					)
  
           return time_image_embeds + time_text_embeds
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 7_6_8    ,      __snake_case = 1_5_3_6					):
           super().__init__()
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
           snake_case					=       nn.LayerNorm(__snake_case					)
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           # image
           snake_case					=       self.image_proj(__snake_case					)
           snake_case					=       self.image_norm(__snake_case					)
           return time_image_embeds
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case = 7_6_8    ,      __snake_case = 1_5_3_6					):
           super().__init__()
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case					)
           snake_case					=       nn.LayerNorm(__snake_case					)
           snake_case					=       nn.Sequential(
               nn.Convad(3    ,      1_6    ,      3    ,      padding=1					)    ,      nn.SiLU()    ,      nn.Convad(1_6    ,      1_6    ,      3    ,      padding=1					)    ,      nn.SiLU()    ,      nn.Convad(1_6    ,      3_2    ,      3    ,      padding=1    ,      stride=2					)    ,      nn.SiLU()    ,      nn.Convad(3_2    ,      3_2    ,      3    ,      padding=1					)    ,      nn.SiLU()    ,      nn.Convad(3_2    ,      9_6    ,      3    ,      padding=1    ,      stride=2					)    ,      nn.SiLU()    ,      nn.Convad(9_6    ,      9_6    ,      3    ,      padding=1					)    ,      nn.SiLU()    ,      nn.Convad(9_6    ,      2_5_6    ,      3    ,      padding=1    ,      stride=2					)    ,      nn.SiLU()    ,      nn.Convad(2_5_6    ,      4    ,      3    ,      padding=1					)    ,      )
  
  
  
  
      def 	a_    ( self    ,      __snake_case    ,      __snake_case					):
           # image
           snake_case					=       self.image_proj(__snake_case					)
           snake_case					=       self.image_norm(__snake_case					)
           snake_case					=       self.input_hint_block(__snake_case					)
           return time_image_embeds, hint
  
  
  
  
  
  
  class 						A__      (					nn.Module			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case=None					):
           super().__init__()
           snake_case					=       dtype
           snake_case					=       nn.Parameter(torch.randn(1    ,      __snake_case					) / embed_dim**0.5					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case    ,      dtype=self.dtype					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case    ,      dtype=self.dtype					)
           snake_case					=       nn.Linear(__snake_case    ,      __snake_case    ,      dtype=self.dtype					)
           snake_case					=       num_heads
           snake_case					=       embed_dim // self.num_heads
  
  
  
  
      def 	a_    ( self    ,      __snake_case					):
           snake_case				,						snake_case				,						snake_case					=       x.size()
  
           def shape(__snake_case					):
                # (bs, length, width) --> (bs, length, n_heads, dim_per_head)
                snake_case					=       x.view(__snake_case    ,      -1    ,      self.num_heads    ,      self.dim_per_head					)
                # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
                snake_case					=       x.transpose(1    ,      2					)
                # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
                snake_case					=       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)
                snake_case					=       x.transpose(1    ,      2					)
                return x
  
           snake_case					=       x.mean(dim=1    ,      keepdim=__snake_case					) + self.positional_embedding.to(x.dtype					)
           snake_case					=       torch.cat([class_token, x]    ,      dim=1					)  # (bs, length+1, width)
  
           # (bs*n_heads, class_token_length, dim_per_head)
           snake_case					=       shape(self.q_proj(__snake_case					)					)
           # (bs*n_heads, length+class_token_length, dim_per_head)
           snake_case					=       shape(self.k_proj(__snake_case					)					)
           snake_case					=       shape(self.v_proj(__snake_case					)					)
  
           # (bs*n_heads, class_token_length, length+class_token_length):
           snake_case					=       1 / math.sqrt(math.sqrt(self.dim_per_head					)					)
           snake_case					=       torch.einsum('''bct,bcs->bts'''    ,      q * scale    ,      k * scale					)  # More stable with f16 than dividing afterwards
           snake_case					=       torch.softmax(weight.float()    ,      dim=-1					).type(weight.dtype					)
  
           # (bs*n_heads, dim_per_head, class_token_length)
           snake_case					=       torch.einsum('''bts,bcs->bct'''    ,      __snake_case    ,      __snake_case					)
  
           # (bs, length+1, width)
           snake_case					=       a.reshape(__snake_case    ,      -1    ,      1					).transpose(1    ,      2					)
  
           return a[:, 0, :]  # cls_token
  
  
  
  
  
style_context: 
  
  
  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 						A__      (					snake_case__ ,      snake_case__ ,      unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      __magic_name__    						=	StableDiffusionParadigmsPipeline
      __magic_name__    						=	TEXT_TO_IMAGE_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_BATCH_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_IMAGE_PARAMS
      __magic_name__    						=	TEXT_TO_IMAGE_IMAGE_PARAMS
      def 	a_    ( self					):
           torch.manual_seed(0					)
           snake_case					=       UNetaDConditionModel(
               block_out_channels=(3_2, 6_4)    ,      layers_per_block=2    ,      sample_size=3_2    ,      in_channels=4    ,      out_channels=4    ,      down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')    ,      up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')    ,      cross_attention_dim=3_2    ,      attention_head_dim=(2, 4)    ,      use_linear_projection=__snake_case    ,      )
           snake_case					=       DDIMParallelScheduler(
               beta_start=0.0_0085    ,      beta_end=0.012    ,      beta_schedule='''scaled_linear'''    ,      clip_sample=__snake_case    ,      set_alpha_to_one=__snake_case    ,      )
           torch.manual_seed(0					)
           snake_case					=       AutoencoderKL(
               block_out_channels=[3_2, 6_4]    ,      in_channels=3    ,      out_channels=3    ,      down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']    ,      up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']    ,      latent_channels=4    ,      sample_size=1_2_8    ,      )
           torch.manual_seed(0					)
           snake_case					=       CLIPTextConfig(
               bos_token_id=0    ,      eos_token_id=2    ,      hidden_size=3_2    ,      intermediate_size=3_7    ,      layer_norm_eps=1E-05    ,      num_attention_heads=4    ,      num_hidden_layers=5    ,      pad_token_id=1    ,      vocab_size=1_0_0_0    ,      hidden_act='''gelu'''    ,      projection_dim=5_1_2    ,      )
           snake_case					=       CLIPTextModel(__snake_case					)
           snake_case					=       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)
  
           snake_case					=       {
               '''unet''': unet,
               '''scheduler''': scheduler,
               '''vae''': vae,
               '''text_encoder''': text_encoder,
               '''tokenizer''': tokenizer,
               '''safety_checker''': None,
               '''feature_extractor''': None,
           }
           return components
      def 	a_    ( self    ,      __snake_case    ,      __snake_case=0					):
           if str(__snake_case					).startswith('''mps'''					):
                snake_case					=       torch.manual_seed(__snake_case					)
           else:
                snake_case					=       torch.Generator(device=__snake_case					).manual_seed(__snake_case					)
           snake_case					=       {
               '''prompt''': '''a photograph of an astronaut riding a horse''',
               '''generator''': generator,
               '''num_inference_steps''': 1_0,
               '''guidance_scale''': 6.0,
               '''output_type''': '''numpy''',
               '''parallel''': 3,
               '''debug''': True,
           }
           return inputs
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       sd_pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1]
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.4773, 0.5417, 0.4723, 0.4925, 0.5631, 0.4752, 0.5240, 0.4935, 0.5023]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           torch.manual_seed(0					)
           snake_case					=       DDPMParallelScheduler()
           torch.manual_seed(0					)
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       sd_pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1]
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.3573, 0.4420, 0.4960, 0.4799, 0.3796, 0.3879, 0.4819, 0.4365, 0.4468]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
      def 	a_    ( self					):
           super().test_inference_batch_consistent(batch_sizes=[1, 2]					)
      def 	a_    ( self					):
           super().test_inference_batch_single_identical(batch_size=2    ,      expected_max_diff=3E-3					)
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
           snake_case					=       self.get_dummy_components()
           snake_case					=       StableDiffusionParadigmsPipeline(**__snake_case					)
           snake_case					=       sd_pipe.to(__snake_case					)
           sd_pipe.set_progress_bar_config(disable=__snake_case					)
  
           snake_case					=       self.get_dummy_inputs(__snake_case					)
           snake_case					=       '''french fries'''
           snake_case					=       sd_pipe(**__snake_case    ,      negative_prompt=__snake_case					)
           snake_case					=       output.images
           snake_case					=       image[0, -3:, -3:, -1]
  
           assert image.shape == (1, 6_4, 6_4, 3)
  
           snake_case					=       np.array([0.4771, 0.5420, 0.4683, 0.4918, 0.5636, 0.4725, 0.5230, 0.4923, 0.5015]					)
  
           assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2
  
  
  
  
  
  
  @slow
  @require_torch_gpu
  class 						A__      (					unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def 	a_    ( self					):
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()
      def 	a_    ( self    ,      __snake_case=0					):
           snake_case					=       torch.Generator(device=__snake_case					).manual_seed(__snake_case					)
           snake_case					=       {
               '''prompt''': '''a photograph of an astronaut riding a horse''',
               '''generator''': generator,
               '''num_inference_steps''': 1_0,
               '''guidance_scale''': 7.5,
               '''output_type''': '''numpy''',
               '''parallel''': 3,
               '''debug''': True,
           }
           return inputs
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       '''stabilityai/stable-diffusion-2-base'''
           snake_case					=       DDIMParallelScheduler.from_pretrained(__snake_case    ,      subfolder='''scheduler'''					)
           snake_case					=       StableDiffusionParadigmsPipeline.from_pretrained(__snake_case    ,      scheduler=__snake_case    ,      safety_checker=__snake_case					)
           pipe.to(__snake_case					)
           pipe.set_progress_bar_config(disable=__snake_case					)
           pipe.enable_attention_slicing()
  
           snake_case					=       self.get_inputs()
           snake_case					=       pipe(**__snake_case					).images
           snake_case					=       image[0, -3:, -3:, -1].flatten()
  
           assert image.shape == (1, 5_1_2, 5_1_2, 3)
  
           snake_case					=       np.array([0.9622, 0.9602, 0.9748, 0.9591, 0.9630, 0.9691, 0.9661, 0.9631, 0.9741]					)
  
           assert np.abs(expected_slice - image_slice					).max() < 1E-2
  
  
  
  
  