'''simple docstring'''




import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    BertTokenizer,
    ViltConfig,
    ViltForImageAndTextRetrieval,
    ViltForImagesAndTextClassification,
    ViltForMaskedLM,
    ViltForQuestionAnswering,
    ViltImageProcessor,
    ViltProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
_lowercase		:				Union[str, Any]	       =		logging.get_logger(__name__)




def 							lowerCamelCase			(   UpperCAmelCase__ :      int   ,   UpperCAmelCase__ :      Optional[int]=False   ,   UpperCAmelCase__ :      Any=False   ,   UpperCAmelCase__ :      Any=False				)    ->   Dict:
							lowercase_      :    str       		= []
							for i in range(config.num_hidden_layers				):
														# encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
														rename_keys.append((F'''transformer.blocks.{i}.norm1.weight''', F'''vilt.encoder.layer.{i}.layernorm_before.weight''')				)
														rename_keys.append((F'''transformer.blocks.{i}.norm1.bias''', F'''vilt.encoder.layer.{i}.layernorm_before.bias''')				)
														rename_keys.append(
														    (F'''transformer.blocks.{i}.attn.proj.weight''', F'''vilt.encoder.layer.{i}.attention.output.dense.weight''')				)
														rename_keys.append(
														    (F'''transformer.blocks.{i}.attn.proj.bias''', F'''vilt.encoder.layer.{i}.attention.output.dense.bias''')				)
														rename_keys.append((F'''transformer.blocks.{i}.norm2.weight''', F'''vilt.encoder.layer.{i}.layernorm_after.weight''')				)
														rename_keys.append((F'''transformer.blocks.{i}.norm2.bias''', F'''vilt.encoder.layer.{i}.layernorm_after.bias''')				)
														rename_keys.append(
														    (F'''transformer.blocks.{i}.mlp.fc1.weight''', F'''vilt.encoder.layer.{i}.intermediate.dense.weight''')				)
														rename_keys.append((F'''transformer.blocks.{i}.mlp.fc1.bias''', F'''vilt.encoder.layer.{i}.intermediate.dense.bias''')				)
														rename_keys.append((F'''transformer.blocks.{i}.mlp.fc2.weight''', F'''vilt.encoder.layer.{i}.output.dense.weight''')				)
														rename_keys.append((F'''transformer.blocks.{i}.mlp.fc2.bias''', F'''vilt.encoder.layer.{i}.output.dense.bias''')				)

							# embeddings
							rename_keys.extend(
							    [
							        # text embeddings
							        ("""text_embeddings.word_embeddings.weight""", """vilt.embeddings.text_embeddings.word_embeddings.weight"""),
							        (
							            """text_embeddings.position_embeddings.weight""",
							            """vilt.embeddings.text_embeddings.position_embeddings.weight""",
							        ),
							        ("""text_embeddings.position_ids""", """vilt.embeddings.text_embeddings.position_ids"""),
							        (
							            """text_embeddings.token_type_embeddings.weight""",
							            """vilt.embeddings.text_embeddings.token_type_embeddings.weight""",
							        ),
							        ("""text_embeddings.LayerNorm.weight""", """vilt.embeddings.text_embeddings.LayerNorm.weight"""),
							        ("""text_embeddings.LayerNorm.bias""", """vilt.embeddings.text_embeddings.LayerNorm.bias"""),
							        # patch embeddings
							        ("""transformer.cls_token""", """vilt.embeddings.cls_token"""),
							        ("""transformer.patch_embed.proj.weight""", """vilt.embeddings.patch_embeddings.projection.weight"""),
							        ("""transformer.patch_embed.proj.bias""", """vilt.embeddings.patch_embeddings.projection.bias"""),
							        ("""transformer.pos_embed""", """vilt.embeddings.position_embeddings"""),
							        # token type embeddings
							        ("""token_type_embeddings.weight""", """vilt.embeddings.token_type_embeddings.weight"""),
							    ]				)

							# final layernorm + pooler
							rename_keys.extend(
							    [
							        ("""transformer.norm.weight""", """vilt.layernorm.weight"""),
							        ("""transformer.norm.bias""", """vilt.layernorm.bias"""),
							        ("""pooler.dense.weight""", """vilt.pooler.dense.weight"""),
							        ("""pooler.dense.bias""", """vilt.pooler.dense.bias"""),
							    ]				)

							# classifier head(s)
							if vqa_model:
														# classification head
														rename_keys.extend(
														    [
														        ("""vqa_classifier.0.weight""", """classifier.0.weight"""),
														        ("""vqa_classifier.0.bias""", """classifier.0.bias"""),
														        ("""vqa_classifier.1.weight""", """classifier.1.weight"""),
														        ("""vqa_classifier.1.bias""", """classifier.1.bias"""),
														        ("""vqa_classifier.3.weight""", """classifier.3.weight"""),
														        ("""vqa_classifier.3.bias""", """classifier.3.bias"""),
														    ]				)
							elif nlvr_model:
														# classification head
														rename_keys.extend(
														    [
														        ("""nlvr2_classifier.0.weight""", """classifier.0.weight"""),
														        ("""nlvr2_classifier.0.bias""", """classifier.0.bias"""),
														        ("""nlvr2_classifier.1.weight""", """classifier.1.weight"""),
														        ("""nlvr2_classifier.1.bias""", """classifier.1.bias"""),
														        ("""nlvr2_classifier.3.weight""", """classifier.3.weight"""),
														        ("""nlvr2_classifier.3.bias""", """classifier.3.bias"""),
														    ]				)
							else:
														pass

							return rename_keys




