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







				def __init__(       self						,				A_						,				A_=13						,				A_=10						,				A_=3						,				A_=2						,				A_=2						,				A_=True						,				A_=True						,				A_=32						,				A_=5						,				A_=4						,				A_=37						,				A_="gelu"						,				A_=0.1						,				A_=0.1						,				A_=10						,				A_=0.02						,				A_="divided_space_time"						,				A_=None						,				)->		int:







											'''simple docstring'''






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

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







				def 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






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

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

											UpperCamelCase   			=     self.get_config()

											return config, pixel_values, labels







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







											'''simple docstring'''






											UpperCamelCase   			=     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   			=     self.num_labels
											return config







				def 						UpperCAmelCase_    (       self						,				A_						,				A_						,				A_     )->		Tuple:







											'''simple docstring'''






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







				def 						UpperCAmelCase_    (       self						,				A_						,				A_						,				A_     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     TimesformerForVideoClassification(A_     )
											model.to(A_     )
											model.eval()

											UpperCamelCase   			=     model(A_     )

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







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







											'''simple docstring'''






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


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

				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False
				lowerCAmelCase_     			=							False







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







											'''simple docstring'''






											UpperCamelCase   			=     TimesformerModelTester(self     )
											UpperCamelCase   			=     ConfigTester(
											    self						,				config_class=A_						,				has_text_modality=A_						,				hidden_size=37     )







				def 						UpperCAmelCase_    (       self						,				A_						,				A_						,				A_=False     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     copy.deepcopy(A_     )

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

											return inputs_dict







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







											'''simple docstring'''






											self.config_tester.run_common_tests()







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







											'''simple docstring'''






											pass







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







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs_for_common()

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







				def 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCamelCase   			=     ['pixel_values']
																		self.assertListEqual(arg_names[:1]						,				A_     )







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







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A_     )







				def 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_video_classification(*A_     )







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







											'''simple docstring'''






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







				def 						UpperCAmelCase_    (       self     )->		Dict:







											'''simple docstring'''






											if not self.has_attentions:
																		pass

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

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

																									UpperCamelCase   			=     True
																									UpperCamelCase   			=     False
																									UpperCamelCase   			=     True
																									UpperCamelCase   			=     model_class(A_     )
																									model.to(A_     )
																									model.eval()
																									with torch.no_grad():
																																UpperCamelCase   			=     model(**self._prepare_for_class(A_						,				A_     )     )
																									UpperCamelCase   			=     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   			=     True
																									UpperCamelCase   			=     model_class(A_     )
																									model.to(A_     )
																									model.eval()
																									with torch.no_grad():
																																UpperCamelCase   			=     model(**self._prepare_for_class(A_						,				A_     )     )
																									UpperCamelCase   			=     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   			=     len(A_     )

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

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

																									UpperCamelCase   			=     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     )->		Dict:







											'''simple docstring'''






											def check_hidden_states_output(A_						,				A_						,				A_     ):
																		UpperCamelCase   			=     model_class(A_     )
																		model.to(A_     )
																		model.eval()

																		with torch.no_grad():
																									UpperCamelCase   			=     model(**self._prepare_for_class(A_						,				A_     )     )

																		UpperCamelCase   			=     outputs.hidden_states
																		UpperCamelCase   			=     self.model_tester.num_hidden_layers + 1
																		self.assertEqual(len(A_     )						,				A_     )

																		UpperCamelCase   			=     self.model_tester.seq_length

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

											UpperCamelCase			,	UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		UpperCamelCase   			=     True
																		check_hidden_states_output(A_						,				A_						,				A_     )

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

																		check_hidden_states_output(A_						,				A_						,				A_     )







