






'''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:							Optional[int]     ,				UpperCamelCase__:							List[Any]     ,				UpperCamelCase__:							Any=2     ,				UpperCamelCase__:							Optional[Any]=True     ,				UpperCamelCase__:							int=True     ,				UpperCamelCase__:							Dict=10     ,				UpperCamelCase__:							List[Any]=10     ,				UpperCamelCase__:							List[Any]=8     ,				UpperCamelCase__:							List[str]=[2, 4, 4]     ,				UpperCamelCase__:							Optional[int]=3     ,				UpperCamelCase__:							int=768     ,				UpperCamelCase__:							Tuple=5     ,				UpperCamelCase__:							Any=4     ,				UpperCamelCase__:							Any=37     ,				UpperCamelCase__:							Any="gelu_fast"     ,				UpperCamelCase__:							Union[str, Any]=0.0     ,				UpperCamelCase__:							Optional[Any]=0.0     ,				UpperCamelCase__:							Any=0.02     ,				UpperCamelCase__:							str=1e-06     ,				UpperCamelCase__:							Dict=True     ,				UpperCamelCase__:							str=None     ,				):
														lowerCamelCase__   :      str			    =			parent
														lowerCamelCase__   :      Optional[Any]			    =			batch_size
														lowerCamelCase__   :      str			    =			is_training
														lowerCamelCase__   :      Tuple			    =			use_labels
														lowerCamelCase__   :      Union[str, Any]			    =			num_labels
														lowerCamelCase__   :      List[Any]			    =			image_size
														lowerCamelCase__   :      List[Any]			    =			num_frames
														lowerCamelCase__   :      Union[str, Any]			    =			tubelet_size
														lowerCamelCase__   :      List[Any]			    =			num_channels
														lowerCamelCase__   :      int			    =			hidden_size
														lowerCamelCase__   :      int			    =			num_hidden_layers
														lowerCamelCase__   :      str			    =			num_attention_heads
														lowerCamelCase__   :      int			    =			intermediate_size
														lowerCamelCase__   :      Dict			    =			hidden_act
														lowerCamelCase__   :      List[str]			    =			hidden_dropout_prob
														lowerCamelCase__   :      str			    =			attention_probs_dropout_prob
														lowerCamelCase__   :      Union[str, Any]			    =			initializer_range
														lowerCamelCase__   :      Optional[Any]			    =			layer_norm_eps
														lowerCamelCase__   :      List[str]			    =			qkv_bias
														lowerCamelCase__   :      str			    =			scope

														lowerCamelCase__   :      int			    =			(
														    (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]	):
														lowerCamelCase__   :      Optional[Any]			    =			floats_tensor(
														    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

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

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

														return config, pixel_values, labels




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Any			    =			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__   :      int			    =			self.num_labels
														return config




							def 						lowerCamelCase_							(				self:							Union[str, Any]     ,				UpperCamelCase__:							List[str]     ,				UpperCamelCase__:							Optional[Any]     ,				UpperCamelCase__:							str	):
														lowerCamelCase__   :      Tuple			    =			VivitModel(config=UpperCamelCase__	)
														model.to(UpperCamelCase__	)
														model.eval()
														lowerCamelCase__   :      List[Any]			    =			model(UpperCamelCase__	)
														self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)	)




							def 						lowerCamelCase_							(				self:							Any     ,				UpperCamelCase__:							Dict     ,				UpperCamelCase__:							int     ,				UpperCamelCase__:							Dict	):
														lowerCamelCase__   :      Tuple			    =			VivitForVideoClassification(UpperCamelCase__	)
														model.to(UpperCamelCase__	)
														model.eval()

														lowerCamelCase__   :      List[Any]			    =			model(UpperCamelCase__	)

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




							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														lowerCamelCase__   :      List[Any]			    =			self.prepare_config_and_inputs()
														lowerCamelCase__    ,						lowerCamelCase__    ,						lowerCamelCase__   :      Tuple			    =			config_and_inputs
														lowerCamelCase__   :      List[Any]			    =			{"""pixel_values""": pixel_values}
														return config, inputs_dict



@require_torch
class _lowercase      (							_lowercase							, unittest.TestCase						):
							a							 =			(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

							a							 =			False
							a							 =			False
							a							 =			False
							a							 =			False




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      List[Any]			    =			VivitModelTester(self	)
														lowerCamelCase__   :      str			    =			ConfigTester(self     ,				config_class=UpperCamelCase__     ,				has_text_modality=UpperCamelCase__     ,				hidden_size=37	)




							def 						lowerCamelCase_							(				self:							List[Any]     ,				UpperCamelCase__:							List[str]     ,				UpperCamelCase__:							Any     ,				UpperCamelCase__:							Optional[Any]=False	):
														lowerCamelCase__   :      str			    =			copy.deepcopy(UpperCamelCase__	)

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

														return inputs_dict




							def 						lowerCamelCase_							(				self:							List[str]	):
														self.config_tester.run_common_tests()




							@unittest.skip(reason="""Vivit does not use inputs_embeds"""	)
							def 						lowerCamelCase_							(				self:							int	):
														pass




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__    ,						lowerCamelCase__   :      str			    =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCamelCase__   :      Tuple			    =			model_class(UpperCamelCase__	)
																					self.assertIsInstance(model.get_input_embeddings()     ,				(nn.Module)	)
																					lowerCamelCase__   :      Union[str, Any]			    =			model.get_output_embeddings()
																					self.assertTrue(x is None or isinstance(UpperCamelCase__     ,				nn.Linear	)	)




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__    ,						lowerCamelCase__   :      Union[str, Any]			    =			self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCamelCase__   :      List[Any]			    =			["""pixel_values""", """head_mask"""]
																					self.assertListEqual(arg_names[:2]     ,				UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Any	):
														lowerCamelCase__   :      int			    =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Dict			    =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_video_classification(*UpperCamelCase__	)




							@slow
							def 						lowerCamelCase_							(				self:							List[Any]	):
														for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCamelCase__   :      Optional[Any]			    =			VivitModel.from_pretrained(UpperCamelCase__	)
																					self.assertIsNotNone(UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__    ,						lowerCamelCase__   :      Dict			    =			self.model_tester.prepare_config_and_inputs_for_common()
														lowerCamelCase__   :      List[Any]			    =			True

														for model_class in self.all_model_classes:
																					lowerCamelCase__   :      Optional[Any]			    =			self.model_tester.seq_length

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

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

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

																					# Check attention is always last and order is fine
																					lowerCamelCase__   :      List[Any]			    =			True
																					lowerCamelCase__   :      Dict			    =			True
																					lowerCamelCase__   :      List[str]			    =			model_class(UpperCamelCase__	)
																					model.to(UpperCamelCase__	)
																					model.eval()
																					with torch.no_grad():
																												lowerCamelCase__   :      List[str]			    =			model(**self._prepare_for_class(UpperCamelCase__     ,				UpperCamelCase__	)	)

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

																					lowerCamelCase__   :      Optional[Any]			    =			outputs.attentions

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

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

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

																					lowerCamelCase__   :      List[str]			    =			self.model_tester.seq_length

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

														lowerCamelCase__    ,						lowerCamelCase__   :      Optional[int]			    =			self.model_tester.prepare_config_and_inputs_for_common()

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

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

																					check_hidden_states_output(UpperCamelCase__     ,				UpperCamelCase__     ,				UpperCamelCase__	)




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





def 					SCREAMING_SNAKE_CASE_			() ->			Optional[int]:
							lowerCamelCase__   :      int			    =			hf_hub_download(
							    repo_id="""hf-internal-testing/spaghetti-video"""							,     filename="""eating_spaghetti_32_frames.npy"""							,     repo_type="""dataset""" )
							lowerCamelCase__   :      Any			    =			np.load(UpperCamelCase )
							return list(UpperCamelCase )



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




							@cached_property
							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														return VivitImageProcessor() if is_vision_available() else None




							@slow
							def 						lowerCamelCase_							(				self:							Dict	):
														lowerCamelCase__   :      List[str]			    =			VivitForVideoClassification.from_pretrained("""google/vivit-b-16x2-kinetics400"""	).to(UpperCamelCase__	)

														lowerCamelCase__   :      Any			    =			self.default_image_processor
														lowerCamelCase__   :      int			    =			prepare_video()
														lowerCamelCase__   :      Union[str, Any]			    =			image_processor(UpperCamelCase__     ,				return_tensors="""pt"""	).to(UpperCamelCase__	)

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

														# verify the logits
														lowerCamelCase__   :      Optional[int]			    =			torch.Size((1, 400)	)
														self.assertEqual(outputs.logits.shape     ,				UpperCamelCase__	)

														# taken from original model
														lowerCamelCase__   :      Dict			    =			torch.tensor([-1.0_543, 2.0_764, -0.2_104, 0.4_439, -0.9_658]	).to(UpperCamelCase__	)

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









'''simple docstring'''







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
			from PIL import Image

class _lowercase      (							unittest.TestCase							, _lowercase						):




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""	)
														self.tool.setup()
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""     ,				remote=UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							int	):
														lowerCamelCase__   :      List[Any]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[Any]			    =			self.tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Tuple			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Dict			    =			self.remote_tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      str			    =			self.tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[int]			    =			self.remote_tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)

