


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

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

			lowerCAmelCase__  :     Union[str, Any]       	=   None
			if self.use_labels:
					lowerCAmelCase__  :     Union[str, Any]       	=   ids_tensor([self.batch_size]						,   self.num_labels						)

			lowerCAmelCase__  :     int       	=   self.get_config()

			return config, pixel_values, labels




	def 				UpperCAmelCase__(   self		:						Union[str, Any]						)->			List[Any]:
			lowerCAmelCase__  :     Optional[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						,   )
			lowerCAmelCase__  :     Optional[Any]       	=   self.num_labels
			return config




	def 				UpperCAmelCase__(   self		:						Union[str, Any]						,   _SCREAMING_SNAKE_CASE		:						Optional[int]						,   _SCREAMING_SNAKE_CASE		:						Tuple						,   _SCREAMING_SNAKE_CASE		:						Union[str, Any]						)->			Dict:
			lowerCAmelCase__  :     Optional[int]       	=   VivitModel(config=_SCREAMING_SNAKE_CASE						)
			model.to(_SCREAMING_SNAKE_CASE						)
			model.eval()
			lowerCAmelCase__  :     Optional[int]       	=   model(_SCREAMING_SNAKE_CASE						)
			self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size)						)




	def 				UpperCAmelCase__(   self		:						Optional[int]						,   _SCREAMING_SNAKE_CASE		:						Dict						,   _SCREAMING_SNAKE_CASE		:						Optional[Any]						,   _SCREAMING_SNAKE_CASE		:						Dict						)->			str:
			lowerCAmelCase__  :     Optional[int]       	=   VivitForVideoClassification(_SCREAMING_SNAKE_CASE						)
			model.to(_SCREAMING_SNAKE_CASE						)
			model.eval()

			lowerCAmelCase__  :     List[str]       	=   model(_SCREAMING_SNAKE_CASE						)

			# verify the logits shape
			lowerCAmelCase__  :     Any       	=   torch.Size((self.batch_size, self.num_labels)						)
			self.parent.assertEqual(result.logits.shape						,   _SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						int						)->			str:
			lowerCAmelCase__  :     Optional[int]       	=   self.prepare_config_and_inputs()
			lowerCAmelCase__	,      lowerCAmelCase__	,      lowerCAmelCase__  :     str       	=   config_and_inputs
			lowerCAmelCase__  :     Tuple       	=   {'''pixel_values''': pixel_values}
			return config, inputs_dict





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

	_a    :    Optional[Any]        =				False
	_a    :    List[str]        =				False
	_a    :    List[Any]        =				False
	_a    :    str        =				False




	def 				UpperCAmelCase__(   self		:						Optional[int]						)->			List[Any]:
			lowerCAmelCase__  :     int       	=   VivitModelTester(self						)
			lowerCAmelCase__  :     Any       	=   ConfigTester(self						,   config_class=_SCREAMING_SNAKE_CASE						,   has_text_modality=_SCREAMING_SNAKE_CASE						,   hidden_size=37						)




	def 				UpperCAmelCase__(   self		:						int						,   _SCREAMING_SNAKE_CASE		:						Optional[int]						,   _SCREAMING_SNAKE_CASE		:						Any						,   _SCREAMING_SNAKE_CASE		:						Dict=False						)->			List[str]:
			lowerCAmelCase__  :     List[str]       	=   copy.deepcopy(_SCREAMING_SNAKE_CASE						)

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

			return inputs_dict




	def 				UpperCAmelCase__(   self		:						int						)->			str:
			self.config_tester.run_common_tests()




	@unittest.skip(reason='''Vivit does not use inputs_embeds'''						)
	def 				UpperCAmelCase__(   self		:						Any						)->			List[Any]:
			pass




	def 				UpperCAmelCase__(   self		:						Any						)->			Optional[Any]:
			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(_SCREAMING_SNAKE_CASE						)
					self.assertIsInstance(model.get_input_embeddings()						,   (nn.Module)						)
					lowerCAmelCase__  :     List[str]       	=   model.get_output_embeddings()
					self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE						,   nn.Linear						)						)




	def 				UpperCAmelCase__(   self		:						Tuple						)->			Optional[Any]:
			lowerCAmelCase__	,      lowerCAmelCase__  :     List[str]       	=   self.model_tester.prepare_config_and_inputs_for_common()

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

					lowerCAmelCase__  :     List[str]       	=   ['''pixel_values''', '''head_mask''']
					self.assertListEqual(arg_names[:2]						,   _SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						List[Any]						)->			Any:
			lowerCAmelCase__  :     int       	=   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						Optional[int]						)->			Optional[Any]:
			lowerCAmelCase__  :     List[str]       	=   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_video_classification(*_SCREAMING_SNAKE_CASE						)




	@slow
	def 				UpperCAmelCase__(   self		:						Optional[int]						)->			List[str]:
			for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase__  :     Union[str, Any]       	=   VivitModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
					self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)




	def 				UpperCAmelCase__(   self		:						Optional[int]						)->			str:
			lowerCAmelCase__	,      lowerCAmelCase__  :     str       	=   self.model_tester.prepare_config_and_inputs_for_common()
			lowerCAmelCase__  :     int       	=   True

			for model_class in self.all_model_classes:
					lowerCAmelCase__  :     List[Any]       	=   self.model_tester.seq_length

					lowerCAmelCase__  :     Dict       	=   True
					lowerCAmelCase__  :     Optional[Any]       	=   False
					lowerCAmelCase__  :     str       	=   True
					lowerCAmelCase__  :     Optional[int]       	=   model_class(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					with torch.no_grad():
							lowerCAmelCase__  :     Optional[int]       	=   model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						,   _SCREAMING_SNAKE_CASE						)						)
					lowerCAmelCase__  :     Optional[Any]       	=   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"]
					lowerCAmelCase__  :     Optional[Any]       	=   True
					lowerCAmelCase__  :     Optional[Any]       	=   model_class(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					with torch.no_grad():
							lowerCAmelCase__  :     List[Any]       	=   model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						,   _SCREAMING_SNAKE_CASE						)						)
					lowerCAmelCase__  :     List[str]       	=   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]						,   )
					lowerCAmelCase__  :     Optional[Any]       	=   len(_SCREAMING_SNAKE_CASE						)

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

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

					lowerCAmelCase__  :     Union[str, Any]       	=   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 				UpperCAmelCase__(   self		:						List[Any]						)->			Any:
			def check_hidden_states_output(_SCREAMING_SNAKE_CASE		:						Union[str, Any]						,   _SCREAMING_SNAKE_CASE		:						Tuple						,   _SCREAMING_SNAKE_CASE		:						List[Any]						):
					lowerCAmelCase__  :     str       	=   model_class(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()

					with torch.no_grad():
							lowerCAmelCase__  :     Optional[Any]       	=   model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						,   _SCREAMING_SNAKE_CASE						)						)

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

					lowerCAmelCase__  :     str       	=   self.model_tester.seq_length

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

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

			for model_class in self.all_model_classes:
					lowerCAmelCase__  :     Any       	=   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"]
					lowerCAmelCase__  :     int       	=   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 				UpperCAmelCase__(   self		:						Union[str, Any]						)->			str:
			pass






