


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			:


       """simple docstring"""




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


             '''simple docstring'''



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

             lowerCamelCase__		:    int   =  (
                 (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							:      Tuple	):


             '''simple docstring'''



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

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

             lowerCamelCase__		:    str   =  self.get_config()

             return config, pixel_values, labels





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[int]   =  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__		:    Dict   =  self.num_labels
             return config





       def 						lowerCAmelCase					(       self							:      List[str] ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  VivitModel(config=__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()
             lowerCamelCase__		:    Union[str, Any]   =  model(__lowerCamelCase	)
             self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)	)





       def 						lowerCAmelCase					(       self							:      List[Any] ,				__lowerCamelCase							:      int ,				__lowerCamelCase							:      Any ,				__lowerCamelCase							:      Tuple	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  VivitForVideoClassification(__lowerCamelCase	)
             model.to(__lowerCamelCase	)
             model.eval()

             lowerCamelCase__		:    Tuple   =  model(__lowerCamelCase	)

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





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    List[Any]   =  self.prepare_config_and_inputs()
             lowerCamelCase__		:    Any   =  config_and_inputs
             lowerCamelCase__		:    List[str]   =  {"pixel_values": pixel_values}
             return config, inputs_dict







@require_torch
class       _lowercase			(							lowercase__							,  unittest.TestCase):


       """simple docstring"""




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

       A__ 						=      False
       A__ 						=      False
       A__ 						=      False
       A__ 						=      False





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             lowerCamelCase__		:    List[str]   =  VivitModelTester(self	)
             lowerCamelCase__		:    Tuple   =  ConfigTester(self ,				config_class=__lowerCamelCase ,				has_text_modality=__lowerCamelCase ,				hidden_size=37	)





       def 						lowerCAmelCase					(       self							:      Dict ,				__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      List[str]=False	):


             '''simple docstring'''



             lowerCamelCase__		:    Tuple   =  copy.deepcopy(__lowerCamelCase	)

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

             return inputs_dict





       def 						lowerCAmelCase					(       self							:      Optional[int]	):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       @unittest.skip(reason="Vivit does not use inputs_embeds"	)
       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



             pass





       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



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

             for model_class in self.all_model_classes:
                   lowerCamelCase__		:    Union[str, Any]   =  model_class(__lowerCamelCase	)
                   self.assertIsInstance(model.get_input_embeddings() ,				(nn.Module)	)
                   lowerCamelCase__		:    str   =  model.get_output_embeddings()
                   self.assertTrue(x is None or isinstance(__lowerCamelCase ,				nn.Linear	)	)





       def 						lowerCAmelCase					(       self							:      str	):


             '''simple docstring'''



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

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

                   lowerCamelCase__		:    List[Any]   =  ["pixel_values", "head_mask"]
                   self.assertListEqual(arg_names[:2] ,				__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase	)





       def 						lowerCAmelCase					(       self							:      Optional[int]	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_video_classification(*__lowerCamelCase	)





       @slow
       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



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





       def 						lowerCAmelCase					(       self							:      Optional[int]	):


             '''simple docstring'''



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

             for model_class in self.all_model_classes:
                   lowerCamelCase__		:    Optional[int]   =  self.model_tester.seq_length

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

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

                   self.assertListEqual(
                       list(attentions[0].shape[-3:]	) ,				[self.model_tester.num_attention_heads, seq_len, seq_len] ,				)
                   lowerCamelCase__		:    List[str]   =  len(__lowerCamelCase	)

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

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

                   lowerCamelCase__		:    Tuple   =  outputs.attentions

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


             '''simple docstring'''



             def check_hidden_states_output(__lowerCamelCase							:      List[Any] ,				__lowerCamelCase							:      Dict ,				__lowerCamelCase							:      Any	):
                   lowerCamelCase__		:    List[str]   =  model_class(__lowerCamelCase	)
                   model.to(__lowerCamelCase	)
                   model.eval()

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

                   lowerCamelCase__		:    Tuple   =  outputs.hidden_states
                   lowerCamelCase__		:    str   =  self.model_tester.num_hidden_layers + 1
                   self.assertEqual(len(__lowerCamelCase	) ,				__lowerCamelCase	)

                   lowerCamelCase__		:    int   =  self.model_tester.seq_length

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

             lowerCamelCase__		:    str   =  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   lowerCamelCase__		:    Union[str, Any]   =  True
                   check_hidden_states_output(__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase	)

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

                   check_hidden_states_output(__lowerCamelCase ,				__lowerCamelCase ,				__lowerCamelCase	)





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


             '''simple docstring'''



             pass



def 				lowercase_		(		):

      """simple docstring"""




      lowerCamelCase__		:    Union[str, 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       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       @cached_property
       def 						lowerCAmelCase					(       self							:      Tuple	):


             '''simple docstring'''



             return VivitImageProcessor() if is_vision_available() else None





       @slow
       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"	).to(__lowerCamelCase	)

             lowerCamelCase__		:    Optional[Any]   =  self.default_image_processor
             lowerCamelCase__		:    Union[str, Any]   =  prepare_video()
             lowerCamelCase__		:    Union[str, Any]   =  image_processor(__lowerCamelCase ,				return_tensors="pt"	).to(__lowerCamelCase	)

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

             # verify the logits
             lowerCamelCase__		:    Optional[int]   =  torch.Size((1, 400)	)
             self.assertEqual(outputs.logits.shape ,				__lowerCamelCase	)

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

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






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import EfficientNetImageProcessor







class       _lowercase			(							unittest.TestCase):


       """simple docstring"""




       def __init__(       self							:      Optional[Any] ,				__lowerCamelCase							:      List[str] ,				__lowerCamelCase							:      int=13 ,				__lowerCamelCase							:      Tuple=3 ,				__lowerCamelCase							:      Optional[Any]=18 ,				__lowerCamelCase							:      List[Any]=30 ,				__lowerCamelCase							:      Union[str, Any]=400 ,				__lowerCamelCase							:      Union[str, Any]=True ,				__lowerCamelCase							:      Union[str, Any]=None ,				__lowerCamelCase							:      Optional[Any]=True ,				__lowerCamelCase							:      str=[0.5, 0.5, 0.5] ,				__lowerCamelCase							:      Dict=[0.5, 0.5, 0.5] ,				):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  size if size is not None else {"height": 18, "width": 18}
             lowerCamelCase__		:    Dict   =  parent
             lowerCamelCase__		:    str   =  batch_size
             lowerCamelCase__		:    Tuple   =  num_channels
             lowerCamelCase__		:    Tuple   =  image_size
             lowerCamelCase__		:    Any   =  min_resolution
             lowerCamelCase__		:    Dict   =  max_resolution
             lowerCamelCase__		:    Tuple   =  do_resize
             lowerCamelCase__		:    List[str]   =  size
             lowerCamelCase__		:    Optional[Any]   =  do_normalize
             lowerCamelCase__		:    Union[str, Any]   =  image_mean
             lowerCamelCase__		:    Optional[Any]   =  image_std





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             return {
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
                 "do_normalize": self.do_normalize,
                 "do_resize": self.do_resize,
                 "size": self.size,
             }







@require_torch
@require_vision
class       _lowercase			(							lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__ 						=      EfficientNetImageProcessor if is_vision_available() else None





       def 						lowerCAmelCase					(       self							:      Optional[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  EfficientNetImageProcessorTester(self	)





       @property
       def 						lowerCAmelCase					(       self							:      Union[str, Any]	):


             '''simple docstring'''



             return self.image_processor_tester.prepare_image_processor_dict()





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Union[str, Any]   =  self.image_processing_class(**self.image_processor_dict	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_mean"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"image_std"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_normalize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"do_resize"	)	)
             self.assertTrue(hasattr(__lowerCamelCase ,				"size"	)	)





       def 						lowerCAmelCase					(       self							:      Any	):


             '''simple docstring'''



             lowerCamelCase__		:    Optional[Any]   =  self.image_processing_class.from_dict(self.image_processor_dict	)
             self.assertEqual(image_processor.size ,				{"height": 18, "width": 18}	)

             lowerCamelCase__		:    Optional[int]   =  self.image_processing_class.from_dict(self.image_processor_dict ,				size=42	)
             self.assertEqual(image_processor.size ,				{"height": 42, "width": 42}	)





       def 						lowerCAmelCase					(       self							:      Dict	):


             '''simple docstring'''



             lowerCamelCase__		:    Any   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PIL images
             lowerCamelCase__		:    Any   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				Image.Image	)

             # Test not batched input
             lowerCamelCase__		:    Union[str, Any]   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Dict   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[Any]	):


             '''simple docstring'''



             lowerCamelCase__		:    Dict   =  self.image_processing_class(**self.image_processor_dict	)
             # create random numpy tensors
             lowerCamelCase__		:    List[Any]   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase ,				numpify=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				np.ndarray	)

             # Test not batched input
             lowerCamelCase__		:    List[str]   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    Union[str, Any]   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)





       def 						lowerCAmelCase					(       self							:      List[str]	):


             '''simple docstring'''



             lowerCamelCase__		:    int   =  self.image_processing_class(**self.image_processor_dict	)
             # create random PyTorch tensors
             lowerCamelCase__		:    int   =  prepare_image_inputs(self.image_processor_tester ,				equal_resolution=__lowerCamelCase ,				torchify=__lowerCamelCase	)
             for image in image_inputs:
                   self.assertIsInstance(__lowerCamelCase ,				torch.Tensor	)

             # Test not batched input
             lowerCamelCase__		:    Dict   =  image_processing(image_inputs[0] ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     1,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)

             # Test batched
             lowerCamelCase__		:    List[Any]   =  image_processing(__lowerCamelCase ,				return_tensors="pt"	).pixel_values
             self.assertEqual(
                 encoded_images.shape ,				(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.size["height"],
                     self.image_processor_tester.size["width"],
                 ) ,				)


