


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       :						Optional[int]	,      _lowerCAmelCase       :						str	,      _lowerCAmelCase       :						Tuple=2	,      _lowerCAmelCase       :						int=True	,      _lowerCAmelCase       :						Union[str, Any]=True	,      _lowerCAmelCase       :						str=1_0	,      _lowerCAmelCase       :						int=1_0	,      _lowerCAmelCase       :						Any=8	,      _lowerCAmelCase       :						Optional[Any]=[2, 4, 4]	,      _lowerCAmelCase       :						List[str]=3	,      _lowerCAmelCase       :						List[str]=7_6_8	,      _lowerCAmelCase       :						str=5	,      _lowerCAmelCase       :						str=4	,      _lowerCAmelCase       :						Optional[Any]=3_7	,      _lowerCAmelCase       :						Union[str, Any]="gelu_fast"	,      _lowerCAmelCase       :						List[Any]=0.0	,      _lowerCAmelCase       :						Optional[int]=0.0	,      _lowerCAmelCase       :						Dict=0.02	,      _lowerCAmelCase       :						List[str]=1E-0_6	,      _lowerCAmelCase       :						Optional[Any]=True	,      _lowerCAmelCase       :						int=None	,      )							->					int:



											'''simple docstring'''





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

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



											'''simple docstring'''





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

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

											__magic_name__					=      self.get_config()

											return config, pixel_values, labels
					def 	a			(						self       :						int  )							->					Optional[Any]:



											'''simple docstring'''





											__magic_name__					=      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	,      )
											__magic_name__					=      self.num_labels
											return config
					def 	a			(						self       :						Optional[int]	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						Any	,      _lowerCAmelCase       :						str  )							->					List[str]:



											'''simple docstring'''





											__magic_name__					=      VivitModel(config=_lowerCAmelCase  )
											model.to(_lowerCAmelCase  )
											model.eval()
											__magic_name__					=      model(_lowerCAmelCase  )
											self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)  )
					def 	a			(						self       :						Union[str, Any]	,      _lowerCAmelCase       :						List[str]	,      _lowerCAmelCase       :						Any	,      _lowerCAmelCase       :						List[str]  )							->					str:



											'''simple docstring'''





											__magic_name__					=      VivitForVideoClassification(_lowerCAmelCase  )
											model.to(_lowerCAmelCase  )
											model.eval()

											__magic_name__					=      model(_lowerCAmelCase  )

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







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



											'''simple docstring'''





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







