






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







     def __init__(self     :						str					,					lowercase__     :						Dict					,					lowercase__     :						Optional[Any]=2					,					lowercase__     :						str=True					,					lowercase__     :						Dict=True					,					lowercase__     :						str=10					,					lowercase__     :						Optional[int]=10					,					lowercase__     :						Dict=8					,					lowercase__     :						List[str]=[2, 4, 4]					,					lowercase__     :						int=3					,					lowercase__     :						List[str]=768					,					lowercase__     :						Optional[Any]=5					,					lowercase__     :						Optional[Any]=4					,					lowercase__     :						Tuple=37					,					lowercase__     :						List[str]="gelu_fast"					,					lowercase__     :						Dict=0.0					,					lowercase__     :						Dict=0.0					,					lowercase__     :						Dict=0.02					,					lowercase__     :						Union[str, Any]=1e-06					,					lowercase__     :						Union[str, Any]=True					,					lowercase__     :						Optional[int]=None					,					)	->							Union[str, Any]:
           __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      _UpperCamelCase (self     :						Tuple       )	->							int:
           __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      _UpperCamelCase (self     :						Union[str, Any]       )	->							List[Any]:
           __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      _UpperCamelCase (self     :						str					,					lowercase__     :						Optional[int]					,					lowercase__     :						int					,					lowercase__     :						Tuple       )	->							List[Any]:
           __A		 =   VivitModel(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           __A		 =   model(lowercase__       )
           self.parent.assertEqual(result.last_hidden_state.shape					,					(self.batch_size, self.seq_length, self.hidden_size)       )






     def      _UpperCamelCase (self     :						Any					,					lowercase__     :						Dict					,					lowercase__     :						Tuple					,					lowercase__     :						Optional[Any]       )	->							Tuple:
           __A		 =   VivitForVideoClassification(lowercase__       )
           model.to(lowercase__       )
           model.eval()

           __A		 =   model(lowercase__       )

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






     def      _UpperCamelCase (self     :						Optional[Any]       )	->							Tuple:
           __A		 =   self.prepare_config_and_inputs()
           __A ,					__A ,					__A		 =   config_and_inputs
           __A		 =   {"pixel_values": pixel_values}
           return config, inputs_dict







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







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

     _snake_case							   =	False
     _snake_case							   =	False
     _snake_case							   =	False
     _snake_case							   =	False






     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							str:
           __A		 =   VivitModelTester(self       )
           __A		 =   ConfigTester(self					,					config_class=lowercase__					,					has_text_modality=lowercase__					,					hidden_size=37       )






     def      _UpperCamelCase (self     :						List[Any]					,					lowercase__     :						Union[str, Any]					,					lowercase__     :						int					,					lowercase__     :						Tuple=False       )	->							Union[str, Any]:
           __A		 =   copy.deepcopy(lowercase__       )

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

           return inputs_dict






     def      _UpperCamelCase (self     :						List[Any]       )	->							Any:
           self.config_tester.run_common_tests()






     @unittest.skip(reason="Vivit does not use inputs_embeds"       )
     def      _UpperCamelCase (self     :						int       )	->							Dict:
           pass






     def      _UpperCamelCase (self     :						int       )	->							str:
           __A ,					__A		 =   self.model_tester.prepare_config_and_inputs_for_common()

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






     def      _UpperCamelCase (self     :						Dict       )	->							Optional[int]:
           __A ,					__A		 =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 __A		 =   model_class(lowercase__       )
                 __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]					,					lowercase__       )






     def      _UpperCamelCase (self     :						List[str]       )	->							Union[str, Any]:
           __A		 =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase__       )






     def      _UpperCamelCase (self     :						int       )	->							Tuple:
           __A		 =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_video_classification(*lowercase__       )






     @slow
     def      _UpperCamelCase (self     :						Optional[int]       )	->							Optional[Any]:
           for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __A		 =   VivitModel.from_pretrained(lowercase__       )
                 self.assertIsNotNone(lowercase__       )






     def      _UpperCamelCase (self     :						int       )	->							Optional[int]:
           __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(lowercase__       )
                 model.to(lowercase__       )
                 model.eval()
                 with torch.no_grad():
                       __A		 =   model(**self._prepare_for_class(lowercase__					,					lowercase__       )       )
                 __A		 =   outputs.attentions
                 self.assertEqual(len(lowercase__       )					,					self.model_tester.num_hidden_layers       )

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

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

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

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

                 __A		 =   outputs.attentions

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

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






     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							Tuple:
           def check_hidden_states_output(lowercase__     :						Union[str, Any]					,					lowercase__     :						int					,					lowercase__     :						List[str]       ):
                 __A		 =   model_class(lowercase__       )
                 model.to(lowercase__       )
                 model.eval()

                 with torch.no_grad():
                       __A		 =   model(**self._prepare_for_class(lowercase__					,					lowercase__       )       )

                 __A		 =   outputs.hidden_states
                 __A		 =   self.model_tester.num_hidden_layers + 1
                 self.assertEqual(len(lowercase__       )					,					lowercase__       )

                 __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(lowercase__					,					lowercase__					,					lowercase__       )

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

                 check_hidden_states_output(lowercase__					,					lowercase__					,					lowercase__       )






     @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."       )
     def      _UpperCamelCase (self     :						Tuple       )	->							Optional[Any]:
           pass







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






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







     @cached_property
     def      _UpperCamelCase (self     :						Tuple       )	->							str:
           return VivitImageProcessor() if is_vision_available() else None






     @slow
     def      _UpperCamelCase (self     :						Optional[Any]       )	->							Optional[int]:
           __A		 =   VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400"       ).to(lowercase__       )

           __A		 =   self.default_image_processor
           __A		 =   prepare_video()
           __A		 =   image_processor(lowercase__					,					return_tensors="pt"       ).to(lowercase__       )

           # forward pass
           with torch.no_grad():
                 __A		 =   model(**lowercase__       )

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

           # taken from original model
           __A		 =   torch.tensor([-1.05_43, 2.07_64, -0.21_04, 0.44_39, -0.96_58]       ).to(lowercase__       )

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










'''simple docstring'''







import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment






class        a__     (      unittest.TestCase    ):
     '''simple docstring'''







     def      _UpperCamelCase (self     :						int       )	->							Optional[int]:
           __A		 =   inspect.getfile(accelerate.test_utils       )
           __A		 =   os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["scripts", "test_sync.py"]       )






     @require_cpu
     def      _UpperCamelCase (self     :						str       )	->							Optional[Any]:
           debug_launcher(test_sync.main					,					num_processes=1       )






     @require_cpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							Optional[Any]:
           debug_launcher(test_sync.main       )






     @require_single_gpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							List[str]:
           test_sync.main()






     @require_multi_gpu
     def      _UpperCamelCase (self     :						Optional[int]       )	->							List[str]:
           print(f"Found {torch.cuda.device_count()} devices."       )
           __A		 =   ["torchrun", f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
           with patch_environment(omp_num_threads=1       ):
                 execute_subprocess_async(lowercase__					,					env=os.environ.copy()       )


