




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

					__lowercase	:       Tuple				=   (
					    (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 					_lowerCamelCase    (		self   )		->   List[str]:
					__lowercase	:       int				=   floats_tensor(
					    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]   )

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

					__lowercase	:       Optional[Any]				=   self.get_config()

					return config, pixel_values, labels
		def 					_lowerCamelCase    (		self   )		->   int:
					__lowercase	:       Tuple				=   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							,		)
					__lowercase	:       Union[str, Any]				=   self.num_labels
					return config
		def 					_lowerCamelCase    (		self							,		UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_   )		->   Optional[int]:
					__lowercase	:       str				=   VivitModel(config=UpperCamelCase_   )
					model.to(UpperCamelCase_   )
					model.eval()
					__lowercase	:       Union[str, Any]				=   model(UpperCamelCase_   )
					self.parent.assertEqual(result.last_hidden_state.shape							,		(self.batch_size, self.seq_length, self.hidden_size)   )
		def 					_lowerCamelCase    (		self							,		UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_   )		->   str:
					__lowercase	:       Dict				=   VivitForVideoClassification(UpperCamelCase_   )
					model.to(UpperCamelCase_   )
					model.eval()

					__lowercase	:       Optional[int]				=   model(UpperCamelCase_   )

					# verify the logits shape
					__lowercase	:       Optional[int]				=   torch.Size((self.batch_size, self.num_labels)   )
					self.parent.assertEqual(result.logits.shape							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   List[str]:
					__lowercase	:       int				=   self.prepare_config_and_inputs()
					__lowercase	,__lowercase	,__lowercase	:       Tuple				=   config_and_inputs
					__lowercase	:       List[str]				=   {'''pixel_values''': pixel_values}
					return config, inputs_dict


@require_torch
class        UpperCAmelCase_  (	snake_case ,			unittest.TestCase						):
		UpperCamelCase									=(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

		UpperCamelCase									=False
		UpperCamelCase									=False
		UpperCamelCase									=False
		UpperCamelCase									=False
		def 					_lowerCamelCase    (		self   )		->   List[Any]:
					__lowercase	:       Optional[int]				=   VivitModelTester(self   )
					__lowercase	:       int				=   ConfigTester(self							,		config_class=UpperCamelCase_							,		has_text_modality=UpperCamelCase_							,		hidden_size=37   )
		def 					_lowerCamelCase    (		self							,		UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_=False   )		->   Union[str, Any]:
					__lowercase	:       int				=   copy.deepcopy(UpperCamelCase_   )

					if return_labels:
								if model_class in get_values(UpperCamelCase_   ):
											__lowercase	:       Optional[Any]				=   torch.zeros(
											    self.model_tester.batch_size							,		dtype=torch.long							,		device=UpperCamelCase_   )

					return inputs_dict
		def 					_lowerCamelCase    (		self   )		->   Tuple:
					self.config_tester.run_common_tests()
		@unittest.skip(reason='''Vivit does not use inputs_embeds'''   )
		def 					_lowerCamelCase    (		self   )		->   Dict:
					pass
		def 					_lowerCamelCase    (		self   )		->   Any:
					__lowercase	,__lowercase	:       int				=   self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								__lowercase	:       List[str]				=   model_class(UpperCamelCase_   )
								self.assertIsInstance(model.get_input_embeddings()							,		(nn.Module)   )
								__lowercase	:       int				=   model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(UpperCamelCase_							,		nn.Linear   )   )
		def 					_lowerCamelCase    (		self   )		->   int:
					__lowercase	,__lowercase	:       Any				=   self.model_tester.prepare_config_and_inputs_for_common()

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

								__lowercase	:       Tuple				=   ['''pixel_values''', '''head_mask''']
								self.assertListEqual(arg_names[:2]							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Tuple:
					__lowercase	:       List[Any]				=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Dict:
					__lowercase	:       Optional[int]				=   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_video_classification(*UpperCamelCase_   )
		@slow
		def 					_lowerCamelCase    (		self   )		->   Any:
					for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowercase	:       Any				=   VivitModel.from_pretrained(UpperCamelCase_   )
								self.assertIsNotNone(UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   int:
					__lowercase	,__lowercase	:       List[str]				=   self.model_tester.prepare_config_and_inputs_for_common()
					__lowercase	:       int				=   True

					for model_class in self.all_model_classes:
								__lowercase	:       Optional[Any]				=   self.model_tester.seq_length

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

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

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

								__lowercase	:       Optional[Any]				=   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 					_lowerCamelCase    (		self   )		->   str:
					def check_hidden_states_output(UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_   ):
								__lowercase	:       str				=   model_class(UpperCamelCase_   )
								model.to(UpperCamelCase_   )
								model.eval()

								with torch.no_grad():
											__lowercase	:       str				=   model(**self._prepare_for_class(UpperCamelCase_							,		UpperCamelCase_   )   )

								__lowercase	:       Any				=   outputs.hidden_states
								__lowercase	:       Any				=   self.model_tester.num_hidden_layers + 1
								self.assertEqual(len(UpperCamelCase_   )							,		UpperCamelCase_   )

								__lowercase	:       List[Any]				=   self.model_tester.seq_length

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

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

					for model_class in self.all_model_classes:
								__lowercase	:       Dict				=   True
								check_hidden_states_output(UpperCamelCase_							,		UpperCamelCase_							,		UpperCamelCase_   )

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								__lowercase	:       Tuple				=   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 					_lowerCamelCase    (		self   )		->   Optional[int]:
					pass







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


@require_torch
@require_vision
class        UpperCAmelCase_  (	unittest.TestCase						):
		@cached_property
		def 					_lowerCamelCase    (		self   )		->   str:
					return VivitImageProcessor() if is_vision_available() else None
		@slow
		def 					_lowerCamelCase    (		self   )		->   Tuple:
					__lowercase	:       List[str]				=   VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''   ).to(UpperCamelCase_   )

					__lowercase	:       Union[str, Any]				=   self.default_image_processor
					__lowercase	:       Optional[int]				=   prepare_video()
					__lowercase	:       Optional[Any]				=   image_processor(UpperCamelCase_							,		return_tensors='''pt'''   ).to(UpperCamelCase_   )

					# forward pass
					with torch.no_grad():
								__lowercase	:       Dict				=   model(**UpperCamelCase_   )

					# verify the logits
					__lowercase	:       Optional[Any]				=   torch.Size((1, 4_00)   )
					self.assertEqual(outputs.logits.shape							,		UpperCamelCase_   )

					# taken from original model
					__lowercase	:       List[Any]				=   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(UpperCamelCase_   )

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









"""simple docstring"""




import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
			from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

			from .test_feature_extraction_speechta import floats_list


a_            =   get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')

@require_torch
class        UpperCAmelCase_  (	unittest.TestCase						):
		def 					_lowerCamelCase    (		self   )		->   Dict:
					__lowercase	:       Union[str, Any]				=   tempfile.mkdtemp()

					__lowercase	:       List[str]				=   SpeechTaTokenizer(UpperCamelCase_   )
					tokenizer.save_pretrained(self.tmpdirname   )

					__lowercase	:       Dict				=   {
					    '''feature_size''': 1,
					    '''padding_value''': 0.0,
					    '''sampling_rate''': 1_60_00,
					    '''do_normalize''': False,
					    '''num_mel_bins''': 80,
					    '''hop_length''': 16,
					    '''win_length''': 64,
					    '''win_function''': '''hann_window''',
					    '''fmin''': 80,
					    '''fmax''': 76_00,
					    '''mel_floor''': 1E-10,
					    '''reduction_factor''': 2,
					    '''return_attention_mask''': True,
					}

					__lowercase	:       List[Any]				=   os.path.join(self.tmpdirname							,		UpperCamelCase_   )
					with open(self.feature_extraction_file							,		'''w'''							,		encoding='''utf-8'''   ) as fp:
								fp.write(json.dumps(UpperCamelCase_   ) + '''\n'''   )
		def 					_lowerCamelCase    (		self							,		**UpperCamelCase_   )		->   Union[str, Any]:
					return SpeechTaTokenizer.from_pretrained(self.tmpdirname							,		**UpperCamelCase_   )
		def 					_lowerCamelCase    (		self							,		**UpperCamelCase_   )		->   Optional[Any]:
					return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname							,		**UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Any:
					shutil.rmtree(self.tmpdirname   )
		def 					_lowerCamelCase    (		self   )		->   Union[str, Any]:
					__lowercase	:       List[str]				=   self.get_tokenizer()
					__lowercase	:       int				=   self.get_feature_extractor()

					__lowercase	:       Any				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					processor.save_pretrained(self.tmpdirname   )
					__lowercase	:       List[Any]				=   SpeechTaProcessor.from_pretrained(self.tmpdirname   )

					self.assertEqual(processor.tokenizer.get_vocab()							,		tokenizer.get_vocab()   )
					self.assertIsInstance(processor.tokenizer							,		UpperCamelCase_   )

					self.assertEqual(processor.feature_extractor.to_json_string()							,		feature_extractor.to_json_string()   )
					self.assertIsInstance(processor.feature_extractor							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Optional[Any]:
					__lowercase	:       Optional[int]				=   SpeechTaProcessor(tokenizer=self.get_tokenizer()							,		feature_extractor=self.get_feature_extractor()   )
					processor.save_pretrained(self.tmpdirname   )

					__lowercase	:       str				=   self.get_tokenizer(bos_token='''(BOS)'''							,		eos_token='''(EOS)'''   )
					__lowercase	:       Dict				=   self.get_feature_extractor(do_normalize=UpperCamelCase_							,		padding_value=1.0   )

					__lowercase	:       Tuple				=   SpeechTaProcessor.from_pretrained(
					    self.tmpdirname							,		bos_token='''(BOS)'''							,		eos_token='''(EOS)'''							,		do_normalize=UpperCamelCase_							,		padding_value=1.0   )

					self.assertEqual(processor.tokenizer.get_vocab()							,		tokenizer_add_kwargs.get_vocab()   )
					self.assertIsInstance(processor.tokenizer							,		UpperCamelCase_   )

					self.assertEqual(processor.feature_extractor.to_json_string()							,		feature_extractor_add_kwargs.to_json_string()   )
					self.assertIsInstance(processor.feature_extractor							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   List[str]:
					__lowercase	:       Optional[Any]				=   self.get_feature_extractor()
					__lowercase	:       Optional[Any]				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       int				=   floats_list((3, 10_00)   )

					__lowercase	:       str				=   feature_extractor(audio=UpperCamelCase_							,		return_tensors='''np'''   )
					__lowercase	:       Dict				=   processor(audio=UpperCamelCase_							,		return_tensors='''np'''   )

					for key in input_feat_extract.keys():
								self.assertAlmostEqual(input_feat_extract[key].sum()							,		input_processor[key].sum()							,		delta=1E-2   )
		def 					_lowerCamelCase    (		self   )		->   List[Any]:
					__lowercase	:       str				=   self.get_feature_extractor()
					__lowercase	:       str				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Optional[int]				=   floats_list((3, 10_00)   )

					__lowercase	:       List[str]				=   feature_extractor(audio_target=UpperCamelCase_							,		return_tensors='''np'''   )
					__lowercase	:       List[str]				=   processor(audio_target=UpperCamelCase_							,		return_tensors='''np'''   )

					for key in input_feat_extract.keys():
								self.assertAlmostEqual(input_feat_extract[key].sum()							,		input_processor[key].sum()							,		delta=1E-2   )
		def 					_lowerCamelCase    (		self   )		->   Any:
					__lowercase	:       List[Any]				=   self.get_feature_extractor()
					__lowercase	:       List[str]				=   self.get_tokenizer()

					__lowercase	:       Union[str, Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       List[str]				=   '''This is a test string'''

					__lowercase	:       Any				=   processor(text=UpperCamelCase_   )
					__lowercase	:       Optional[int]				=   tokenizer(UpperCamelCase_   )

					for key in encoded_tok.keys():
								self.assertListEqual(encoded_tok[key]							,		encoded_processor[key]   )
		def 					_lowerCamelCase    (		self   )		->   str:
					__lowercase	:       int				=   self.get_feature_extractor()
					__lowercase	:       List[str]				=   self.get_tokenizer()

					__lowercase	:       List[Any]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Union[str, Any]				=   '''This is a test string'''

					__lowercase	:       Tuple				=   processor(text_target=UpperCamelCase_   )
					__lowercase	:       Optional[int]				=   tokenizer(UpperCamelCase_   )

					for key in encoded_tok.keys():
								self.assertListEqual(encoded_tok[key]							,		encoded_processor[key]   )
		def 					_lowerCamelCase    (		self   )		->   Optional[Any]:
					__lowercase	:       Optional[int]				=   self.get_feature_extractor()
					__lowercase	:       str				=   self.get_tokenizer()

					__lowercase	:       int				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					__lowercase	:       Optional[Any]				=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					__lowercase	:       str				=   processor.batch_decode(UpperCamelCase_   )
					__lowercase	:       int				=   tokenizer.batch_decode(UpperCamelCase_   )

					self.assertListEqual(UpperCamelCase_							,		UpperCamelCase_   )
		def 					_lowerCamelCase    (		self   )		->   Optional[int]:
					__lowercase	:       Union[str, Any]				=   self.get_feature_extractor()
					__lowercase	:       int				=   self.get_tokenizer()

					__lowercase	:       Optional[int]				=   SpeechTaProcessor(tokenizer=UpperCamelCase_							,		feature_extractor=UpperCamelCase_   )

					self.assertListEqual(
					    processor.model_input_names							,		feature_extractor.model_input_names							,		msg='''`processor` and `feature_extractor` model input names do not match'''							,		)



