





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:
     '''simple docstring'''


     def __init__(						self							,    snake_case_							,    snake_case_=2							,    snake_case_=True							,    snake_case_=True							,    snake_case_=10							,    snake_case_=10							,    snake_case_=8							,    snake_case_=[2, 4, 4]							,    snake_case_=3							,    snake_case_=768							,    snake_case_=5							,    snake_case_=4							,    snake_case_=37							,    snake_case_="gelu_fast"							,    snake_case_=0.0							,    snake_case_=0.0							,    snake_case_=0.02							,    snake_case_=1E-06							,    snake_case_=True							,    snake_case_=None							,    ):
         _A    =       parent
         _A    =       batch_size
         _A    =       is_training
         _A    =       use_labels
         _A    =       num_labels
         _A    =       image_size
         _A    =       num_frames
         _A    =       tubelet_size
         _A    =       num_channels
         _A    =       hidden_size
         _A    =       num_hidden_layers
         _A    =       num_attention_heads
         _A    =       intermediate_size
         _A    =       hidden_act
         _A    =       hidden_dropout_prob
         _A    =       attention_probs_dropout_prob
         _A    =       initializer_range
         _A    =       layer_norm_eps
         _A    =       qkv_bias
         _A    =       scope

         _A    =       (
             (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       ):
         _A    =       floats_tensor(
             [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]       )

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

         _A    =       self.get_config()

         return config, pixel_values, labels




     def 					lowerCAmelCase__		(						self       ):
         _A    =       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							,    )
         _A    =       self.num_labels
         return config




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_							,    snake_case_       ):
         _A    =       VivitModel(config=snake_case_       )
         model.to(snake_case_       )
         model.eval()
         _A    =       model(snake_case_       )
         self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)       )




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_							,    snake_case_       ):
         _A    =       VivitForVideoClassification(snake_case_       )
         model.to(snake_case_       )
         model.eval()

         _A    =       model(snake_case_       )

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




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







@require_torch
class     lowerCamelCase(	__snake_case ,   unittest.TestCase  ):
     '''simple docstring'''


     __magic_name__        = (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

     __magic_name__        = False
     __magic_name__        = False
     __magic_name__        = False
     __magic_name__        = False




     def 					lowerCAmelCase__		(						self       ):
         _A    =       VivitModelTester(self       )
         _A    =       ConfigTester(self							,    config_class=snake_case_							,    has_text_modality=snake_case_							,    hidden_size=37       )




     def 					lowerCAmelCase__		(						self							,    snake_case_							,    snake_case_							,    snake_case_=False       ):
         _A    =       copy.deepcopy(snake_case_       )

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

         return inputs_dict




     def 					lowerCAmelCase__		(						self       ):
         self.config_tester.run_common_tests()




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




     def 					lowerCAmelCase__		(						self       ):
         _A, _A    =       self.model_tester.prepare_config_and_inputs_for_common()

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




     def 					lowerCAmelCase__		(						self       ):
         _A, _A    =       self.model_tester.prepare_config_and_inputs_for_common()

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

             _A    =       ['pixel_values', 'head_mask']
             self.assertListEqual(arg_names[:2]							,    snake_case_       )




     def 					lowerCAmelCase__		(						self       ):
         _A    =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*snake_case_       )




     def 					lowerCAmelCase__		(						self       ):
         _A    =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_video_classification(*snake_case_       )




     @slow
     def 					lowerCAmelCase__		(						self       ):
         for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    =       VivitModel.from_pretrained(snake_case_       )
             self.assertIsNotNone(snake_case_       )




     def 					lowerCAmelCase__		(						self       ):
         _A, _A    =       self.model_tester.prepare_config_and_inputs_for_common()
         _A    =       True

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

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

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             _A    =       True
             _A    =       model_class(snake_case_       )
             model.to(snake_case_       )
             model.eval()
             with torch.no_grad():
                 _A    =       model(**self._prepare_for_class(snake_case_							,    snake_case_       )       )
             _A    =       outputs.attentions
             self.assertEqual(len(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]							,    )
             _A    =       len(snake_case_       )

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

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

             _A    =       outputs.attentions

             self.assertEqual(len(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       ):
         def check_hidden_states_output(snake_case_							,    snake_case_							,    snake_case_       ):
             _A    =       model_class(snake_case_       )
             model.to(snake_case_       )
             model.eval()

             with torch.no_grad():
                 _A    =       model(**self._prepare_for_class(snake_case_							,    snake_case_       )       )

             _A    =       outputs.hidden_states
             _A    =       self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(snake_case_       )							,    snake_case_       )

             _A    =       self.model_tester.seq_length

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

         _A, _A    =       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             _A    =       True
             check_hidden_states_output(snake_case_							,    snake_case_							,    snake_case_       )

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

             check_hidden_states_output(snake_case_							,    snake_case_							,    snake_case_       )




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





def 							__lowerCAmelCase(					)       ->   Tuple:




    """simple docstring"""




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







@require_torch
@require_vision
class     lowerCamelCase(	unittest.TestCase  ):
     '''simple docstring'''


     @cached_property
     def 					lowerCAmelCase__		(						self       ):
         return VivitImageProcessor() if is_vision_available() else None




     @slow
     def 					lowerCAmelCase__		(						self       ):
         _A    =       VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400'       ).to(snake_case_       )

         _A    =       self.default_image_processor
         _A    =       prepare_video()
         _A    =       image_processor(snake_case_							,    return_tensors='pt'       ).to(snake_case_       )

         # forward pass
         with torch.no_grad():
             _A    =       model(**snake_case_       )

         # verify the logits
         _A    =       torch.Size((1, 400)       )
         self.assertEqual(outputs.logits.shape							,    snake_case_       )

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

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












from transformers import SqueezeBertTokenizer, SqueezeBertTokenizerFast
from transformers.testing_utils import require_tokenizers, slow

from ..bert.test_tokenization_bert import BertTokenizationTest







@require_tokenizers
class     lowerCamelCase(	__snake_case  ):
     '''simple docstring'''


     __magic_name__        = SqueezeBertTokenizer
     __magic_name__        = SqueezeBertTokenizerFast
     __magic_name__        = True




     def 					lowerCAmelCase__		(						self							,    **snake_case_       ):
         return SqueezeBertTokenizerFast.from_pretrained(self.tmpdirname							,    **snake_case_       )




     @slow
     def 					lowerCAmelCase__		(						self       ):
         _A    =       SqueezeBertTokenizer.from_pretrained('squeezebert/squeezebert-mnli-headless'       )

         _A    =       tokenizer.encode('sequence builders'							,    add_special_tokens=snake_case_       )
         _A    =       tokenizer.encode('multi-sequence build'							,    add_special_tokens=snake_case_       )

         _A    =       tokenizer.build_inputs_with_special_tokens(snake_case_       )
         _A    =       tokenizer.build_inputs_with_special_tokens(snake_case_							,    snake_case_       )

         assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
         assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
             tokenizer.sep_token_id
         ]





