





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 SCREAMING_SNAKE_CASE_						:






						def __init__(      self      :      Any		,   _A      :      List[str]		,   _A      :      Optional[Any]=13		,   _A      :      str=10		,   _A      :      Dict=3		,   _A      :      int=2		,   _A      :      List[str]=2		,   _A      :      Union[str, Any]=True		,   _A      :      Any=True		,   _A      :      List[Any]=32		,   _A      :      int=5		,   _A      :      Dict=4		,   _A      :      Any=37		,   _A      :      str="gelu"		,   _A      :      Optional[int]=0.1		,   _A      :      str=0.1		,   _A      :      Any=10		,   _A      :      int=0.0_2		,   _A      :      List[str]="divided_space_time"		,   _A      :      str=None		,   )							->    Optional[Any]:







							"""simple docstring"""


							snake_case_		:			Any											=	parent
							snake_case_		:			Optional[int]											=	batch_size
							snake_case_		:			Dict											=	image_size
							snake_case_		:			str											=	num_channels
							snake_case_		:			Union[str, Any]											=	patch_size
							snake_case_		:			Union[str, Any]											=	num_frames
							snake_case_		:			Optional[int]											=	is_training
							snake_case_		:			Tuple											=	use_labels
							snake_case_		:			int											=	hidden_size
							snake_case_		:			Union[str, Any]											=	num_hidden_layers
							snake_case_		:			int											=	num_attention_heads
							snake_case_		:			Any											=	intermediate_size
							snake_case_		:			Optional[int]											=	hidden_act
							snake_case_		:			int											=	hidden_dropout_prob
							snake_case_		:			Optional[int]											=	attention_probs_dropout_prob
							snake_case_		:			str											=	attention_type
							snake_case_		:			Any											=	initializer_range
							snake_case_		:			List[Any]											=	scope
							snake_case_		:			Tuple											=	num_labels

							# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
							snake_case_		:			Dict											=	(image_size // patch_size) ** 2
							snake_case_		:			Union[str, Any]											=	(num_frames) * self.num_patches_per_frame + 1






						def UpperCAmelCase_				(      self      :      str	)							->    int:







							"""simple docstring"""


							snake_case_		:			int											=	floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

							snake_case_		:			Optional[int]											=	None
							if self.use_labels:
								snake_case_		:			Tuple											=	ids_tensor([self.batch_size]		,   self.num_labels	)

							snake_case_		:			str											=	self.get_config()

							return config, pixel_values, labels






						def UpperCAmelCase_				(      self      :      Tuple	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			Tuple											=	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		,   )
							snake_case_		:			Union[str, Any]											=	self.num_labels
							return config






						def UpperCAmelCase_				(      self      :      Any		,   _A      :      Union[str, Any]		,   _A      :      Optional[Any]		,   _A      :      List[Any]	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	TimesformerModel(config=_A	)
							model.to(_A	)
							model.eval()
							snake_case_		:			List[str]											=	model(_A	)
							self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)	)






						def UpperCAmelCase_				(      self      :      Optional[Any]		,   _A      :      Any		,   _A      :      Dict		,   _A      :      List[Any]	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			Optional[Any]											=	TimesformerForVideoClassification(_A	)
							model.to(_A	)
							model.eval()

							snake_case_		:			Dict											=	model(_A	)

							# verify the logits shape
							snake_case_		:			Dict											=	torch.Size((self.batch_size, self.num_labels)	)
							self.parent.assertEqual(result.logits.shape		,   _A	)






						def UpperCAmelCase_				(      self      :      Dict	)							->    int:







							"""simple docstring"""


							snake_case_		:			Tuple											=	self.prepare_config_and_inputs()
							snake_case_						,snake_case_						,snake_case_		:			Any											=	config_and_inputs
							snake_case_		:			Any											=	{'pixel_values': pixel_values}
							return config, inputs_dict
@require_torch
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				snake_case_       ,				unittest.TestCase					):
						__magic_name__:			List[Any]       =       (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
						__magic_name__:			Optional[Any]       =       (
						    {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification}
						    if is_torch_available()
						    else {}
						)

						__magic_name__:			str       =       False
						__magic_name__:			int       =       False
						__magic_name__:			Optional[Any]       =       False
						__magic_name__:			List[Any]       =       False






						def UpperCAmelCase_				(      self      :      Any	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			List[str]											=	TimesformerModelTester(self	)
							snake_case_		:			List[Any]											=	ConfigTester(
							    self		,   config_class=_A		,   has_text_modality=_A		,   hidden_size=37	)






						def UpperCAmelCase_				(      self      :      Union[str, Any]		,   _A      :      List[str]		,   _A      :      Tuple		,   _A      :      int=False	)							->    Tuple:







							"""simple docstring"""


							snake_case_		:			List[str]											=	copy.deepcopy(_A	)

							if return_labels:
								if model_class in get_values(_A	):
									snake_case_		:			Optional[Any]											=	torch.zeros(
									    self.model_tester.batch_size		,   dtype=torch.long		,   device=_A	)

							return inputs_dict






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    str:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						@unittest.skip(reason='TimeSformer does not use inputs_embeds'	)
						def UpperCAmelCase_				(      self      :      str	)							->    Tuple:







							"""simple docstring"""


							pass






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Dict:







							"""simple docstring"""


							snake_case_						,snake_case_		:			Union[str, Any]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			List[str]											=	model_class(_A	)
								self.assertIsInstance(model.get_input_embeddings()		,   (nn.Module)	)
								snake_case_		:			List[Any]											=	model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(_A		,   nn.Linear	)	)






						def UpperCAmelCase_				(      self      :      str	)							->    int:







							"""simple docstring"""


							snake_case_						,snake_case_		:			List[str]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			str											=	model_class(_A	)
								snake_case_		:			List[str]											=	inspect.signature(model.forward	)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_		:			Union[str, Any]											=	[*signature.parameters.keys()]

								snake_case_		:			int											=	['pixel_values']
								self.assertListEqual(arg_names[:1]		,   _A	)






						def UpperCAmelCase_				(      self      :      List[Any]	)							->    List[Any]:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A	)






						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			Any											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*_A	)






						@slow
						def UpperCAmelCase_				(      self      :      str	)							->    Union[str, Any]:







							"""simple docstring"""


							for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_		:			int											=	TimesformerModel.from_pretrained(_A	)
								self.assertIsNotNone(_A	)






						def UpperCAmelCase_				(      self      :      List[str]	)							->    int:







							"""simple docstring"""


							if not self.has_attentions:
								pass

							else:
								snake_case_						,snake_case_		:			Union[str, Any]											=	self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_		:			Tuple											=	True

								for model_class in self.all_model_classes:
									snake_case_		:			Dict											=	self.model_tester.seq_length
									snake_case_		:			Optional[int]											=	self.model_tester.num_frames

									snake_case_		:			Tuple											=	True
									snake_case_		:			Any											=	False
									snake_case_		:			List[str]											=	True
									snake_case_		:			Union[str, Any]											=	model_class(_A	)
									model.to(_A	)
									model.eval()
									with torch.no_grad():
										snake_case_		:			Tuple											=	model(**self._prepare_for_class(_A		,   _A	)	)
									snake_case_		:			Optional[Any]											=	outputs.attentions
									self.assertEqual(len(_A	)		,   self.model_tester.num_hidden_layers	)

									# check that output_attentions also work using config
									del inputs_dict["output_attentions"]
									snake_case_		:			List[Any]											=	True
									snake_case_		:			List[str]											=	model_class(_A	)
									model.to(_A	)
									model.eval()
									with torch.no_grad():
										snake_case_		:			Tuple											=	model(**self._prepare_for_class(_A		,   _A	)	)
									snake_case_		:			Any											=	outputs.attentions
									self.assertEqual(len(_A	)		,   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]		,   )
									snake_case_		:			List[Any]											=	len(_A	)

									# Check attention is always last and order is fine
									snake_case_		:			str											=	True
									snake_case_		:			int											=	True
									snake_case_		:			Tuple											=	model_class(_A	)
									model.to(_A	)
									model.eval()
									with torch.no_grad():
										snake_case_		:			Dict											=	model(**self._prepare_for_class(_A		,   _A	)	)

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

									snake_case_		:			Optional[Any]											=	outputs.attentions

									self.assertEqual(len(_A	)		,   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 UpperCAmelCase_				(      self      :      str	)							->    int:







							"""simple docstring"""


							def check_hidden_states_output(_A      :      Optional[Any]		,   _A      :      Optional[Any]		,   _A      :      List[Any]	):
								snake_case_		:			List[str]											=	model_class(_A	)
								model.to(_A	)
								model.eval()

								with torch.no_grad():
									snake_case_		:			Union[str, Any]											=	model(**self._prepare_for_class(_A		,   _A	)	)

								snake_case_		:			Dict											=	outputs.hidden_states
								snake_case_		:			List[Any]											=	self.model_tester.num_hidden_layers + 1
								self.assertEqual(len(_A	)		,   _A	)

								snake_case_		:			Union[str, Any]											=	self.model_tester.seq_length

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

							snake_case_						,snake_case_		:			str											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			str											=	True
								check_hidden_states_output(_A		,   _A		,   _A	)

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								snake_case_		:			Union[str, Any]											=	True

								check_hidden_states_output(_A		,   _A		,   _A	)


def 							SCREAMING_SNAKE_CASE__						(    ):
	snake_case_		:			List[Any]											=	hf_hub_download(
	    repo_id='hf-internal-testing/spaghetti-video'   , filename='eating_spaghetti.npy'   , repo_type='dataset'   )
	snake_case_		:			List[Any]											=	np.load(__a   )
	return list(__a   )


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






						@cached_property
						def UpperCAmelCase_				(      self      :      Tuple	)							->    int:







							"""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 UpperCAmelCase_				(      self      :      int	)							->    Dict:







							"""simple docstring"""


							snake_case_		:			Any											=	TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'	).to(
							    _A	)

							snake_case_		:			Union[str, Any]											=	self.default_image_processor
							snake_case_		:			str											=	prepare_video()
							snake_case_		:			Dict											=	image_processor(video[:8]		,   return_tensors='pt'	).to(_A	)

							# forward pass
							with torch.no_grad():
								snake_case_		:			Optional[Any]											=	model(**_A	)

							# verify the logits
							snake_case_		:			List[Any]											=	torch.Size((1, 400)	)
							self.assertEqual(outputs.logits.shape		,   _A	)

							snake_case_		:			List[Any]											=	torch.tensor([-0.3_0_1_6, -0.7_7_1_3, -0.4_2_0_5]	).to(_A	)

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








import numpy as np


def 							SCREAMING_SNAKE_CASE__						(    __a   ):
	return (2 / (1 + np.exp(-2 * vector   ))) - 1


if __name__ == "__main__":
						import doctest

						doctest.testmod()

