





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_       (     UpperCamelCase__    ):
							lowerCamelCase_           =					42




class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__    ):


							@register_to_config
							def __init__(							self    :Optional[Any]							,				__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							,				)   ->   List[Any]:



									"""simple docstring"""



									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=__A							,				num_channels=__A							,				eps=1E-6							,				affine=__A		)
									SCREAMING_SNAKE_CASE__			=      nn.Linear(__A							,				__A		)

									# 3. Define transformers blocks
									SCREAMING_SNAKE_CASE__			=      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__			=      nn.Linear(__A							,				__A		)


							def      _snake_case  (							self    :Any							,				__A    :int							,				__A    :int=None							,				__A    :Tuple=None							,				__A    :str=None							,				__A    :str=1							,				__A    :str=None							,				__A    :bool = True							,				)   ->   List[str]:



									"""simple docstring"""



									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(__A							,				__A							,				__A							,				__A							,				__A		)
									SCREAMING_SNAKE_CASE__			=      hidden_states.permute(0							,				2							,				1							,				3							,				4		)

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

									SCREAMING_SNAKE_CASE__			=      self.proj_in(__A		)

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

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

									SCREAMING_SNAKE_CASE__			=      hidden_states + residual

									if not return_dict:
											return (output,)

									return TransformerTemporalModelOutput(sample=__A		)






from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCamelCase   				=			logging.get_logger(__name__)

_lowerCamelCase   				=			{
    'shi-labs/nat-mini-in1k-224': 'https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json',
    # See all Nat models at https://huggingface.co/models?filter=nat
}




class   UpperCamelCase_       (     UpperCamelCase__				,  UpperCamelCase__    ):
							lowerCamelCase_           =					"nat"

							lowerCamelCase_           =					{
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}


							def __init__(							self    :List[Any]							,				__A    :Optional[Any]=4							,				__A    :Any=3							,				__A    :Optional[int]=64							,				__A    :Optional[int]=[3, 4, 6, 5]							,				__A    :Union[str, Any]=[2, 4, 8, 16]							,				__A    :Optional[Any]=7							,				__A    :Optional[Any]=3.0							,				__A    :List[Any]=True							,				__A    :int=0.0							,				__A    :Dict=0.0							,				__A    :Optional[Any]=0.1							,				__A    :str="gelu"							,				__A    :Optional[Any]=0.0_2							,				__A    :Optional[int]=1E-5							,				__A    :Optional[int]=0.0							,				__A    :Optional[Any]=None							,				__A    :Union[str, Any]=None							,				**__A    :Union[str, Any]							,				)   ->   Optional[int]:



									"""simple docstring"""



									super().__init__(**__A		)

									SCREAMING_SNAKE_CASE__			=      patch_size
									SCREAMING_SNAKE_CASE__			=      num_channels
									SCREAMING_SNAKE_CASE__			=      embed_dim
									SCREAMING_SNAKE_CASE__			=      depths
									SCREAMING_SNAKE_CASE__			=      len(__A		)
									SCREAMING_SNAKE_CASE__			=      num_heads
									SCREAMING_SNAKE_CASE__			=      kernel_size
									SCREAMING_SNAKE_CASE__			=      mlp_ratio
									SCREAMING_SNAKE_CASE__			=      qkv_bias
									SCREAMING_SNAKE_CASE__			=      hidden_dropout_prob
									SCREAMING_SNAKE_CASE__			=      attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__			=      drop_path_rate
									SCREAMING_SNAKE_CASE__			=      hidden_act
									SCREAMING_SNAKE_CASE__			=      layer_norm_eps
									SCREAMING_SNAKE_CASE__			=      initializer_range
									# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
									# this indicates the channel dimension after the last stage of the model
									SCREAMING_SNAKE_CASE__			=      int(embed_dim * 2 ** (len(__A		) - 1)		)
									SCREAMING_SNAKE_CASE__			=      layer_scale_init_value
									SCREAMING_SNAKE_CASE__			=      ["""stem"""] + [f'''stage{idx}''' for idx in range(1							,				len(__A		) + 1		)]
									SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      get_aligned_output_features_output_indices(
									    out_features=__A							,				out_indices=__A							,				stage_names=self.stage_names		)