




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




				'''simple docstring'''





				def __init__(self ,  _UpperCAmelCase ,  _UpperCAmelCase=1_3 ,  _UpperCAmelCase=1_0 ,  _UpperCAmelCase=3 ,  _UpperCAmelCase=2 ,  _UpperCAmelCase=2 ,  _UpperCAmelCase=True ,  _UpperCAmelCase=True ,  _UpperCAmelCase=3_2 ,  _UpperCAmelCase=5 ,  _UpperCAmelCase=4 ,  _UpperCAmelCase=3_7 ,  _UpperCAmelCase="gelu" ,  _UpperCAmelCase=0.1 ,  _UpperCAmelCase=0.1 ,  _UpperCAmelCase=1_0 ,  _UpperCAmelCase=0.02 ,  _UpperCAmelCase="divided_space_time" ,  _UpperCAmelCase=None ,  )	->       List[Any]:
							__UpperCamelCase      :			List[Any]									=   parent
							__UpperCamelCase      :			Union[str, Any]									=   batch_size
							__UpperCamelCase      :			List[Any]									=   image_size
							__UpperCamelCase      :			Union[str, Any]									=   num_channels
							__UpperCamelCase      :			Dict									=   patch_size
							__UpperCamelCase      :			Optional[int]									=   num_frames
							__UpperCamelCase      :			List[str]									=   is_training
							__UpperCamelCase      :			List[str]									=   use_labels
							__UpperCamelCase      :			Dict									=   hidden_size
							__UpperCamelCase      :			int									=   num_hidden_layers
							__UpperCamelCase      :			str									=   num_attention_heads
							__UpperCamelCase      :			List[Any]									=   intermediate_size
							__UpperCamelCase      :			List[str]									=   hidden_act
							__UpperCamelCase      :			Union[str, Any]									=   hidden_dropout_prob
							__UpperCamelCase      :			Tuple									=   attention_probs_dropout_prob
							__UpperCamelCase      :			int									=   attention_type
							__UpperCamelCase      :			Union[str, Any]									=   initializer_range
							__UpperCamelCase      :			Union[str, Any]									=   scope
							__UpperCamelCase      :			Tuple									=   num_labels

							# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
							__UpperCamelCase      :			str									=   (image_size // patch_size) ** 2
							__UpperCamelCase      :			Any									=   (num_frames) * self.num_patches_per_frame + 1
				def        a_       (self			)	->       Tuple:
							__UpperCamelCase      :			Union[str, Any]									=   floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]			)

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

							__UpperCamelCase      :			Any									=   self.get_config()

							return config, pixel_values, labels
				def        a_       (self			)	->       Optional[Any]:
							__UpperCamelCase      :			Optional[Any]									=   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      :			Optional[Any]									=   self.num_labels
							return config
				def        a_       (self ,  _UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase			)	->       Dict:
							__UpperCamelCase      :			Union[str, Any]									=   TimesformerModel(config=__a			)
							model.to(__a			)
							model.eval()
							__UpperCamelCase      :			Dict									=   model(__a			)
							self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size)			)
				def        a_       (self ,  _UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase			)	->       int:
							__UpperCamelCase      :			List[Any]									=   TimesformerForVideoClassification(__a			)
							model.to(__a			)
							model.eval()

							__UpperCamelCase      :			List[Any]									=   model(__a			)

							# verify the logits shape
							__UpperCamelCase      :			List[str]									=   torch.Size((self.batch_size, self.num_labels)			)
							self.parent.assertEqual(result.logits.shape ,  __a			)





				def        a_       (self			)	->       List[Any]:
							__UpperCamelCase      :			List[Any]									=   self.prepare_config_and_inputs()
							__UpperCamelCase		,					__UpperCamelCase		,					__UpperCamelCase      :			Tuple									=   config_and_inputs
							__UpperCamelCase      :			List[Any]									=   {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class 					A		( lowerCamelCase_			,							lowerCamelCase_			,							unittest.TestCase		):




				'''simple docstring'''





				A      	=				(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
				A      	=				(
				    {"feature-extraction": TimesformerModel, "video-classification": TimesformerForVideoClassification}
				    if is_torch_available()
				    else {}
				)

				A      	=				False
				A      	=				False
				A      	=				False
				A      	=				False
				def        a_       (self			)	->       str:
							__UpperCamelCase      :			Dict									=   TimesformerModelTester(self			)
							__UpperCamelCase      :			Any									=   ConfigTester(
							    self ,  config_class=__a ,  has_text_modality=__a ,  hidden_size=3_7			)
				def        a_       (self ,  _UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase=False			)	->       Tuple:
							__UpperCamelCase      :			Union[str, Any]									=   copy.deepcopy(__a			)

							if return_labels:
										if model_class in get_values(__a			):
													__UpperCamelCase      :			Tuple									=   torch.zeros(
													    self.model_tester.batch_size ,  dtype=torch.long ,  device=__a			)

							return inputs_dict
				def        a_       (self			)	->       int:
							self.config_tester.run_common_tests()
				@unittest.skip(reason="TimeSformer does not use inputs_embeds"			)
				def        a_       (self			)	->       List[Any]:
							pass
				def        a_       (self			)	->       Tuple:
							__UpperCamelCase		,					__UpperCamelCase      :			Union[str, Any]									=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										__UpperCamelCase      :			Optional[int]									=   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        a_       (self			)	->       Tuple:
							__UpperCamelCase		,					__UpperCamelCase      :			Union[str, Any]									=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										__UpperCamelCase      :			List[Any]									=   model_class(__a			)
										__UpperCamelCase      :			Optional[int]									=   inspect.signature(model.forward			)
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										__UpperCamelCase      :			str									=   [*signature.parameters.keys()]

										__UpperCamelCase      :			Tuple									=   ["pixel_values"]
										self.assertListEqual(arg_names[:1] ,  __a			)
				def        a_       (self			)	->       Tuple:
							__UpperCamelCase      :			Optional[int]									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__a			)
				def        a_       (self			)	->       List[str]:
							__UpperCamelCase      :			int									=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*__a			)
				@slow
				def        a_       (self			)	->       List[Any]:
							for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__UpperCamelCase      :			Optional[Any]									=   TimesformerModel.from_pretrained(__a			)
										self.assertIsNotNone(__a			)
				def        a_       (self			)	->       Optional[Any]:
							if not self.has_attentions:
										pass

							else:
										__UpperCamelCase		,					__UpperCamelCase      :			Optional[int]									=   self.model_tester.prepare_config_and_inputs_for_common()
										__UpperCamelCase      :			Dict									=   True

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

													__UpperCamelCase      :			Tuple									=   True
													__UpperCamelCase      :			Optional[Any]									=   False
													__UpperCamelCase      :			Any									=   True
													__UpperCamelCase      :			Tuple									=   model_class(__a			)
													model.to(__a			)
													model.eval()
													with torch.no_grad():
																__UpperCamelCase      :			str									=   model(**self._prepare_for_class(__a ,  __a			)			)
													__UpperCamelCase      :			Optional[int]									=   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      :			Optional[int]									=   True
													__UpperCamelCase      :			str									=   model_class(__a			)
													model.to(__a			)
													model.eval()
													with torch.no_grad():
																__UpperCamelCase      :			Optional[int]									=   model(**self._prepare_for_class(__a ,  __a			)			)
													__UpperCamelCase      :			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(attentions[0].shape[-3:]			) ,  [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] ,  )
													__UpperCamelCase      :			Optional[Any]									=   len(__a			)

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

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

													__UpperCamelCase      :			int									=   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        a_       (self			)	->       Optional[int]:
							def check_hidden_states_output(_UpperCAmelCase ,  _UpperCAmelCase ,  _UpperCAmelCase			):
										__UpperCamelCase      :			List[str]									=   model_class(__a			)
										model.to(__a			)
										model.eval()

										with torch.no_grad():
													__UpperCamelCase      :			Optional[int]									=   model(**self._prepare_for_class(__a ,  __a			)			)

										__UpperCamelCase      :			Dict									=   outputs.hidden_states
										__UpperCamelCase      :			Dict									=   self.model_tester.num_hidden_layers + 1
										self.assertEqual(len(__a			) ,  __a			)

										__UpperCamelCase      :			Dict									=   self.model_tester.seq_length

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

							__UpperCamelCase		,					__UpperCamelCase      :			List[str]									=   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      :			Any									=   True

										check_hidden_states_output(__a ,  __a ,  __a			)







