"""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        __SCREAMING_SNAKE_CASE     :







				def __init__(    self    :List[str]      ,__UpperCAmelCase    :Any      ,__UpperCAmelCase    :str=2      ,__UpperCAmelCase    :str=True      ,__UpperCAmelCase    :List[str]=True      ,__UpperCAmelCase    :Dict=10      ,__UpperCAmelCase    :List[str]=10      ,__UpperCAmelCase    :Any=8      ,__UpperCAmelCase    :Optional[int]=[2, 4, 4]      ,__UpperCAmelCase    :str=3      ,__UpperCAmelCase    :List[str]=7_68      ,__UpperCAmelCase    :int=5      ,__UpperCAmelCase    :Any=4      ,__UpperCAmelCase    :List[str]=37      ,__UpperCAmelCase    :Optional[int]="gelu_fast"      ,__UpperCAmelCase    :Dict=0.0      ,__UpperCAmelCase    :List[Any]=0.0      ,__UpperCAmelCase    :Any=0.02      ,__UpperCAmelCase    :Tuple=1E-06      ,__UpperCAmelCase    :Optional[Any]=True      ,__UpperCAmelCase    :Tuple=None      ,)      ->					List[str]:




									"""simple docstring"""



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

									lowerCamelCase__		:   Dict  				=					(
									    (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    :List[str]  )      ->					str:




									"""simple docstring"""



									lowerCamelCase__		:   Tuple  				=					floats_tensor(
									    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

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

									lowerCamelCase__		:   str  				=					self.get_config()

									return config, pixel_values, labels







				def   lowercase_		(    self    :Tuple  )      ->					List[str]:




									"""simple docstring"""



									lowerCamelCase__		:   Optional[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__		:   Tuple  				=					self.num_labels
									return config







				def   lowercase_		(    self    :Union[str, Any]      ,__UpperCAmelCase    :Union[str, Any]      ,__UpperCAmelCase    :int      ,__UpperCAmelCase    :Optional[Any]  )      ->					List[Any]:




									"""simple docstring"""



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







				def   lowercase_		(    self    :List[Any]      ,__UpperCAmelCase    :List[str]      ,__UpperCAmelCase    :Union[str, Any]      ,__UpperCAmelCase    :List[str]  )      ->					Dict:




									"""simple docstring"""



									lowerCamelCase__		:   List[str]  				=					VivitForVideoClassification(UpperCAmelCase__  )
									model.to(UpperCAmelCase__  )
									model.eval()

									lowerCamelCase__		:   str  				=					model(UpperCAmelCase__  )

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







				def   lowercase_		(    self    :str  )      ->					Tuple:




									"""simple docstring"""



									lowerCamelCase__		:   List[str]  				=					self.prepare_config_and_inputs()
									lowerCamelCase__				,						lowerCamelCase__				,						lowerCamelCase__		:   List[str]  				=					config_and_inputs
									lowerCamelCase__		:   Optional[int]  				=					{'''pixel_values''': pixel_values}
									return config, inputs_dict





@require_torch
class        __SCREAMING_SNAKE_CASE     (   _lowerCAmelCase						,       unittest.TestCase							):
				UpperCAmelCase	=      (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

				UpperCAmelCase	=      False
				UpperCAmelCase	=      False
				UpperCAmelCase	=      False
				UpperCAmelCase	=      False







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




									"""simple docstring"""



									lowerCamelCase__		:   Union[str, Any]  				=					VivitModelTester(self  )
									lowerCamelCase__		:   Any  				=					ConfigTester(self      ,config_class=UpperCAmelCase__      ,has_text_modality=UpperCAmelCase__      ,hidden_size=37  )







				def   lowercase_		(    self    :str      ,__UpperCAmelCase    :Dict      ,__UpperCAmelCase    :str      ,__UpperCAmelCase    :Optional[int]=False  )      ->					Optional[int]:




									"""simple docstring"""



									lowerCamelCase__		:   Tuple  				=					copy.deepcopy(UpperCAmelCase__  )

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

									return inputs_dict







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




									"""simple docstring"""



									self.config_tester.run_common_tests()







				@unittest.skip(reason='''Vivit does not use inputs_embeds'''  )
				def   lowercase_		(    self    :Optional[Any]  )      ->					int:




									"""simple docstring"""



									pass







				def   lowercase_		(    self    :int  )      ->					Dict:




									"""simple docstring"""



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

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







				def   lowercase_		(    self    :List[str]  )      ->					int:




									"""simple docstring"""



									lowerCamelCase__				,						lowerCamelCase__		:   List[Any]  				=					self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCamelCase__		:   str  				=					['''pixel_values''', '''head_mask''']
														self.assertListEqual(arg_names[:2]      ,UpperCAmelCase__  )







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




									"""simple docstring"""



									lowerCamelCase__		:   str  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCAmelCase__  )







				def   lowercase_		(    self    :Optional[int]  )      ->					Dict:




									"""simple docstring"""



									lowerCamelCase__		:   List[Any]  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_video_classification(*UpperCAmelCase__  )







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




									"""simple docstring"""



									for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCamelCase__		:   Tuple  				=					VivitModel.from_pretrained(UpperCAmelCase__  )
														self.assertIsNotNone(UpperCAmelCase__  )







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




									"""simple docstring"""



									lowerCamelCase__				,						lowerCamelCase__		:   Dict  				=					self.model_tester.prepare_config_and_inputs_for_common()
									lowerCamelCase__		:   Tuple  				=					True

									for model_class in self.all_model_classes:
														lowerCamelCase__		:   int  				=					self.model_tester.seq_length

														lowerCamelCase__		:   Union[str, Any]  				=					True
														lowerCamelCase__		:   str  				=					False
														lowerCamelCase__		:   List[str]  				=					True
														lowerCamelCase__		:   Dict  				=					model_class(UpperCAmelCase__  )
														model.to(UpperCAmelCase__  )
														model.eval()
														with torch.no_grad():
																			lowerCamelCase__		:   Union[str, Any]  				=					model(**self._prepare_for_class(UpperCAmelCase__      ,UpperCAmelCase__  )  )
														lowerCamelCase__		:   List[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__		:   str  				=					True
														lowerCamelCase__		:   str  				=					model_class(UpperCAmelCase__  )
														model.to(UpperCAmelCase__  )
														model.eval()
														with torch.no_grad():
																			lowerCamelCase__		:   Dict  				=					model(**self._prepare_for_class(UpperCAmelCase__      ,UpperCAmelCase__  )  )
														lowerCamelCase__		:   Dict  				=					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__		:   Tuple  				=					True
														lowerCamelCase__		:   Optional[Any]  				=					True
														lowerCamelCase__		:   Dict  				=					model_class(UpperCAmelCase__  )
														model.to(UpperCAmelCase__  )
														model.eval()
														with torch.no_grad():
																			lowerCamelCase__		:   Dict  				=					model(**self._prepare_for_class(UpperCAmelCase__      ,UpperCAmelCase__  )  )

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

														lowerCamelCase__		:   Dict  				=					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   lowercase_		(    self    :Union[str, Any]  )      ->					Union[str, Any]:




									"""simple docstring"""



									def check_hidden_states_output(__UpperCAmelCase    :Optional[int]      ,__UpperCAmelCase    :int      ,__UpperCAmelCase    :Tuple  ):
														lowerCamelCase__		:   Dict  				=					model_class(UpperCAmelCase__  )
														model.to(UpperCAmelCase__  )
														model.eval()

														with torch.no_grad():
																			lowerCamelCase__		:   Dict  				=					model(**self._prepare_for_class(UpperCAmelCase__      ,UpperCAmelCase__  )  )

														lowerCamelCase__		:   Optional[Any]  				=					outputs.hidden_states
														lowerCamelCase__		:   Dict  				=					self.model_tester.num_hidden_layers + 1
														self.assertEqual(len(UpperCAmelCase__  )      ,UpperCAmelCase__  )

														lowerCamelCase__		:   str  				=					self.model_tester.seq_length

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

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

									for model_class in self.all_model_classes:
														lowerCamelCase__		:   List[Any]  				=					True
														check_hidden_states_output(UpperCAmelCase__      ,UpperCAmelCase__      ,UpperCAmelCase__  )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowerCamelCase__		:   Optional[Any]  				=					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   lowercase_		(    self    :List[str]  )      ->					Any:




									"""simple docstring"""



									pass





def 				__a       (			):







					"""simple docstring"""





					lowerCamelCase__		:   Union[str, Any]  				=					hf_hub_download(
					    repo_id='''hf-internal-testing/spaghetti-video'''							, filename='''eating_spaghetti_32_frames.npy'''							, repo_type='''dataset'''						)
					lowerCamelCase__		:   Any  				=					np.load(lowerCamelCase_						)
					return list(lowerCamelCase_						)





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







				@cached_property
				def   lowercase_		(    self    :Any  )      ->					int:




									"""simple docstring"""



									return VivitImageProcessor() if is_vision_available() else None







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




									"""simple docstring"""



									lowerCamelCase__		:   Optional[int]  				=					VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''  ).to(UpperCAmelCase__  )

									lowerCamelCase__		:   Any  				=					self.default_image_processor
									lowerCamelCase__		:   str  				=					prepare_video()
									lowerCamelCase__		:   Dict  				=					image_processor(UpperCAmelCase__      ,return_tensors='''pt'''  ).to(UpperCAmelCase__  )

									# forward pass
									with torch.no_grad():
														lowerCamelCase__		:   List[Any]  				=					model(**UpperCAmelCase__  )

									# verify the logits
									lowerCamelCase__		:   Union[str, Any]  				=					torch.Size((1, 4_00)  )
									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 torch

from transformers import AutoTokenizer


class       lowerCamelCase_		(      lowercase ):





				"""simple docstring"""







				def __init__(    self							,		UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					AutoTokenizer.from_pretrained(UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__  ):

								if isinstance(UpperCAmelCase__							,		UpperCAmelCase__  ) and all(isinstance(_x							,		UpperCAmelCase__  ) for _x in x  ):
												SCREAMING_SNAKE_CASE__  				=					None
												for l in x:
																SCREAMING_SNAKE_CASE__  				=					self.tokenizer(
																    UpperCAmelCase__							,		padding="max_length"							,		max_length=384							,		truncation=UpperCAmelCase__							,		return_tensors="pt"							,		)
																SCREAMING_SNAKE_CASE__  				=					torch.tensor([len(UpperCAmelCase__  )]  )
																if d is not None:
																				for k in d.keys():
																								SCREAMING_SNAKE_CASE__  				=					torch.cat((d[k], t[k])							,		0  )
																else:
																				SCREAMING_SNAKE_CASE__  				=					t

												SCREAMING_SNAKE_CASE__  				=					torch.tensor(self.tokenizer.convert_tokens_to_ids("[E]"  )  )
												SCREAMING_SNAKE_CASE__  				=					torch.tensor(self.tokenizer.convert_tokens_to_ids("[/E]"  )  )

								elif isinstance(UpperCAmelCase__							,		UpperCAmelCase__  ) and all(isinstance(_x							,		UpperCAmelCase__  ) for _x in x  ):
												SCREAMING_SNAKE_CASE__  				=					self.tokenizer(
												    UpperCAmelCase__							,		padding="max_length"							,		max_length=384							,		truncation=UpperCAmelCase__							,		return_tensors="pt"							,		)

								else:
												raise Exception(
												    "Type of parameter x was not recognized! Only `list of strings` for query or `list of lists of"
												    " strings` for supports are supported."  )

								return d






				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__=0.70  ):
								SCREAMING_SNAKE_CASE__  				=					[]
								for idx in range(len(W_query["input_ids"]  )  ):
												SCREAMING_SNAKE_CASE__  				=					SCREAMING_SNAKE_CASE__  				=					range(p_start.shape[1]  )

												SCREAMING_SNAKE_CASE__  				=					[]
												for start_id in start_indexes:
																for end_id in end_indexes:
																				if start_id < end_id:
																								output.append(
																								    (
																								        start_id,
																								        end_id,
																								        p_start[idx][start_id].item(),
																								        p_end[idx][end_id].item(),
																								    )  )

												output.sort(key=lambda UpperCAmelCase__  : (tup[2] * tup[3])							,		reverse=UpperCAmelCase__  )
												SCREAMING_SNAKE_CASE__  				=					[]
												for k in range(len(UpperCAmelCase__  )  ):
																if output[k][2] * output[k][3] >= thresh:
																				SCREAMING_SNAKE_CASE__				,   SCREAMING_SNAKE_CASE__  				=					output[k][0], output[k][1]
																				SCREAMING_SNAKE_CASE__  				=					self.tokenizer.decode(W_query["input_ids"][idx][c_start_pos:c_end_pos]  )
																				temp.append((decoded, output[k][2] * output[k][3])  )

												final_outputs.append(UpperCAmelCase__  )

								return final_outputs
