





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        _a(    __A    ):
		lowerCamelCase__     :torch.FloatTensor






class        _a(    __A	,   __A    ):







		@register_to_config
		def __init__(    self ,						__snake_case = 1_6 ,						__snake_case = 8_8 ,						__snake_case = None ,						__snake_case = None ,						__snake_case = 1 ,						__snake_case = 0.0 ,						__snake_case = 3_2 ,						__snake_case = None ,						__snake_case = False ,						__snake_case = None ,						__snake_case = "geglu" ,						__snake_case = True ,						__snake_case = True ,						)      ->      List[str]:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					List[str]				       =   num_attention_heads
							_snake_case       :					str				       =   attention_head_dim
							_snake_case       :					Optional[int]				       =   num_attention_heads * attention_head_dim

							_snake_case       :					Optional[int]				       =   in_channels

							_snake_case       :					Optional[int]				       =   torch.nn.GroupNorm(num_groups=__snake_case ,						num_channels=__snake_case ,						eps=1E-6 ,						affine=__snake_case				)
							_snake_case       :					Optional[int]				       =   nn.Linear(__snake_case ,						__snake_case				)

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

							_snake_case       :					Tuple				       =   nn.Linear(__snake_case ,						__snake_case				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case=None ,						__snake_case=None ,						__snake_case=None ,						__snake_case=1 ,						__snake_case=None ,						__snake_case = True ,						)      ->      int:


							'''simple docstring'''

							_snake_case       , _snake_case       , _snake_case       , _snake_case       :					Union[str, Any]				       =   hidden_states.shape
							_snake_case       :					Optional[int]				       =   batch_frames // num_frames

							_snake_case       :					Tuple				       =   hidden_states

							_snake_case       :					int				       =   hidden_states[None, :].reshape(__snake_case ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case				)
							_snake_case       :					Dict				       =   hidden_states.permute(0 ,						2 ,						1 ,						3 ,						4				)

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

							_snake_case       :					List[str]				       =   self.proj_in(__snake_case				)

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

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

							_snake_case       :					List[Any]				       =   hidden_states + residual

							if not return_dict:
												return (output,)

							return TransformerTemporalModelOutput(sample=__snake_case				)






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCAmelCase :Tuple     =    logging.get_logger(__name__)

__lowerCAmelCase :int     =    {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}






class        _a(    __A    ):
		lowerCamelCase__     :Optional[Any]   				=		'timesformer'







		def __init__(    self ,						__snake_case=2_2_4 ,						__snake_case=1_6 ,						__snake_case=3 ,						__snake_case=8 ,						__snake_case=7_6_8 ,						__snake_case=1_2 ,						__snake_case=1_2 ,						__snake_case=3_0_7_2 ,						__snake_case="gelu" ,						__snake_case=0.0 ,						__snake_case=0.0 ,						__snake_case=0.02 ,						__snake_case=1E-6 ,						__snake_case=True ,						__snake_case="divided_space_time" ,						__snake_case=0 ,						**__snake_case ,						)      ->      str:


							'''simple docstring'''

							super().__init__(**__snake_case				)

							_snake_case       :					Optional[Any]				       =   image_size
							_snake_case       :					Optional[int]				       =   patch_size
							_snake_case       :					str				       =   num_channels
							_snake_case       :					Tuple				       =   num_frames

							_snake_case       :					Union[str, Any]				       =   hidden_size
							_snake_case       :					Optional[int]				       =   num_hidden_layers
							_snake_case       :					List[Any]				       =   num_attention_heads
							_snake_case       :					Optional[int]				       =   intermediate_size
							_snake_case       :					List[str]				       =   hidden_act
							_snake_case       :					Optional[int]				       =   hidden_dropout_prob
							_snake_case       :					Optional[Any]				       =   attention_probs_dropout_prob
							_snake_case       :					Dict				       =   initializer_range
							_snake_case       :					Optional[int]				       =   layer_norm_eps
							_snake_case       :					str				       =   qkv_bias

							_snake_case       :					List[str]				       =   attention_type
							_snake_case       :					Optional[int]				       =   drop_path_rate