def 			__lowerCAmelCase					(    ):
			__UpperCamelCase      :			str									=   hf_hub_download(
			    repo_id="hf-internal-testing/spaghetti-video"					,      filename="eating_spaghetti.npy"					,      repo_type="dataset"						)
			__UpperCamelCase      :			Union[str, Any]									=   np.load(snake_case__						)
			return list(snake_case__						)




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




				'''simple docstring'''





				@cached_property
				def        a_       (self			)	->       Optional[Any]:
							# 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        a_       (self			)	->       Tuple:
							__UpperCamelCase      :			Optional[int]									=   TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"			).to(
							    __a			)

							__UpperCamelCase      :			int									=   self.default_image_processor
							__UpperCamelCase      :			str									=   prepare_video()
							__UpperCamelCase      :			List[Any]									=   image_processor(video[:8] ,  return_tensors="pt"			).to(__a			)

							# forward pass
							with torch.no_grad():
										__UpperCamelCase      :			Optional[int]									=   model(**__a			)

							# verify the logits
							__UpperCamelCase      :			Any									=   torch.Size((1, 4_0_0)			)
							self.assertEqual(outputs.logits.shape ,  __a			)

							__UpperCamelCase      :			int									=   torch.tensor([-0.3_016, -0.7_713, -0.4_205]			).to(__a			)

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