def 		A_(     ):
							UpperCamelCase   			=     hf_hub_download(
							    repo_id='hf-internal-testing/spaghetti-video'		,     filename='eating_spaghetti.npy'		,     repo_type='dataset')
							UpperCamelCase   			=     np.load(A)
							return list(A)





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







				@cached_property
				def 						UpperCAmelCase_    (       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 						UpperCAmelCase_    (       self     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'     ).to(
											    A_     )

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

											# forward pass
											with torch.no_grad():
																		UpperCamelCase   			=     model(**A_     )

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

											UpperCamelCase   			=     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 Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...feature_extraction_utils import FeatureExtractionMixin
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import TensorType, logging


lowerCAmelCase					:    Any           = logging.get_logger(__name__)

lowerCAmelCase					:    Optional[int]           = {
    'deepmind/language-perceiver': 'https://huggingface.co/deepmind/language-perceiver/resolve/main/config.json',
    # See all Perceiver models at https://huggingface.co/models?filter=perceiver
}


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):
				lowerCAmelCase_     			=							"""perceiver"""







				def __init__(       self						,				A_=256						,				A_=1280						,				A_=768						,				A_=1						,				A_=26						,				A_=8						,				A_=8						,				A_=None						,				A_=None						,				A_="kv"						,				A_=1						,				A_=1						,				A_="gelu"						,				A_=0.1						,				A_=0.02						,				A_=1e-12						,				A_=True						,				A_=262						,				A_=2048						,				A_=56						,				A_=[368, 496]						,				A_=16						,				A_=1920						,				A_=16						,				A_=[1, 16, 224, 224]						,				**A_						,				)->		str:







											'''simple docstring'''






											super().__init__(**A_     )

											UpperCamelCase   			=     num_latents
											UpperCamelCase   			=     d_latents
											UpperCamelCase   			=     d_model
											UpperCamelCase   			=     num_blocks
											UpperCamelCase   			=     num_self_attends_per_block
											UpperCamelCase   			=     num_self_attention_heads
											UpperCamelCase   			=     num_cross_attention_heads
											UpperCamelCase   			=     qk_channels
											UpperCamelCase   			=     v_channels
											UpperCamelCase   			=     cross_attention_shape_for_attention
											UpperCamelCase   			=     self_attention_widening_factor
											UpperCamelCase   			=     cross_attention_widening_factor
											UpperCamelCase   			=     hidden_act
											UpperCamelCase   			=     attention_probs_dropout_prob
											UpperCamelCase   			=     initializer_range
											UpperCamelCase   			=     layer_norm_eps
											UpperCamelCase   			=     use_query_residual
											# masked language modeling attributes
											UpperCamelCase   			=     vocab_size
											UpperCamelCase   			=     max_position_embeddings
											# image classification attributes
											UpperCamelCase   			=     image_size
											# flow attributes
											UpperCamelCase   			=     train_size
											# multimodal autoencoding attributes
											UpperCamelCase   			=     num_frames
											UpperCamelCase   			=     audio_samples_per_frame
											UpperCamelCase   			=     samples_per_patch
											UpperCamelCase   			=     output_shape


class 			SCREAMING_SNAKE_CASE__      (     snake_case_):







				@property
				def 						UpperCAmelCase_    (       self     )->		Mapping[str, Mapping[int, str]]:







											'''simple docstring'''






											if self.task == "multiple-choice":
																		UpperCamelCase   			=     {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																		UpperCamelCase   			=     {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('inputs', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]     )







				@property
				def 						UpperCAmelCase_    (       self     )->		float:







											'''simple docstring'''






											return 1e-4







				def 						UpperCAmelCase_    (       self						,				A_						,				A_ = -1						,				A_ = -1						,				A_ = -1						,				A_ = False						,				A_ = None						,				A_ = 3						,				A_ = 40						,				A_ = 40						,				)->		Mapping[str, Any]:







											'''simple docstring'''






											if isinstance(A_						,				A_     ):
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_batch						,				num_token_to_add=0     )
																		# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
																		UpperCamelCase   			=     preprocessor.num_special_tokens_to_add(A_     )
																		UpperCamelCase   			=     compute_effective_axis_dimension(
																		    A_						,				fixed_dimension=OnnxConfig.default_fixed_sequence						,				num_token_to_add=A_     )
																		# Generate dummy inputs according to compute batch and sequence
																		UpperCamelCase   			=     [' '.join(['a']     ) * seq_length] * batch_size
																		UpperCamelCase   			=     dict(preprocessor(A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('input_ids'     )
																		return inputs
											elif isinstance(A_						,				A_     ) and preprocessor.model_input_names[0] == "pixel_values":
																		# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
																		UpperCamelCase   			=     compute_effective_axis_dimension(A_						,				fixed_dimension=OnnxConfig.default_fixed_batch     )
																		UpperCamelCase   			=     self._generate_dummy_images(A_						,				A_						,				A_						,				A_     )
																		UpperCamelCase   			=     dict(preprocessor(images=A_						,				return_tensors=A_     )     )
																		UpperCamelCase   			=     inputs.pop('pixel_values'     )
																		return inputs
											else:
																		raise ValueError(
																		    'Unable to generate dummy inputs for the model. Please provide a tokenizer or a preprocessor.'     )




