




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 ,       __lowercase ,       __lowercase=2 ,       __lowercase=True ,       __lowercase=True ,       __lowercase=10 ,       __lowercase=10 ,       __lowercase=8 ,       __lowercase=[2, 4, 4] ,       __lowercase=3 ,       __lowercase=768 ,       __lowercase=5 ,       __lowercase=4 ,       __lowercase=37 ,       __lowercase="gelu_fast" ,       __lowercase=0.0 ,       __lowercase=0.0 ,       __lowercase=0.02 ,       __lowercase=1E-06 ,       __lowercase=True ,       __lowercase=None ,       ):







						'''simple docstring'''




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

						lowerCamelCase_							:		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 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							floats_tensor(
						    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]					)

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

						lowerCamelCase_							:		Tuple							   =							self.get_config()

						return config, pixel_values, labels

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		str							   =							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_							:		List[str]							   =							self.num_labels
						return config

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							VivitModel(config=__lowercase					)
						model.to(__lowercase					)
						model.eval()
						lowerCamelCase_							:		str							   =							model(__lowercase					)
						self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size)					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							   =							VivitForVideoClassification(__lowercase					)
						model.to(__lowercase					)
						model.eval()

						lowerCamelCase_							:		Optional[Any]							   =							model(__lowercase					)

						# verify the logits shape
						lowerCamelCase_							:		Optional[Any]							   =							torch.Size((self.batch_size, self.num_labels)					)
						self.parent.assertEqual(result.logits.shape ,       __lowercase					)





					def 	lowercase_							(				self					):







						'''simple docstring'''




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


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

					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False
					_lowerCamelCase		  = False

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		int							   =							VivitModelTester(self					)
						lowerCamelCase_							:		List[Any]							   =							ConfigTester(self ,       config_class=__lowercase ,       has_text_modality=__lowercase ,       hidden_size=37					)

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase=False					):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							   =							copy.deepcopy(__lowercase					)

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

						return inputs_dict

					def 	lowercase_							(				self					):







						'''simple docstring'''




						self.config_tester.run_common_tests()

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







						'''simple docstring'''




						pass

					def 	lowercase_							(				self					):







						'''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[str]							   =							model_class(__lowercase					)
							self.assertIsInstance(model.get_input_embeddings() ,       (nn.Module)					)
							lowerCamelCase_							:		List[str]							   =							model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(__lowercase ,       nn.Linear					)					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

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

							lowerCamelCase_							:		Union[str, Any]							   =							['pixel_values', 'head_mask']
							self.assertListEqual(arg_names[:2] ,       __lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Any							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_video_classification(*__lowercase					)

					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Dict							   =							VivitModel.from_pretrained(__lowercase					)
							self.assertIsNotNone(__lowercase					)

					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_   ,			lowerCamelCase_							:		str							   =							self.model_tester.prepare_config_and_inputs_for_common()
						lowerCamelCase_							:		Optional[int]							   =							True

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

							lowerCamelCase_							:		Tuple							   =							True
							lowerCamelCase_							:		str							   =							False
							lowerCamelCase_							:		List[Any]							   =							True
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		List[Any]							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							lowerCamelCase_							:		Optional[int]							   =							True
							lowerCamelCase_							:		List[str]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()
							with torch.no_grad():
								lowerCamelCase_							:		Any							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)
							lowerCamelCase_							:		Dict							   =							outputs.attentions
							self.assertEqual(len(__lowercase					) ,       self.model_tester.num_hidden_layers					)

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

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

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

							lowerCamelCase_							:		List[Any]							   =							outputs.attentions

							self.assertEqual(len(__lowercase					) ,       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					):







						'''simple docstring'''




						def check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					):
							lowerCamelCase_							:		Optional[int]							   =							model_class(__lowercase					)
							model.to(__lowercase					)
							model.eval()

							with torch.no_grad():
								lowerCamelCase_							:		Union[str, Any]							   =							model(**self._prepare_for_class(__lowercase ,       __lowercase					)					)

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

							lowerCamelCase_							:		Union[str, Any]							   =							self.model_tester.seq_length

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

						lowerCamelCase_   ,			lowerCamelCase_							:		Optional[int]							   =							self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							lowerCamelCase_							:		Optional[Any]							   =							True
							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							lowerCamelCase_							:		Any							   =							True

							check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase					)





					@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'					)
					def 	lowercase_							(				self					):







						'''simple docstring'''




						pass






def 					snake_case					(							)							->							Optional[int]:




	'''simple docstring'''


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



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

					@cached_property
					def 	lowercase_							(				self					):







						'''simple docstring'''




						return VivitImageProcessor() if is_vision_available() else None





					@slow
					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							   =							VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'					).to(__lowercase					)

						lowerCamelCase_							:		Any							   =							self.default_image_processor
						lowerCamelCase_							:		Any							   =							prepare_video()
						lowerCamelCase_							:		List[str]							   =							image_processor(__lowercase ,       return_tensors='pt'					).to(__lowercase					)

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

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

						# taken from original model
						lowerCamelCase_							:		List[Any]							   =							torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]					).to(__lowercase					)

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

'''simple docstring'''


import argparse
import logging

import datasets
from tokenizers import Tokenizer, decoders, normalizers, pre_tokenizers, processors
from tokenizers.models import Unigram
from tokenizers.trainers import UnigramTrainer

from transformers import AlbertTokenizerFast


snake_case__     :			Optional[int]      			=  logging.getLogger(__name__)


def 					_lowerCamelCase    (      ):



     """simple docstring"""





     UpperCAmelCase_							:   List[Any]   =      argparse.ArgumentParser(description='Train a unigram tokenizer on the wikitext dataset.'   )
     parser.add_argument(
         '--dataset_name'	,     type=lowerCamelCase_	,     default='wikitext'	,     help='Name of the training. Explore datasets at: hf.co/datasets.'	,     )
     parser.add_argument(
         '--dataset_config'	,     type=lowerCamelCase_	,     default='wikitext-103-raw-v1'	,     help='Configuration name of the dataset.'   )
     parser.add_argument(
         '--batch_size'	,     type=lowerCamelCase_	,     default=1000	,     help='Batch size during training.'	,     )
     parser.add_argument(
         '--vocab_size'	,     type=lowerCamelCase_	,     default=10048	,     help='Size of the desired vocabulary.'	,     )
     parser.add_argument(
         '--limit'	,     default=lowerCamelCase_	,     type=lowerCamelCase_	,     help='Limit the number of shards (used for debugging).'	,     )
     parser.add_argument(
         '--export_to_hub'	,     action='store_true'	,     )

     UpperCAmelCase_							:   str   =      parser.parse_args()
     return args


def 					_lowerCamelCase    (      lowerCamelCase_					:							Dict   ):



     """simple docstring"""





     UpperCAmelCase_							:   List[Any]   =      datasets.load_dataset(args.dataset_name	,     args.dataset_config	,     split='train'   )

     if args.limit is not None:
          UpperCAmelCase_							:   Union[str, Any]   =      min(len(lowerCamelCase_   )	,     args.limit   )
          UpperCAmelCase_							:   int   =      dataset.select(range(lowerCamelCase_   )   )
          logger.info(F'''Limiting the dataset to {args.limit} entries.'''   )

     def batch_iterator():
          for i in range(0	,     len(lowerCamelCase_   )	,     args.batch_size   ):
               yield dataset[i : i + args.batch_size]["text"]

    # Prepare the tokenizer.
     UpperCAmelCase_							:   Optional[Any]   =      Tokenizer(Unigram()   )
     UpperCAmelCase_							:   Union[str, Any]   =      normalizers.Sequence([normalizers.Replace('``'	,     '"'   ), normalizers.Replace('\'\''	,     '"'   )]   )
     UpperCAmelCase_							:   Any   =      pre_tokenizers.Metaspace()

     # Prepare the trainer.
     UpperCAmelCase_							:   Tuple   =      UnigramTrainer(
         unk_token='<unk>'	,     special_tokens=['[CLS]', '[SEP]', '<unk>', '<pad>', '[MASK]']	,     vocab_size=args.vocab_size	,     )

     logger.info('Training the tokenizer.'   )
     tokenizer.train_from_iterator(batch_iterator()	,     trainer=lowerCamelCase_   )
     logger.info('Tokenizer training complete!'   )

     UpperCAmelCase_							:   List[str]   =      tokenizer.token_to_id('[CLS]'   )
     UpperCAmelCase_							:   Optional[int]   =      tokenizer.token_to_id('[SEP]'   )
     UpperCAmelCase_							:   Optional[Any]   =      processors.TemplateProcessing(
         single='[CLS]:0 $A:0 [SEP]:0'	,     pair='[CLS]:0 $A:0 [SEP]:0 $B:1 [SEP]:1'	,     special_tokens=[
             ('[CLS]', cls_token_id),
             ('[SEP]', sep_token_id),
         ]	,     )
     UpperCAmelCase_							:   Any   =      decoders.Metaspace()

     if args.export_to_hub:
          logger.info('Exporting the trained tokenzier to Hub.'   )
          UpperCAmelCase_							:   str   =      AlbertTokenizerFast(tokenizer_object=lowerCamelCase_   )
          new_tokenizer.push_to_hub('unigram-tokenizer-dataset'   )


if __name__ == "__main__":
 snake_case__     :			Optional[Any]      			=  parse_args()
 main(args)






