





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







		def __init__(     self			,    __snake_case			,    __snake_case=2			,    __snake_case=True			,    __snake_case=True			,    __snake_case=1_0			,    __snake_case=1_0			,    __snake_case=8			,    __snake_case=[2, 4, 4]			,    __snake_case=3			,    __snake_case=7_6_8			,    __snake_case=5			,    __snake_case=4			,    __snake_case=3_7			,    __snake_case="gelu_fast"			,    __snake_case=0.0			,    __snake_case=0.0			,    __snake_case=0.02			,    __snake_case=1E-06			,    __snake_case=True			,    __snake_case=None			,    )   ->   str:


							'''simple docstring'''

							_snake_case							:       str												= parent
							_snake_case							:       List[str]												= batch_size
							_snake_case							:       Dict												= is_training
							_snake_case							:       str												= use_labels
							_snake_case							:       int												= num_labels
							_snake_case							:       List[Any]												= image_size
							_snake_case							:       int												= num_frames
							_snake_case							:       List[str]												= tubelet_size
							_snake_case							:       Optional[Any]												= num_channels
							_snake_case							:       str												= hidden_size
							_snake_case							:       Optional[Any]												= num_hidden_layers
							_snake_case							:       int												= num_attention_heads
							_snake_case							:       Union[str, Any]												= intermediate_size
							_snake_case							:       List[str]												= hidden_act
							_snake_case							:       Dict												= hidden_dropout_prob
							_snake_case							:       Tuple												= attention_probs_dropout_prob
							_snake_case							:       Optional[int]												= initializer_range
							_snake_case							:       List[Any]												= layer_norm_eps
							_snake_case							:       Optional[Any]												= qkv_bias
							_snake_case							:       int												= scope

							_snake_case							:       Union[str, Any]												= (
							    (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 							lowercase							(     self					)   ->   Union[str, Any]:


							'''simple docstring'''

							_snake_case							:       int												= floats_tensor(
							    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

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

							return config, pixel_values, labels







		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       int												= 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			,    )
							_snake_case							:       Optional[Any]												= self.num_labels
							return config







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case					)   ->   List[str]:


							'''simple docstring'''

							_snake_case							:       List[Any]												= VivitModel(config=__snake_case					)
							model.to(__snake_case					)
							model.eval()
							_snake_case							:       List[Any]												= model(__snake_case					)
							self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case					)   ->   List[Any]:


							'''simple docstring'''

							_snake_case							:       Dict												= VivitForVideoClassification(__snake_case					)
							model.to(__snake_case					)
							model.eval()

							_snake_case							:       List[Any]												= model(__snake_case					)

							# verify the logits shape
							_snake_case							:       Dict												= torch.Size((self.batch_size, self.num_labels)					)
							self.parent.assertEqual(result.logits.shape			,    __snake_case					)







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


							'''simple docstring'''

							_snake_case							:       List[str]												= self.prepare_config_and_inputs()
							_snake_case							,			_snake_case							,			_snake_case							:       Tuple												= config_and_inputs
							_snake_case							:       List[Any]												= {"pixel_values": pixel_values}
							return config, inputs_dict






