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

							"""simple docstring"""






							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_=768  ,						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.0_2  ,						SCREAMING_SNAKE_CASE_=1E-06  ,						SCREAMING_SNAKE_CASE_=True  ,						SCREAMING_SNAKE_CASE_=None  ,						)-> Any:





													'''simple docstring'''






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

													__UpperCamelCase  	=      (
													    (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      A__		(	self      )-> int:





													'''simple docstring'''






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

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

													__UpperCamelCase  	=      self.get_config()

													return config, pixel_values, labels



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





													'''simple docstring'''






													__UpperCamelCase  	=      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  ,						)
													__UpperCamelCase  	=      self.num_labels
													return config



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_      )-> str:





													'''simple docstring'''






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



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_      )-> int:





													'''simple docstring'''






													__UpperCamelCase  	=      VivitForVideoClassification(__SCREAMING_SNAKE_CASE      )
													model.to(__SCREAMING_SNAKE_CASE      )
													model.eval()

													__UpperCamelCase  	=      model(__SCREAMING_SNAKE_CASE      )

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



							def      A__		(	self      )-> Any:





													'''simple docstring'''






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






@require_torch
class 							SCREAMING_SNAKE_CASE__     (				lowerCamelCase_ ,		unittest.TestCase     ):

							"""simple docstring"""






							_snake_case  		=	(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

							_snake_case  		=	False
							_snake_case  		=	False
							_snake_case  		=	False
							_snake_case  		=	False



							def      A__		(	self      )-> Tuple:





													'''simple docstring'''






													__UpperCamelCase  	=      VivitModelTester(self      )
													__UpperCamelCase  	=      ConfigTester(self  ,						config_class=__SCREAMING_SNAKE_CASE  ,						has_text_modality=__SCREAMING_SNAKE_CASE  ,						hidden_size=37      )



							def      A__		(	self  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_=False      )-> Dict:





													'''simple docstring'''






													__UpperCamelCase  	=      copy.deepcopy(__SCREAMING_SNAKE_CASE      )

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

													return inputs_dict



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





													'''simple docstring'''






													self.config_tester.run_common_tests()



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





													'''simple docstring'''






													pass



							def      A__		(	self      )-> int:





													'''simple docstring'''






													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs_for_common()

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



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





													'''simple docstring'''






													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs_for_common()

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

																			__UpperCamelCase  	=      ['''pixel_values''', '''head_mask''']
																			self.assertListEqual(arg_names[:2]  ,						__SCREAMING_SNAKE_CASE      )



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





													'''simple docstring'''






													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE      )



							def      A__		(	self      )-> Any:





													'''simple docstring'''






													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_video_classification(*__SCREAMING_SNAKE_CASE      )



							@slow
							def      A__		(	self      )-> Optional[Any]:





													'''simple docstring'''






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



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





													'''simple docstring'''






													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs_for_common()
													__UpperCamelCase  	=      True

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

																			__UpperCamelCase  	=      True
																			__UpperCamelCase  	=      False
																			__UpperCamelCase  	=      True
																			__UpperCamelCase  	=      model_class(__SCREAMING_SNAKE_CASE      )
																			model.to(__SCREAMING_SNAKE_CASE      )
																			model.eval()
																			with torch.no_grad():
																									__UpperCamelCase  	=      model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE      )      )
																			__UpperCamelCase  	=      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"]
																			__UpperCamelCase  	=      True
																			__UpperCamelCase  	=      model_class(__SCREAMING_SNAKE_CASE      )
																			model.to(__SCREAMING_SNAKE_CASE      )
																			model.eval()
																			with torch.no_grad():
																									__UpperCamelCase  	=      model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE      )      )
																			__UpperCamelCase  	=      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]  ,						)
																			__UpperCamelCase  	=      len(__SCREAMING_SNAKE_CASE      )

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

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

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





													'''simple docstring'''






													def check_hidden_states_output(SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_  ,						SCREAMING_SNAKE_CASE_      ):
																			__UpperCamelCase  	=      model_class(__SCREAMING_SNAKE_CASE      )
																			model.to(__SCREAMING_SNAKE_CASE      )
																			model.eval()

																			with torch.no_grad():
																									__UpperCamelCase  	=      model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE      )      )

																			__UpperCamelCase  	=      outputs.hidden_states
																			__UpperCamelCase  	=      self.model_tester.num_hidden_layers + 1
																			self.assertEqual(len(__SCREAMING_SNAKE_CASE      )  ,						__SCREAMING_SNAKE_CASE      )

																			__UpperCamelCase  	=      self.model_tester.seq_length

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

													__UpperCamelCase  	=      self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			__UpperCamelCase  	=      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"]
																			__UpperCamelCase  	=      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      A__		(	self      )-> Optional[int]:





													'''simple docstring'''






													pass





