





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


_UpperCAmelCase				:       str	   =				logging.get_logger(__name__)

_UpperCAmelCase				:       Any	   =				{
    """facebook/timesformer""": """https://huggingface.co/facebook/timesformer/resolve/main/config.json""",
}

class    lowerCAmelCase							(					__UpperCamelCase			):
 UpperCAmelCase__													=		"""timesformer"""
 def __init__(      self  :      Optional[int]			,     UpperCAmelCase  :      Any=224			,     UpperCAmelCase  :      Union[str, Any]=16			,     UpperCAmelCase  :      List[str]=3			,     UpperCAmelCase  :      Optional[Any]=8			,     UpperCAmelCase  :      Any=768			,     UpperCAmelCase  :      str=12			,     UpperCAmelCase  :      Dict=12			,     UpperCAmelCase  :      List[Any]=3072			,     UpperCAmelCase  :      Any="gelu"			,     UpperCAmelCase  :      Optional[Any]=0.0			,     UpperCAmelCase  :      str=0.0			,     UpperCAmelCase  :      Tuple=0.0_2			,     UpperCAmelCase  :      str=1e-6			,     UpperCAmelCase  :      List[Any]=True			,     UpperCAmelCase  :      Dict="divided_space_time"			,     UpperCAmelCase  :      Dict=0			,     **UpperCAmelCase  :      Tuple			,     )			->	int:
     super().__init__(**UpperCAmelCase	)

     lowerCamelCase__    :      Tuple					 =		image_size
     lowerCamelCase__    :      Dict					 =		patch_size
     lowerCamelCase__    :      int					 =		num_channels
     lowerCamelCase__    :      Dict					 =		num_frames

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

     lowerCamelCase__    :      int					 =		attention_type
     lowerCamelCase__    :      int					 =		drop_path_rate












import flax.linen as nn
import jax
import jax.numpy as jnp

class    lowerCAmelCase							(					nn.Module			):
 UpperCAmelCase__													=		42
 UpperCAmelCase__													=		jnp.floataa
 def      A_	(      self  :      Any	)			->	Any:
     lowerCamelCase__    :      str					 =		nn.Conv(
         self.out_channels			,     kernel_size=(3, 3)			,     strides=(1, 1)			,     padding=((1, 1), (1, 1))			,     dtype=self.dtype			,     )







 def __call__(      self  :      int			,     UpperCAmelCase  :      Dict	)			->	Optional[Any]:
     lowerCamelCase__ ,							lowerCamelCase__ ,							lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		hidden_states.shape
     lowerCamelCase__    :      Union[str, Any]					 =		jax.image.resize(
         UpperCAmelCase			,     shape=(batch, height * 2, width * 2, channels)			,     method='nearest'			,     )
     lowerCamelCase__    :      Optional[Any]					 =		self.conv(UpperCAmelCase	)
     return hidden_states


class    lowerCAmelCase							(					nn.Module			):
 UpperCAmelCase__													=		42
 UpperCAmelCase__													=		jnp.floataa
 def      A_	(      self  :      List[str]	)			->	int:
     lowerCamelCase__    :      Tuple					 =		nn.Conv(
         self.out_channels			,     kernel_size=(3, 3)			,     strides=(2, 2)			,     padding=((1, 1), (1, 1))			,     dtype=self.dtype			,     )







 def __call__(      self  :      str			,     UpperCAmelCase  :      Union[str, Any]	)			->	Optional[Any]:
     # pad = ((0, 0), (0, 1), (0, 1), (0, 0))  # pad height and width dim
     # hidden_states = jnp.pad(hidden_states, pad_width=pad)
     lowerCamelCase__    :      Optional[Any]					 =		self.conv(UpperCAmelCase	)
     return hidden_states


class    lowerCAmelCase							(					nn.Module			):
 UpperCAmelCase__													=		42
 UpperCAmelCase__													=		None
 UpperCAmelCase__													=		0.0
 UpperCAmelCase__													=		None
 UpperCAmelCase__													=		jnp.floataa
 def      A_	(      self  :      List[str]	)			->	Union[str, Any]:
     lowerCamelCase__    :      Optional[Any]					 =		self.in_channels if self.out_channels is None else self.out_channels

     lowerCamelCase__    :      Tuple					 =		nn.GroupNorm(num_groups=32			,     epsilon=1e-5	)
     lowerCamelCase__    :      int					 =		nn.Conv(
         UpperCAmelCase			,     kernel_size=(3, 3)			,     strides=(1, 1)			,     padding=((1, 1), (1, 1))			,     dtype=self.dtype			,     )

     lowerCamelCase__    :      Union[str, Any]					 =		nn.Dense(UpperCAmelCase			,     dtype=self.dtype	)

     lowerCamelCase__    :      Union[str, Any]					 =		nn.GroupNorm(num_groups=32			,     epsilon=1e-5	)
     lowerCamelCase__    :      List[Any]					 =		nn.Dropout(self.dropout_prob	)
     lowerCamelCase__    :      Tuple					 =		nn.Conv(
         UpperCAmelCase			,     kernel_size=(3, 3)			,     strides=(1, 1)			,     padding=((1, 1), (1, 1))			,     dtype=self.dtype			,     )

     lowerCamelCase__    :      Optional[Any]					 =		self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

     lowerCamelCase__    :      Union[str, Any]					 =		None
     if use_nin_shortcut:
         lowerCamelCase__    :      Dict					 =		nn.Conv(
             UpperCAmelCase			,     kernel_size=(1, 1)			,     strides=(1, 1)			,     padding='VALID'			,     dtype=self.dtype			,     )







 def __call__(      self  :      Optional[Any]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      int=True	)			->	Optional[int]:
     lowerCamelCase__    :      Union[str, Any]					 =		hidden_states
     lowerCamelCase__    :      List[Any]					 =		self.norma(UpperCAmelCase	)
     lowerCamelCase__    :      List[Any]					 =		nn.swish(UpperCAmelCase	)
     lowerCamelCase__    :      Any					 =		self.conva(UpperCAmelCase	)

     lowerCamelCase__    :      Optional[Any]					 =		self.time_emb_proj(nn.swish(UpperCAmelCase	)	)
     lowerCamelCase__    :      List[str]					 =		jnp.expand_dims(jnp.expand_dims(UpperCAmelCase			,     1	)			,     1	)
     lowerCamelCase__    :      List[str]					 =		hidden_states + temb

     lowerCamelCase__    :      Optional[Any]					 =		self.norma(UpperCAmelCase	)
     lowerCamelCase__    :      List[str]					 =		nn.swish(UpperCAmelCase	)
     lowerCamelCase__    :      Optional[int]					 =		self.dropout(UpperCAmelCase			,     UpperCAmelCase	)
     lowerCamelCase__    :      str					 =		self.conva(UpperCAmelCase	)

     if self.conv_shortcut is not None:
         lowerCamelCase__    :      Dict					 =		self.conv_shortcut(UpperCAmelCase	)

     return hidden_states + residual