def 							lowerCamelCase			(   UpperCAmelCase__ :      Tuple   ,   UpperCAmelCase__ :      Any				)    ->   Tuple:
							for i in range(config.num_hidden_layers				):
														lowercase_      :    Union[str, Any]       		= """vilt."""
														# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
														lowercase_      :    Tuple       		= state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.weight'''				)
														lowercase_      :    Optional[Any]       		= state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.bias'''				)
														# next, add query, keys and values (in that order) to the state dict
														lowercase_      :    List[Any]       		= in_proj_weight[
														    : config.hidden_size, :
														]
														lowercase_      :    List[str]       		= in_proj_bias[: config.hidden_size]
														lowercase_      :    List[str]       		= in_proj_weight[
														    config.hidden_size : config.hidden_size * 2, :
														]
														lowercase_      :    List[Any]       		= in_proj_bias[
														    config.hidden_size : config.hidden_size * 2
														]
														lowercase_      :    Any       		= in_proj_weight[
														    -config.hidden_size :, :
														]
														lowercase_      :    Dict       		= in_proj_bias[-config.hidden_size :]




def 							lowerCamelCase			(   UpperCAmelCase__ :      List[str]				)    ->   List[Any]:
							lowercase_      :    Tuple       		= ["""head.weight""", """head.bias"""]
							for k in ignore_keys:
														state_dict.pop(UpperCAmelCase__   ,   UpperCAmelCase__				)




def 							lowerCamelCase			(   UpperCAmelCase__ :      Tuple   ,   UpperCAmelCase__ :      List[str]   ,   UpperCAmelCase__ :      Union[str, Any]				)    ->   Union[str, Any]:
							lowercase_      :    Optional[Any]       		= dct.pop(UpperCAmelCase__				)
							lowercase_      :    Optional[Any]       		= val




