






"""simple docstring"""



import argparse
import os

import align
import numpy as np
import requests
import tensorflow as tf
import torch
from PIL import Image
from tokenizer import Tokenizer

from transformers import (
    AlignConfig,
    AlignModel,
    AlignProcessor,
    BertConfig,
    BertTokenizer,
    EfficientNetConfig,
    EfficientNetImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
_snake_case       :      Dict       =					logging.get_logger(__name__)




def 						lowerCamelCase_				(  __A  :		Tuple			):
					lowerCamelCase__				=tf.image.resize(__A			,	(346, 346)			)
					lowerCamelCase__				=tf.image.crop_to_bounding_box(__A			,	(346 - 289) // 2			,	(346 - 289) // 2			,	289			,	289			)
					return image




def 						lowerCamelCase_				(  ):
					lowerCamelCase__				=EfficientNetConfig.from_pretrained('google/efficientnet-b7'			)
					lowerCamelCase__				=289
					lowerCamelCase__				=640
					lowerCamelCase__				={'0': 'LABEL_0', '1': 'LABEL_1'}
					lowerCamelCase__				={'LABEL_0': 0, 'LABEL_1': 1}
					lowerCamelCase__				=[]

					lowerCamelCase__				=BertConfig()
					lowerCamelCase__				=AlignConfig.from_text_vision_configs(
					    text_config=__A			,	vision_config=__A			,	projection_dim=640			)
					return config




def 						lowerCamelCase_				(  ):
					lowerCamelCase__				='http://images.cocodataset.org/val2017/000000039769.jpg'
					lowerCamelCase__				=Image.open(requests.get(__A			,	stream=__A			).raw			)
					return im




def 						lowerCamelCase_				(  ):
					lowerCamelCase__				=EfficientNetImageProcessor(
					    do_center_crop=__A			,	rescale_factor=1 / 1_2_7.5			,	rescale_offset=__A			,	do_normalize=__A			,	include_top=__A			,	resample=Image.BILINEAR			,	)
					lowerCamelCase__				=BertTokenizer.from_pretrained('bert-base-uncased'			)
					lowerCamelCase__				=64
					lowerCamelCase__				=AlignProcessor(image_processor=__A			,	tokenizer=__A			)
					return processor




def 						lowerCamelCase_				(  __A  :		Union[str, Any]			):
					# EfficientNet image encoder
					lowerCamelCase__				=[v.split('_'			)[0].split('block'			)[1] for v in original_param_names if v.startswith('block'			)]
					lowerCamelCase__				=list(set(__A			)			)
					lowerCamelCase__				=sorted(__A			)
					lowerCamelCase__				=len(__A			)
					lowerCamelCase__				={b: str(__A			) for b, i in zip(__A			,	range(__A			)			)}

					lowerCamelCase__				=[]
					rename_keys.append(('stem_conv/kernel:0', 'embeddings.convolution.weight')			)
					rename_keys.append(('stem_bn/gamma:0', 'embeddings.batchnorm.weight')			)
					rename_keys.append(('stem_bn/beta:0', 'embeddings.batchnorm.bias')			)
					rename_keys.append(('stem_bn/moving_mean:0', 'embeddings.batchnorm.running_mean')			)
					rename_keys.append(('stem_bn/moving_variance:0', 'embeddings.batchnorm.running_var')			)

					for b in block_names:
										lowerCamelCase__				=block_name_mapping[b]
										rename_keys.append((f"block{b}_expand_conv/kernel:0", f"encoder.blocks.{hf_b}.expansion.expand_conv.weight")			)
										rename_keys.append((f"block{b}_expand_bn/gamma:0", f"encoder.blocks.{hf_b}.expansion.expand_bn.weight")			)
										rename_keys.append((f"block{b}_expand_bn/beta:0", f"encoder.blocks.{hf_b}.expansion.expand_bn.bias")			)
										rename_keys.append(
										    (f"block{b}_expand_bn/moving_mean:0", f"encoder.blocks.{hf_b}.expansion.expand_bn.running_mean")			)
										rename_keys.append(
										    (f"block{b}_expand_bn/moving_variance:0", f"encoder.blocks.{hf_b}.expansion.expand_bn.running_var")			)
										rename_keys.append(
										    (f"block{b}_dwconv/depthwise_kernel:0", f"encoder.blocks.{hf_b}.depthwise_conv.depthwise_conv.weight")			)
										rename_keys.append((f"block{b}_bn/gamma:0", f"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.weight")			)
										rename_keys.append((f"block{b}_bn/beta:0", f"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.bias")			)
										rename_keys.append(
										    (f"block{b}_bn/moving_mean:0", f"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_mean")			)
										rename_keys.append(
										    (f"block{b}_bn/moving_variance:0", f"encoder.blocks.{hf_b}.depthwise_conv.depthwise_norm.running_var")			)

										rename_keys.append((f"block{b}_se_reduce/kernel:0", f"encoder.blocks.{hf_b}.squeeze_excite.reduce.weight")			)
										rename_keys.append((f"block{b}_se_reduce/bias:0", f"encoder.blocks.{hf_b}.squeeze_excite.reduce.bias")			)
										rename_keys.append((f"block{b}_se_expand/kernel:0", f"encoder.blocks.{hf_b}.squeeze_excite.expand.weight")			)
										rename_keys.append((f"block{b}_se_expand/bias:0", f"encoder.blocks.{hf_b}.squeeze_excite.expand.bias")			)
										rename_keys.append(
										    (f"block{b}_project_conv/kernel:0", f"encoder.blocks.{hf_b}.projection.project_conv.weight")			)
										rename_keys.append((f"block{b}_project_bn/gamma:0", f"encoder.blocks.{hf_b}.projection.project_bn.weight")			)
										rename_keys.append((f"block{b}_project_bn/beta:0", f"encoder.blocks.{hf_b}.projection.project_bn.bias")			)
										rename_keys.append(
										    (f"block{b}_project_bn/moving_mean:0", f"encoder.blocks.{hf_b}.projection.project_bn.running_mean")			)
										rename_keys.append(
										    (f"block{b}_project_bn/moving_variance:0", f"encoder.blocks.{hf_b}.projection.project_bn.running_var")			)

					lowerCamelCase__				={}
					for item in rename_keys:
										if item[0] in original_param_names:
															lowerCamelCase__				='vision_model.' + item[1]

    # BERT text encoder
					lowerCamelCase__				=[]
					lowerCamelCase__				='tf_bert_model/bert'
					lowerCamelCase__				='text_model'
					for i in range(12			):
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/query/kernel:0",
										        f"{new}.encoder.layer.{i}.attention.self.query.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/query/bias:0",
										        f"{new}.encoder.layer.{i}.attention.self.query.bias",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/key/kernel:0",
										        f"{new}.encoder.layer.{i}.attention.self.key.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/key/bias:0",
										        f"{new}.encoder.layer.{i}.attention.self.key.bias",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/value/kernel:0",
										        f"{new}.encoder.layer.{i}.attention.self.value.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/self/value/bias:0",
										        f"{new}.encoder.layer.{i}.attention.self.value.bias",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/output/dense/kernel:0",
										        f"{new}.encoder.layer.{i}.attention.output.dense.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/output/dense/bias:0",
										        f"{new}.encoder.layer.{i}.attention.output.dense.bias",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/output/LayerNorm/gamma:0",
										        f"{new}.encoder.layer.{i}.attention.output.LayerNorm.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/attention/output/LayerNorm/beta:0",
										        f"{new}.encoder.layer.{i}.attention.output.LayerNorm.bias",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/intermediate/dense/kernel:0",
										        f"{new}.encoder.layer.{i}.intermediate.dense.weight",
										    )			)
										rename_keys.append(
										    (
										        f"{old}/encoder/layer_._{i}/intermediate/dense/bias:0",
										        f"{new}.encoder.layer.{i}.intermediate.dense.bias",
										    )			)
										rename_keys.append(
										    (f"{old}/encoder/layer_._{i}/output/dense/kernel:0", f"{new}.encoder.layer.{i}.output.dense.weight")			)
										rename_keys.append(
										    (f"{old}/encoder/layer_._{i}/output/dense/bias:0", f"{new}.encoder.layer.{i}.output.dense.bias")			)
										rename_keys.append(
										    (f"{old}/encoder/layer_._{i}/output/LayerNorm/gamma:0", f"{new}.encoder.layer.{i}.output.LayerNorm.weight")			)
										rename_keys.append(
										    (f"{old}/encoder/layer_._{i}/output/LayerNorm/beta:0", f"{new}.encoder.layer.{i}.output.LayerNorm.bias")			)

					rename_keys.append((f"{old}/embeddings/word_embeddings/weight:0", f"{new}.embeddings.word_embeddings.weight")			)
					rename_keys.append(
					    (f"{old}/embeddings/position_embeddings/embeddings:0", f"{new}.embeddings.position_embeddings.weight")			)
					rename_keys.append(
					    (f"{old}/embeddings/token_type_embeddings/embeddings:0", f"{new}.embeddings.token_type_embeddings.weight")			)
					rename_keys.append((f"{old}/embeddings/LayerNorm/gamma:0", f"{new}.embeddings.LayerNorm.weight")			)
					rename_keys.append((f"{old}/embeddings/LayerNorm/beta:0", f"{new}.embeddings.LayerNorm.bias")			)

					rename_keys.append((f"{old}/pooler/dense/kernel:0", f"{new}.pooler.dense.weight")			)
					rename_keys.append((f"{old}/pooler/dense/bias:0", f"{new}.pooler.dense.bias")			)
					rename_keys.append(('dense/kernel:0', 'text_projection.weight')			)
					rename_keys.append(('dense/bias:0', 'text_projection.bias')			)
					rename_keys.append(('dense/bias:0', 'text_projection.bias')			)
					rename_keys.append(('temperature:0', 'temperature')			)

					for item in rename_keys:
										if item[0] in original_param_names:
															lowerCamelCase__				=item[1]
					return key_mapping




