






"""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        UpperCamelCase			(	snake_case_      ):
 UpperCamelCase					:       torch.FloatTensor







class        UpperCamelCase			(	snake_case_		,     snake_case_      ):


 @register_to_config
 def __init__(     self		:						int							,						UpperCAmelCase__		:						int = 16							,						UpperCAmelCase__		:						int = 88							,						UpperCAmelCase__		:						Optional[int] = None							,						UpperCAmelCase__		:						Optional[int] = None							,						UpperCAmelCase__		:						int = 1							,						UpperCAmelCase__		:						float = 0.0							,						UpperCAmelCase__		:						int = 32							,						UpperCAmelCase__		:						Optional[int] = None							,						UpperCAmelCase__		:						bool = False							,						UpperCAmelCase__		:						Optional[int] = None							,						UpperCAmelCase__		:						str = "geglu"							,						UpperCAmelCase__		:						bool = True							,						UpperCAmelCase__		:						bool = True							,						)					->       str:
   super().__init__()
   _a  :       List[str]             =	num_attention_heads
   _a  :       str             =	attention_head_dim
   _a  :       List[Any]             =	num_attention_heads * attention_head_dim

   _a  :       int             =	in_channels

   _a  :       str             =	torch.nn.GroupNorm(num_groups=UpperCAmelCase__							,						num_channels=UpperCAmelCase__							,						eps=1E-6							,						affine=UpperCAmelCase__							)
   _a  :       Tuple             =	nn.Linear(UpperCAmelCase__							,						UpperCAmelCase__							)

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

   _a  :       Any             =	nn.Linear(UpperCAmelCase__							,						UpperCAmelCase__							)







 def _lowercase	(     self		:						Optional[Any]							,						UpperCAmelCase__		:						int							,						UpperCAmelCase__		:						List[Any]=None							,						UpperCAmelCase__		:						Optional[Any]=None							,						UpperCAmelCase__		:						int=None							,						UpperCAmelCase__		:						Union[str, Any]=1							,						UpperCAmelCase__		:						int=None							,						UpperCAmelCase__		:						bool = True							,						)					->       Dict:
   _a      ,  _a      ,  _a      ,  _a  :       List[Any]             =	hidden_states.shape
   _a  :       str             =	batch_frames // num_frames

   _a  :       Any             =	hidden_states

   _a  :       Dict             =	hidden_states[None, :].reshape(UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							)
   _a  :       Tuple             =	hidden_states.permute(0							,						2							,						1							,						3							,						4							)

   _a  :       Tuple             =	self.norm(UpperCAmelCase__							)
   _a  :       Optional[int]             =	hidden_states.permute(0							,						3							,						4							,						2							,						1							).reshape(batch_size * height * width							,						UpperCAmelCase__							,						UpperCAmelCase__							)

   _a  :       Union[str, Any]             =	self.proj_in(UpperCAmelCase__							)

   # 2. Blocks
   for block in self.transformer_blocks:
     _a  :       Dict             =	block(
         UpperCAmelCase__							,						encoder_hidden_states=UpperCAmelCase__							,						timestep=UpperCAmelCase__							,						cross_attention_kwargs=UpperCAmelCase__							,						class_labels=UpperCAmelCase__							,						)

   # 3. Output
   _a  :       Tuple             =	self.proj_out(UpperCAmelCase__							)
   _a  :       Optional[int]             =	(
       hidden_states[None, None, :]
       .reshape(UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							)
       .permute(0							,						3							,						4							,						1							,						2							)
       .contiguous()
   )
   _a  :       List[str]             =	hidden_states.reshape(UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							)

   _a  :       Optional[int]             =	hidden_states + residual

   if not return_dict:
     return (output,)

   return TransformerTemporalModelOutput(sample=UpperCAmelCase__							)









"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_snake_case          =   {
    'configuration_perceiver': ['PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PerceiverConfig', 'PerceiverOnnxConfig'],
    'tokenization_perceiver': ['PerceiverTokenizer'],
}

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _snake_case          =   ['PerceiverFeatureExtractor']
 _snake_case          =   ['PerceiverImageProcessor']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _snake_case          =   [
     'PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST',
     'PerceiverForImageClassificationConvProcessing',
     'PerceiverForImageClassificationFourier',
     'PerceiverForImageClassificationLearned',
     'PerceiverForMaskedLM',
     'PerceiverForMultimodalAutoencoding',
     'PerceiverForOpticalFlow',
     'PerceiverForSequenceClassification',
     'PerceiverLayer',
     'PerceiverModel',
     'PerceiverPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig
 from .tokenization_perceiver import PerceiverTokenizer

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_perceiver import PerceiverFeatureExtractor
  from .image_processing_perceiver import PerceiverImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_perceiver import (
      PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST,
      PerceiverForImageClassificationConvProcessing,
      PerceiverForImageClassificationFourier,
      PerceiverForImageClassificationLearned,
      PerceiverForMaskedLM,
      PerceiverForMultimodalAutoencoding,
      PerceiverForOpticalFlow,
      PerceiverForSequenceClassification,
      PerceiverLayer,
      PerceiverModel,
      PerceiverPreTrainedModel,
  )

else:
 import sys

 _snake_case          =   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

