




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      _UpperCamelCase :
      def __init__(     self				:Optional[Any]	, lowerCamelCase				:Tuple	, lowerCamelCase				:List[Any]=2	, lowerCamelCase				:Optional[Any]=True	, lowerCamelCase				:Optional[Any]=True	, lowerCamelCase				:List[Any]=10	, lowerCamelCase				:Dict=10	, lowerCamelCase				:int=8	, lowerCamelCase				:Tuple=[2, 4, 4]	, lowerCamelCase				:Tuple=3	, lowerCamelCase				:List[Any]=768	, lowerCamelCase				:str=5	, lowerCamelCase				:int=4	, lowerCamelCase				:Union[str, Any]=37	, lowerCamelCase				:Dict="gelu_fast"	, lowerCamelCase				:Optional[Any]=0.0	, lowerCamelCase				:str=0.0	, lowerCamelCase				:Union[str, Any]=0.02	, lowerCamelCase				:Tuple=1e-06	, lowerCamelCase				:str=True	, lowerCamelCase				:Tuple=None	, )       ->     int:
       UpperCAmelCase__								=  parent
       UpperCAmelCase__								=  batch_size
       UpperCAmelCase__								=  is_training
       UpperCAmelCase__								=  use_labels
       UpperCAmelCase__								=  num_labels
       UpperCAmelCase__								=  image_size
       UpperCAmelCase__								=  num_frames
       UpperCAmelCase__								=  tubelet_size
       UpperCAmelCase__								=  num_channels
       UpperCAmelCase__								=  hidden_size
       UpperCAmelCase__								=  num_hidden_layers
       UpperCAmelCase__								=  num_attention_heads
       UpperCAmelCase__								=  intermediate_size
       UpperCAmelCase__								=  hidden_act
       UpperCAmelCase__								=  hidden_dropout_prob
       UpperCAmelCase__								=  attention_probs_dropout_prob
       UpperCAmelCase__								=  initializer_range
       UpperCAmelCase__								=  layer_norm_eps
       UpperCAmelCase__								=  qkv_bias
       UpperCAmelCase__								=  scope

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

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

       UpperCAmelCase__								=  self.get_config()

       return config, pixel_values, labels
      def 		UpperCAmelCase_  (     self				:int )       ->     int:
       UpperCAmelCase__								=  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	, )
       UpperCAmelCase__								=  self.num_labels
       return config
      def 		UpperCAmelCase_  (     self				:List[Any]	, lowerCamelCase				:List[Any]	, lowerCamelCase				:Dict	, lowerCamelCase				:Union[str, Any] )       ->     Dict:
       UpperCAmelCase__								=  VivitModel(config=lowerCamelCase )
       model.to(lowerCamelCase )
       model.eval()
       UpperCAmelCase__								=  model(lowerCamelCase )
       self.parent.assertEqual(result.last_hidden_state.shape	, (self.batch_size, self.seq_length, self.hidden_size) )
      def 		UpperCAmelCase_  (     self				:Tuple	, lowerCamelCase				:Optional[int]	, lowerCamelCase				:List[str]	, lowerCamelCase				:Union[str, Any] )       ->     Union[str, Any]:
       UpperCAmelCase__								=  VivitForVideoClassification(lowerCamelCase )
       model.to(lowerCamelCase )
       model.eval()

       UpperCAmelCase__								=  model(lowerCamelCase )

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







      def 		UpperCAmelCase_  (     self				:Dict )       ->     Union[str, Any]:
       UpperCAmelCase__								=  self.prepare_config_and_inputs()
       UpperCAmelCase__	,	UpperCAmelCase__	,	UpperCAmelCase__								=  config_and_inputs
       UpperCAmelCase__								=  {"pixel_values": pixel_values}
       return config, inputs_dict



@require_torch
class      _UpperCamelCase (			lowerCAmelCase		,    unittest.TestCase						):
      UpperCAmelCase_            =						(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

      UpperCAmelCase_            =						False
      UpperCAmelCase_            =						False
      UpperCAmelCase_            =						False
      UpperCAmelCase_            =						False
      def 		UpperCAmelCase_  (     self				:Dict )       ->     Tuple:
       UpperCAmelCase__								=  VivitModelTester(self )
       UpperCAmelCase__								=  ConfigTester(self	, config_class=lowerCamelCase	, has_text_modality=lowerCamelCase	, hidden_size=37 )
      def 		UpperCAmelCase_  (     self				:Any	, lowerCamelCase				:Tuple	, lowerCamelCase				:Optional[int]	, lowerCamelCase				:Optional[Any]=False )       ->     List[str]:
       UpperCAmelCase__								=  copy.deepcopy(lowerCamelCase )

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

       return inputs_dict
      def 		UpperCAmelCase_  (     self				:Tuple )       ->     Optional[Any]:
       self.config_tester.run_common_tests()
      @unittest.skip(reason="Vivit does not use inputs_embeds" )
      def 		UpperCAmelCase_  (     self				:int )       ->     Tuple:
       pass
      def 		UpperCAmelCase_  (     self				:List[str] )       ->     List[Any]:
       UpperCAmelCase__	,	UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__								=  model_class(lowerCamelCase )
        self.assertIsInstance(model.get_input_embeddings()	, (nn.Module) )
        UpperCAmelCase__								=  model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowerCamelCase	, nn.Linear ) )
      def 		UpperCAmelCase_  (     self				:Union[str, Any] )       ->     List[str]:
       UpperCAmelCase__	,	UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase__								=  ["pixel_values", "head_mask"]
        self.assertListEqual(arg_names[:2]	, lowerCamelCase )
      def 		UpperCAmelCase_  (     self				:Tuple )       ->     List[Any]:
       UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase )
      def 		UpperCAmelCase_  (     self				:int )       ->     Union[str, Any]:
       UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_video_classification(*lowerCamelCase )
      @slow
      def 		UpperCAmelCase_  (     self				:Optional[Any] )       ->     Dict:
       for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__								=  VivitModel.from_pretrained(lowerCamelCase )
        self.assertIsNotNone(lowerCamelCase )
      def 		UpperCAmelCase_  (     self				:int )       ->     Any:
       UpperCAmelCase__	,	UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase__								=  True

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

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

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

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

        UpperCAmelCase__								=  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 		UpperCAmelCase_  (     self				:Any )       ->     Dict:
       def check_hidden_states_output(lowerCamelCase				:Optional[Any]	, lowerCamelCase				:List[str]	, lowerCamelCase				:str ):
        UpperCAmelCase__								=  model_class(lowerCamelCase )
        model.to(lowerCamelCase )
        model.eval()

        with torch.no_grad():
         UpperCAmelCase__								=  model(**self._prepare_for_class(lowerCamelCase	, lowerCamelCase ) )

        UpperCAmelCase__								=  outputs.hidden_states
        UpperCAmelCase__								=  self.model_tester.num_hidden_layers + 1
        self.assertEqual(len(lowerCamelCase )	, lowerCamelCase )

        UpperCAmelCase__								=  self.model_tester.seq_length

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

       UpperCAmelCase__	,	UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCAmelCase__								=  True
        check_hidden_states_output(lowerCamelCase	, lowerCamelCase	, lowerCamelCase )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        UpperCAmelCase__								=  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 		UpperCAmelCase_  (     self				:List[Any] )       ->     str:
       pass



