



'''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      __magic_name__						:







							def __init__(							self :	Any     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	str=13     ,_UpperCAmelCase :	Union[str, Any]=10     ,_UpperCAmelCase :	List[str]=3     ,_UpperCAmelCase :	List[Any]=2     ,_UpperCAmelCase :	List[str]=2     ,_UpperCAmelCase :	Any=True     ,_UpperCAmelCase :	Tuple=True     ,_UpperCAmelCase :	int=32     ,_UpperCAmelCase :	str=5     ,_UpperCAmelCase :	Any=4     ,_UpperCAmelCase :	Dict=37     ,_UpperCAmelCase :	List[Any]="gelu"     ,_UpperCAmelCase :	List[str]=0.1     ,_UpperCAmelCase :	List[Any]=0.1     ,_UpperCAmelCase :	Optional[int]=10     ,_UpperCAmelCase :	List[Any]=0.02     ,_UpperCAmelCase :	List[str]="divided_space_time"     ,_UpperCAmelCase :	List[str]=None     ,):
									_a					:				List[Any]				=							parent
									_a					:				str				=							batch_size
									_a					:				Tuple				=							image_size
									_a					:				Any				=							num_channels
									_a					:				Tuple				=							patch_size
									_a					:				int				=							num_frames
									_a					:				Dict				=							is_training
									_a					:				Optional[Any]				=							use_labels
									_a					:				List[Any]				=							hidden_size
									_a					:				Any				=							num_hidden_layers
									_a					:				Optional[Any]				=							num_attention_heads
									_a					:				Optional[int]				=							intermediate_size
									_a					:				Tuple				=							hidden_act
									_a					:				Optional[Any]				=							hidden_dropout_prob
									_a					:				Union[str, Any]				=							attention_probs_dropout_prob
									_a					:				int				=							attention_type
									_a					:				Optional[Any]				=							initializer_range
									_a					:				Optional[int]				=							scope
									_a					:				List[str]				=							num_labels

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







							def 							__lowercase      (							self :	Tuple		):
									_a					:				List[str]				=							floats_tensor(
									    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]		)

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

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

									return config, pixel_values, labels







							def 							__lowercase      (							self :	Dict		):
									_a					:				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     ,)
									_a					:				Union[str, Any]				=							self.num_labels
									return config







							def 							__lowercase      (							self :	Optional[Any]     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Union[str, Any]		):
									_a					:				List[Any]				=							TimesformerModel(config=_UpperCAmelCase		)
									model.to(_UpperCAmelCase		)
									model.eval()
									_a					:				Optional[Any]				=							model(_UpperCAmelCase		)
									self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size)		)







							def 							__lowercase      (							self :	Union[str, Any]     ,_UpperCAmelCase :	int     ,_UpperCAmelCase :	Optional[int]     ,_UpperCAmelCase :	Optional[int]		):
									_a					:				Optional[int]				=							TimesformerForVideoClassification(_UpperCAmelCase		)
									model.to(_UpperCAmelCase		)
									model.eval()

									_a					:				int				=							model(_UpperCAmelCase		)

									# verify the logits shape
									_a					:				Any				=							torch.Size((self.batch_size, self.num_labels)		)
									self.parent.assertEqual(result.logits.shape     ,_UpperCAmelCase		)







							def 							__lowercase      (							self :	int		):
									_a					:				Any				=							self.prepare_config_and_inputs()
									_a	, _a	, _a					:				Any				=							config_and_inputs
									_a					:				Dict				=							{'pixel_values': pixel_values}
									return config, inputs_dict

@require_torch
class      __magic_name__						(   _UpperCamelCase      ,						_UpperCamelCase      ,						unittest.TestCase       ):
							lowerCAmelCase	:   Tuple           = (TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
							lowerCAmelCase	:   List[str]           = (
							    {'feature-extraction': TimesformerModel, 'video-classification': TimesformerForVideoClassification}
							    if is_torch_available()
							    else {}
							)

							lowerCAmelCase	:   str           = False
							lowerCAmelCase	:   Union[str, Any]           = False
							lowerCAmelCase	:   Dict           = False
							lowerCAmelCase	:   Dict           = False







							def 							__lowercase      (							self :	int		):
									_a					:				List[str]				=							TimesformerModelTester(self		)
									_a					:				int				=							ConfigTester(
									    self     ,config_class=_UpperCAmelCase     ,has_text_modality=_UpperCAmelCase     ,hidden_size=37		)







							def 							__lowercase      (							self :	Tuple     ,_UpperCAmelCase :	Dict     ,_UpperCAmelCase :	Any     ,_UpperCAmelCase :	List[str]=False		):
									_a					:				Tuple				=							copy.deepcopy(_UpperCAmelCase		)

									if return_labels:
											if model_class in get_values(_UpperCAmelCase		):
													_a					:				Dict				=							torch.zeros(
													    self.model_tester.batch_size     ,dtype=torch.long     ,device=_UpperCAmelCase		)

									return inputs_dict







							def 							__lowercase      (							self :	List[str]		):
									self.config_tester.run_common_tests()







							@unittest.skip(reason='TimeSformer does not use inputs_embeds'		)
							def 							__lowercase      (							self :	Optional[Any]		):
									pass







							def 							__lowercase      (							self :	str		):
									_a	, _a					:				Any				=							self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a					:				Union[str, Any]				=							model_class(_UpperCAmelCase		)
											self.assertIsInstance(model.get_input_embeddings()     ,(nn.Module)		)
											_a					:				Optional[int]				=							model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(_UpperCAmelCase     ,nn.Linear		)		)







							def 							__lowercase      (							self :	Any		):
									_a	, _a					:				Optional[Any]				=							self.model_tester.prepare_config_and_inputs_for_common()

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

											_a					:				Any				=							['pixel_values']
											self.assertListEqual(arg_names[:1]     ,_UpperCAmelCase		)







							def 							__lowercase      (							self :	Dict		):
									_a					:				List[Any]				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase		)







							def 							__lowercase      (							self :	Tuple		):
									_a					:				str				=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_video_classification(*_UpperCAmelCase		)







							@slow
							def 							__lowercase      (							self :	Optional[Any]		):
									for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_a					:				Any				=							TimesformerModel.from_pretrained(_UpperCAmelCase		)
											self.assertIsNotNone(_UpperCAmelCase		)







							def 							__lowercase      (							self :	Union[str, Any]		):
									if not self.has_attentions:
											pass

									else:
											_a	, _a					:				Dict				=							self.model_tester.prepare_config_and_inputs_for_common()
											_a					:				Dict				=							True

											for model_class in self.all_model_classes:
													_a					:				List[str]				=							self.model_tester.seq_length
													_a					:				str				=							self.model_tester.num_frames

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

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

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

													_a					:				List[str]				=							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 							__lowercase      (							self :	Union[str, Any]		):
									def check_hidden_states_output(_UpperCAmelCase :	Tuple     ,_UpperCAmelCase :	Optional[Any]     ,_UpperCAmelCase :	Tuple		):
											_a					:				Optional[int]				=							model_class(_UpperCAmelCase		)
											model.to(_UpperCAmelCase		)
											model.eval()

											with torch.no_grad():
													_a					:				Dict				=							model(**self._prepare_for_class(_UpperCAmelCase     ,_UpperCAmelCase		)		)

											_a					:				List[Any]				=							outputs.hidden_states
											_a					:				Tuple				=							self.model_tester.num_hidden_layers + 1
											self.assertEqual(len(_UpperCAmelCase		)     ,_UpperCAmelCase		)

											_a					:				Any				=							self.model_tester.seq_length

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

									_a	, _a					:				Optional[int]				=							self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											_a					:				Any				=							True
											check_hidden_states_output(_UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase		)

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

											check_hidden_states_output(_UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase		)



def 					__lowerCamelCase					(				)      ->				Any:
		_a					:				Any				=							hf_hub_download(
		    repo_id='hf-internal-testing/spaghetti-video'						,      filename='eating_spaghetti.npy'						,      repo_type='dataset'  )
		_a					:				List[str]				=							np.load(lowerCAmelCase_  )
		return list(lowerCAmelCase_  )

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







							@cached_property
							def 							__lowercase      (							self :	int		):
									# logits were tested with a different mean and std, so we use the same here
									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 							__lowercase      (							self :	List[str]		):
									_a					:				Any				=							TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'		).to(
									    _UpperCAmelCase		)

									_a					:				Dict				=							self.default_image_processor
									_a					:				Optional[Any]				=							prepare_video()
									_a					:				Any				=							image_processor(video[:8]     ,return_tensors='pt'		).to(_UpperCAmelCase		)

									# forward pass
									with torch.no_grad():
											_a					:				str				=							model(**_UpperCAmelCase		)

									# verify the logits
									_a					:				Any				=							torch.Size((1, 400)		)
									self.assertEqual(outputs.logits.shape     ,_UpperCAmelCase		)

									_a					:				List[Any]				=							torch.tensor([-0.30_16, -0.77_13, -0.42_05]		).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_tf_available, is_torch_available


__lowerCAmelCase						      =       {
    '''configuration_data2vec_audio''': ['''DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Data2VecAudioConfig'''],
    '''configuration_data2vec_text''': [
        '''DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''Data2VecTextConfig''',
        '''Data2VecTextOnnxConfig''',
    ],
    '''configuration_data2vec_vision''': [
        '''DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''Data2VecVisionConfig''',
        '''Data2VecVisionOnnxConfig''',
    ],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase						      =       [
							    '''DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''Data2VecAudioForAudioFrameClassification''',
							    '''Data2VecAudioForCTC''',
							    '''Data2VecAudioForSequenceClassification''',
							    '''Data2VecAudioForXVector''',
							    '''Data2VecAudioModel''',
							    '''Data2VecAudioPreTrainedModel''',
							]
							__lowerCAmelCase						      =       [
							    '''DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''Data2VecTextForCausalLM''',
							    '''Data2VecTextForMaskedLM''',
							    '''Data2VecTextForMultipleChoice''',
							    '''Data2VecTextForQuestionAnswering''',
							    '''Data2VecTextForSequenceClassification''',
							    '''Data2VecTextForTokenClassification''',
							    '''Data2VecTextModel''',
							    '''Data2VecTextPreTrainedModel''',
							]
							__lowerCAmelCase						      =       [
							    '''DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''Data2VecVisionForImageClassification''',
							    '''Data2VecVisionForMaskedImageModeling''',
							    '''Data2VecVisionForSemanticSegmentation''',
							    '''Data2VecVisionModel''',
							    '''Data2VecVisionPreTrainedModel''',
							]

if is_tf_available():
							__lowerCAmelCase						      =       [
							    '''TFData2VecVisionForImageClassification''',
							    '''TFData2VecVisionForSemanticSegmentation''',
							    '''TFData2VecVisionModel''',
							    '''TFData2VecVisionPreTrainedModel''',
							]

if TYPE_CHECKING:
							from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig
							from .configuration_dataavec_text import (
							    DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    DataaVecTextConfig,
							    DataaVecTextOnnxConfig,
							)
							from .configuration_dataavec_vision import (
							    DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    DataaVecVisionConfig,
							    DataaVecVisionOnnxConfig,
							)

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_dataavec_audio import (
														    DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DataaVecAudioForAudioFrameClassification,
														    DataaVecAudioForCTC,
														    DataaVecAudioForSequenceClassification,
														    DataaVecAudioForXVector,
														    DataaVecAudioModel,
														    DataaVecAudioPreTrainedModel,
														)
														from .modeling_dataavec_text import (
														    DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DataaVecTextForCausalLM,
														    DataaVecTextForMaskedLM,
														    DataaVecTextForMultipleChoice,
														    DataaVecTextForQuestionAnswering,
														    DataaVecTextForSequenceClassification,
														    DataaVecTextForTokenClassification,
														    DataaVecTextModel,
														    DataaVecTextPreTrainedModel,
														)
														from .modeling_dataavec_vision import (
														    DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DataaVecVisionForImageClassification,
														    DataaVecVisionForMaskedImageModeling,
														    DataaVecVisionForSemanticSegmentation,
														    DataaVecVisionModel,
														    DataaVecVisionPreTrainedModel,
														)
							if is_tf_available():
														from .modeling_tf_dataavec_vision import (
														    TFDataaVecVisionForImageClassification,
														    TFDataaVecVisionForSemanticSegmentation,
														    TFDataaVecVisionModel,
														    TFDataaVecVisionPreTrainedModel,
														)

else:
							import sys

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