@torch.no_grad()
def 							lowerCamelCase			(   UpperCAmelCase__ :      str   ,   UpperCAmelCase__ :      Optional[int]				)    ->   List[str]:
							lowercase_      :    int       		= ViltConfig(image_size=384   ,   patch_size=32   ,   tie_word_embeddings=UpperCAmelCase__				)
							lowercase_      :    str       		= False
							lowercase_      :    str       		= False
							lowercase_      :    str       		= False
							lowercase_      :    Optional[int]       		= False
							if "vqa" in checkpoint_url:
														lowercase_      :    int       		= True
														lowercase_      :    Optional[Any]       		= 3129
														lowercase_      :    int       		= """huggingface/label-files"""
														lowercase_      :    Dict       		= """vqa2-id2label.json"""
														lowercase_      :    Dict       		= json.load(open(hf_hub_download(UpperCAmelCase__   ,   UpperCAmelCase__   ,   repo_type="""dataset"""				)   ,   """r"""				)				)
														lowercase_      :    Optional[int]       		= {int(UpperCAmelCase__				): v for k, v in idalabel.items()}
														lowercase_      :    Optional[Any]       		= idalabel
														lowercase_      :    Optional[int]       		= {v: k for k, v in idalabel.items()}
														lowercase_      :    List[Any]       		= ViltForQuestionAnswering(UpperCAmelCase__				)
							elif "nlvr" in checkpoint_url:
														lowercase_      :    Any       		= True
														lowercase_      :    Tuple       		= 2
														lowercase_      :    Optional[int]       		= {0: """False""", 1: """True"""}
														lowercase_      :    Union[str, Any]       		= {v: k for k, v in config.idalabel.items()}
														lowercase_      :    List[str]       		= 3
														lowercase_      :    Tuple       		= ViltForImagesAndTextClassification(UpperCAmelCase__				)
							elif "irtr" in checkpoint_url:
														lowercase_      :    Optional[int]       		= True
														lowercase_      :    Optional[int]       		= ViltForImageAndTextRetrieval(UpperCAmelCase__				)
							elif "mlm_itm" in checkpoint_url:
														lowercase_      :    Optional[int]       		= True
														lowercase_      :    Tuple       		= ViltForMaskedLM(UpperCAmelCase__				)
							else:
														raise ValueError("""Unknown model type"""				)

							# load state_dict of original model, remove and rename some keys
							lowercase_      :    Tuple       		= torch.hub.load_state_dict_from_url(UpperCAmelCase__   ,   map_location="""cpu"""				)["""state_dict"""]
							lowercase_      :    int       		= create_rename_keys(UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__				)
							for src, dest in rename_keys:
														rename_key(UpperCAmelCase__   ,   UpperCAmelCase__   ,   UpperCAmelCase__				)
							read_in_q_k_v(UpperCAmelCase__   ,   UpperCAmelCase__				)
							if mlm_model or irtr_model:
														lowercase_      :    Union[str, Any]       		= ["""itm_score.fc.weight""", """itm_score.fc.bias"""]
														for k in ignore_keys:
																					state_dict.pop(UpperCAmelCase__   ,   UpperCAmelCase__				)

    # load state dict into HuggingFace model
							model.eval()
							if mlm_model:
														lowercase_		,		lowercase_      :    Tuple       		= model.load_state_dict(UpperCAmelCase__   ,   strict=UpperCAmelCase__				)
														assert missing_keys == ["mlm_score.decoder.bias"]
							else:
														model.load_state_dict(UpperCAmelCase__				)

							# Define processor
							lowercase_      :    Optional[Any]       		= ViltImageProcessor(size=384				)
							lowercase_      :    Optional[int]       		= BertTokenizer.from_pretrained("""bert-base-uncased"""				)
							lowercase_      :    Any       		= ViltProcessor(UpperCAmelCase__   ,   UpperCAmelCase__				)

							# Forward pass on example inputs (image + text)
							if nlvr_model:
														lowercase_      :    int       		= Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg"""   ,   stream=UpperCAmelCase__				).raw				)
														lowercase_      :    Optional[int]       		= Image.open(requests.get("""https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg"""   ,   stream=UpperCAmelCase__				).raw				)
														lowercase_      :    Optional[int]       		= (
														    """The left image contains twice the number of dogs as the right image, and at least two dogs in total are"""
														    """ standing."""
														)
														lowercase_      :    int       		= processor(UpperCAmelCase__   ,   UpperCAmelCase__   ,   return_tensors="""pt"""				)
														lowercase_      :    Dict       		= processor(UpperCAmelCase__   ,   UpperCAmelCase__   ,   return_tensors="""pt"""				)
														lowercase_      :    Any       		= model(
														    input_ids=encoding_a.input_ids   ,   pixel_values=encoding_a.pixel_values   ,   pixel_values_a=encoding_a.pixel_values   ,   )
							else:
														lowercase_      :    Any       		= Image.open(requests.get("""http://images.cocodataset.org/val2017/000000039769.jpg"""   ,   stream=UpperCAmelCase__				).raw				)
														if mlm_model:
																					lowercase_      :    str       		= """a bunch of [MASK] laying on a [MASK]."""
														else:
																					lowercase_      :    List[str]       		= """How many cats are there?"""
														lowercase_      :    Union[str, Any]       		= processor(UpperCAmelCase__   ,   UpperCAmelCase__   ,   return_tensors="""pt"""				)
														lowercase_      :    Dict       		= model(**UpperCAmelCase__				)

							# Verify outputs
							if mlm_model:
														lowercase_      :    List[str]       		= torch.Size([1, 11, 30522]				)
														lowercase_      :    List[Any]       		= torch.tensor([-12.5061, -12.5123, -12.5174]				)
														assert outputs.logits.shape == expected_shape
														assert torch.allclose(outputs.logits[0, 0, :3]   ,   UpperCAmelCase__   ,   atol=1e-4				)

														# verify masked token prediction equals "cats"
														lowercase_      :    Any       		= outputs.logits[0, 4, :].argmax(-1				).item()
														assert tokenizer.decode([predicted_id]				) == "cats"
							elif vqa_model:
														lowercase_      :    Any       		= torch.Size([1, 3129]				)
														lowercase_      :    Optional[Any]       		= torch.tensor([-15.9495, -18.1472, -10.3041]				)
														assert torch.allclose(outputs.logits[0, :3]   ,   UpperCAmelCase__   ,   atol=1e-4				)
														assert outputs.logits.shape == expected_shape
														assert torch.allclose(outputs.logits[0, 0, :3]   ,   UpperCAmelCase__   ,   atol=1e-4				)

														# verify vqa prediction equals "2"
														lowercase_      :    List[Any]       		= outputs.logits.argmax(-1				).item()
														assert model.config.idalabel[predicted_idx] == "2"
							elif nlvr_model:
														lowercase_      :    List[Any]       		= torch.Size([1, 2]				)
														lowercase_      :    List[Any]       		= torch.tensor([-2.8721, 2.1291]				)
														assert torch.allclose(outputs.logits[0, :3]   ,   UpperCAmelCase__   ,   atol=1e-4				)
														assert outputs.logits.shape == expected_shape

							Path(UpperCAmelCase__				).mkdir(exist_ok=UpperCAmelCase__				)
							print(F'''Saving model and processor to {pytorch_dump_folder_path}'''				)
							model.save_pretrained(UpperCAmelCase__				)
							processor.save_pretrained(UpperCAmelCase__				)