def 						A_			(				)   ->     Optional[int]:



						'''simple docstring'''

						__UpperCamelCase  	=      hf_hub_download(
						    repo_id='''hf-internal-testing/spaghetti-video'''			,   filename='''eating_spaghetti_32_frames.npy'''			,   repo_type='''dataset'''			)
						__UpperCamelCase  	=      np.load(__SCREAMING_SNAKE_CASE			)
						return list(__SCREAMING_SNAKE_CASE			)






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

							"""simple docstring"""






							@cached_property
							def      A__		(	self      )-> Tuple:





													'''simple docstring'''






													return VivitImageProcessor() if is_vision_available() else None



							@slow
							def      A__		(	self      )-> Union[str, Any]:





													'''simple docstring'''






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

													__UpperCamelCase  	=      self.default_image_processor
													__UpperCamelCase  	=      prepare_video()
													__UpperCamelCase  	=      image_processor(__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''      ).to(__SCREAMING_SNAKE_CASE      )

													# forward pass
													with torch.no_grad():
																			__UpperCamelCase  	=      model(**__SCREAMING_SNAKE_CASE      )

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

													# taken from original model
													__UpperCamelCase  	=      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(__SCREAMING_SNAKE_CASE      )

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






'''simple docstring'''







import importlib
from typing import List, Optional, Union

from seqeval.metrics import accuracy_score, classification_report

import datasets


_lowercase					:    Optional[int]       				=			"\\n@inproceedings{ramshaw-marcus-1995-text,\n    title = \"Text Chunking using Transformation-Based Learning\",\n    author = \"Ramshaw, Lance  and\n      Marcus, Mitch\",\n    booktitle = \"Third Workshop on Very Large Corpora\",\n    year = \"1995\",\n    url = \"https://www.aclweb.org/anthology/W95-0107\",\n}\n@misc{seqeval,\n  title={{seqeval}: A Python framework for sequence labeling evaluation},\n  url={https://github.com/chakki-works/seqeval},\n  note={Software available from https://github.com/chakki-works/seqeval},\n  author={Hiroki Nakayama},\n  year={2018},\n}\n"

_lowercase					:    List[Any]       				=			"\\nseqeval is a Python framework for sequence labeling evaluation.\nseqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.\n\nThis is well-tested by using the Perl script conlleval, which can be used for\nmeasuring the performance of a system that has processed the CoNLL-2000 shared task data.\n\nseqeval supports following formats:\nIOB1\nIOB2\nIOE1\nIOE2\nIOBES\n\nSee the [README.md] file at https://github.com/chakki-works/seqeval for more information.\n"

