






"""simple docstring"""



import argparse
import logging
import os
from pathlib import Path
from typing import Any, Dict

import pytorch_lightning as pl
from pytorch_lightning.utilities import rank_zero_info

from transformers import (
    AdamW,
    AutoConfig,
    AutoModel,
    AutoModelForPreTraining,
    AutoModelForQuestionAnswering,
    AutoModelForSeqaSeqLM,
    AutoModelForSequenceClassification,
    AutoModelForTokenClassification,
    AutoModelWithLMHead,
    AutoTokenizer,
    PretrainedConfig,
    PreTrainedTokenizer,
)
from transformers.optimization import (
    Adafactor,
    get_cosine_schedule_with_warmup,
    get_cosine_with_hard_restarts_schedule_with_warmup,
    get_linear_schedule_with_warmup,
    get_polynomial_decay_schedule_with_warmup,
)
from transformers.utils.versions import require_version


_snake_case       :      Dict       =					logging.getLogger(__name__)

require_version("pytorch_lightning>=1.0.4")

_snake_case       :      str       =					{
    '''base''': AutoModel,
    '''sequence-classification''': AutoModelForSequenceClassification,
    '''question-answering''': AutoModelForQuestionAnswering,
    '''pretraining''': AutoModelForPreTraining,
    '''token-classification''': AutoModelForTokenClassification,
    '''language-modeling''': AutoModelWithLMHead,
    '''summarization''': AutoModelForSeqaSeqLM,
    '''translation''': AutoModelForSeqaSeqLM,
}


# update this and the import above to support new schedulers from transformers.optimization
_snake_case       :      Tuple       =					{
    '''linear''': get_linear_schedule_with_warmup,
    '''cosine''': get_cosine_schedule_with_warmup,
    '''cosine_w_restarts''': get_cosine_with_hard_restarts_schedule_with_warmup,
    '''polynomial''': get_polynomial_decay_schedule_with_warmup,
    # '': get_constant_schedule,             # not supported for now
    # '': get_constant_schedule_with_warmup, # not supported for now
}
_snake_case       :      List[Any]       =					sorted(arg_to_scheduler.keys())
_snake_case       :      Optional[int]       =					'''{''' + ''', '''.join(arg_to_scheduler_choices) + '''}'''




