

"""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       snake_case__  :







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





								'''simple docstring'''



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

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







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



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

								UpperCAmelCase			:   Any      =				None
								if self.use_labels:
													UpperCAmelCase			:   str      =				ids_tensor([self.batch_size]			,  self.num_labels  )

								UpperCAmelCase			:   Optional[Any]      =				self.get_config()

								return config, pixel_values, labels







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   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			,  )
								UpperCAmelCase			:   List[Any]      =				self.num_labels
								return config







			def 			__lowerCAmelCase				(			self			: Dict			,  lowercase			: Tuple			,  lowercase			: str			,  lowercase			: Union[str, Any]  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: Dict			,  lowercase			: List[Any]			,  lowercase			: Tuple			,  lowercase			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   Any      =				TimesformerForVideoClassification(__A  )
								model.to(__A  )
								model.eval()

								UpperCAmelCase			:   Dict      =				model(__A  )

								# verify the logits shape
								UpperCAmelCase			:   Union[str, Any]      =				torch.Size((self.batch_size, self.num_labels)  )
								self.parent.assertEqual(result.logits.shape			,  __A  )







			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				self.prepare_config_and_inputs()
								UpperCAmelCase			:   List[str]      =				config_and_inputs
								UpperCAmelCase			:   Tuple      =				{"pixel_values": pixel_values}
								return config, inputs_dict







@require_torch
class       snake_case__  ( UpperCamelCase_					,  UpperCamelCase_					,  unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
			SCREAMING_SNAKE_CASE__     				=						(
			    {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
			    if is_torch_available()
			    else {}
			)

			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				TimesformerModelTester(self  )
								UpperCAmelCase			:   Tuple      =				ConfigTester(
								    self			,  config_class=__A			,  has_text_modality=__A			,  hidden_size=37  )







			def 			__lowerCAmelCase				(			self			: Dict			,  lowercase			: Tuple			,  lowercase			: Dict			,  lowercase			: Union[str, Any]=False  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				copy.deepcopy(__A  )

								if return_labels:
													if model_class in get_values(__A  ):
																		UpperCAmelCase			:   List[str]      =				torch.zeros(
																		    self.model_tester.batch_size			,  dtype=torch.long			,  device=__A  )

								return inputs_dict







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								self.config_tester.run_common_tests()







			@unittest.skip(reason="TimeSformer does not use inputs_embeds"  )
			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



								pass







			def 			__lowerCAmelCase				(			self			: Dict  ):





								'''simple docstring'''



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

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







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   str      =				self.model_tester.prepare_config_and_inputs_for_common()

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

													UpperCAmelCase			:   int      =				["pixel_values"]
													self.assertListEqual(arg_names[:1]			,  __A  )







			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   str      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_video_classification(*__A  )







			@slow
			def 			__lowerCAmelCase				(			self			: Union[str, Any]  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								if not self.has_attentions:
													pass

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

													for model_class in self.all_model_classes:
																		UpperCAmelCase			:   List[Any]      =				self.model_tester.seq_length
																		UpperCAmelCase			:   Optional[int]      =				self.model_tester.num_frames

																		UpperCAmelCase			:   int      =				True
																		UpperCAmelCase			:   Optional[Any]      =				False
																		UpperCAmelCase			:   Optional[int]      =				True
																		UpperCAmelCase			:   Optional[int]      =				model_class(__A  )
																		model.to(__A  )
																		model.eval()
																		with torch.no_grad():
																							UpperCAmelCase			:   str      =				model(**self._prepare_for_class(__A			,  __A  )  )
																		UpperCAmelCase			:   Dict      =				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"]
																		UpperCAmelCase			:   List[Any]      =				True
																		UpperCAmelCase			:   List[str]      =				model_class(__A  )
																		model.to(__A  )
																		model.eval()
																		with torch.no_grad():
																							UpperCAmelCase			:   int      =				model(**self._prepare_for_class(__A			,  __A  )  )
																		UpperCAmelCase			:   Dict      =				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]			,  )
																		UpperCAmelCase			:   Dict      =				len(__A  )

																		# Check attention is always last and order is fine
																		UpperCAmelCase			:   Union[str, Any]      =				True
																		UpperCAmelCase			:   List[str]      =				True
																		UpperCAmelCase			:   Optional[Any]      =				model_class(__A  )
																		model.to(__A  )
																		model.eval()
																		with torch.no_grad():
																							UpperCAmelCase			:   str      =				model(**self._prepare_for_class(__A			,  __A  )  )

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

																		UpperCAmelCase			:   str      =				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 			__lowerCAmelCase				(			self			: Optional[int]  ):





								'''simple docstring'''



								def check_hidden_states_output(lowercase			: str			,  lowercase			: Dict			,  lowercase			: Optional[Any]  ):
													UpperCAmelCase			:   Optional[int]      =				model_class(__A  )
													model.to(__A  )
													model.eval()

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

													UpperCAmelCase			:   Dict      =				outputs.hidden_states
													UpperCAmelCase			:   Union[str, Any]      =				self.model_tester.num_hidden_layers + 1
													self.assertEqual(len(__A  )			,  __A  )

													UpperCAmelCase			:   Tuple      =				self.model_tester.seq_length

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

								UpperCAmelCase			:   int      =				self.model_tester.prepare_config_and_inputs_for_common()

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

													# check that output_hidden_states also work using config
													del inputs_dict["output_hidden_states"]
													UpperCAmelCase			:   str      =				True

													check_hidden_states_output(__A			,  __A			,  __A  )



def   lowercase_	(    ):




					'''simple docstring'''






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







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







			@cached_property
			def 			__lowerCAmelCase				(			self			: 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 			__lowerCAmelCase				(			self			: Optional[int]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"  ).to(
								    __A  )

								UpperCAmelCase			:   Tuple      =				self.default_image_processor
								UpperCAmelCase			:   str      =				prepare_video()
								UpperCAmelCase			:   str      =				image_processor(video[:8]			,  return_tensors="pt"  ).to(__A  )

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

								# verify the logits
								UpperCAmelCase			:   Optional[Any]      =				torch.Size((1, 4_00)  )
								self.assertEqual(outputs.logits.shape			,  __A  )

								UpperCAmelCase			:   Tuple      =				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  )  )





"""simple docstring"""






def   lowercase_	(    _lowercase       :		list							,      _lowercase       :		int							,      _lowercase       :		int = 0							,      _lowercase       :		int = 0	):




					'''simple docstring'''






					UpperCAmelCase			:   Tuple      =				right or len(_lowercase	) - 1
					if left > right:
										return -1
					elif list_data[left] == key:
										return left
					elif list_data[right] == key:
										return right
					else:
										return search(_lowercase							,      _lowercase							,      left + 1							,      right - 1	)


if __name__ == "__main__":
	import doctest

	doctest.testmod()