def 	lowerCamelCase_       (			):




		"""simple docstring"""






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





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




	@cached_property
	def 				UpperCAmelCase__(   self		:						str						)->			Optional[int]:
			return VivitImageProcessor() if is_vision_available() else None




	@slow
	def 				UpperCAmelCase__(   self		:						Dict						)->			Optional[int]:
			lowerCAmelCase__  :     Tuple       	=   VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''						).to(_SCREAMING_SNAKE_CASE						)

			lowerCAmelCase__  :     List[str]       	=   self.default_image_processor
			lowerCAmelCase__  :     List[str]       	=   prepare_video()
			lowerCAmelCase__  :     Optional[int]       	=   image_processor(_SCREAMING_SNAKE_CASE						,   return_tensors='''pt'''						).to(_SCREAMING_SNAKE_CASE						)

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

			# verify the logits
			lowerCAmelCase__  :     List[Any]       	=   torch.Size((1, 400)						)
			self.assertEqual(outputs.logits.shape						,   _SCREAMING_SNAKE_CASE						)

			# taken from original model
			lowerCAmelCase__  :     Optional[Any]       	=   torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]						).to(_SCREAMING_SNAKE_CASE						)

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








import math






def 	lowerCamelCase_       (			_a			):




		"""simple docstring"""






		if 1 < number < 4:
				# 2 and 3 are primes
				return True
		elif number < 2 or number % 2 == 0 or number % 3 == 0:
				# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
				return False

		# All primes number are in format of 6k +/- 1
		for i in range(5      ,  int(math.sqrt(_a			) + 1			)      ,  6			):
				if number % i == 0 or number % (i + 2) == 0:
						return False
		return True






def 	lowerCamelCase_       (			_a = 1_000      ,  _a = 1_000			):




		"""simple docstring"""






		lowerCAmelCase__  :     Union[str, Any]       	=   [0, 0, 0]  # length, a, b
		for a in range((a_limit * -1) + 1      ,  _a			):
				for b in range(2      ,  _a			):
						if is_prime(_a			):
								lowerCAmelCase__  :     str       	=   0
								lowerCAmelCase__  :     int       	=   0
								while is_prime((n**2) + (a * n) + b			):
										count += 1
										n += 1
								if count > longest[0]:
										lowerCAmelCase__  :     Dict       	=   [count, a, b]
		lowerCAmelCase__  :     int       	=   longest[1] * longest[2]
		return ans


if __name__ == "__main__":
		print(solution(1000, 1000))