@require_torch
class 							_a(     __A						, unittest.TestCase     ):
		lowerCamelCase__  :Dict 					=		(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

		lowerCamelCase__  :str 					=		False
		lowerCamelCase__  :Dict 					=		False
		lowerCamelCase__  :Any 					=		False
		lowerCamelCase__  :Union[str, Any] 					=		False







		def 							lowercase							(     self					)   ->   Any:


							'''simple docstring'''

							_snake_case							:       Optional[Any]												= VivitModelTester(self					)
							_snake_case							:       Optional[Any]												= ConfigTester(self			,    config_class=__snake_case			,    has_text_modality=__snake_case			,    hidden_size=3_7					)







		def 							lowercase							(     self			,    __snake_case			,    __snake_case			,    __snake_case=False					)   ->   List[str]:


							'''simple docstring'''

							_snake_case							:       Tuple												= copy.deepcopy(__snake_case					)

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

							return inputs_dict







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


							'''simple docstring'''

							self.config_tester.run_common_tests()







		@unittest.skip(reason="Vivit does not use inputs_embeds"					)
		def 							lowercase							(     self					)   ->   str:


							'''simple docstring'''

							pass







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


							'''simple docstring'''

							_snake_case							,			_snake_case							:       List[str]												= self.model_tester.prepare_config_and_inputs_for_common()

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







		def 							lowercase							(     self					)   ->   str:


							'''simple docstring'''

							_snake_case							,			_snake_case							:       Tuple												= self.model_tester.prepare_config_and_inputs_for_common()

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

												_snake_case							:       Union[str, Any]												= ["pixel_values", "head_mask"]
												self.assertListEqual(arg_names[:2]			,    __snake_case					)







		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							_snake_case							:       Optional[Any]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__snake_case					)







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


							'''simple docstring'''

							_snake_case							:       List[str]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_video_classification(*__snake_case					)







		@slow
		def 							lowercase							(     self					)   ->   List[str]:


							'''simple docstring'''

							for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												_snake_case							:       Optional[Any]												= VivitModel.from_pretrained(__snake_case					)
												self.assertIsNotNone(__snake_case					)







		def 							lowercase							(     self					)   ->   Dict:


							'''simple docstring'''

							_snake_case							,			_snake_case							:       int												= self.model_tester.prepare_config_and_inputs_for_common()
							_snake_case							:       Dict												= True

							for model_class in self.all_model_classes:
												_snake_case							:       Any												= self.model_tester.seq_length

												_snake_case							:       int												= True
												_snake_case							:       Dict												= False
												_snake_case							:       Tuple												= True
												_snake_case							:       Any												= model_class(__snake_case					)
												model.to(__snake_case					)
												model.eval()
												with torch.no_grad():
																	_snake_case							:       Union[str, Any]												= model(**self._prepare_for_class(__snake_case			,    __snake_case					)					)
												_snake_case							:       Optional[int]												= outputs.attentions
												self.assertEqual(len(__snake_case					)			,    self.model_tester.num_hidden_layers					)

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

												# Check attention is always last and order is fine
												_snake_case							:       int												= True
												_snake_case							:       List[Any]												= True
												_snake_case							:       Tuple												= model_class(__snake_case					)
												model.to(__snake_case					)
												model.eval()
												with torch.no_grad():
																	_snake_case							:       Optional[Any]												= model(**self._prepare_for_class(__snake_case			,    __snake_case					)					)

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

												_snake_case							:       List[str]												= outputs.attentions

												self.assertEqual(len(__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 							lowercase							(     self					)   ->   List[Any]:


							'''simple docstring'''

							def check_hidden_states_output(__snake_case			,    __snake_case			,    __snake_case					):
												_snake_case							:       Optional[Any]												= model_class(__snake_case					)
												model.to(__snake_case					)
												model.eval()

												with torch.no_grad():
																	_snake_case							:       List[str]												= model(**self._prepare_for_class(__snake_case			,    __snake_case					)					)

												_snake_case							:       Optional[int]												= outputs.hidden_states
												_snake_case							:       Union[str, Any]												= self.model_tester.num_hidden_layers + 1
												self.assertEqual(len(__snake_case					)			,    __snake_case					)

												_snake_case							:       Optional[Any]												= self.model_tester.seq_length

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

							_snake_case							,			_snake_case							:       Tuple												= self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												_snake_case							:       Optional[int]												= True
												check_hidden_states_output(__snake_case			,    __snake_case			,    __snake_case					)

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												_snake_case							:       Tuple												= True

												check_hidden_states_output(__snake_case			,    __snake_case			,    __snake_case					)







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


							'''simple docstring'''

							pass







def  A					(				):
					_snake_case							:       List[str]												= hf_hub_download(
					    repo_id="hf-internal-testing/spaghetti-video"	,	filename="eating_spaghetti_32_frames.npy"	,	repo_type="dataset"					)
					_snake_case							:       List[str]												= np.load(UpperCAmelCase					)
					return list(UpperCAmelCase					)






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







		@cached_property
		def 							lowercase							(     self					)   ->   Tuple:


							'''simple docstring'''

							return VivitImageProcessor() if is_vision_available() else None







		@slow
		def 							lowercase							(     self					)   ->   List[str]:


							'''simple docstring'''

							_snake_case							:       Union[str, Any]												= VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"					).to(__snake_case					)

							_snake_case							:       List[str]												= self.default_image_processor
							_snake_case							:       str												= prepare_video()
							_snake_case							:       Dict												= image_processor(__snake_case			,    return_tensors="pt"					).to(__snake_case					)

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

							# verify the logits
							_snake_case							:       int												= torch.Size((1, 4_0_0)					)
							self.assertEqual(outputs.logits.shape			,    __snake_case					)

							# taken from original model
							_snake_case							:       Optional[Any]												= torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]					).to(__snake_case					)

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






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCAmelCase			:Optional[int]	 =     {
    'configuration_mobilenet_v1': [
        'MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileNetV1Config',
        'MobileNetV1OnnxConfig',
    ],
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase			:Dict	 =     ['MobileNetV1FeatureExtractor']
							__lowerCAmelCase			:List[Any]	 =     ['MobileNetV1ImageProcessor']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase			:Optional[int]	 =     [
							    'MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST',
							    'MobileNetV1ForImageClassification',
							    'MobileNetV1Model',
							    'MobileNetV1PreTrainedModel',
							    'load_tf_weights_in_mobilenet_v1',
							]


if TYPE_CHECKING:
							from .configuration_mobilenet_va import (
							    MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    MobileNetVaConfig,
							    MobileNetVaOnnxConfig,
							)

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor
														from .image_processing_mobilenet_va import MobileNetVaImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_mobilenet_va import (
														    MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MobileNetVaForImageClassification,
														    MobileNetVaModel,
														    MobileNetVaPreTrainedModel,
														    load_tf_weights_in_mobilenet_va,
														)


else:
							import sys

							__lowerCAmelCase			:Optional[int]	 =     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)