def 						lowerCamelCase_				(  __A  :		List[Any]			,	__A  :		Dict			,	__A  :		str			):
					list(hf_params.keys()			)

					for key, value in tf_params.items():
										if key not in key_mapping:
															continue

										lowerCamelCase__				=key_mapping[key]
										if "_conv" in key and "kernel" in key:
															lowerCamelCase__				=torch.from_numpy(__A			).permute(3			,	2			,	0			,	1			)
										elif "embeddings" in key:
															lowerCamelCase__				=torch.from_numpy(__A			)
										elif "depthwise_kernel" in key:
															lowerCamelCase__				=torch.from_numpy(__A			).permute(2			,	3			,	0			,	1			)
										elif "kernel" in key:
															lowerCamelCase__				=torch.from_numpy(np.transpose(__A			)			)
										elif "temperature" in key:
															lowerCamelCase__				=value
										elif "bn/gamma" or "bn/beta" in key:
															lowerCamelCase__				=torch.from_numpy(np.transpose(__A			)			).squeeze()
										else:
															lowerCamelCase__				=torch.from_numpy(__A			)

										# Replace HF parameters with original TF model parameters
										hf_params[hf_key].copy_(__A			)




@torch.no_grad()
def 						lowerCamelCase_				(  __A  :		List[str]			,	__A  :		Union[str, Any]			,	__A  :		str			,	__A  :		int			):
					lowerCamelCase__				=64
					lowerCamelCase__				=Tokenizer(__A			)
					lowerCamelCase__				=align.Align('efficientnet-b7'			,	'bert-base'			,	640			,	__A			,	tok.get_vocab_size()			)
					original_model.compile()
					original_model.load_weights(__A			)

					lowerCamelCase__				=original_model.trainable_variables
					lowerCamelCase__				=original_model.non_trainable_variables
					lowerCamelCase__				={param.name: param.numpy() for param in tf_params}
					for param in tf_non_train_params:
										lowerCamelCase__				=param.numpy()
					lowerCamelCase__				=list(tf_params.keys()			)

					# Load HuggingFace model
					lowerCamelCase__				=get_align_config()
					lowerCamelCase__				=AlignModel(__A			).eval()
					lowerCamelCase__				=hf_model.state_dict()

					# Create src-to-dst parameter name mapping dictionary
					print('Converting parameters...'			)
					lowerCamelCase__				=rename_keys(__A			)
					replace_params(__A			,	__A			,	__A			)

					# Initialize processor
					lowerCamelCase__				=get_processor()
					lowerCamelCase__				=processor(
					    images=prepare_img()			,	text='A picture of a cat'			,	padding='max_length'			,	max_length=64			,	return_tensors='pt'			)

					# HF model inference
					hf_model.eval()
					with torch.no_grad():
										lowerCamelCase__				=hf_model(**__A			)

					lowerCamelCase__				=outputs.image_embeds.detach().numpy()
					lowerCamelCase__				=outputs.text_embeds.detach().numpy()

					# Original model inference
					lowerCamelCase__				=False
					lowerCamelCase__				=EfficientNetImageProcessor(
					    do_center_crop=__A			,	do_rescale=__A			,	do_normalize=__A			,	include_top=__A			,	resample=Image.BILINEAR			,	)
					lowerCamelCase__				=tf_image_processor(images=prepare_img()			,	return_tensors='tf'			,	data_format='channels_last'			)['pixel_values']
					lowerCamelCase__				=tok(tf.constant(['A picture of a cat']			)			)

					lowerCamelCase__				=original_model.image_encoder(__A			,	training=__A			)
					lowerCamelCase__				=original_model.text_encoder(__A			,	training=__A			)

					lowerCamelCase__				=tf.nn.la_normalize(__A			,	axis=-1			)
					lowerCamelCase__				=tf.nn.la_normalize(__A			,	axis=-1			)

					# Check whether original and HF model outputs match  -> np.allclose
					if not np.allclose(__A			,	__A			,	atol=1E-3			):
										raise ValueError('The predicted image features are not the same.'			)
					if not np.allclose(__A			,	__A			,	atol=1E-3			):
										raise ValueError('The predicted text features are not the same.'			)
					print('Model outputs match!'			)

					if save_model:
										# Create folder to save model
										if not os.path.isdir(__A			):
															os.mkdir(__A			)
										# Save converted model and image processor
										hf_model.save_pretrained(__A			)
										processor.save_pretrained(__A			)

					if push_to_hub:
										# Push model and image processor to hub
										print('Pushing converted ALIGN to the hub...'			)
										processor.push_to_hub('align-base'			)
										hf_model.push_to_hub('align-base'			)