_lowercase					:    int       				=			"\nProduces labelling scores along with its sufficient statistics\nfrom a source against one or more references.\n\nArgs:\n    predictions: List of List of predicted labels (Estimated targets as returned by a tagger)\n    references: List of List of reference labels (Ground truth (correct) target values)\n    suffix: True if the IOB prefix is after type, False otherwise. default: False\n    scheme: Specify target tagging scheme. Should be one of [\"IOB1\", \"IOB2\", \"IOE1\", \"IOE2\", \"IOBES\", \"BILOU\"].\n        default: None\n    mode: Whether to count correct entity labels with incorrect I/B tags as true positives or not.\n        If you want to only count exact matches, pass mode=\"strict\". default: None.\n    sample_weight: Array-like of shape (n_samples,), weights for individual samples. default: None\n    zero_division: Which value to substitute as a metric value when encountering zero division. Should be on of 0, 1,\n        \"warn\". \"warn\" acts as 0, but the warning is raised.\n\nReturns:\n    'scores': dict. Summary of the scores for overall and per type\n        Overall:\n            'accuracy': accuracy,\n            'precision': precision,\n            'recall': recall,\n            'f1': F1 score, also known as balanced F-score or F-measure,\n        Per type:\n            'precision': precision,\n            'recall': recall,\n            'f1': F1 score, also known as balanced F-score or F-measure\nExamples:\n\n    >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]\n    >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]\n    >>> seqeval = datasets.load_metric(\"seqeval\")\n    >>> results = seqeval.compute(predictions=predictions, references=references)\n    >>> print(list(results.keys()))\n    ['MISC', 'PER', 'overall_precision', 'overall_recall', 'overall_f1', 'overall_accuracy']\n    >>> print(results[\"overall_f1\"])\n    0.5\n    >>> print(results[\"PER\"][\"f1\"])\n    1.0\n"






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,		_KWARGS_DESCRIPTION	)
class 					lowerCAmelCase__  (    datasets.Metric	):






					def       _snake_case       (  self						):


											"""simple docstring"""





											return datasets.MetricInfo(
											    description=_DESCRIPTION							,				citation=_CITATION							,				homepage='''https://github.com/chakki-works/seqeval'''							,				inputs_description=_KWARGS_DESCRIPTION							,				features=datasets.Features(
											        {
											            '''predictions''': datasets.Sequence(datasets.Value('''string'''							,				id='''label'''						)							,				id='''sequence'''						),
											            '''references''': datasets.Sequence(datasets.Value('''string'''							,				id='''label'''						)							,				id='''sequence'''						),
											        }						)							,				codebase_urls=['''https://github.com/chakki-works/seqeval''']							,				reference_urls=['''https://github.com/chakki-works/seqeval''']							,				)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE = False							,				__SCREAMING_SNAKE_CASE = None							,				__SCREAMING_SNAKE_CASE = None							,				__SCREAMING_SNAKE_CASE = None							,				__SCREAMING_SNAKE_CASE = "warn"							,				):


											"""simple docstring"""





											if scheme is not None:
																	try:
																							lowercase_      :	List[str]						=			importlib.import_module('''seqeval.scheme'''						)
																							lowercase_      :	int						=			getattr(__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE						)
																	except AttributeError:
																							raise ValueError(F'''Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {scheme}'''						)
											lowercase_      :	List[Any]						=			classification_report(
											    y_true=__SCREAMING_SNAKE_CASE							,				y_pred=__SCREAMING_SNAKE_CASE							,				suffix=__SCREAMING_SNAKE_CASE							,				output_dict=__SCREAMING_SNAKE_CASE							,				scheme=__SCREAMING_SNAKE_CASE							,				mode=__SCREAMING_SNAKE_CASE							,				sample_weight=__SCREAMING_SNAKE_CASE							,				zero_division=__SCREAMING_SNAKE_CASE							,				)
											report.pop('''macro avg'''						)
											report.pop('''weighted avg'''						)
											lowercase_      :	int						=			report.pop('''micro avg'''						)

											lowercase_      :	List[Any]						=			{
											    type_name: {
											        '''precision''': score['''precision'''],
											        '''recall''': score['''recall'''],
											        '''f1''': score['''f1-score'''],
											        '''number''': score['''support'''],
											    }
											    for type_name, score in report.items()
											}
											lowercase_      :	Optional[Any]						=			overall_score['''precision''']
											lowercase_      :	Optional[Any]						=			overall_score['''recall''']
											lowercase_      :	Optional[int]						=			overall_score['''f1-score''']
											lowercase_      :	Optional[int]						=			accuracy_score(y_true=__SCREAMING_SNAKE_CASE							,				y_pred=__SCREAMING_SNAKE_CASE						)

											return scores



