






"""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 ConvNextConfig, ConvNextForImageClassification, ConvNextImageProcessor
from transformers.utils import logging


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




def 						lowerCamelCase_				(  __A  :		Tuple			):
					lowerCamelCase__				=ConvNextConfig()

					if "tiny" in checkpoint_url:
										lowerCamelCase__				=[3, 3, 9, 3]
										lowerCamelCase__				=[96, 192, 384, 768]
					if "small" in checkpoint_url:
										lowerCamelCase__				=[3, 3, 27, 3]
										lowerCamelCase__				=[96, 192, 384, 768]
					if "base" in checkpoint_url:
										lowerCamelCase__				=[3, 3, 27, 3]
										lowerCamelCase__				=[128, 256, 512, 1024]
					if "large" in checkpoint_url:
										lowerCamelCase__				=[3, 3, 27, 3]
										lowerCamelCase__				=[192, 384, 768, 1536]
					if "xlarge" in checkpoint_url:
										lowerCamelCase__				=[3, 3, 27, 3]
										lowerCamelCase__				=[256, 512, 1024, 2048]

					if "1k" in checkpoint_url:
										lowerCamelCase__				=1000
										lowerCamelCase__				='imagenet-1k-id2label.json'
										lowerCamelCase__				=(1, 1000)
					else:
										lowerCamelCase__				=2_1841
										lowerCamelCase__				='imagenet-22k-id2label.json'
										lowerCamelCase__				=(1, 2_1841)

					lowerCamelCase__				='huggingface/label-files'
					lowerCamelCase__				=num_labels
					lowerCamelCase__				=json.load(open(hf_hub_download(__A			,	__A			,	repo_type='dataset'			)			,	'r'			)			)
					lowerCamelCase__				={int(__A			): v for k, v in idalabel.items()}
					if "1k" not in checkpoint_url:
										# this dataset contains 21843 labels but the model only has 21841
										# we delete the classes as mentioned in https://github.com/google-research/big_transfer/issues/18
										del idalabel[9205]
										del idalabel[1_5027]
					lowerCamelCase__				=idalabel
					lowerCamelCase__				={v: k for k, v in idalabel.items()}
					lowerCamelCase__				=hidden_sizes
					lowerCamelCase__				=depths

					return config, expected_shape




def 						lowerCamelCase_				(  __A  :		List[Any]			):
					if "downsample_layers.0.0" in name:
										lowerCamelCase__				=name.replace('downsample_layers.0.0'			,	'embeddings.patch_embeddings'			)
					if "downsample_layers.0.1" in name:
										lowerCamelCase__				=name.replace('downsample_layers.0.1'			,	'embeddings.norm'			)  # we rename to layernorm later on
					if "downsample_layers.1.0" in name:
										lowerCamelCase__				=name.replace('downsample_layers.1.0'			,	'stages.1.downsampling_layer.0'			)
					if "downsample_layers.1.1" in name:
										lowerCamelCase__				=name.replace('downsample_layers.1.1'			,	'stages.1.downsampling_layer.1'			)
					if "downsample_layers.2.0" in name:
										lowerCamelCase__				=name.replace('downsample_layers.2.0'			,	'stages.2.downsampling_layer.0'			)
					if "downsample_layers.2.1" in name:
										lowerCamelCase__				=name.replace('downsample_layers.2.1'			,	'stages.2.downsampling_layer.1'			)
					if "downsample_layers.3.0" in name:
										lowerCamelCase__				=name.replace('downsample_layers.3.0'			,	'stages.3.downsampling_layer.0'			)
					if "downsample_layers.3.1" in name:
										lowerCamelCase__				=name.replace('downsample_layers.3.1'			,	'stages.3.downsampling_layer.1'			)
					if "stages" in name and "downsampling_layer" not in name:
										# stages.0.0. for instance should be renamed to stages.0.layers.0.
										lowerCamelCase__				=name[: len('stages.0'			)] + '.layers' + name[len('stages.0'			) :]
					if "stages" in name:
										lowerCamelCase__				=name.replace('stages'			,	'encoder.stages'			)
					if "norm" in name:
										lowerCamelCase__				=name.replace('norm'			,	'layernorm'			)
					if "gamma" in name:
										lowerCamelCase__				=name.replace('gamma'			,	'layer_scale_parameter'			)
					if "head" in name:
										lowerCamelCase__				=name.replace('head'			,	'classifier'			)

					return name




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




@torch.no_grad()
def 						lowerCamelCase_				(  __A  :		Any			,	__A  :		List[str]			):
					lowerCamelCase__				,     lowerCamelCase__				=get_convnext_config(__A			)
					# load original state_dict from URL
					lowerCamelCase__				=torch.hub.load_state_dict_from_url(__A			)['model']
					# rename keys
					for key in state_dict.copy().keys():
										lowerCamelCase__				=state_dict.pop(__A			)
										lowerCamelCase__				=val
					# add prefix to all keys expect classifier head
					for key in state_dict.copy().keys():
										lowerCamelCase__				=state_dict.pop(__A			)
										if not key.startswith('classifier'			):
															lowerCamelCase__				='convnext.' + key
										lowerCamelCase__				=val

					# load HuggingFace model
					lowerCamelCase__				=ConvNextForImageClassification(__A			)
					model.load_state_dict(__A			)
					model.eval()

					# Check outputs on an image, prepared by ConvNextImageProcessor
					lowerCamelCase__				=224 if '224' in checkpoint_url else 384
					lowerCamelCase__				=ConvNextImageProcessor(size=__A			)
					lowerCamelCase__				=image_processor(images=prepare_img()			,	return_tensors='pt'			).pixel_values

					lowerCamelCase__				=model(__A			).logits

					# note: the logits below were obtained without center cropping
					if checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_tiny_1k_224_ema.pth":
										lowerCamelCase__				=torch.tensor([-0.1_2_1_0, -0.6_6_0_5, 0.1_9_1_8]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_small_1k_224_ema.pth":
										lowerCamelCase__				=torch.tensor([-0.4_4_7_3, -0.1_8_4_7, -0.6_3_6_5]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_1k_224_ema.pth":
										lowerCamelCase__				=torch.tensor([0.4_5_2_5, 0.7_5_3_9, 0.0_3_0_8]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_1k_384.pth":
										lowerCamelCase__				=torch.tensor([0.3_5_6_1, 0.6_3_5_0, -0.0_3_8_4]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_1k_224_ema.pth":
										lowerCamelCase__				=torch.tensor([0.4_1_7_4, -0.0_9_8_9, 0.1_4_8_9]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_1k_384.pth":
										lowerCamelCase__				=torch.tensor([0.2_5_1_3, -0.1_3_4_9, -0.1_6_1_3]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_224.pth":
										lowerCamelCase__				=torch.tensor([1.2_9_8_0, 0.3_6_3_1, -0.1_1_9_8]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_224.pth":
										lowerCamelCase__				=torch.tensor([1.2_9_6_3, 0.1_2_2_7, 0.1_7_2_3]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_224.pth":
										lowerCamelCase__				=torch.tensor([1.7_9_5_6, 0.8_3_9_0, 0.2_8_2_0]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_1k_224.pth":
										lowerCamelCase__				=torch.tensor([-0.2_8_2_2, -0.0_5_0_2, -0.0_8_7_8]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_base_22k_1k_384.pth":
										lowerCamelCase__				=torch.tensor([-0.5_6_7_2, -0.0_7_3_0, -0.4_3_4_8]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_1k_224.pth":
										lowerCamelCase__				=torch.tensor([0.2_6_8_1, 0.2_3_6_5, 0.6_2_4_6]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_large_22k_1k_384.pth":
										lowerCamelCase__				=torch.tensor([-0.2_6_4_2, 0.3_9_3_1, 0.5_1_1_6]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_1k_224_ema.pth":
										lowerCamelCase__				=torch.tensor([-0.6_6_7_7, -0.1_8_7_3, -0.8_3_7_9]			)
					elif checkpoint_url == "https://dl.fbaipublicfiles.com/convnext/convnext_xlarge_22k_1k_384_ema.pth":
										lowerCamelCase__				=torch.tensor([-0.7_7_4_9, -0.2_9_6_7, -0.6_4_4_4]			)
					else:
										raise ValueError(f"Unknown URL: {checkpoint_url}"			)

					assert torch.allclose(logits[0, :3]			,	__A			,	atol=1E-3			)
					assert logits.shape == expected_shape

					Path(__A			).mkdir(exist_ok=__A			)
					print(f"Saving model to {pytorch_dump_folder_path}"			)
					model.save_pretrained(__A			)
					print(f"Saving image processor to {pytorch_dump_folder_path}"			)
					image_processor.save_pretrained(__A			)

					print('Pushing model to the hub...'			)
					lowerCamelCase__				='convnext'
					if "tiny" in checkpoint_url:
										model_name += "-tiny"
					elif "small" in checkpoint_url:
										model_name += "-small"
					elif "base" in checkpoint_url:
										model_name += "-base"
					elif "xlarge" in checkpoint_url:
										model_name += "-xlarge"
					elif "large" in checkpoint_url:
										model_name += "-large"
					if "224" in checkpoint_url:
										model_name += "-224"
					elif "384" in checkpoint_url:
										model_name += "-384"
					if "22k" in checkpoint_url and "1k" not in checkpoint_url:
										model_name += "-22k"
					if "22k" in checkpoint_url and "1k" in checkpoint_url:
										model_name += "-22k-1k"

					model.push_to_hub(
					    repo_path_or_name=Path(__A			,	__A			)			,	organization='nielsr'			,	commit_message='Add model'			,	)


if __name__ == "__main__":
							_snake_case       :      List[Any]       =					argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    "--checkpoint_url",
							    default="https://dl.fbaipublicfiles.com/convnext/convnext_tiny_1k_224_ema.pth",
							    type=str,
							    help="URL of the original ConvNeXT checkpoint you'd like to convert.",
							)
							parser.add_argument(
							    "--pytorch_dump_folder_path",
							    default=None,
							    type=str,
							    required=True,
							    help="Path to the output PyTorch model directory.",
							)

							_snake_case       :      int       =					parser.parse_args()
							convert_convnext_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 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))