




'''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 	SCREAMING_SNAKE_CASE:

       def __init__(   self ,		lowerCamelCase__ ,		lowerCamelCase__=2 ,		lowerCamelCase__=True ,		lowerCamelCase__=True ,		lowerCamelCase__=10 ,		lowerCamelCase__=10 ,		lowerCamelCase__=8 ,		lowerCamelCase__=[2, 4, 4] ,		lowerCamelCase__=3 ,		lowerCamelCase__=768 ,		lowerCamelCase__=5 ,		lowerCamelCase__=4 ,		lowerCamelCase__=37 ,		lowerCamelCase__="gelu_fast" ,		lowerCamelCase__=0.0 ,		lowerCamelCase__=0.0 ,		lowerCamelCase__=0.02 ,		lowerCamelCase__=1E-0_6 ,		lowerCamelCase__=True ,		lowerCamelCase__=None ,		)   -> Tuple:

           """simple docstring"""



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

           __lowercase          =						(
               (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    snake_case__      (   self					)   -> Any:

           """simple docstring"""



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

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

           __lowercase          =						self.get_config()

           return config, pixel_values, labels

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

           """simple docstring"""



           __lowercase          =						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 ,		)
           __lowercase          =						self.num_labels
           return config

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__					)   -> Tuple:

           """simple docstring"""



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

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__					)   -> List[Any]:

           """simple docstring"""



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

           __lowercase          =						model(lowerCamelCase__					)

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




       def    snake_case__      (   self					)   -> Any:

           """simple docstring"""



           __lowercase          =						self.prepare_config_and_inputs()
           __lowercase    ,__lowercase    ,__lowercase          =						config_and_inputs
           __lowercase          =						{"""pixel_values""": pixel_values}
           return config, inputs_dict






@require_torch
class 	SCREAMING_SNAKE_CASE(    __A       ,				unittest.TestCase					):
       snake_case_			:					Tuple   = (VivitModel, VivitForVideoClassification) if is_torch_available() else ()

       snake_case_			:					str   = False
       snake_case_			:					Union[str, Any]   = False
       snake_case_			:					Optional[Any]   = False
       snake_case_			:					Dict   = False

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

           """simple docstring"""



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

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__=False					)   -> Optional[Any]:

           """simple docstring"""



           __lowercase          =						copy.deepcopy(lowerCamelCase__					)

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

           return inputs_dict

       def    snake_case__      (   self					)   -> Dict:

           """simple docstring"""



           self.config_tester.run_common_tests()

       @unittest.skip(reason="""Vivit does not use inputs_embeds"""					)
       def    snake_case__      (   self					)   -> Any:

           """simple docstring"""



           pass

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

           """simple docstring"""



           __lowercase    ,__lowercase          =						self.model_tester.prepare_config_and_inputs_for_common()

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

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

           """simple docstring"""



           __lowercase    ,__lowercase          =						self.model_tester.prepare_config_and_inputs_for_common()

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

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

       def    snake_case__      (   self					)   -> Any:

           """simple docstring"""



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

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

           """simple docstring"""



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

       @slow
       def    snake_case__      (   self					)   -> Optional[int]:

           """simple docstring"""



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

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

           """simple docstring"""



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

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

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

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

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

               __lowercase          =						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    snake_case__      (   self					)   -> Dict:

           """simple docstring"""



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

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

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

               __lowercase          =						self.model_tester.seq_length

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

           __lowercase    ,__lowercase          =						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               __lowercase          =						True
               check_hidden_states_output(lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__					)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __lowercase          =						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    snake_case__      (   self					)   -> Union[str, Any]:

           """simple docstring"""



           pass

def     snake_case_  (       ):



    """simple docstring"""




    __lowercase          =						hf_hub_download(
        repo_id="""hf-internal-testing/spaghetti-video"""   ,filename="""eating_spaghetti_32_frames.npy"""   ,repo_type="""dataset"""      )
    __lowercase          =						np.load(a__      )
    return list(a__      )






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

       @cached_property
       def    snake_case__      (   self					)   -> List[str]:

           """simple docstring"""



           return VivitImageProcessor() if is_vision_available() else None




       @slow
       def    snake_case__      (   self					)   -> Optional[int]:

           """simple docstring"""



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

           __lowercase          =						self.default_image_processor
           __lowercase          =						prepare_video()
           __lowercase          =						image_processor(lowerCamelCase__ ,		return_tensors="""pt"""					).to(lowerCamelCase__					)

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

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

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

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










'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A							:				Tuple			     =	{
    """configuration_dpr""": ["""DPR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DPRConfig"""],
    """tokenization_dpr""": [
        """DPRContextEncoderTokenizer""",
        """DPRQuestionEncoderTokenizer""",
        """DPRReaderOutput""",
        """DPRReaderTokenizer""",
    ],
}


try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				str			     =	[
      """DPRContextEncoderTokenizerFast""",
      """DPRQuestionEncoderTokenizerFast""",
      """DPRReaderTokenizerFast""",
  ]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				Tuple			     =	[
      """DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPRContextEncoder""",
      """DPRPretrainedContextEncoder""",
      """DPRPreTrainedModel""",
      """DPRPretrainedQuestionEncoder""",
      """DPRPretrainedReader""",
      """DPRQuestionEncoder""",
      """DPRReader""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				Tuple			     =	[
      """TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFDPRContextEncoder""",
      """TFDPRPretrainedContextEncoder""",
      """TFDPRPretrainedQuestionEncoder""",
      """TFDPRPretrainedReader""",
      """TFDPRQuestionEncoder""",
      """TFDPRReader""",
  ]


if TYPE_CHECKING:
  from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
  from .tokenization_dpr import (
      DPRContextEncoderTokenizer,
      DPRQuestionEncoderTokenizer,
      DPRReaderOutput,
      DPRReaderTokenizer,
  )

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_dpr_fast import (
        DPRContextEncoderTokenizerFast,
        DPRQuestionEncoderTokenizerFast,
        DPRReaderTokenizerFast,
    )

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_dpr import (
        DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPRContextEncoder,
        DPRPretrainedContextEncoder,
        DPRPreTrainedModel,
        DPRPretrainedQuestionEncoder,
        DPRPretrainedReader,
        DPRQuestionEncoder,
        DPRReader,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_dpr import (
        TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFDPRContextEncoder,
        TFDPRPretrainedContextEncoder,
        TFDPRPretrainedQuestionEncoder,
        TFDPRPretrainedReader,
        TFDPRQuestionEncoder,
        TFDPRReader,
    )

else:
  import sys

  A							:				int			     =	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