def   lowerCAmelCase						(						):




 """simple docstring"""







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


@require_torch
@require_vision
class      _UpperCamelCase (			unittest.TestCase						):
      @cached_property
      def 		UpperCAmelCase_  (     self				:List[Any] )       ->     Optional[Any]:
       return VivitImageProcessor() if is_vision_available() else None







      @slow
      def 		UpperCAmelCase_  (     self				:int )       ->     List[str]:
       UpperCAmelCase__								=  VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400" ).to(lowerCamelCase )

       UpperCAmelCase__								=  self.default_image_processor
       UpperCAmelCase__								=  prepare_video()
       UpperCAmelCase__								=  image_processor(lowerCamelCase	, return_tensors="pt" ).to(lowerCamelCase )

       # forward pass
       with torch.no_grad():
        UpperCAmelCase__								=  model(**lowerCamelCase )

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

       # taken from original model
       UpperCAmelCase__								=  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 ) )











from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCAmelCase  :				Dict						=				logging.get_logger(__name__)

_lowerCAmelCase  :				Optional[int]						=				{
    "google/rembert": "https://huggingface.co/google/rembert/resolve/main/config.json",
    # See all RemBERT models at https://huggingface.co/models?filter=rembert
}
class      _UpperCamelCase (			lowerCAmelCase						):
      UpperCAmelCase_            =						"""rembert"""
      def __init__(     self				:Union[str, Any]	, lowerCamelCase				:Union[str, Any]=25_0300	, lowerCamelCase				:Optional[Any]=1152	, lowerCamelCase				:Optional[int]=32	, lowerCamelCase				:Union[str, Any]=18	, lowerCamelCase				:str=256	, lowerCamelCase				:Any=1664	, lowerCamelCase				:Any=4608	, lowerCamelCase				:List[str]="gelu"	, lowerCamelCase				:Optional[Any]=0.0	, lowerCamelCase				:Optional[int]=0.0	, lowerCamelCase				:Dict=0.1	, lowerCamelCase				:List[str]=512	, lowerCamelCase				:int=2	, lowerCamelCase				:str=0.02	, lowerCamelCase				:Optional[int]=1e-12	, lowerCamelCase				:List[Any]=True	, lowerCamelCase				:Optional[Any]=0	, lowerCamelCase				:Optional[Any]=312	, lowerCamelCase				:int=313	, **lowerCamelCase				:Dict	, )       ->     Any:
       super().__init__(pad_token_id=lowerCamelCase	, bos_token_id=lowerCamelCase	, eos_token_id=lowerCamelCase	, **lowerCamelCase )

       UpperCAmelCase__								=  vocab_size
       UpperCAmelCase__								=  input_embedding_size
       UpperCAmelCase__								=  output_embedding_size
       UpperCAmelCase__								=  max_position_embeddings
       UpperCAmelCase__								=  hidden_size
       UpperCAmelCase__								=  num_hidden_layers
       UpperCAmelCase__								=  num_attention_heads
       UpperCAmelCase__								=  intermediate_size
       UpperCAmelCase__								=  hidden_act
       UpperCAmelCase__								=  hidden_dropout_prob
       UpperCAmelCase__								=  attention_probs_dropout_prob
       UpperCAmelCase__								=  classifier_dropout_prob
       UpperCAmelCase__								=  initializer_range
       UpperCAmelCase__								=  type_vocab_size
       UpperCAmelCase__								=  layer_norm_eps
       UpperCAmelCase__								=  use_cache
       UpperCAmelCase__								=  False



class      _UpperCamelCase (			lowerCAmelCase						):
      @property
      def 		UpperCAmelCase_  (     self				:List[Any] )       ->     Mapping[str, Mapping[int, str]]:
       if self.task == "multiple-choice":
        UpperCAmelCase__								=  {0: "batch", 1: "choice", 2: "sequence"}
       else:
        UpperCAmelCase__								=  {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
               ("token_type_ids", dynamic_axis),
           ] )







      @property
      def 		UpperCAmelCase_  (     self				:Union[str, Any] )       ->     float:
       return 1e-4





