
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     __UpperCamelCase (							lowerCAmelCase__       ):






     """simple docstring"""







     lowerCAmelCase_							   =			42






class     __UpperCamelCase (							lowerCAmelCase__ ,						lowerCAmelCase__       ):






     """simple docstring"""







     @register_to_config
     def __init__(				self							:							Dict     ,						_A							:							int = 16     ,						_A							:							int = 88     ,						_A							:							Optional[int] = None     ,						_A							:							Optional[int] = None     ,						_A							:							int = 1     ,						_A							:							float = 0.0     ,						_A							:							int = 32     ,						_A							:							Optional[int] = None     ,						_A							:							bool = False     ,						_A							:							Optional[int] = None     ,						_A							:							str = "geglu"     ,						_A							:							bool = True     ,						_A							:							bool = True     ,						):







       """simple docstring"""




       super().__init__()
       __SCREAMING_SNAKE_CASE      :	Dict					=		num_attention_heads
       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		attention_head_dim
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		num_attention_heads * attention_head_dim

       __SCREAMING_SNAKE_CASE      :	Tuple					=		in_channels

       __SCREAMING_SNAKE_CASE      :	str					=		torch.nn.GroupNorm(num_groups=_A     ,						num_channels=_A     ,						eps=1e-6     ,						affine=_A )
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		nn.Linear(_A     ,						_A )

       # 3. Define transformers blocks
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		nn.ModuleList(
           [
               BasicTransformerBlock(
                   _A     ,						_A     ,						_A     ,						dropout=_A     ,						cross_attention_dim=_A     ,						activation_fn=_A     ,						attention_bias=_A     ,						double_self_attention=_A     ,						norm_elementwise_affine=_A     ,						)
               for d in range(_A )
           ] )

       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		nn.Linear(_A     ,						_A )




     def  UpperCAmelCase__							(				self							:							str     ,						_A							:							Dict     ,						_A							:							int=None     ,						_A							:							Tuple=None     ,						_A							:							Dict=None     ,						_A							:							List[Any]=1     ,						_A							:							Union[str, Any]=None     ,						_A							:							bool = True     ,						):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE      :	List[Any]					=		hidden_states.shape
       __SCREAMING_SNAKE_CASE      :	Any					=		batch_frames // num_frames

       __SCREAMING_SNAKE_CASE      :	Dict					=		hidden_states

       __SCREAMING_SNAKE_CASE      :	str					=		hidden_states[None, :].reshape(_A     ,						_A     ,						_A     ,						_A     ,						_A )
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		hidden_states.permute(0     ,						2     ,						1     ,						3     ,						4 )

       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		self.norm(_A )
       __SCREAMING_SNAKE_CASE      :	List[str]					=		hidden_states.permute(0     ,						3     ,						4     ,						2     ,						1 ).reshape(batch_size * height * width     ,						_A     ,						_A )

       __SCREAMING_SNAKE_CASE      :	List[Any]					=		self.proj_in(_A )

       # 2. Blocks
       for block in self.transformer_blocks:
         __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		block(
             _A     ,						encoder_hidden_states=_A     ,						timestep=_A     ,						cross_attention_kwargs=_A     ,						class_labels=_A     ,						)

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

       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		hidden_states + residual

       if not return_dict:
         return (output,)

       return TransformerTemporalModelOutput(sample=_A )







'''simple docstring'''




import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch




@require_torch
class      UpperCamelCase__					:




					"""simple docstring"""


					@property
					def lowerCamelCase_			(						self				:			Union[str, Any]						):

							'''simple docstring'''



							return self.get_dummy_input()


					@property
					def lowerCamelCase_			(						self				:			List[Any]						):

							'''simple docstring'''



							if self.block_type == "down":
									return (4, 32, 16, 16)
							elif self.block_type == "mid":
									return (4, 32, 32, 32)
							elif self.block_type == "up":
									return (4, 32, 64, 64)

							raise ValueError(f'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''						)


					def lowerCamelCase_			(						self				:			Union[str, Any]      ,						lowerCamelCase_				:			Dict=True      ,						lowerCamelCase_				:			List[Any]=False      ,						lowerCamelCase_				:			str=False      ,						lowerCamelCase_				:			Optional[int]=False      ,						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Dict		=			4
							SCREAMING_SNAKE_CASE :				Any		=			32
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			(32, 32)

							SCREAMING_SNAKE_CASE :				int		=			torch.manual_seed(0						)
							SCREAMING_SNAKE_CASE :				Optional[Any]		=			torch.device(lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Tuple		=			(batch_size, num_channels) + sizes
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			randn_tensor(lowerCamelCase_      ,						generator=lowerCamelCase_      ,						device=lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			{"""hidden_states""": hidden_states}

							if include_temb:
									SCREAMING_SNAKE_CASE :				Union[str, Any]		=			1_28
									SCREAMING_SNAKE_CASE :				Optional[Any]		=			randn_tensor((batch_size, temb_channels)      ,						generator=lowerCamelCase_      ,						device=lowerCamelCase_						)

							if include_res_hidden_states_tuple:
									SCREAMING_SNAKE_CASE :				str		=			torch.manual_seed(1						)
									SCREAMING_SNAKE_CASE :				int		=			(randn_tensor(lowerCamelCase_      ,						generator=lowerCamelCase_      ,						device=lowerCamelCase_						),)

							if include_encoder_hidden_states:
									SCREAMING_SNAKE_CASE :				Optional[int]		=			floats_tensor((batch_size, 32, 32)						).to(lowerCamelCase_						)

							if include_skip_sample:
									SCREAMING_SNAKE_CASE :				str		=			randn_tensor(((batch_size, 3) + sizes)      ,						generator=lowerCamelCase_      ,						device=lowerCamelCase_						)

							return dummy_input


					def lowerCamelCase_			(						self				:			Optional[Any]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Dict		=			{
							    """in_channels""": 32,
							    """out_channels""": 32,
							    """temb_channels""": 1_28,
							}
							if self.block_type == "up":
									SCREAMING_SNAKE_CASE :				List[str]		=			32

							if self.block_type == "mid":
									init_dict.pop("""out_channels"""						)

							SCREAMING_SNAKE_CASE :				Tuple		=			self.dummy_input
							return init_dict, inputs_dict


					def lowerCamelCase_			(						self				:			Any      ,						lowerCamelCase_				:			Any						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Optional[Any]		=			self.prepare_init_args_and_inputs_for_common()
							SCREAMING_SNAKE_CASE :				Tuple		=			self.block_class(**lowerCamelCase_						)
							unet_block.to(lowerCamelCase_						)
							unet_block.eval()

							with torch.no_grad():
									SCREAMING_SNAKE_CASE :				List[str]		=			unet_block(**lowerCamelCase_						)

							if isinstance(lowerCamelCase_      ,						lowerCamelCase_						):
									SCREAMING_SNAKE_CASE :				Dict		=			output[0]

							self.assertEqual(output.shape      ,						self.output_shape						)

							SCREAMING_SNAKE_CASE :				Dict		=			output[0, -1, -3:, -3:]
							SCREAMING_SNAKE_CASE :				List[Any]		=			torch.tensor(lowerCamelCase_						).to(lowerCamelCase_						)
							assert torch_all_close(output_slice.flatten()      ,						lowerCamelCase_      ,						atol=5e-3						)



					@unittest.skipIf(torch_device == """mps"""      ,						"""Training is not supported in mps"""						)
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE,							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.prepare_init_args_and_inputs_for_common()
							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.block_class(**lowerCamelCase_						)
							model.to(lowerCamelCase_						)
							model.train()
							SCREAMING_SNAKE_CASE :				Tuple		=			model(**lowerCamelCase_						)

							if isinstance(lowerCamelCase_      ,						lowerCamelCase_						):
									SCREAMING_SNAKE_CASE :				Optional[Any]		=			output[0]

							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			torch.device(lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				Optional[int]		=			randn_tensor(output.shape      ,						device=lowerCamelCase_						)
							SCREAMING_SNAKE_CASE :				str		=			torch.nn.functional.mse_loss(lowerCamelCase_      ,						lowerCamelCase_						)
							loss.backward()