if __name__ == "__main__":
						_lowercase		:				Dict	       =		argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    "--checkpoint_url",
						    default="https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt",
						    type=str,
						    help="URL of the checkpoint you'd like to convert.",
						)
						parser.add_argument(
						    "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
						)

						_lowercase		:				int	       =		parser.parse_args()
						convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)






'''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       :						int       ,    lowercase_       :						Optional[Any]       ,    lowercase_       :						List[Any]=13       ,    lowercase_       :						List[str]=10       ,    lowercase_       :						Union[str, Any]=3       ,    lowercase_       :						str=2       ,    lowercase_       :						Optional[Any]=2       ,    lowercase_       :						int=True       ,    lowercase_       :						List[Any]=True       ,    lowercase_       :						Union[str, Any]=32       ,    lowercase_       :						Union[str, Any]=5       ,    lowercase_       :						str=4       ,    lowercase_       :						Dict=37       ,    lowercase_       :						Tuple="gelu"       ,    lowercase_       :						int=0.1       ,    lowercase_       :						Optional[Any]=0.1       ,    lowercase_       :						Any=10       ,    lowercase_       :						Tuple=0.02       ,    lowercase_       :						Any="divided_space_time"       ,    lowercase_       :						Tuple=None       ,    ):
										lowercase_      :    int       		= parent
										lowercase_      :    str       		= batch_size
										lowercase_      :    List[str]       		= image_size
										lowercase_      :    str       		= num_channels
										lowercase_      :    List[Any]       		= patch_size
										lowercase_      :    Optional[Any]       		= num_frames
										lowercase_      :    Dict       		= is_training
										lowercase_      :    int       		= use_labels
										lowercase_      :    List[str]       		= hidden_size
										lowercase_      :    Dict       		= num_hidden_layers
										lowercase_      :    Dict       		= num_attention_heads
										lowercase_      :    Any       		= intermediate_size
										lowercase_      :    Optional[int]       		= hidden_act
										lowercase_      :    Optional[Any]       		= hidden_dropout_prob
										lowercase_      :    List[Any]       		= attention_probs_dropout_prob
										lowercase_      :    Any       		= attention_type
										lowercase_      :    Union[str, Any]       		= initializer_range
										lowercase_      :    List[str]       		= scope
										lowercase_      :    Optional[int]       		= num_labels

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										lowercase_      :    Optional[Any]       		= floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

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

										return config, pixel_values, labels







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										lowercase_      :    int       		= 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       ,    )
										lowercase_      :    Any       		= self.num_labels
										return config







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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						List[str]       ,    lowercase_       :						str					):
										lowercase_      :    Dict       		= TimesformerForVideoClassification(lowercase_					)
										model.to(lowercase_					)
										model.eval()

										lowercase_      :    int       		= model(lowercase_					)

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_      :    List[str]       		= self.prepare_config_and_inputs()
										lowercase_		,		lowercase_		,		lowercase_      :    int       		= config_and_inputs
										lowercase_      :    List[Any]       		= {"""pixel_values""": pixel_values}
										return config, inputs_dict





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

			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False
			UpperCamelCase__       = False







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										lowercase_      :    Any       		= TimesformerModelTester(self					)
										lowercase_      :    Union[str, Any]       		= ConfigTester(
										    self       ,    config_class=lowercase_       ,    has_text_modality=lowercase_       ,    hidden_size=37					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						int       ,    lowercase_       :						Any       ,    lowercase_       :						List[str]       ,    lowercase_       :						Tuple=False					):
										lowercase_      :    List[Any]       		= copy.deepcopy(lowercase_					)

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

										return inputs_dict







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										self.config_tester.run_common_tests()







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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										lowercase_		,		lowercase_      :    Any       		= self.model_tester.prepare_config_and_inputs_for_common()

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_		,		lowercase_      :    str       		= self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowercase_      :    str       		= ["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    List[str]       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    str       		= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*lowercase_					)







			@slow
			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_      :    Any       		= TimesformerModel.from_pretrained(lowercase_					)
																	self.assertIsNotNone(lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										if not self.has_attentions:
																	pass

										else:
																	lowercase_		,		lowercase_      :    Any       		= self.model_tester.prepare_config_and_inputs_for_common()
																	lowercase_      :    List[str]       		= True

																	for model_class in self.all_model_classes:
																								lowercase_      :    str       		= self.model_tester.seq_length
																								lowercase_      :    int       		= self.model_tester.num_frames

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

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

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

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

																								lowercase_      :    Optional[Any]       		= outputs.attentions

																								self.assertEqual(len(lowercase_					)       ,    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 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										def check_hidden_states_output(lowercase_       :						Union[str, Any]       ,    lowercase_       :						Optional[int]       ,    lowercase_       :						Dict					):
																	lowercase_      :    List[str]       		= model_class(lowercase_					)
																	model.to(lowercase_					)
																	model.eval()

																	with torch.no_grad():
																								lowercase_      :    Optional[Any]       		= model(**self._prepare_for_class(lowercase_       ,    lowercase_					)					)

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

																	lowercase_      :    List[Any]       		= self.model_tester.seq_length

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

										lowercase_		,		lowercase_      :    List[Any]       		= self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	lowercase_      :    List[str]       		= True
																	check_hidden_states_output(lowercase_       ,    lowercase_       ,    lowercase_					)

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

																	check_hidden_states_output(lowercase_       ,    lowercase_       ,    lowercase_					)




def 							lowerCamelCase			(   )    ->   Optional[int]:
							lowercase_      :    List[str]       		= hf_hub_download(
							    repo_id="""hf-internal-testing/spaghetti-video"""   ,   filename="""eating_spaghetti.npy"""   ,   repo_type="""dataset"""				)
							lowercase_      :    List[Any]       		= np.load(UpperCAmelCase__				)
							return list(UpperCAmelCase__				)





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







			@cached_property
			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										# 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 			SCREAMING_SNAKE_CASE_  (			self       :						Dict					):
										lowercase_      :    Any       		= TimesformerForVideoClassification.from_pretrained("""facebook/timesformer-base-finetuned-k400"""					).to(
										    lowercase_					)

										lowercase_      :    Optional[Any]       		= self.default_image_processor
										lowercase_      :    Any       		= prepare_video()
										lowercase_      :    Optional[int]       		= image_processor(video[:8]       ,    return_tensors="""pt"""					).to(lowercase_					)

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

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

										lowercase_      :    int       		= torch.tensor([-0.30_16, -0.77_13, -0.42_05]					).to(lowercase_					)

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





