


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 						A__      :







    """simple docstring"""

    def __init__( self    ,      __snake_case    ,      __snake_case=2    ,      __snake_case=True    ,      __snake_case=True    ,      __snake_case=1_0    ,      __snake_case=1_0    ,      __snake_case=8    ,      __snake_case=[2, 4, 4]    ,      __snake_case=3    ,      __snake_case=7_6_8    ,      __snake_case=5    ,      __snake_case=4    ,      __snake_case=3_7    ,      __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    ,      ):
         snake_case					=       parent
         snake_case					=       batch_size
         snake_case					=       is_training
         snake_case					=       use_labels
         snake_case					=       num_labels
         snake_case					=       image_size
         snake_case					=       num_frames
         snake_case					=       tubelet_size
         snake_case					=       num_channels
         snake_case					=       hidden_size
         snake_case					=       num_hidden_layers
         snake_case					=       num_attention_heads
         snake_case					=       intermediate_size
         snake_case					=       hidden_act
         snake_case					=       hidden_dropout_prob
         snake_case					=       attention_probs_dropout_prob
         snake_case					=       initializer_range
         snake_case					=       layer_norm_eps
         snake_case					=       qkv_bias
         snake_case					=       scope

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

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

         snake_case					=       self.get_config()

         return config, pixel_values, labels
    def 	a_    ( self					):
         snake_case					=       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    ,      )
         snake_case					=       self.num_labels
         return config
    def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
         snake_case					=       VivitModel(config=UpperCamelCase__					)
         model.to(UpperCamelCase__					)
         model.eval()
         snake_case					=       model(UpperCamelCase__					)
         self.parent.assertEqual(result.last_hidden_state.shape    ,      (self.batch_size, self.seq_length, self.hidden_size)					)
    def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
         snake_case					=       VivitForVideoClassification(UpperCamelCase__					)
         model.to(UpperCamelCase__					)
         model.eval()

         snake_case					=       model(UpperCamelCase__					)

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




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






@require_torch
class 						A__      (					__SCREAMING_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 	a_    ( self					):
         snake_case					=       VivitModelTester(self					)
         snake_case					=       ConfigTester(self    ,      config_class=UpperCamelCase__    ,      has_text_modality=UpperCamelCase__    ,      hidden_size=3_7					)
    def 	a_    ( self    ,      __snake_case    ,      __snake_case    ,      __snake_case=False					):
         snake_case					=       copy.deepcopy(UpperCamelCase__					)

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

         return inputs_dict
    def 	a_    ( self					):
         self.config_tester.run_common_tests()
    @unittest.skip(reason='''Vivit does not use inputs_embeds'''					)
    def 	a_    ( self					):
         pass
    def 	a_    ( self					):
         snake_case					=       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              snake_case					=       model_class(UpperCamelCase__					)
              self.assertIsInstance(model.get_input_embeddings()    ,      (nn.Module)					)
              snake_case					=       model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(UpperCamelCase__    ,      nn.Linear					)					)
    def 	a_    ( self					):
         snake_case					=       self.model_tester.prepare_config_and_inputs_for_common()

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

              snake_case					=       ['''pixel_values''', '''head_mask''']
              self.assertListEqual(arg_names[:2]    ,      UpperCamelCase__					)
    def 	a_    ( self					):
         snake_case					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase__					)
    def 	a_    ( self					):
         snake_case					=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_video_classification(*UpperCamelCase__					)
    @slow
    def 	a_    ( self					):







         for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              snake_case					=       VivitModel.from_pretrained(UpperCamelCase__					)
              self.assertIsNotNone(UpperCamelCase__					)
    def 	a_    ( self					):
         snake_case					=       self.model_tester.prepare_config_and_inputs_for_common()
         snake_case					=       True

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

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

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

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

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

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

              snake_case					=       outputs.attentions

              self.assertEqual(len(UpperCamelCase__					)    ,      self.model_tester.num_hidden_layers					)

              self.assertListEqual(
                  list(self_attentions[0].shape[-3:]					)    ,      [self.model_tester.num_attention_heads, seq_len, seq_len]    ,      )
    def 	a_    ( self					):







         def check_hidden_states_output(__snake_case    ,      __snake_case    ,      __snake_case					):
              snake_case					=       model_class(UpperCamelCase__					)
              model.to(UpperCamelCase__					)
              model.eval()

              with torch.no_grad():
                   snake_case					=       model(**self._prepare_for_class(UpperCamelCase__    ,      UpperCamelCase__					)					)

              snake_case					=       outputs.hidden_states
              snake_case					=       self.model_tester.num_hidden_layers + 1
              self.assertEqual(len(UpperCamelCase__					)    ,      UpperCamelCase__					)

              snake_case					=       self.model_tester.seq_length

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

         snake_case					=       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              snake_case					=       True
              check_hidden_states_output(UpperCamelCase__    ,      UpperCamelCase__    ,      UpperCamelCase__					)

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

              check_hidden_states_output(UpperCamelCase__    ,      UpperCamelCase__    ,      UpperCamelCase__					)




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






def     UpperCAmelCase__		():



     """simple docstring"""






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

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







    """simple docstring"""

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




    @slow
    def 	a_    ( self					):
         snake_case					=       VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''					).to(UpperCamelCase__					)

         snake_case					=       self.default_image_processor
         snake_case					=       prepare_video()
         snake_case					=       image_processor(UpperCamelCase__    ,      return_tensors='''pt'''					).to(UpperCamelCase__					)

         # forward pass
         with torch.no_grad():
              snake_case					=       model(**UpperCamelCase__					)

         # verify the logits
         snake_case					=       torch.Size((1, 4_0_0)					)
         self.assertEqual(outputs.logits.shape    ,      UpperCamelCase__					)

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

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







import torch

from accelerate import Accelerator, DistributedType

class 						A__      :







    """simple docstring"""

    def __enter__( self					):
         if self.enabled:
              snake_case					=       self.model.no_sync()
              self.model_sync_obj.__enter__()

         return self
    def __exit__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
         if self.enabled:
              # Average all models on exit
              self._sync_and_avg_model_params()
              self.model_sync_obj.__exit__(__snake_case    ,      __snake_case    ,      __snake_case					)
    def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = True					):
         if accelerator.distributed_type not in [
             DistributedType.NO,
             DistributedType.MULTI_CPU,
             DistributedType.MULTI_GPU,
         ]:
              raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''					)
         snake_case					=       enabled and accelerator.distributed_type != DistributedType.NO
         snake_case					=       0
         if self.enabled:
              snake_case					=       accelerator
              snake_case					=       model
              snake_case					=       local_sgd_steps
    def 	a_    ( self					):
         self.num_steps += 1
         if not self.enabled:
              return

         if self.num_steps % self.local_sgd_steps == 0:
              self._sync_and_avg_model_params()




    def 	a_    ( self					):
         self.accelerator.wait_for_everyone()
         with self.accelerator.autocast():
              for param in self.model.parameters():
                   snake_case					=       self.accelerator.reduce(param.data    ,      reduction='''mean'''					)