@require_torch
class      __snake_case				(   _UpperCAmelCase	,      unittest.TestCase):
					SCREAMING_SNAKE_CASE_		:       Dict								=			(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

					SCREAMING_SNAKE_CASE_		:       Union[str, Any]								=			False
					SCREAMING_SNAKE_CASE_		:       List[str]								=			False
					SCREAMING_SNAKE_CASE_		:       Optional[int]								=			False
					SCREAMING_SNAKE_CASE_		:       int								=			False
					def 	a			(						self       :						List[str]  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__					=      VivitModelTester(self  )
											__magic_name__					=      ConfigTester(self	,      config_class=_lowerCAmelCase	,      has_text_modality=_lowerCAmelCase	,      hidden_size=3_7  )
					def 	a			(						self       :						Dict	,      _lowerCAmelCase       :						str	,      _lowerCAmelCase       :						Optional[int]	,      _lowerCAmelCase       :						Union[str, Any]=False  )							->					Union[str, Any]:



											'''simple docstring'''





											__magic_name__					=      copy.deepcopy(_lowerCAmelCase  )

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

											return inputs_dict
					def 	a			(						self       :						int  )							->					Tuple:



											'''simple docstring'''





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



											'''simple docstring'''





											pass
					def 	a			(						self       :						Union[str, Any]  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__		,      __magic_name__					=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	__magic_name__					=      model_class(_lowerCAmelCase  )
																	self.assertIsInstance(model.get_input_embeddings()	,      (nn.Module)  )
																	__magic_name__					=      model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(_lowerCAmelCase	,      nn.Linear  )  )
					def 	a			(						self       :						Optional[int]  )							->					List[str]:



											'''simple docstring'''





											__magic_name__		,      __magic_name__					=      self.model_tester.prepare_config_and_inputs_for_common()

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

																	__magic_name__					=      ['pixel_values', 'head_mask']
																	self.assertListEqual(arg_names[:2]	,      _lowerCAmelCase  )
					def 	a			(						self       :						List[Any]  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCAmelCase  )
					def 	a			(						self       :						str  )							->					Any:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_video_classification(*_lowerCAmelCase  )
					@slow
					def 	a			(						self       :						Dict  )							->					List[str]:



											'''simple docstring'''





											for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__magic_name__					=      VivitModel.from_pretrained(_lowerCAmelCase  )
																	self.assertIsNotNone(_lowerCAmelCase  )
					def 	a			(						self       :						Optional[int]  )							->					int:



											'''simple docstring'''





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

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

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

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

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

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

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

																	__magic_name__					=      outputs.attentions

																	self.assertEqual(len(_lowerCAmelCase  )	,      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       :						Any  )							->					List[Any]:



											'''simple docstring'''





											def check_hidden_states_output(_lowerCAmelCase       :						Dict	,      _lowerCAmelCase       :						str	,      _lowerCAmelCase       :						Any  ):
																	__magic_name__					=      model_class(_lowerCAmelCase  )
																	model.to(_lowerCAmelCase  )
																	model.eval()

																	with torch.no_grad():
																							__magic_name__					=      model(**self._prepare_for_class(_lowerCAmelCase	,      _lowerCAmelCase  )  )

																	__magic_name__					=      outputs.hidden_states
																	__magic_name__					=      self.model_tester.num_hidden_layers + 1
																	self.assertEqual(len(_lowerCAmelCase  )	,      _lowerCAmelCase  )

																	__magic_name__					=      self.model_tester.seq_length

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

											__magic_name__		,      __magic_name__					=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	__magic_name__					=      True
																	check_hidden_states_output(_lowerCAmelCase	,      _lowerCAmelCase	,      _lowerCAmelCase  )

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

																	check_hidden_states_output(_lowerCAmelCase	,      _lowerCAmelCase	,      _lowerCAmelCase  )







					@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'  )
					def 	a			(						self       :						Tuple  )							->					Optional[Any]:



											'''simple docstring'''





											pass







def 				UpperCamelCase_	(					)->   Optional[int]:




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




@require_torch
@require_vision
class      __snake_case				(   unittest.TestCase):
					@cached_property
					def 	a			(						self       :						Optional[int]  )							->					str:



											'''simple docstring'''





											return VivitImageProcessor() if is_vision_available() else None







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



											'''simple docstring'''





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

											__magic_name__					=      self.default_image_processor
											__magic_name__					=      prepare_video()
											__magic_name__					=      image_processor(_lowerCAmelCase	,      return_tensors='pt'  ).to(_lowerCAmelCase  )

											# forward pass
											with torch.no_grad():
																	__magic_name__					=      model(**_lowerCAmelCase  )

											# verify the logits
											__magic_name__					=      torch.Size((1, 4_0_0)  )
											self.assertEqual(outputs.logits.shape	,      _lowerCAmelCase  )

											# taken from original model
											__magic_name__					=      torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]  ).to(_lowerCAmelCase  )

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





from itertools import count
from math import asin, pi, sqrt
def 				UpperCamelCase_	(					__snake_case      :    float  )->   float:




						'''simple docstring'''
						return (
						    (1 - 2 * point) * sqrt(point - point**2  ) + 2 * point + asin(sqrt(1 - point  )  )
						) / 4
def 				UpperCamelCase_	(					__snake_case      :    int  )->   float:




						'''simple docstring'''
						__magic_name__					=      (circles_number + 1 - sqrt(2 * circles_number  )) / (
						    2 * (circles_number**2 + 1)
						)
						__magic_name__					=      circles_number * intersection_y

						__magic_name__					=      intersection_x * intersection_y / 2
						__magic_name__					=      circle_bottom_arc_integral(
						    1 / 2  ) - circle_bottom_arc_integral(__snake_case  )

						return triangle_area + concave_region_area
def 				UpperCamelCase_	(					__snake_case      :    float = 1 / 10_00  )->   int:




						'''simple docstring'''
						__magic_name__					=      (1 - pi / 4) / 4

						for n in count(1  ):
												if concave_triangle_area(__snake_case  ) / l_section_area < fraction:
																		return n

						return -1


if __name__ == "__main__":
					print(f'''{solution() = }''')

