


"""simple docstring"""



from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin





@dataclass
class _lowerCAmelCase    (				snake_case_     ):
   __UpperCAmelCase			:	torch.FloatTensor





class _lowerCAmelCase    (				snake_case_   ,							snake_case_     ):







   @register_to_config
   def __init__(     self						,     UpperCamelCase__ = 16						,     UpperCamelCase__ = 88						,     UpperCamelCase__ = None						,     UpperCamelCase__ = None						,     UpperCamelCase__ = 1						,     UpperCamelCase__ = 0.0						,     UpperCamelCase__ = 32						,     UpperCamelCase__ = None						,     UpperCamelCase__ = False						,     UpperCamelCase__ = None						,     UpperCamelCase__ = "geglu"						,     UpperCamelCase__ = True						,     UpperCamelCase__ = True						,     )     ->		Union[str, Any]:

       '''simple docstring'''




       super().__init__()
       snake_case   :				Any				 =   num_attention_heads
       snake_case   :				Dict				 =   attention_head_dim
       snake_case   :				Union[str, Any]				 =   num_attention_heads * attention_head_dim

       snake_case   :				List[str]				 =   in_channels

       snake_case   :				int				 =   torch.nn.GroupNorm(num_groups=UpperCamelCase__						,     num_channels=UpperCamelCase__						,     eps=1e-6						,     affine=UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   nn.Linear(UpperCamelCase__						,     UpperCamelCase__						)

       # 3. Define transformers blocks
       snake_case   :				Union[str, Any]				 =   nn.ModuleList(
           [
               BasicTransformerBlock(
                   UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     dropout=UpperCamelCase__						,     cross_attention_dim=UpperCamelCase__						,     activation_fn=UpperCamelCase__						,     attention_bias=UpperCamelCase__						,     double_self_attention=UpperCamelCase__						,     norm_elementwise_affine=UpperCamelCase__						,     )
               for d in range(UpperCamelCase__						)
           ]						)

       snake_case   :				List[str]				 =   nn.Linear(UpperCamelCase__						,     UpperCamelCase__						)







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__=None						,     UpperCamelCase__=None						,     UpperCamelCase__=None						,     UpperCamelCase__=1						,     UpperCamelCase__=None						,     UpperCamelCase__ = True						,     )     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Tuple				 =   hidden_states.shape
       snake_case   :				Union[str, Any]				 =   batch_frames // num_frames

       snake_case   :				Any				 =   hidden_states

       snake_case   :				Tuple				 =   hidden_states[None, :].reshape(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)
       snake_case   :				int				 =   hidden_states.permute(0						,     2						,     1						,     3						,     4						)

       snake_case   :				int				 =   self.norm(UpperCamelCase__						)
       snake_case   :				List[Any]				 =   hidden_states.permute(0						,     3						,     4						,     2						,     1						).reshape(batch_size * height * width						,     UpperCamelCase__						,     UpperCamelCase__						)

       snake_case   :				Any				 =   self.proj_in(UpperCamelCase__						)

       # 2. Blocks
       for block in self.transformer_blocks:
           snake_case   :				Tuple				 =   block(
               UpperCamelCase__						,     encoder_hidden_states=UpperCamelCase__						,     timestep=UpperCamelCase__						,     cross_attention_kwargs=UpperCamelCase__						,     class_labels=UpperCamelCase__						,     )

       # 3. Output
       snake_case   :				Tuple				 =   self.proj_out(UpperCamelCase__						)
       snake_case   :				List[str]				 =   (
           hidden_states[None, None, :]
           .reshape(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)
           .permute(0						,     3						,     4						,     1						,     2						)
           .contiguous()
       )
       snake_case   :				Any				 =   hidden_states.reshape(UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)

       snake_case   :				Tuple				 =   hidden_states + residual

       if not return_dict:
           return (output,)

       return TransformerTemporalModelOutput(sample=UpperCamelCase__						)









"""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 _lowerCAmelCase    (				unittest.TestCase     ):







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				int				 =   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 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   self.dummy_uncond_unet
       snake_case   :				Tuple				 =   KarrasVeScheduler()

       snake_case   :				int				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Optional[Any]				 =   torch.manual_seed(0						)
       snake_case   :				List[Any]				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Dict				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						,     return_dict=UpperCamelCase__						)[0]

       snake_case   :				Tuple				 =   image[0, -3:, -3:, -1]
       snake_case   :				Tuple				 =   image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case   :				int				 =   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 _lowerCAmelCase    (				unittest.TestCase     ):







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   "google/ncsnpp-celebahq-256"
       snake_case   :				List[str]				 =   UNetaDModel.from_pretrained(UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   KarrasVeScheduler()

       snake_case   :				Optional[int]				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Union[str, Any]				 =   pipe(num_inference_steps=20						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				int				 =   image[0, -3:, -3:, -1]
       assert image.shape == (1, 256, 256, 3)
       snake_case   :				Any				 =   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