if __name__ == "__main__":
						_snake_case       :      int       =					argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    "--checkpoint_path",
						    default="./weights/model-weights",
						    type=str,
						    help="Path to the pretrained TF ALIGN checkpoint.",
						)
						parser.add_argument(
						    "--pytorch_dump_folder_path",
						    default="hf_model",
						    type=str,
						    help="Path to the output PyTorch model directory.",
						)
						parser.add_argument("--save_model", action="store_true", help="Save model to local")
						parser.add_argument("--push_to_hub", action="store_true", help="Push model and image processor to the hub")

						_snake_case       :      int       =					parser.parse_args()
						convert_align_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.save_model, args.push_to_hub)







"""simple docstring"""



import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VivitConfig
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


if is_torch_available():
						import torch
						from torch import nn

						from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VivitForVideoClassification, VivitModel
						from transformers.models.vivit.modeling_vivit import VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from transformers import VivitImageProcessor




class        __lowercase:






						def __init__(					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE=2    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=10    ,						_SCREAMING_SNAKE_CASE=10    ,						_SCREAMING_SNAKE_CASE=8    ,						_SCREAMING_SNAKE_CASE=[2, 4, 4]    ,						_SCREAMING_SNAKE_CASE=3    ,						_SCREAMING_SNAKE_CASE=7_68    ,						_SCREAMING_SNAKE_CASE=5    ,						_SCREAMING_SNAKE_CASE=4    ,						_SCREAMING_SNAKE_CASE=37    ,						_SCREAMING_SNAKE_CASE="gelu_fast"    ,						_SCREAMING_SNAKE_CASE=0.0    ,						_SCREAMING_SNAKE_CASE=0.0    ,						_SCREAMING_SNAKE_CASE=0.02    ,						_SCREAMING_SNAKE_CASE=1E-06    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=None    ,						)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				=parent
											lowerCamelCase__				=batch_size
											lowerCamelCase__				=is_training
											lowerCamelCase__				=use_labels
											lowerCamelCase__				=num_labels
											lowerCamelCase__				=image_size
											lowerCamelCase__				=num_frames
											lowerCamelCase__				=tubelet_size
											lowerCamelCase__				=num_channels
											lowerCamelCase__				=hidden_size
											lowerCamelCase__				=num_hidden_layers
											lowerCamelCase__				=num_attention_heads
											lowerCamelCase__				=intermediate_size
											lowerCamelCase__				=hidden_act
											lowerCamelCase__				=hidden_dropout_prob
											lowerCamelCase__				=attention_probs_dropout_prob
											lowerCamelCase__				=initializer_range
											lowerCamelCase__				=layer_norm_eps
											lowerCamelCase__				=qkv_bias
											lowerCamelCase__				=scope

											lowerCamelCase__				=(
											    (self.image_size // self.tubelet_size[2])
											    * (self.image_size // self.tubelet_size[1])
											    * (self.num_frames // self.tubelet_size[0])
											) + 1  # CLS token






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




											"""simple docstring"""





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

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

											lowerCamelCase__				=self.get_config()

											return config, pixel_values, labels






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				=VivitConfig(
											    num_frames=self.num_frames    ,						image_size=self.image_size    ,						tubelet_size=self.tubelet_size    ,						num_channels=self.num_channels    ,						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    ,						layer_norm_eps=self.layer_norm_eps    ,						qkv_bias=self.qkv_bias    ,						)
											lowerCamelCase__				=self.num_labels
											return config






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  int:




											"""simple docstring"""





											lowerCamelCase__				=VivitModel(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__				=model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size))






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  str:




											"""simple docstring"""





											lowerCamelCase__				=VivitForVideoClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

											lowerCamelCase__				=model(_SCREAMING_SNAKE_CASE)

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






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





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




@require_torch
class        __lowercase(       _UpperCamelCase						,							unittest.TestCase  ):
						lowerCAmelCase_		:							str=       (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

						lowerCAmelCase_		:							str=       False
						lowerCAmelCase_		:							Tuple=       False
						lowerCAmelCase_		:							Union[str, Any]=       False
						lowerCAmelCase_		:							Dict=       False






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




											"""simple docstring"""





											lowerCamelCase__				=VivitModelTester(self)
											lowerCamelCase__				=ConfigTester(self    ,						config_class=_SCREAMING_SNAKE_CASE    ,						has_text_modality=_SCREAMING_SNAKE_CASE    ,						hidden_size=37)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE=False)			->  Optional[int]:




											"""simple docstring"""





											lowerCamelCase__				=copy.deepcopy(_SCREAMING_SNAKE_CASE)

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

											return inputs_dict






						def 		lowerCAmelCase   (					self)			->  Tuple:




											"""simple docstring"""





											self.config_tester.run_common_tests()






						@unittest.skip(reason='Vivit does not use inputs_embeds')
						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											pass






						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

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






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

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

																lowerCamelCase__				=['pixel_values', 'head_mask']
																self.assertListEqual(arg_names[:2]    ,						_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  Optional[int]:




											"""simple docstring"""





											lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





											lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_video_classification(*_SCREAMING_SNAKE_CASE)






						@slow
						def 		lowerCAmelCase   (					self)			->  Tuple:




											"""simple docstring"""





											for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowerCamelCase__				=VivitModel.from_pretrained(_SCREAMING_SNAKE_CASE)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE)






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




											"""simple docstring"""





											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()
											lowerCamelCase__				=True

											for model_class in self.all_model_classes:
																lowerCamelCase__				=self.model_tester.seq_length

																lowerCamelCase__				=True
																lowerCamelCase__				=False
																lowerCamelCase__				=True
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))
																lowerCamelCase__				=outputs.attentions
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)    ,						self.model_tester.num_hidden_layers)

																# check that output_attentions also work using config
																del inputs_dict["output_attentions"]
																lowerCamelCase__				=True
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))
																lowerCamelCase__				=outputs.attentions
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)    ,						self.model_tester.num_hidden_layers)

																self.assertListEqual(
																    list(attentions[0].shape[-3:])    ,						[self.model_tester.num_attention_heads, seq_len, seq_len]    ,						)
																lowerCamelCase__				=len(_SCREAMING_SNAKE_CASE)

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

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

																lowerCamelCase__				=outputs.attentions

																self.assertEqual(len(_SCREAMING_SNAKE_CASE)    ,						self.model_tester.num_hidden_layers)

																self.assertListEqual(
																    list(self_attentions[0].shape[-3:])    ,						[self.model_tester.num_attention_heads, seq_len, seq_len]    ,						)






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											def check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE):
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()

																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))

																lowerCamelCase__				=outputs.hidden_states
																lowerCamelCase__				=self.model_tester.num_hidden_layers + 1
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)    ,						_SCREAMING_SNAKE_CASE)

																lowerCamelCase__				=self.model_tester.seq_length

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

											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																lowerCamelCase__				=True
																check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)

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

																check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)






						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											pass




def 						lowerCamelCase_				(  ):
					lowerCamelCase__				=hf_hub_download(
					    repo_id='hf-internal-testing/spaghetti-video'			,	filename='eating_spaghetti_32_frames.npy'			,	repo_type='dataset'			)
					lowerCamelCase__				=np.load(__A			)
					return list(__A			)




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






						@cached_property
						def 		lowerCAmelCase   (					self)			->  Union[str, Any]:




											"""simple docstring"""





											return VivitImageProcessor() if is_vision_available() else None






						@slow
						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





											lowerCamelCase__				=VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400').to(_SCREAMING_SNAKE_CASE)

											lowerCamelCase__				=self.default_image_processor
											lowerCamelCase__				=prepare_video()
											lowerCamelCase__				=image_processor(_SCREAMING_SNAKE_CASE    ,						return_tensors='pt').to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																lowerCamelCase__				=model(**_SCREAMING_SNAKE_CASE)

											# verify the logits
											lowerCamelCase__				=torch.Size((1, 4_00))
											self.assertEqual(outputs.logits.shape    ,						_SCREAMING_SNAKE_CASE)

											# taken from original model
											lowerCamelCase__				=torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]).to(_SCREAMING_SNAKE_CASE)

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