'''simple docstring'''







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase_					:    int      	=			logging.get_logger(__name__)

lowerCAmelCase_					:    Tuple      	=			{
    'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json',
    'google/bigbird-roberta-large': 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json',
    'google/bigbird-base-trivia-itc': 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json',
    # See all BigBird models at https://huggingface.co/models?filter=big_bird
}
class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				__a            ='big_bird'




				def __init__(  self     :					Optional[int]					,				__a     :					Dict=5_03_58					,				__a     :					str=7_68					,				__a     :					List[Any]=12					,				__a     :					List[str]=12					,				__a     :					Union[str, Any]=30_72					,				__a     :					str="gelu_new"					,				__a     :					Dict=0.1					,				__a     :					Union[str, Any]=0.1					,				__a     :					Any=40_96					,				__a     :					int=2					,				__a     :					Tuple=0.02					,				__a     :					List[Any]=1e-1_2					,				__a     :					int=True					,				__a     :					List[str]=0					,				__a     :					Tuple=1					,				__a     :					Optional[Any]=2					,				__a     :					Tuple=66					,				__a     :					str="block_sparse"					,				__a     :					Tuple=True					,				__a     :					Optional[int]=False					,				__a     :					str=64					,				__a     :					Tuple=3					,				__a     :					Any=None					,				**__a     :					Dict					,				):
								super().__init__(
								    pad_token_id=__a					,				bos_token_id=__a					,				eos_token_id=__a					,				sep_token_id=__a					,				**__a					,				)

								_a								= vocab_size
								_a								= max_position_embeddings
								_a								= hidden_size
								_a								= num_hidden_layers
								_a								= num_attention_heads
								_a								= intermediate_size
								_a								= hidden_act
								_a								= hidden_dropout_prob
								_a								= attention_probs_dropout_prob
								_a								= initializer_range
								_a								= type_vocab_size
								_a								= layer_norm_eps
								_a								= use_cache

								_a								= rescale_embeddings
								_a								= attention_type
								_a								= use_bias
								_a								= block_size
								_a								= num_random_blocks
								_a								= classifier_dropout


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				@property
				def   UpperCamelCase__   (  self     :					Optional[int]					):
								if self.task == "multiple-choice":
												_a								= {0: "batch", 1: "choice", 2: "sequence"}
								else:
												_a								= {0: "batch", 1: "sequence"}
								return OrderedDict(
								    [
								        ("input_ids", dynamic_axis),
								        ("attention_mask", dynamic_axis),
								    ]					)
