

from collections import OrderedDict
from typing import Mapping

from packaging import version

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


a_						     =    logging.get_logger(__name__)

a_						     =    {
    'microsoft/swin-tiny-patch4-window7-224': (
        'https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json'
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}






class _UpperCamelCase							(							__A							,	__A   ):
			'''simple docstring'''







			lowerCamelCase__           ='swin'

			lowerCamelCase__           ={
			    'num_attention_heads': 'num_heads',
			    'num_hidden_layers': 'num_layers',
			}
			def __init__(			self							: Tuple      ,      a							: Union[str, Any]=224      ,      a							: Optional[int]=4      ,      a							: Optional[Any]=3      ,      a							: Optional[Any]=96      ,      a							: int=[2, 2, 6, 2]      ,      a							: Any=[3, 6, 12, 24]      ,      a							: Union[str, Any]=7      ,      a							: Optional[Any]=4.0      ,      a							: Tuple=True      ,      a							: List[str]=0.0      ,      a							: str=0.0      ,      a							: List[str]=0.1      ,      a							: Union[str, Any]="gelu"      ,      a							: str=False      ,      a							: Tuple=0.02      ,      a							: int=1e-5      ,      a							: Optional[int]=32      ,      a							: Optional[Any]=None      ,      a							: List[str]=None      ,      **a							: Any      ,      )							->       List[Any]:

				"""simple docstring"""

				super().__init__(**a	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       image_size
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       patch_size
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       num_channels
				SCREAMING_SNAKE_CASE   :      str				     =       embed_dim
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       depths
				SCREAMING_SNAKE_CASE   :      str				     =       len(a	)
				SCREAMING_SNAKE_CASE   :      Any				     =       num_heads
				SCREAMING_SNAKE_CASE   :      str				     =       window_size
				SCREAMING_SNAKE_CASE   :      int				     =       mlp_ratio
				SCREAMING_SNAKE_CASE   :      List[str]				     =       qkv_bias
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       hidden_dropout_prob
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       drop_path_rate
				SCREAMING_SNAKE_CASE   :      int				     =       hidden_act
				SCREAMING_SNAKE_CASE   :      int				     =       use_absolute_embeddings
				SCREAMING_SNAKE_CASE   :      int				     =       layer_norm_eps
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       encoder_stride
				# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
				# this indicates the channel dimension after the last stage of the model
				SCREAMING_SNAKE_CASE   :      int				     =       int(embed_dim * 2 ** (len(a	) - 1)	)
				SCREAMING_SNAKE_CASE   :      int				     =       ["stem"] + [F"stage{idx}" for idx in range(1      ,      len(a	) + 1	)]
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[Any]				     =       get_aligned_output_features_output_indices(
				    out_features=a      ,      out_indices=a      ,      stage_names=self.stage_names	)






class _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			lowerCamelCase__           =version.parse('1.11'   )
			@property
			def 							__UpperCamelCase (			self							: Dict	)							->       Mapping[str, Mapping[int, str]]:

				"""simple docstring"""

				return OrderedDict(
				    [
				        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
				    ]	)






			@property
			def 							__UpperCamelCase (			self							: str	)							->       float:

				"""simple docstring"""

				return 1e-4


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							(							__A   ):
			'''simple docstring'''







			lowerCamelCase__           =42






class _UpperCamelCase							(							__A							,	__A   ):
			'''simple docstring'''







			@register_to_config
			def __init__(			self							: str      ,      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      ,      )							->       str:

				"""simple docstring"""

				super().__init__()
				SCREAMING_SNAKE_CASE   :      str				     =       num_attention_heads
				SCREAMING_SNAKE_CASE   :      Tuple				     =       attention_head_dim
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       num_attention_heads * attention_head_dim

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       in_channels

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       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[str]				     =       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[int]				     =       nn.Linear(a      ,      a	)






			def 							__UpperCamelCase (			self							: Any      ,      a							: int      ,      a							: Optional[int]=None      ,      a							: List[str]=None      ,      a							: List[str]=None      ,      a							: Union[str, Any]=1      ,      a							: Any=None      ,      a							: bool = True      ,      )							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Any				     =       hidden_states.shape
				SCREAMING_SNAKE_CASE   :      Any				     =       batch_frames // num_frames

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       hidden_states

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       hidden_states[None, :].reshape(a      ,      a      ,      a      ,      a      ,      a	)
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       hidden_states.permute(0      ,      2      ,      1      ,      3      ,      4	)

				SCREAMING_SNAKE_CASE   :      Tuple				     =       self.norm(a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       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[int]				     =       block(
					    a      ,      encoder_hidden_states=a      ,      timestep=a      ,      cross_attention_kwargs=a      ,      class_labels=a      ,      )

				# 3. Output
				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.proj_out(a	)
				SCREAMING_SNAKE_CASE   :      int				     =       (
				    hidden_states[None, None, :]
				    .reshape(a      ,      a      ,      a      ,      a      ,      a	)
				    .permute(0      ,      3      ,      4      ,      1      ,      2	)
				    .contiguous()
				)
				SCREAMING_SNAKE_CASE   :      Dict				     =       hidden_states.reshape(a      ,      a      ,      a      ,      a	)

				SCREAMING_SNAKE_CASE   :      int				     =       hidden_states + residual

				if not return_dict:
					return (output,)

				return TransformerTemporalModelOutput(sample=a	)