



'''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 				__lowerCAmelCase:






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




					"""simple docstring"""







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

					SCREAMING_SNAKE_CASE_						:str										=      (
					    (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  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]	)

					SCREAMING_SNAKE_CASE_						:Tuple										=      None
					if self.use_labels:
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      ids_tensor([self.batch_size]      ,       self.num_labels	)

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.get_config()

					return config, pixel_values, labels






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







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






			def 				_lowercase	(   self  :							Any      ,       SCREAMING_SNAKE_CASE  :							Any      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      VivitModel(config=SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()
					SCREAMING_SNAKE_CASE_						:List[str]										=      model(SCREAMING_SNAKE_CASE	)
					self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)	)






			def 				_lowercase	(   self  :							int      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      VivitForVideoClassification(SCREAMING_SNAKE_CASE	)
					model.to(SCREAMING_SNAKE_CASE	)
					model.eval()

					SCREAMING_SNAKE_CASE_						:Tuple										=      model(SCREAMING_SNAKE_CASE	)

					# verify the logits shape
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      torch.Size((self.batch_size, self.num_labels)	)
					self.parent.assertEqual(result.logits.shape      ,       SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







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




@require_torch
class 				__lowerCAmelCase(							lowerCAmelCase__		,			unittest.TestCase      ):
			__snake_case  :		Any		=      (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

			__snake_case  :		Dict		=      False
			__snake_case  :		List[Any]		=      False
			__snake_case  :		List[str]		=      False
			__snake_case  :		Dict		=      False






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      VivitModelTester(self	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      ConfigTester(self      ,       config_class=SCREAMING_SNAKE_CASE      ,       has_text_modality=SCREAMING_SNAKE_CASE      ,       hidden_size=37	)






			def 				_lowercase	(   self  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							str      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Any=False	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[int]										=      copy.deepcopy(SCREAMING_SNAKE_CASE	)

					if return_labels:
							if model_class in get_values(SCREAMING_SNAKE_CASE	):
									SCREAMING_SNAKE_CASE_						:Dict										=      torch.zeros(
									    self.model_tester.batch_size      ,       dtype=torch.long      ,       device=SCREAMING_SNAKE_CASE	)

					return inputs_dict






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					self.config_tester.run_common_tests()






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




					"""simple docstring"""







					pass






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







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

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






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs_for_common()

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

							SCREAMING_SNAKE_CASE_						:Any										=      ['pixel_values', 'head_mask']
							self.assertListEqual(arg_names[:2]      ,       SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_video_classification(*SCREAMING_SNAKE_CASE	)






			@slow
			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							SCREAMING_SNAKE_CASE_						:str										=      VivitModel.from_pretrained(SCREAMING_SNAKE_CASE	)
							self.assertIsNotNone(SCREAMING_SNAKE_CASE	)






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.prepare_config_and_inputs_for_common()
					SCREAMING_SNAKE_CASE_						:Tuple										=      True

					for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_						:Tuple										=      self.model_tester.seq_length

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

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

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

							SCREAMING_SNAKE_CASE_						:Dict										=      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 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					def check_hidden_states_output(SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Tuple      ,       SCREAMING_SNAKE_CASE  :							Tuple	):
							SCREAMING_SNAKE_CASE_						:List[Any]										=      model_class(SCREAMING_SNAKE_CASE	)
							model.to(SCREAMING_SNAKE_CASE	)
							model.eval()

							with torch.no_grad():
									SCREAMING_SNAKE_CASE_						:Optional[int]										=      model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,       SCREAMING_SNAKE_CASE	)	)

							SCREAMING_SNAKE_CASE_						:int										=      outputs.hidden_states
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.model_tester.num_hidden_layers + 1
							self.assertEqual(len(SCREAMING_SNAKE_CASE	)      ,       SCREAMING_SNAKE_CASE	)

							SCREAMING_SNAKE_CASE_						:Dict										=      self.model_tester.seq_length

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

					SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						:List[str]										=      self.model_tester.prepare_config_and_inputs_for_common()

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




					"""simple docstring"""







					pass







def  SCREAMING_SNAKE_CASE__ (				):
		SCREAMING_SNAKE_CASE_						:Tuple										=      hf_hub_download(
		    repo_id='hf-internal-testing/spaghetti-video' ,		filename='eating_spaghetti_32_frames.npy' ,		repo_type='dataset'     )
		SCREAMING_SNAKE_CASE_						:Any										=      np.load(SCREAMING_SNAKE_CASE     )
		return list(SCREAMING_SNAKE_CASE     )





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






			@cached_property
			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					return VivitImageProcessor() if is_vision_available() else None






			@slow
			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'	).to(SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Tuple										=      self.default_image_processor
					SCREAMING_SNAKE_CASE_						:str										=      prepare_video()
					SCREAMING_SNAKE_CASE_						:Any										=      image_processor(SCREAMING_SNAKE_CASE      ,       return_tensors='pt'	).to(SCREAMING_SNAKE_CASE	)

					# forward pass
					with torch.no_grad():
							SCREAMING_SNAKE_CASE_						:Any										=      model(**SCREAMING_SNAKE_CASE	)

					# verify the logits
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.Size((1, 400)	)
					self.assertEqual(outputs.logits.shape      ,       SCREAMING_SNAKE_CASE	)

					# taken from original model
					SCREAMING_SNAKE_CASE_						:int										=      torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]	).to(SCREAMING_SNAKE_CASE	)

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





'''simple docstring'''







import matplotlib.pyplot as plt
import numpy as np







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:str										=      len(SCREAMING_SNAKE_CASE     )  # Number of training samples
		SCREAMING_SNAKE_CASE_						:List[Any]										=      np.eye(SCREAMING_SNAKE_CASE     )  # Initialize weights as identity matrix
		for j in range(SCREAMING_SNAKE_CASE     ):
				SCREAMING_SNAKE_CASE_						:Tuple										=      point - x_train[j]
				SCREAMING_SNAKE_CASE_						:Optional[int]										=      np.exp(diff @ diff.T / (-2.0 * tau**2)     )

		return weights







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:int										=      weight_matrix(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     )
		SCREAMING_SNAKE_CASE_						:str										=      np.linalg.inv(x_train.T @ weight_mat @ x_train     ) @ (
		    x_train.T @ weight_mat @ y_train.T
		)

		return weight







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:int										=      np.zeros(len(SCREAMING_SNAKE_CASE     )     )  # Initialize array of predictions
		for i, item in enumerate(SCREAMING_SNAKE_CASE     ):
				SCREAMING_SNAKE_CASE_						:Dict										=      item @ local_weight(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     )

		return y_pred







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE     ):
		import seaborn as sns

		SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      sns.load_dataset(SCREAMING_SNAKE_CASE     )
		SCREAMING_SNAKE_CASE_						:Tuple										=      np.array(data[x_name]     )
		SCREAMING_SNAKE_CASE_						:Optional[int]										=      np.array(data[y_name]     )

		SCREAMING_SNAKE_CASE_						:str										=      np.ones(len(SCREAMING_SNAKE_CASE     )     )

		# pairing elements of one and x_data
		SCREAMING_SNAKE_CASE_						:Optional[int]										=      np.column_stack((one, x_data)     )

		return x_train, x_data, y_data







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		):
		SCREAMING_SNAKE_CASE_						:Tuple										=      np.sort(SCREAMING_SNAKE_CASE ,		axis=0     )
		plt.scatter(SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE ,		color='blue'     )
		plt.plot(
		    x_train_sorted[:, 1] ,		preds[x_train[:, 1].argsort(0     )] ,		color='yellow' ,		linewidth=5 ,		)
		plt.title('Local Weighted Regression'     )
		plt.xlabel(SCREAMING_SNAKE_CASE     )
		plt.ylabel(SCREAMING_SNAKE_CASE     )
		plt.show()


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							# Demo with a dataset from the seaborn module
							SCREAMING_SNAKE_CASE__	,							SCREAMING_SNAKE_CASE__	,							SCREAMING_SNAKE_CASE__     : str				=   load_data("""tips""", """total_bill""", """tip""")
							SCREAMING_SNAKE_CASE__     : str				=   local_weight_regression(training_data_x, tip, 5)
							plot_preds(training_data_x, predictions, total_bill, tip, """total_bill""", """tip""")
