


"""simple docstring"""



import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
		    TimesformerForVideoClassification,
		    TimesformerModel,
		)
		from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from transformers import VideoMAEImageProcessor







class    __a						:







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase=13      ,	UpperCAmelCase=10      ,	UpperCAmelCase=3      ,	UpperCAmelCase=2      ,	UpperCAmelCase=2      ,	UpperCAmelCase=True      ,	UpperCAmelCase=True      ,	UpperCAmelCase=32      ,	UpperCAmelCase=5      ,	UpperCAmelCase=4      ,	UpperCAmelCase=37      ,	UpperCAmelCase="gelu"      ,	UpperCAmelCase=0.1      ,	UpperCAmelCase=0.1      ,	UpperCAmelCase=10      ,	UpperCAmelCase=0.0_2      ,	UpperCAmelCase="divided_space_time"      ,	UpperCAmelCase=None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			parent
					lowerCAmelCase_      							=			batch_size
					lowerCAmelCase_      							=			image_size
					lowerCAmelCase_      							=			num_channels
					lowerCAmelCase_      							=			patch_size
					lowerCAmelCase_      							=			num_frames
					lowerCAmelCase_      							=			is_training
					lowerCAmelCase_      							=			use_labels
					lowerCAmelCase_      							=			hidden_size
					lowerCAmelCase_      							=			num_hidden_layers
					lowerCAmelCase_      							=			num_attention_heads
					lowerCAmelCase_      							=			intermediate_size
					lowerCAmelCase_      							=			hidden_act
					lowerCAmelCase_      							=			hidden_dropout_prob
					lowerCAmelCase_      							=			attention_probs_dropout_prob
					lowerCAmelCase_      							=			attention_type
					lowerCAmelCase_      							=			initializer_range
					lowerCAmelCase_      							=			scope
					lowerCAmelCase_      							=			num_labels

					# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
					lowerCAmelCase_      							=			(image_size // patch_size) ** 2
					lowerCAmelCase_      							=			(num_frames) * self.num_patches_per_frame + 1







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]   )

					lowerCAmelCase_      							=			None
					if self.use_labels:
						lowerCAmelCase_      							=			ids_tensor([self.batch_size]      ,	self.num_labels   )

					lowerCAmelCase_      							=			self.get_config()

					return config, pixel_values, labels







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TimesformerConfig(
					    image_size=self.image_size      ,	patch_size=self.patch_size      ,	num_channels=self.num_channels      ,	num_frames=self.num_frames      ,	hidden_size=self.hidden_size      ,	num_hidden_layers=self.num_hidden_layers      ,	num_attention_heads=self.num_attention_heads      ,	intermediate_size=self.intermediate_size      ,	hidden_act=self.hidden_act      ,	hidden_dropout_prob=self.hidden_dropout_prob      ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,	initializer_range=self.initializer_range      ,	attention_type=self.attention_type      ,	)
					lowerCAmelCase_      							=			self.num_labels
					return config







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TimesformerModel(config=UpperCAmelCase   )
					model.to(UpperCAmelCase   )
					model.eval()
					lowerCAmelCase_      							=			model(UpperCAmelCase   )
					self.parent.assertEqual(result.last_hidden_state.shape      ,	(self.batch_size, self.seq_length, self.hidden_size)   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TimesformerForVideoClassification(UpperCAmelCase   )
					model.to(UpperCAmelCase   )
					model.eval()

					lowerCAmelCase_      							=			model(UpperCAmelCase   )

					# verify the logits shape
					lowerCAmelCase_      							=			torch.Size((self.batch_size, self.num_labels)   )
					self.parent.assertEqual(result.logits.shape      ,	UpperCAmelCase   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.prepare_config_and_inputs()
					lowerCAmelCase_							,	lowerCAmelCase_							,	lowerCAmelCase_      							=			config_and_inputs
					lowerCAmelCase_      							=			{'''pixel_values''': pixel_values}
					return config, inputs_dict







@require_torch
class    __a						( __snake_case					,			__snake_case					,			unittest.TestCase							):
				lowerCamelCase     :       Dict			       =(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
				lowerCamelCase     :       int			       =(
				    {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
				    if is_torch_available()
				    else {}
				)

				lowerCamelCase     :       str			       =False
				lowerCamelCase     :       Optional[int]			       =False
				lowerCamelCase     :       str			       =False
				lowerCamelCase     :       Optional[int]			       =False







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TimesformerModelTester(self   )
					lowerCAmelCase_      							=			ConfigTester(
					    self      ,	config_class=UpperCAmelCase      ,	has_text_modality=UpperCAmelCase      ,	hidden_size=37   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase=False   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			copy.deepcopy(UpperCAmelCase   )

					if return_labels:
						if model_class in get_values(UpperCAmelCase   ):
							lowerCAmelCase_      							=			torch.zeros(
							    self.model_tester.batch_size      ,	dtype=torch.long      ,	device=UpperCAmelCase   )

					return inputs_dict







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					self.config_tester.run_common_tests()







				@unittest.skip(reason='''TimeSformer does not use inputs_embeds'''   )
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					pass







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
						self.assertIsInstance(model.get_input_embeddings()      ,	(nn.Module)   )
						lowerCAmelCase_      							=			model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(UpperCAmelCase      ,	nn.Linear   )   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
						lowerCAmelCase_      							=			inspect.signature(model.forward   )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						lowerCAmelCase_      							=			[*signature.parameters.keys()]

						lowerCAmelCase_      							=			['''pixel_values''']
						self.assertListEqual(arg_names[:1]      ,	UpperCAmelCase   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCAmelCase   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase   )







				@slow
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowerCAmelCase_      							=			TimesformerModel.from_pretrained(UpperCAmelCase   )
						self.assertIsNotNone(UpperCAmelCase   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					if not self.has_attentions:
						pass

					else:
						lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()
						lowerCAmelCase_      							=			True

						for model_class in self.all_model_classes:
							lowerCAmelCase_      							=			self.model_tester.seq_length
							lowerCAmelCase_      							=			self.model_tester.num_frames

							lowerCAmelCase_      							=			True
							lowerCAmelCase_      							=			False
							lowerCAmelCase_      							=			True
							lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
							model.to(UpperCAmelCase   )
							model.eval()
							with torch.no_grad():
								lowerCAmelCase_      							=			model(**self._prepare_for_class(UpperCAmelCase      ,	UpperCAmelCase   )   )
							lowerCAmelCase_      							=			outputs.attentions
							self.assertEqual(len(UpperCAmelCase   )      ,	self.model_tester.num_hidden_layers   )

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							lowerCAmelCase_      							=			True
							lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
							model.to(UpperCAmelCase   )
							model.eval()
							with torch.no_grad():
								lowerCAmelCase_      							=			model(**self._prepare_for_class(UpperCAmelCase      ,	UpperCAmelCase   )   )
							lowerCAmelCase_      							=			outputs.attentions
							self.assertEqual(len(UpperCAmelCase   )      ,	self.model_tester.num_hidden_layers   )

							# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
							self.assertListEqual(
							    list(attentions[0].shape[-3:]   )      ,	[self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]      ,	)
							lowerCAmelCase_      							=			len(UpperCAmelCase   )

							# Check attention is always last and order is fine
							lowerCAmelCase_      							=			True
							lowerCAmelCase_      							=			True
							lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
							model.to(UpperCAmelCase   )
							model.eval()
							with torch.no_grad():
								lowerCAmelCase_      							=			model(**self._prepare_for_class(UpperCAmelCase      ,	UpperCAmelCase   )   )

							self.assertEqual(out_len + 1      ,	len(UpperCAmelCase   )   )

							lowerCAmelCase_      							=			outputs.attentions

							self.assertEqual(len(UpperCAmelCase   )      ,	self.model_tester.num_hidden_layers   )

							# attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
							self.assertListEqual(
							    list(self_attentions[0].shape[-3:]   )      ,	[self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1]      ,	)







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					def check_hidden_states_output(UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):
						lowerCAmelCase_      							=			model_class(UpperCAmelCase   )
						model.to(UpperCAmelCase   )
						model.eval()

						with torch.no_grad():
							lowerCAmelCase_      							=			model(**self._prepare_for_class(UpperCAmelCase      ,	UpperCAmelCase   )   )

						lowerCAmelCase_      							=			outputs.hidden_states
						lowerCAmelCase_      							=			self.model_tester.num_hidden_layers + 1
						self.assertEqual(len(UpperCAmelCase   )      ,	UpperCAmelCase   )

						lowerCAmelCase_      							=			self.model_tester.seq_length

						self.assertListEqual(
						    list(hidden_states[0].shape[-2:]   )      ,	[seq_length, self.model_tester.hidden_size]      ,	)

					lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						lowerCAmelCase_      							=			True
						check_hidden_states_output(UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowerCAmelCase_      							=			True

						check_hidden_states_output(UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   )




def      UpperCAmelCase     (       )						-> Optional[int]:


	"""simple docstring"""




	lowerCAmelCase_      							=			hf_hub_download(
	    repo_id='''hf-internal-testing/spaghetti-video'''			,   filename='''eating_spaghetti.npy'''			,   repo_type='''dataset'''	)
	lowerCAmelCase_      							=			np.load(_lowercase	)
	return list(_lowercase	)







@require_torch
@require_vision
class    __a						( unittest.TestCase							):







				@cached_property
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					return (
					    VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]      ,	image_std=[0.5, 0.5, 0.5]   )
					    if is_vision_available()
					    else None
					)







				@slow
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TimesformerForVideoClassification.from_pretrained('''facebook/timesformer-base-finetuned-k400'''   ).to(
					    UpperCAmelCase   )

					lowerCAmelCase_      							=			self.default_image_processor
					lowerCAmelCase_      							=			prepare_video()
					lowerCAmelCase_      							=			image_processor(video[:8]      ,	return_tensors='''pt'''   ).to(UpperCAmelCase   )

					# forward pass
					with torch.no_grad():
						lowerCAmelCase_      							=			model(**UpperCAmelCase   )

					# verify the logits
					lowerCAmelCase_      							=			torch.Size((1, 400)   )
					self.assertEqual(outputs.logits.shape      ,	UpperCAmelCase   )

					lowerCAmelCase_      							=			torch.tensor([-0.3_0_1_6, -0.7_7_1_3, -0.4_2_0_5]   ).to(UpperCAmelCase   )

					self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,	UpperCAmelCase      ,	atol=1E-4   )   )



"""simple docstring"""



from typing import TYPE_CHECKING

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


lowercase_							=							{
    'configuration_layoutlmv2': ['LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LayoutLMv2Config'],
    'processing_layoutlmv2': ['LayoutLMv2Processor'],
    'tokenization_layoutlmv2': ['LayoutLMv2Tokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['LayoutLMv2TokenizerFast']

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							['LayoutLMv2FeatureExtractor']
		lowercase_							=							['LayoutLMv2ImageProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_							=							[
		    'LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'LayoutLMv2ForQuestionAnswering',
		    'LayoutLMv2ForSequenceClassification',
		    'LayoutLMv2ForTokenClassification',
		    'LayoutLMv2Layer',
		    'LayoutLMv2Model',
		    'LayoutLMv2PreTrainedModel',
		]

if TYPE_CHECKING:
		from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
		from .processing_layoutlmva import LayoutLMvaProcessor
		from .tokenization_layoutlmva import LayoutLMvaTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_layoutlmva import (
				    LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaLayer,
				    LayoutLMvaModel,
				    LayoutLMvaPreTrainedModel,
				)
else:
		import sys

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