



'''simple docstring'''





import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class __A     (    unittest.TestCase		):



 '''simple docstring'''







 @property
 def    a__    (self			)       ->	Union[str, Any]:


      """simple docstring"""



      torch.manual_seed(0			)
      _a           =    UNetaDModel(
          block_out_channels=(32, 64)					,  layers_per_block=2					,  sample_size=32					,  in_channels=3					,  out_channels=3					,  down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')					,  up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')					,  )
      return model



 def    a__    (self			)       ->	str:


      """simple docstring"""



      _a           =    self.dummy_uncond_unet
      _a           =    KarrasVeScheduler()

      _a           =    KarrasVePipeline(unet=A					,  scheduler=A			)
      pipe.to(A			)
      pipe.set_progress_bar_config(disable=A			)

      _a           =    torch.manual_seed(0			)
      _a           =    pipe(num_inference_steps=2					,  generator=A					,  output_type='''numpy'''			).images

      _a           =    torch.manual_seed(0			)
      _a           =    pipe(num_inference_steps=2					,  generator=A					,  output_type='''numpy'''					,  return_dict=A			)[0]

      _a           =    image[0, -3:, -3:, -1]
      _a           =    image_from_tuple[0, -3:, -3:, -1]

      assert image.shape == (1, 32, 32, 3)
      _a           =    np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]			)

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






@slow
@require_torch
class __A     (    unittest.TestCase		):



 '''simple docstring'''







 def    a__    (self			)       ->	str:


      """simple docstring"""



      _a           =    '''google/ncsnpp-celebahq-256'''
      _a           =    UNetaDModel.from_pretrained(A			)
      _a           =    KarrasVeScheduler()

      _a           =    KarrasVePipeline(unet=A					,  scheduler=A			)
      pipe.to(A			)
      pipe.set_progress_bar_config(disable=A			)

      _a           =    torch.manual_seed(0			)
      _a           =    pipe(num_inference_steps=20					,  generator=A					,  output_type='''numpy'''			).images

      _a           =    image[0, -3:, -3:, -1]
      assert image.shape == (1, 256, 256, 3)
      _a           =    np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]			)

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






import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class __UpperCamelCase     (				_a					,_a		):






					'''simple docstring'''



					@register_to_config
					def __init__(				self				,      *,
					    lowerCamelCase__ = 4				,      lowerCamelCase__ = 7_6_8				,      lowerCamelCase__				,      lowerCamelCase__				,      ):
						super().__init__()

						UpperCAmelCase__:	int									= nn.Parameter(torch.zeros(lowerCamelCase__						)						)

						# parameters for additional clip time embeddings
						UpperCAmelCase__:	Optional[Any]									= nn.Linear(lowerCamelCase__				,      lowerCamelCase__						)
						UpperCAmelCase__:	str									= nn.Linear(lowerCamelCase__				,      lowerCamelCase__						)

						# parameters for encoder hidden states
						UpperCAmelCase__:	Tuple									= clip_extra_context_tokens
						UpperCAmelCase__:	List[str]									= nn.Linear(
						    lowerCamelCase__				,      self.clip_extra_context_tokens * cross_attention_dim						)
						UpperCAmelCase__:	Union[str, Any]									= nn.Linear(lowerCamelCase__				,      lowerCamelCase__						)
						UpperCAmelCase__:	str									= nn.LayerNorm(lowerCamelCase__						)





					def    _UpperCAmelCase    (				self				,      *, lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__						):
						if do_classifier_free_guidance:
							# Add the classifier free guidance embeddings to the image embeddings
							UpperCAmelCase__:	Any									= image_embeddings.shape[0]
							UpperCAmelCase__:	Optional[Any]									= self.learned_classifier_free_guidance_embeddings.unsqueeze(0						)
							UpperCAmelCase__:	Union[str, Any]									= classifier_free_guidance_embeddings.expand(
							    lowerCamelCase__				,      -1						)
							UpperCAmelCase__:	Tuple									= torch.cat([classifier_free_guidance_embeddings, image_embeddings]				,      dim=0						)

						# The image embeddings batch size and the text embeddings batch size are equal
						assert image_embeddings.shape[0] == prompt_embeds.shape[0]

						UpperCAmelCase__:	str									= prompt_embeds.shape[0]

						# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
						# adding CLIP embeddings to the existing timestep embedding, ...
						UpperCAmelCase__:	List[str]									= self.embedding_proj(lowerCamelCase__						)
						UpperCAmelCase__:	Optional[int]									= self.clip_image_embeddings_project_to_time_embeddings(lowerCamelCase__						)
						UpperCAmelCase__:	Any									= time_projected_image_embeddings + time_projected_prompt_embeds

						# ... and by projecting CLIP embeddings into four
						# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
						UpperCAmelCase__:	Dict									= self.clip_extra_context_tokens_proj(lowerCamelCase__						)
						UpperCAmelCase__:	Optional[Any]									= clip_extra_context_tokens.reshape(lowerCamelCase__				,      -1				,      self.clip_extra_context_tokens						)
						UpperCAmelCase__:	Dict									= clip_extra_context_tokens.permute(0				,      2				,      1						)

						UpperCAmelCase__:	Any									= self.encoder_hidden_states_proj(lowerCamelCase__						)
						UpperCAmelCase__:	List[str]									= self.text_encoder_hidden_states_norm(lowerCamelCase__						)
						UpperCAmelCase__:	Optional[int]									= torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]				,      dim=1						)

						return text_encoder_hidden_states, additive_clip_time_embeddings