class        __lowercase(       pl.LightningModule  ):






						def __init__(					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE=None    ,						_SCREAMING_SNAKE_CASE="base"    ,						_SCREAMING_SNAKE_CASE=None    ,						_SCREAMING_SNAKE_CASE=None    ,						_SCREAMING_SNAKE_CASE=None    ,						**_SCREAMING_SNAKE_CASE    ,						)			->  List[str]:




											"""simple docstring"""





											super().__init__()
											# TODO: move to self.save_hyperparameters()
											# self.save_hyperparameters()
											# can also expand arguments into trainer signature for easier reading

											self.save_hyperparameters(_SCREAMING_SNAKE_CASE)
											lowerCamelCase__				=0
											lowerCamelCase__				=Path(self.hparams.output_dir)
											lowerCamelCase__				=self.hparams.cache_dir if self.hparams.cache_dir else None
											if config is None:
																lowerCamelCase__				=AutoConfig.from_pretrained(
																    self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path    ,						**({'num_labels': num_labels} if num_labels is not None else {})    ,						cache_dir=_SCREAMING_SNAKE_CASE    ,						**_SCREAMING_SNAKE_CASE    ,						)
											else:
																lowerCamelCase__				=config

											lowerCamelCase__				=("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout")
											for p in extra_model_params:
																if getattr(self.hparams    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE):
																					assert hasattr(self.config    ,						_SCREAMING_SNAKE_CASE), F"model config doesn\'t have a `{p}` attribute"
																					setattr(self.config    ,						_SCREAMING_SNAKE_CASE    ,						getattr(self.hparams    ,						_SCREAMING_SNAKE_CASE))

											if tokenizer is None:
																lowerCamelCase__				=AutoTokenizer.from_pretrained(
																    self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path    ,						cache_dir=_SCREAMING_SNAKE_CASE    ,						)
											else:
																lowerCamelCase__				=tokenizer
											lowerCamelCase__				=MODEL_MODES[mode]
											if model is None:
																lowerCamelCase__				=self.model_type.from_pretrained(
																    self.hparams.model_name_or_path    ,						from_tf=bool('.ckpt' in self.hparams.model_name_or_path)    ,						config=self.config    ,						cache_dir=_SCREAMING_SNAKE_CASE    ,						)
											else:
																lowerCamelCase__				=model






						def 		lowerCAmelCase   (					self    ,						*_SCREAMING_SNAKE_CASE    ,						**_SCREAMING_SNAKE_CASE)			->  Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__				=self.model_type.from_pretrained(*_SCREAMING_SNAKE_CASE    ,						**_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  List[Any]:




											"""simple docstring"""





											lowerCamelCase__				=arg_to_scheduler[self.hparams.lr_scheduler]
											lowerCamelCase__				=get_schedule_func(
											    self.opt    ,						num_warmup_steps=self.hparams.warmup_steps    ,						num_training_steps=self.total_steps())
											lowerCamelCase__				={"scheduler": scheduler, "interval": "step", "frequency": 1}
											return scheduler






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				=self.model
											lowerCamelCase__				=["bias", "LayerNorm.weight"]
											lowerCamelCase__				=[
											    {
											        "params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
											        "weight_decay": self.hparams.weight_decay,
											    },
											    {
											        "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
											        "weight_decay": 0.0,
											    },
											]
											if self.hparams.adafactor:
																lowerCamelCase__				=Adafactor(
																    _SCREAMING_SNAKE_CASE    ,						lr=self.hparams.learning_rate    ,						scale_parameter=_SCREAMING_SNAKE_CASE    ,						relative_step=_SCREAMING_SNAKE_CASE)

											else:
																lowerCamelCase__				=AdamW(
																    _SCREAMING_SNAKE_CASE    ,						lr=self.hparams.learning_rate    ,						eps=self.hparams.adam_epsilon)
											lowerCamelCase__				=optimizer

											lowerCamelCase__				=self.get_lr_scheduler()

											return [optimizer], [scheduler]






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  Any:




											"""simple docstring"""





											return self.validation_step(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE)			->  Optional[Any]:




											"""simple docstring"""





											return self.validation_end(_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  Union[str, Any]:




											"""simple docstring"""





											lowerCamelCase__				=max(1    ,						self.hparams.gpus)  # TODO: consider num_tpu_cores
											lowerCamelCase__				=self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices
											return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE)			->  Union[str, Any]:




											"""simple docstring"""





											if mode == "test":
																lowerCamelCase__				=len(self.test_dataloader().dataset)
											else:
																lowerCamelCase__				=self.get_dataloader('train'    ,						self.hparams.train_batch_size    ,						shuffle=_SCREAMING_SNAKE_CASE)
																lowerCamelCase__				=len(self.train_dataloader().dataset)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE = False)			->  List[Any]:




											"""simple docstring"""





											raise NotImplementedError('You must implement this for your task')






						def 		lowerCAmelCase   (					self)			->  Tuple:




											"""simple docstring"""





											return self.train_loader






						def 		lowerCAmelCase   (					self)			->  int:




											"""simple docstring"""





											return self.get_dataloader('dev'    ,						self.hparams.eval_batch_size    ,						shuffle=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  Optional[Any]:




											"""simple docstring"""





											return self.get_dataloader('test'    ,						self.hparams.eval_batch_size    ,						shuffle=_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE)			->  Any:




											"""simple docstring"""





											return os.path.join(
											    self.hparams.data_dir    ,						'cached_{}_{}_{}'.format(
											        _SCREAMING_SNAKE_CASE    ,						list(filter(_SCREAMING_SNAKE_CASE    ,						self.hparams.model_name_or_path.split('/'))).pop()    ,						str(self.hparams.max_seq_length)    ,						)    ,						)






						@pl.utilities.rank_zero_only
						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE)			->  List[str]:




											"""simple docstring"""





											lowerCamelCase__				=self.output_dir.joinpath('best_tfmr')
											lowerCamelCase__				=self.step_count
											self.model.save_pretrained(_SCREAMING_SNAKE_CASE)
											self.tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE)






						@staticmethod
						def 		lowerCAmelCase   (					_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  Optional[int]:




											"""simple docstring"""





											parser.add_argument(
											    '--model_name_or_path'    ,						default=_SCREAMING_SNAKE_CASE    ,						type=_SCREAMING_SNAKE_CASE    ,						required=_SCREAMING_SNAKE_CASE    ,						help='Path to pretrained model or model identifier from huggingface.co/models'    ,						)
											parser.add_argument(
											    '--config_name'    ,						default=''    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Pretrained config name or path if not the same as model_name')
											parser.add_argument(
											    '--tokenizer_name'    ,						default=_SCREAMING_SNAKE_CASE    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Pretrained tokenizer name or path if not the same as model_name'    ,						)
											parser.add_argument(
											    '--cache_dir'    ,						default=''    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Where do you want to store the pre-trained models downloaded from huggingface.co'    ,						)
											parser.add_argument(
											    '--encoder_layerdrop'    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Encoder layer dropout probability (Optional). Goes into model.config'    ,						)
											parser.add_argument(
											    '--decoder_layerdrop'    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Decoder layer dropout probability (Optional). Goes into model.config'    ,						)
											parser.add_argument(
											    '--dropout'    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Dropout probability (Optional). Goes into model.config'    ,						)
											parser.add_argument(
											    '--attention_dropout'    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Attention dropout probability (Optional). Goes into model.config'    ,						)
											parser.add_argument('--learning_rate'    ,						default=5E-5    ,						type=_SCREAMING_SNAKE_CASE    ,						help='The initial learning rate for Adam.')
											parser.add_argument(
											    '--lr_scheduler'    ,						default='linear'    ,						choices=_SCREAMING_SNAKE_CASE    ,						metavar=_SCREAMING_SNAKE_CASE    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Learning rate scheduler'    ,						)
											parser.add_argument('--weight_decay'    ,						default=0.0    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Weight decay if we apply some.')
											parser.add_argument('--adam_epsilon'    ,						default=1E-8    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Epsilon for Adam optimizer.')
											parser.add_argument('--warmup_steps'    ,						default=0    ,						type=_SCREAMING_SNAKE_CASE    ,						help='Linear warmup over warmup_steps.')
											parser.add_argument('--num_workers'    ,						default=4    ,						type=_SCREAMING_SNAKE_CASE    ,						help='kwarg passed to DataLoader')
											parser.add_argument('--num_train_epochs'    ,						dest='max_epochs'    ,						default=3    ,						type=_SCREAMING_SNAKE_CASE)
											parser.add_argument('--train_batch_size'    ,						default=32    ,						type=_SCREAMING_SNAKE_CASE)
											parser.add_argument('--eval_batch_size'    ,						default=32    ,						type=_SCREAMING_SNAKE_CASE)
											parser.add_argument('--adafactor'    ,						action='store_true')




class        __lowercase(       pl.Callback  ):






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  Optional[int]:




											"""simple docstring"""





											lowerCamelCase__				=trainer.lr_schedulers[0]["scheduler"]
											lowerCamelCase__				={F"lr_group_{i}": lr for i, lr in enumerate(lr_scheduler.get_lr())}
											pl_module.logger.log_metrics(_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  Union[str, Any]:




											"""simple docstring"""





											rank_zero_info('***** Validation results *****')
											lowerCamelCase__				=trainer.callback_metrics
											# Log results
											for key in sorted(_SCREAMING_SNAKE_CASE):
																if key not in ["log", "progress_bar"]:
																					rank_zero_info('{} = {}\n'.format(_SCREAMING_SNAKE_CASE    ,						str(metrics[key])))






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  List[str]:




											"""simple docstring"""





											rank_zero_info('***** Test results *****')
											lowerCamelCase__				=trainer.callback_metrics
											# Log and save results to file
											lowerCamelCase__				=os.path.join(pl_module.hparams.output_dir    ,						'test_results.txt')
											with open(_SCREAMING_SNAKE_CASE    ,						'w') as writer:
																for key in sorted(_SCREAMING_SNAKE_CASE):
																					if key not in ["log", "progress_bar"]:
																										rank_zero_info('{} = {}\n'.format(_SCREAMING_SNAKE_CASE    ,						str(metrics[key])))
																										writer.write('{} = {}\n'.format(_SCREAMING_SNAKE_CASE    ,						str(metrics[key])))




def 						lowerCamelCase_				(  __A  :		Tuple			,	__A  :		int			):
					#  To allow all pl args uncomment the following line
					#  parser = pl.Trainer.add_argparse_args(parser)
					parser.add_argument(
					    '--output_dir'			,	default=__lowerCAmelCase			,	type=__lowerCAmelCase			,	required=__lowerCAmelCase			,	help='The output directory where the model predictions and checkpoints will be written.'			,	)
					parser.add_argument(
					    '--fp16'			,	action='store_true'			,	help='Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit'			,	)

					parser.add_argument(
					    '--fp16_opt_level'			,	type=__lowerCAmelCase			,	default='O2'			,	help=(
					        'For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\'].'
					        'See details at https://nvidia.github.io/apex/amp.html'
					    )			,	)
					parser.add_argument('--n_tpu_cores'			,	dest='tpu_cores'			,	type=__lowerCAmelCase			)
					parser.add_argument('--max_grad_norm'			,	dest='gradient_clip_val'			,	default=1.0			,	type=__lowerCAmelCase			,	help='Max gradient norm'			)
					parser.add_argument('--do_train'			,	action='store_true'			,	help='Whether to run training.'			)
					parser.add_argument('--do_predict'			,	action='store_true'			,	help='Whether to run predictions on the test set.'			)
					parser.add_argument(
					    '--gradient_accumulation_steps'			,	dest='accumulate_grad_batches'			,	type=__lowerCAmelCase			,	default=1			,	help='Number of updates steps to accumulate before performing a backward/update pass.'			,	)
					parser.add_argument('--seed'			,	type=__lowerCAmelCase			,	default=42			,	help='random seed for initialization'			)
					parser.add_argument(
					    '--data_dir'			,	default=__lowerCAmelCase			,	type=__lowerCAmelCase			,	required=__lowerCAmelCase			,	help='The input data dir. Should contain the training files for the CoNLL-2003 NER task.'			,	)




def 						lowerCamelCase_				(  __A  :		int			,	__A  :		Dict			,	__A  :		Union[str, Any]=None			,	__A  :		Dict=True			,	__A  :		int=[]			,	__A  :		List[str]=None			,	__A  :		Any=None			,	**__A  :		Tuple			,	):
					pl.seed_everything(args.seed			)

					# init model
					lowerCamelCase__				=Path(model.hparams.output_dir			)
					odir.mkdir(exist_ok=__lowerCAmelCase			)

					# add custom checkpoints
					if checkpoint_callback is None:
										lowerCamelCase__				=pl.callbacks.ModelCheckpoint(
										    filepath=args.output_dir			,	prefix='checkpoint'			,	monitor='val_loss'			,	mode='min'			,	save_top_k=1			)
					if early_stopping_callback:
										extra_callbacks.append(__lowerCAmelCase			)
					if logging_callback is None:
										lowerCamelCase__				=LoggingCallback()

					lowerCamelCase__				={}

					# TODO: remove with PyTorch 1.6 since pl uses native amp
					if args.fpaa:
										lowerCamelCase__				=16
										lowerCamelCase__				=args.fpaa_opt_level

					if args.gpus > 1:
										lowerCamelCase__				="ddp"

					lowerCamelCase__				=args.accumulate_grad_batches
					lowerCamelCase__				=extra_train_kwargs.get('accelerator'			,	__lowerCAmelCase			)
					lowerCamelCase__				=extra_train_kwargs.get('profiler'			,	__lowerCAmelCase			)

					lowerCamelCase__				=pl.Trainer.from_argparse_args(
					    __lowerCAmelCase			,	weights_summary=__lowerCAmelCase			,	callbacks=[logging_callback] + extra_callbacks			,	logger=__lowerCAmelCase			,	checkpoint_callback=__lowerCAmelCase			,	**__lowerCAmelCase			,	)

					if args.do_train:
										trainer.fit(__lowerCAmelCase			)

					return trainer







"""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        __lowercase:






						def __init__(					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE=2    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=10    ,						_SCREAMING_SNAKE_CASE=10    ,						_SCREAMING_SNAKE_CASE=8    ,						_SCREAMING_SNAKE_CASE=[2, 4, 4]    ,						_SCREAMING_SNAKE_CASE=3    ,						_SCREAMING_SNAKE_CASE=7_68    ,						_SCREAMING_SNAKE_CASE=5    ,						_SCREAMING_SNAKE_CASE=4    ,						_SCREAMING_SNAKE_CASE=37    ,						_SCREAMING_SNAKE_CASE="gelu_fast"    ,						_SCREAMING_SNAKE_CASE=0.0    ,						_SCREAMING_SNAKE_CASE=0.0    ,						_SCREAMING_SNAKE_CASE=0.02    ,						_SCREAMING_SNAKE_CASE=1E-06    ,						_SCREAMING_SNAKE_CASE=True    ,						_SCREAMING_SNAKE_CASE=None    ,						)			->  Dict:




											"""simple docstring"""





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

											lowerCamelCase__				=(
											    (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)			->  Union[str, Any]:




											"""simple docstring"""





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

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

											lowerCamelCase__				=self.get_config()

											return config, pixel_values, labels






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				=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__				=self.num_labels
											return config






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  int:




											"""simple docstring"""





											lowerCamelCase__				=VivitModel(config=_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()
											lowerCamelCase__				=model(_SCREAMING_SNAKE_CASE)
											self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size))






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  str:




											"""simple docstring"""





											lowerCamelCase__				=VivitForVideoClassification(_SCREAMING_SNAKE_CASE)
											model.to(_SCREAMING_SNAKE_CASE)
											model.eval()

											lowerCamelCase__				=model(_SCREAMING_SNAKE_CASE)

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






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





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




@require_torch
class        __lowercase(       _UpperCamelCase						,							unittest.TestCase  ):
						lowerCAmelCase_		:							str=       (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

						lowerCAmelCase_		:							str=       False
						lowerCAmelCase_		:							Tuple=       False
						lowerCAmelCase_		:							Union[str, Any]=       False
						lowerCAmelCase_		:							Dict=       False






						def 		lowerCAmelCase   (					self)			->  List[str]:




											"""simple docstring"""





											lowerCamelCase__				=VivitModelTester(self)
											lowerCamelCase__				=ConfigTester(self    ,						config_class=_SCREAMING_SNAKE_CASE    ,						has_text_modality=_SCREAMING_SNAKE_CASE    ,						hidden_size=37)






						def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE=False)			->  Optional[int]:




											"""simple docstring"""





											lowerCamelCase__				=copy.deepcopy(_SCREAMING_SNAKE_CASE)

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

											return inputs_dict






						def 		lowerCAmelCase   (					self)			->  Tuple:




											"""simple docstring"""





											self.config_tester.run_common_tests()






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




											"""simple docstring"""





											pass






						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

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






						def 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

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

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






						def 		lowerCAmelCase   (					self)			->  Optional[int]:




											"""simple docstring"""





											lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





											lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_video_classification(*_SCREAMING_SNAKE_CASE)






						@slow
						def 		lowerCAmelCase   (					self)			->  Tuple:




											"""simple docstring"""





											for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowerCamelCase__				=VivitModel.from_pretrained(_SCREAMING_SNAKE_CASE)
																self.assertIsNotNone(_SCREAMING_SNAKE_CASE)






						def 		lowerCAmelCase   (					self)			->  List[Any]:




											"""simple docstring"""





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

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

																lowerCamelCase__				=True
																lowerCamelCase__				=False
																lowerCamelCase__				=True
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))
																lowerCamelCase__				=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__				=True
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()
																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))
																lowerCamelCase__				=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__				=len(_SCREAMING_SNAKE_CASE)

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

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

																lowerCamelCase__				=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 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											def check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE):
																lowerCamelCase__				=model_class(_SCREAMING_SNAKE_CASE)
																model.to(_SCREAMING_SNAKE_CASE)
																model.eval()

																with torch.no_grad():
																					lowerCamelCase__				=model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE))

																lowerCamelCase__				=outputs.hidden_states
																lowerCamelCase__				=self.model_tester.num_hidden_layers + 1
																self.assertEqual(len(_SCREAMING_SNAKE_CASE)    ,						_SCREAMING_SNAKE_CASE)

																lowerCamelCase__				=self.model_tester.seq_length

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

											lowerCamelCase__				,     lowerCamelCase__				=self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																lowerCamelCase__				=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__				=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 		lowerCAmelCase   (					self)			->  Dict:




											"""simple docstring"""





											pass




def 						lowerCamelCase_				(  ):
					lowerCamelCase__				=hf_hub_download(
					    repo_id='hf-internal-testing/spaghetti-video'			,	filename='eating_spaghetti_32_frames.npy'			,	repo_type='dataset'			)
					lowerCamelCase__				=np.load(__A			)
					return list(__A			)




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






						@cached_property
						def 		lowerCAmelCase   (					self)			->  Union[str, Any]:




											"""simple docstring"""





											return VivitImageProcessor() if is_vision_available() else None






						@slow
						def 		lowerCAmelCase   (					self)			->  str:




											"""simple docstring"""





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

											lowerCamelCase__				=self.default_image_processor
											lowerCamelCase__				=prepare_video()
											lowerCamelCase__				=image_processor(_SCREAMING_SNAKE_CASE    ,						return_tensors='pt').to(_SCREAMING_SNAKE_CASE)

											# forward pass
											with torch.no_grad():
																lowerCamelCase__				=model(**_SCREAMING_SNAKE_CASE)

											# verify the logits
											lowerCamelCase__				=torch.Size((1, 4_00))
											self.assertEqual(outputs.logits.shape    ,						_SCREAMING_SNAKE_CASE)

											# taken from original model
											lowerCamelCase__				=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))