



'''simple docstring'''



# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowerCAmelCase	:		str				=   abspath(join(dirname(__file__), '''src'''))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='''ignore''', category=FutureWarning)



def   __lowerCAmelCase					(							lowerCamelCase   :						Any					):






	'''simple docstring'''




	config.addinivalue_line(
	    "markers"				,		"is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"					)
	config.addinivalue_line(
	    "markers"				,		"is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"					)
	config.addinivalue_line("markers"				,		"is_pipeline_test: mark test to run only when pipelines are tested"					)
	config.addinivalue_line("markers"				,		"is_staging_test: mark test to run only in the staging environment"					)
	config.addinivalue_line("markers"				,		"accelerate_tests: mark test that require accelerate"					)
	config.addinivalue_line("markers"				,		"tool_tests: mark the tool tests that are run on their specific schedule"					)



def   __lowerCAmelCase					(							lowerCamelCase   :						str					):






	'''simple docstring'''




	from transformers.testing_utils import pytest_addoption_shared

	pytest_addoption_shared(lowerCamelCase					)



def   __lowerCAmelCase					(							lowerCamelCase   :						Tuple					):






	'''simple docstring'''




	from transformers.testing_utils import pytest_terminal_summary_main

	__lowerCAmelCase												=  terminalreporter.config.getoption("--make-reports"					)
	if make_reports:
		pytest_terminal_summary_main(lowerCamelCase				,		id=lowerCamelCase					)



def   __lowerCAmelCase					(							lowerCamelCase   :						List[str]				,		lowerCamelCase   :						Tuple					):






	'''simple docstring'''




	if exitstatus == 5:
		__lowerCAmelCase												=  0


# Doctest custom flag to ignore output.
lowerCAmelCase	:		List[Any]				=   doctest.register_optionflag('''IGNORE_RESULT''')

lowerCAmelCase	:		Any				=   doctest.OutputChecker






class 					UpperCAmelCase__	(		UpperCamelCase__ ):



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Dict:
						if IGNORE_RESULT & optionflags:
							return True
						return OutputChecker.check_output(self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )


lowerCAmelCase	:		List[str]				=   CustomOutputChecker
lowerCAmelCase	:		int				=   HfDoctestModule




lowerCAmelCase	:		Dict				=   HfDocTestParser




'''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__	(		UpperCamelCase__ ):
					a      :     torch.FloatTensor






class 					UpperCAmelCase__	(		UpperCamelCase__							,       UpperCamelCase__ ):



					@register_to_config
					def __init__(    self			,				UpperCamelCase = 16			,				UpperCamelCase = 88			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = 1			,				UpperCamelCase = 0.0			,				UpperCamelCase = 32			,				UpperCamelCase = None			,				UpperCamelCase = False			,				UpperCamelCase = None			,				UpperCamelCase = "geglu"			,				UpperCamelCase = True			,				UpperCamelCase = True			,				)     ->			List[str]:
						super().__init__()
						__lowerCAmelCase												=  num_attention_heads
						__lowerCAmelCase												=  attention_head_dim
						__lowerCAmelCase												=  num_attention_heads * attention_head_dim

						__lowerCAmelCase												=  in_channels

						__lowerCAmelCase												=  torch.nn.GroupNorm(num_groups=UpperCamelCase			,				num_channels=UpperCamelCase			,				eps=1E-6			,				affine=UpperCamelCase     )
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )

						# 3. Define transformers blocks
						__lowerCAmelCase												=  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     )
						    ]     )

						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase=None			,				UpperCamelCase=None			,				UpperCamelCase=None			,				UpperCamelCase=1			,				UpperCamelCase=None			,				UpperCamelCase = True			,				)     ->			List[str]:
						__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  hidden_states.shape
						__lowerCAmelCase												=  batch_frames // num_frames

						__lowerCAmelCase												=  hidden_states

						__lowerCAmelCase												=  hidden_states[None, :].reshape(UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  hidden_states.permute(0			,				2			,				1			,				3			,				4     )

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

						__lowerCAmelCase												=  self.proj_in(UpperCamelCase     )

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

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

						__lowerCAmelCase												=  hidden_states + residual

						if not return_dict:
							return (output,)

						return TransformerTemporalModelOutput(sample=UpperCamelCase     )