




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




			def __init__(      self       ,							a__       ,							a__=2       ,							a__=True       ,							a__=True       ,							a__=10       ,							a__=10       ,							a__=8       ,							a__=[2, 4, 4]       ,							a__=3       ,							a__=768       ,							a__=5       ,							a__=4       ,							a__=37       ,							a__="gelu_fast"       ,							a__=0.0       ,							a__=0.0       ,							a__=0.0_2       ,							a__=1e-06       ,							a__=True       ,							a__=None       ,							)       ->       int:

										'''simple docstring'''




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

										snake_case_			      =   (
										    (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							)       ->       Tuple:

										'''simple docstring'''




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

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

										snake_case_			      =   self.get_config()

										return config, pixel_values, labels




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

										'''simple docstring'''




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




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__							)       ->       int:

										'''simple docstring'''




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




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__							)       ->       Optional[Any]:

										'''simple docstring'''




										snake_case_			      =   VivitForVideoClassification(a__							)
										model.to(a__							)
										model.eval()

										snake_case_			      =   model(a__							)

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




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

										'''simple docstring'''




										snake_case_			      =   self.prepare_config_and_inputs()
										snake_case_      ,   snake_case_      ,   snake_case_			      =   config_and_inputs
										snake_case_			      =   {"pixel_values": pixel_values}
										return config, inputs_dict




@require_torch
class 				_snake_case	(			lowercase_		,		unittest.TestCase							):
			lowerCAmelCase_  :   Any		 =					(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

			lowerCAmelCase_  :   Optional[Any]		 =					False
			lowerCAmelCase_  :   Dict		 =					False
			lowerCAmelCase_  :   Any		 =					False
			lowerCAmelCase_  :   Any		 =					False




			def      lowerCAmelCase__       (      self							)       ->       Any:

										'''simple docstring'''




										snake_case_			      =   VivitModelTester(self							)
										snake_case_			      =   ConfigTester(self       ,							config_class=a__       ,							has_text_modality=a__       ,							hidden_size=37							)




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__=False							)       ->       List[str]:

										'''simple docstring'''




										snake_case_			      =   copy.deepcopy(a__							)

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

										return inputs_dict




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

										'''simple docstring'''




										self.config_tester.run_common_tests()




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

										'''simple docstring'''




										pass




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

										'''simple docstring'''




										snake_case_      ,   snake_case_			      =   self.model_tester.prepare_config_and_inputs_for_common()

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




			def      lowerCAmelCase__       (      self							)       ->       Any:

										'''simple docstring'''




										snake_case_      ,   snake_case_			      =   self.model_tester.prepare_config_and_inputs_for_common()

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

																	snake_case_			      =   ["pixel_values", "head_mask"]
																	self.assertListEqual(arg_names[:2]       ,							a__							)




			def      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




										snake_case_			      =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*a__							)




			def      lowerCAmelCase__       (      self							)       ->       Any:

										'''simple docstring'''




										snake_case_			      =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*a__							)




			@slow
			def      lowerCAmelCase__       (      self							)       ->       str:

										'''simple docstring'''




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




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

										'''simple docstring'''




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

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

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

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

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

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

																	snake_case_			      =   outputs.attentions

																	self.assertEqual(len(a__							)       ,							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							)       ->       int:

										'''simple docstring'''




										def check_hidden_states_output(a__       ,							a__       ,							a__							):
																	snake_case_			      =   model_class(a__							)
																	model.to(a__							)
																	model.eval()

																	with torch.no_grad():
																								snake_case_			      =   model(**self._prepare_for_class(a__       ,							a__							)							)

																	snake_case_			      =   outputs.hidden_states
																	snake_case_			      =   self.model_tester.num_hidden_layers + 1
																	self.assertEqual(len(a__							)       ,							a__							)

																	snake_case_			      =   self.model_tester.seq_length

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

										snake_case_      ,   snake_case_			      =   self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																	snake_case_			      =   True
																	check_hidden_states_output(a__       ,							a__       ,							a__							)

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

																	check_hidden_states_output(a__       ,							a__       ,							a__							)




			@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   UpperCamelCase_(			):

							'''simple docstring'''






							snake_case_			      =   hf_hub_download(
							    repo_id="hf-internal-testing/spaghetti-video"							,       filename="eating_spaghetti_32_frames.npy"							,       repo_type="dataset"  )
							snake_case_			      =   np.load(snake_case  )
							return list(snake_case  )




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




			@cached_property
			def      lowerCAmelCase__       (      self							)       ->       Optional[Any]:

										'''simple docstring'''




										return VivitImageProcessor() if is_vision_available() else None




			@slow
			def      lowerCAmelCase__       (      self							)       ->       List[Any]:

										'''simple docstring'''




										snake_case_			      =   VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"							).to(a__							)

										snake_case_			      =   self.default_image_processor
										snake_case_			      =   prepare_video()
										snake_case_			      =   image_processor(a__       ,							return_tensors="pt"							).to(a__							)

										# forward pass
										with torch.no_grad():
																	snake_case_			      =   model(**a__							)

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

										# taken from original model
										snake_case_			      =   torch.tensor([-1.0_5_4_3, 2.0_7_6_4, -0.2_1_0_4, 0.4_4_3_9, -0.9_6_5_8]							).to(a__							)

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










'''simple docstring'''





from typing import TYPE_CHECKING

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


_SCREAMING_SNAKE_CASE							:     Dict   	=				{
    "configuration_mobilevitv2": [
        "MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "MobileViTV2Config",
        "MobileViTV2OnnxConfig",
    ],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_SCREAMING_SNAKE_CASE							:     Optional[Any]   	=				[
		    "MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "MobileViTV2ForImageClassification",
		    "MobileViTV2ForSemanticSegmentation",
		    "MobileViTV2Model",
		    "MobileViTV2PreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_mobilevitva import (
		    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    MobileViTVaConfig,
		    MobileViTVaOnnxConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mobilevitva import (
				    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MobileViTVaForImageClassification,
				    MobileViTVaForSemanticSegmentation,
				    MobileViTVaModel,
				    MobileViTVaPreTrainedModel,
				)

else:
		import sys

		_SCREAMING_SNAKE_CASE							:     Any   	=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




