


"""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      __lowerCamelCase	:

     '''simple docstring'''


     def __init__(   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=2     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=10     ,					__UpperCAmelCase=10     ,					__UpperCAmelCase=8     ,					__UpperCAmelCase=[2, 4, 4]     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=768     ,					__UpperCAmelCase=5     ,					__UpperCAmelCase=4     ,					__UpperCAmelCase=37     ,					__UpperCAmelCase="gelu_fast"     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=0.02     ,					__UpperCAmelCase=1e-0_6     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=None     ,					)							->				List[str]:
          _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				)							->				Optional[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				)							->				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     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				List[Any]:
          _a     						=					VivitModel(config=__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()
          _a     						=					model(__UpperCAmelCase				)
          self.parent.assertEqual(result.last_hidden_state.shape     ,					(self.batch_size, self.seq_length, self.hidden_size)				)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)							->				int:
          _a     						=					VivitForVideoClassification(__UpperCAmelCase				)
          model.to(__UpperCAmelCase				)
          model.eval()

          _a     						=					model(__UpperCAmelCase				)

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





     def 				_UpperCAmelCase   (   self				)							->				Optional[Any]:
          _a     						=					self.prepare_config_and_inputs()
          _a						,   _a						,   _a     						=					config_and_inputs
          _a     						=					{'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_torch
class      __lowerCamelCase	(       a__	,       unittest.TestCase					):

     '''simple docstring'''
     A_							:						Any     =		(VivitModel, VivitForVideoClassification) if is_torch_available() else ()

     A_							:						Union[str, Any]     =		False
     A_							:						List[Any]     =		False
     A_							:						List[Any]     =		False
     A_							:						str     =		False


     def 				_UpperCAmelCase   (   self				)							->				Dict:
          _a     						=					VivitModelTester(self				)
          _a     						=					ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					has_text_modality=__UpperCAmelCase     ,					hidden_size=37				)


     def 				_UpperCAmelCase   (   self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase=False				)							->				Tuple:
          _a     						=					copy.deepcopy(__UpperCAmelCase				)

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

          return inputs_dict


     def 				_UpperCAmelCase   (   self				)							->				int:
          self.config_tester.run_common_tests()


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


     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()

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


     def 				_UpperCAmelCase   (   self				)							->				Optional[int]:
          _a						,   _a     						=					self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               _a     						=					model_class(__UpperCAmelCase				)
               _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]     ,					__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				List[Any]:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase				)


     def 				_UpperCAmelCase   (   self				)							->				Dict:
          _a     						=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_video_classification(*__UpperCAmelCase				)


     @slow
     def 				_UpperCAmelCase   (   self				)							->				List[str]:
          for model_name in VIVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _a     						=					VivitModel.from_pretrained(__UpperCAmelCase				)
               self.assertIsNotNone(__UpperCAmelCase				)


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

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

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

               _a     						=					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 				_UpperCAmelCase   (   self				)							->				Union[str, Any]:
          def check_hidden_states_output(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):
               _a     						=					model_class(__UpperCAmelCase				)
               model.to(__UpperCAmelCase				)
               model.eval()

               with torch.no_grad():
                    _a     						=					model(**self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)				)

               _a     						=					outputs.hidden_states
               _a     						=					self.model_tester.num_hidden_layers + 1
               self.assertEqual(len(__UpperCAmelCase				)     ,					__UpperCAmelCase				)

               _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(__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               _a     						=					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 				_UpperCAmelCase   (   self				)							->				Dict:
          pass







def    A_ ( ):






     """simple docstring"""

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


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

     '''simple docstring'''


     @cached_property
     def 				_UpperCAmelCase   (   self				)							->				Dict:
          return VivitImageProcessor() if is_vision_available() else None





     @slow
     def 				_UpperCAmelCase   (   self				)							->				str:
          _a     						=					VivitForVideoClassification.from_pretrained('''google/vivit-b-16x2-kinetics400'''				).to(__UpperCAmelCase				)

          _a     						=					self.default_image_processor
          _a     						=					prepare_video()
          _a     						=					image_processor(__UpperCAmelCase     ,					return_tensors='''pt'''				).to(__UpperCAmelCase				)

          # forward pass
          with torch.no_grad():
               _a     						=					model(**__UpperCAmelCase				)

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

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



"""simple docstring"""






import argparse

import torch

from transformers import RoFormerConfig, RoFormerForMaskedLM, load_tf_weights_in_roformer
from transformers.utils import logging


logging.set_verbosity_info()
def    A_ ( _lowerCAmelCase  :       Dict,  _lowerCAmelCase  :       Tuple,  _lowerCAmelCase  :       Optional[Any]							):






     """simple docstring"""

     _a     						=					RoFormerConfig.from_json_file(_lowerCAmelCase							)
     print(f'Building PyTorch model from configuration: {config}'							)
     _a     						=					RoFormerForMaskedLM(_lowerCAmelCase							)

     # Load weights from tf checkpoint
     load_tf_weights_in_roformer(_lowerCAmelCase,  _lowerCAmelCase,  _lowerCAmelCase							)

     # Save pytorch-model
     print(f'Save PyTorch model to {pytorch_dump_path}'							)
     torch.save(model.state_dict(),  _lowerCAmelCase,  _use_new_zipfile_serialization=_lowerCAmelCase							)


if __name__ == "__main__":
   __snake_case												=							argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
   )
   parser.add_argument(
       '''--bert_config_file''',
       default=None,
       type=str,
       required=True,
       help=(
           '''The config json file corresponding to the pre-trained BERT model. \n'''
           '''This specifies the model architecture.'''
       ),
   )
   parser.add_argument(
       '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
   )
   __snake_case												=							parser.parse_args()
   convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)