

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

  def __init__(       self    ,		_A    ,		_A=2    ,		_A=True    ,		_A=True    ,		_A=10    ,		_A=10    ,		_A=8    ,		_A=[2, 4, 4]    ,		_A=3    ,		_A=768    ,		_A=5    ,		_A=4    ,		_A=37    ,		_A="gelu_fast"    ,		_A=0.0    ,		_A=0.0    ,		_A=0.0_2    ,		_A=1E-06    ,		_A=True    ,		_A=None    ,		):
      SCREAMING_SNAKE_CASE_						=    parent
      SCREAMING_SNAKE_CASE_						=    batch_size
      SCREAMING_SNAKE_CASE_						=    is_training
      SCREAMING_SNAKE_CASE_						=    use_labels
      SCREAMING_SNAKE_CASE_						=    num_labels
      SCREAMING_SNAKE_CASE_						=    image_size
      SCREAMING_SNAKE_CASE_						=    num_frames
      SCREAMING_SNAKE_CASE_						=    tubelet_size
      SCREAMING_SNAKE_CASE_						=    num_channels
      SCREAMING_SNAKE_CASE_						=    hidden_size
      SCREAMING_SNAKE_CASE_						=    num_hidden_layers
      SCREAMING_SNAKE_CASE_						=    num_attention_heads
      SCREAMING_SNAKE_CASE_						=    intermediate_size
      SCREAMING_SNAKE_CASE_						=    hidden_act
      SCREAMING_SNAKE_CASE_						=    hidden_dropout_prob
      SCREAMING_SNAKE_CASE_						=    attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_						=    initializer_range
      SCREAMING_SNAKE_CASE_						=    layer_norm_eps
      SCREAMING_SNAKE_CASE_						=    qkv_bias
      SCREAMING_SNAKE_CASE_						=    scope

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

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

      SCREAMING_SNAKE_CASE_						=    self.get_config()

      return config, pixel_values, labels

  def     lowerCAmelCase__  (       self):
      SCREAMING_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    ,		)
      SCREAMING_SNAKE_CASE_						=    self.num_labels
      return config

  def     lowerCAmelCase__  (       self    ,		_A    ,		_A    ,		_A):
      SCREAMING_SNAKE_CASE_						=    VivitModel(config=_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_						=    model(_A)
      self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size))

  def     lowerCAmelCase__  (       self    ,		_A    ,		_A    ,		_A):
      SCREAMING_SNAKE_CASE_						=    VivitForVideoClassification(_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_						=    model(_A)

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


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






@require_torch
class    __snake_case	(     lowerCAmelCase__  ,			unittest.TestCase							):
  __lowerCAmelCase  :						List[Any]    			=	(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

  __lowerCAmelCase  :						Tuple    			=	False
  __lowerCAmelCase  :						int    			=	False
  __lowerCAmelCase  :						Optional[Any]    			=	False
  __lowerCAmelCase  :						Any    			=	False

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    VivitModelTester(self)
      SCREAMING_SNAKE_CASE_						=    ConfigTester(self    ,		config_class=_A    ,		has_text_modality=_A    ,		hidden_size=37)

  def     lowerCAmelCase__  (       self    ,		_A    ,		_A    ,		_A=False):
      SCREAMING_SNAKE_CASE_						=    copy.deepcopy(_A)

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

      return inputs_dict

  def     lowerCAmelCase__  (       self):
      self.config_tester.run_common_tests()

  @unittest.skip(reason='Vivit does not use inputs_embeds')
  def     lowerCAmelCase__  (       self):
      pass

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_				,      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs_for_common()

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

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_				,      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs_for_common()

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

          SCREAMING_SNAKE_CASE_						=    ['pixel_values', 'head_mask']
          self.assertListEqual(arg_names[:2]    ,		_A)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_A)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_video_classification(*_A)

  @slow
  def     lowerCAmelCase__  (       self):
      for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE_						=    VivitModel.from_pretrained(_A)
          self.assertIsNotNone(_A)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_				,      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs_for_common()
      SCREAMING_SNAKE_CASE_						=    True

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

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

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

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

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

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

          SCREAMING_SNAKE_CASE_						=    outputs.attentions

          self.assertEqual(len(_A)    ,		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):
      def check_hidden_states_output(_A    ,		_A    ,		_A):
          SCREAMING_SNAKE_CASE_						=    model_class(_A)
          model.to(_A)
          model.eval()

          with torch.no_grad():
              SCREAMING_SNAKE_CASE_						=    model(**self._prepare_for_class(_A    ,		_A))

          SCREAMING_SNAKE_CASE_						=    outputs.hidden_states
          SCREAMING_SNAKE_CASE_						=    self.model_tester.num_hidden_layers + 1
          self.assertEqual(len(_A)    ,		_A)

          SCREAMING_SNAKE_CASE_						=    self.model_tester.seq_length

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

      SCREAMING_SNAKE_CASE_				,      SCREAMING_SNAKE_CASE_						=    self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
          SCREAMING_SNAKE_CASE_						=    True
          check_hidden_states_output(_A    ,		_A    ,		_A)

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

          check_hidden_states_output(_A    ,		_A    ,		_A)


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






def        _UpperCAmelCase							(	):





    """simple docstring"""


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





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

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


  @slow
  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    VivitForVideoClassification.from_pretrained('google/vivit-b-16x2-kinetics400').to(_A)

      SCREAMING_SNAKE_CASE_						=    self.default_image_processor
      SCREAMING_SNAKE_CASE_						=    prepare_video()
      SCREAMING_SNAKE_CASE_						=    image_processor(_A    ,		return_tensors='pt').to(_A)

      # forward pass
      with torch.no_grad():
          SCREAMING_SNAKE_CASE_						=    model(**_A)

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

      # taken from original model
      SCREAMING_SNAKE_CASE_						=    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(_A)

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





import unittest

from transformers import ErnieMTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


UpperCamelCase__	:   List[str]								=     get_tests_dir("fixtures/spiece.model")



@require_sentencepiece
@require_tokenizers
class    __snake_case	(     lowerCAmelCase__  ,			unittest.TestCase							):
  __lowerCAmelCase  :						Union[str, Any]    			=	ErnieMTokenizer
  __lowerCAmelCase  :						Union[str, Any]    			=	False
  __lowerCAmelCase  :						Optional[int]    			=	True
  __lowerCAmelCase  :						List[str]    			=	False
  __lowerCAmelCase  :						List[str]    			=	False

  def     lowerCAmelCase__  (       self):
      super().setUp()

      # We have a SentencePiece fixture for testing
      SCREAMING_SNAKE_CASE_						=    ErnieMTokenizer(_A    ,		unk_token='<unk>'    ,		pad_token='<pad>')
      tokenizer.save_pretrained(self.tmpdirname)

  def     lowerCAmelCase__  (       self    ,		_A):
      SCREAMING_SNAKE_CASE_						=    'this is a test'
      SCREAMING_SNAKE_CASE_						=    'this is a test'
      return input_text, output_text

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    '<pad>'
      SCREAMING_SNAKE_CASE_						=    0

      self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A)    ,		_A)
      self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A)    ,		_A)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    list(self.get_tokenizer().get_vocab().keys())

      self.assertEqual(vocab_keys[0]    ,		'<pad>')
      self.assertEqual(vocab_keys[1]    ,		'<unk>')
      self.assertEqual(vocab_keys[-1]    ,		'▁eloquent')
      self.assertEqual(len(_A)    ,		30000)

  def     lowerCAmelCase__  (       self):
      self.assertEqual(self.get_tokenizer().vocab_size    ,		30000)

  def     lowerCAmelCase__  (       self):
      if not self.test_rust_tokenizer:
          return

      SCREAMING_SNAKE_CASE_						=    self.get_tokenizer()
      SCREAMING_SNAKE_CASE_						=    self.get_rust_tokenizer()

      SCREAMING_SNAKE_CASE_						=    'I was born in 92000, and this is falsé.'

      SCREAMING_SNAKE_CASE_						=    tokenizer.tokenize(_A)
      SCREAMING_SNAKE_CASE_						=    rust_tokenizer.tokenize(_A)
      self.assertListEqual(_A    ,		_A)

      SCREAMING_SNAKE_CASE_						=    tokenizer.encode(_A    ,		add_special_tokens=_A)
      SCREAMING_SNAKE_CASE_						=    rust_tokenizer.encode(_A    ,		add_special_tokens=_A)
      self.assertListEqual(_A    ,		_A)

      SCREAMING_SNAKE_CASE_						=    self.get_rust_tokenizer()
      SCREAMING_SNAKE_CASE_						=    tokenizer.encode(_A)
      SCREAMING_SNAKE_CASE_						=    rust_tokenizer.encode(_A)
      self.assertListEqual(_A    ,		_A)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    ErnieMTokenizer(_A    ,		do_lower_case=_A    ,		unk_token='<unk>'    ,		pad_token='<pad>')

      SCREAMING_SNAKE_CASE_						=    tokenizer.tokenize('This is a test')
      self.assertListEqual(_A    ,		['▁this', '▁is', '▁a', '▁test'])

      self.assertListEqual(tokenizer.convert_tokens_to_ids(_A)    ,		[48, 25, 21, 1289])

      SCREAMING_SNAKE_CASE_						=    tokenizer.tokenize('I was born in 92000, and this is falsé.')
      # ErnieMTokenizer(paddlenlp implementation) outputs '9' instead of '_9' so to mimic that '_9' is changed to '9'
      self.assertListEqual(
          _A    ,		['▁i', '▁was', '▁born', '▁in', '9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.'])
      SCREAMING_SNAKE_CASE_						=    tokenizer.convert_tokens_to_ids(_A)
      self.assertListEqual(_A    ,		[31, 23, 386, 19, 518, 3050, 15, 17, 48, 25, 8256, 18, 1, 9])

      SCREAMING_SNAKE_CASE_						=    tokenizer.convert_ids_to_tokens(_A)
      self.assertListEqual(
          _A    ,		['▁i', '▁was', '▁born', '▁in', '9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.']    ,		)

  def     lowerCAmelCase__  (       self):
      SCREAMING_SNAKE_CASE_						=    ErnieMTokenizer(_A    ,		unk_token='<unk>'    ,		pad_token='<pad>')

      SCREAMING_SNAKE_CASE_						=    tokenizer.encode('sequence builders')
      SCREAMING_SNAKE_CASE_						=    tokenizer.encode('multi-sequence build')

      SCREAMING_SNAKE_CASE_						=    tokenizer.build_inputs_with_special_tokens(_A)
      SCREAMING_SNAKE_CASE_						=    tokenizer.build_inputs_with_special_tokens(_A    ,		_A)

      assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
      assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + [
          tokenizer.sep_token_id
      ] + text_a + [tokenizer.sep_token_id]


  @slow
  def     lowerCAmelCase__  (       self):
      # fmt: off
      SCREAMING_SNAKE_CASE_						=    {'input_ids': [[0, 11062, 82772, 7, 15, 82772, 538, 51529, 237, 17198, 1290, 206, 9, 215175, 1314, 136, 17198, 1290, 206, 9, 56359, 42, 122009, 9, 16466, 16, 87344, 4537, 9, 4717, 78381, 6, 159958, 7, 15, 24480, 618, 4, 527, 22693, 9, 304, 4, 2777, 24480, 9874, 4, 43523, 594, 4, 803, 18392, 33189, 18, 4, 43523, 24447, 5, 5, 5, 16, 100, 24955, 83658, 9626, 144057, 15, 839, 22335, 16, 136, 24955, 83658, 83479, 15, 39102, 724, 16, 678, 645, 6460, 1328, 4589, 42, 122009, 115774, 23, 3559, 1328, 46876, 7, 136, 53894, 1940, 42227, 41159, 17721, 823, 425, 4, 27512, 98722, 206, 136, 5531, 4970, 919, 17336, 5, 2], [0, 20080, 618, 83, 82775, 47, 479, 9, 1517, 73, 53894, 333, 80581, 110117, 18811, 5256, 1295, 51, 152526, 297, 7986, 390, 124416, 538, 35431, 214, 98, 15044, 25737, 136, 7108, 43701, 23, 756, 135355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 63773, 119455, 6, 147797, 88203, 7, 645, 70, 21, 3285, 10269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}
      # fmt: on

      self.tokenizer_integration_test_util(
          expected_encoding=_A    ,		model_name='susnato/ernie-m-base_pytorch'    ,		sequences=[
              'Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides '
              'general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet...) for Natural '
              'Language Understanding (NLU) and Natural Language Generation (NLG) with over32+ pretrained '
              'models in100+ languages and deep interoperability between Jax, PyTorch and TensorFlow.',
              'BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly '
              'conditioning on both left and right context in all layers.',
              'The quick brown fox jumps over the lazy dog.',
          ]    ,		)


