
"""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 						SCREAMING_SNAKE_CASE_ (						__a ):




		"""simple docstring"""





		__lowercase							:  torch.FloatTensor



class 						SCREAMING_SNAKE_CASE_ (						__a     ,				__a ):




		"""simple docstring"""





		@register_to_config
		def __init__(     self							,  lowerCAmelCase__ = 1_6							,  lowerCAmelCase__ = 8_8							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = 1							,  lowerCAmelCase__ = 0.0							,  lowerCAmelCase__ = 3_2							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = False							,  lowerCAmelCase__ = None							,  lowerCAmelCase__ = "geglu"							,  lowerCAmelCase__ = True							,  lowerCAmelCase__ = True							,  ):
				super().__init__()
				__SCREAMING_SNAKE_CASE            = num_attention_heads
				__SCREAMING_SNAKE_CASE            = attention_head_dim
				__SCREAMING_SNAKE_CASE            = num_attention_heads * attention_head_dim

				__SCREAMING_SNAKE_CASE            = in_channels

				__SCREAMING_SNAKE_CASE            = torch.nn.GroupNorm(num_groups=lowerCAmelCase__							,  num_channels=lowerCAmelCase__							,  eps=1E-6							,  affine=lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = nn.Linear(lowerCAmelCase__							,  lowerCAmelCase__)

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

				__SCREAMING_SNAKE_CASE            = nn.Linear(lowerCAmelCase__							,  lowerCAmelCase__)




		def 	snake_case_     (     self							,  lowerCAmelCase__							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  lowerCAmelCase__=None							,  lowerCAmelCase__=1							,  lowerCAmelCase__=None							,  lowerCAmelCase__ = True							,  ):
				__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = hidden_states.shape
				__SCREAMING_SNAKE_CASE            = batch_frames // num_frames

				__SCREAMING_SNAKE_CASE            = hidden_states

				__SCREAMING_SNAKE_CASE            = hidden_states[None, :].reshape(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = hidden_states.permute(0							,  2							,  1							,  3							,  4)

				__SCREAMING_SNAKE_CASE            = self.norm(lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = hidden_states.permute(0							,  3							,  4							,  2							,  1).reshape(batch_size * height * width							,  lowerCAmelCase__							,  lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = self.proj_in(lowerCAmelCase__)

				# 2. Blocks
				for block in self.transformer_blocks:
						__SCREAMING_SNAKE_CASE            = block(
						    lowerCAmelCase__							,  encoder_hidden_states=lowerCAmelCase__							,  timestep=lowerCAmelCase__							,  cross_attention_kwargs=lowerCAmelCase__							,  class_labels=lowerCAmelCase__							,  )

				# 3. Output
				__SCREAMING_SNAKE_CASE            = self.proj_out(lowerCAmelCase__)
				__SCREAMING_SNAKE_CASE            = (
				    hidden_states[None, None, :]
				    .reshape(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__)
				    .permute(0							,  3							,  4							,  1							,  2)
				    .contiguous()
				)
				__SCREAMING_SNAKE_CASE            = hidden_states.reshape(lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__							,  lowerCAmelCase__)

				__SCREAMING_SNAKE_CASE            = hidden_states + residual

				if not return_dict:
						return (output,)

				return TransformerTemporalModelOutput(sample=lowerCAmelCase__)






"""simple docstring"""





def 						_lowerCAmelCase				(				UpperCamelCase_				):

		if isinstance(UpperCamelCase_  ,				UpperCamelCase_				):
				raise TypeError("""'float' object cannot be interpreted as an integer"""				)
		if isinstance(UpperCamelCase_  ,				UpperCamelCase_				):
				raise TypeError("""'str' object cannot be interpreted as an integer"""				)

		if num == 0:
				return "0b0"

		__SCREAMING_SNAKE_CASE            = False

		if num < 0:
				__SCREAMING_SNAKE_CASE            = True
				__SCREAMING_SNAKE_CASE            = -num

		__SCREAMING_SNAKE_CASE            = []
		while num > 0:
				binary.insert(0  ,				num % 2				)
				num >>= 1

		if negative:
				return "-0b" + "".join(str(UpperCamelCase_				) for e in binary				)

		return "0b" + "".join(str(UpperCamelCase_				) for e in binary				)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




