




import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import TimesformerConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
      TimesformerForVideoClassification,
      TimesformerModel,
  )
  from transformers.models.timesformer.modeling_timesformer import TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor
class 			_UpperCamelCase      :
      def __init__(			self   :str ,      lowerCamelCase   :Any ,      lowerCamelCase   :List[Any]=13 ,      lowerCamelCase   :Tuple=10 ,      lowerCamelCase   :Union[str, Any]=3 ,      lowerCamelCase   :Dict=2 ,      lowerCamelCase   :List[Any]=2 ,      lowerCamelCase   :Tuple=True ,      lowerCamelCase   :Optional[int]=True ,      lowerCamelCase   :List[Any]=32 ,      lowerCamelCase   :Tuple=5 ,      lowerCamelCase   :List[Any]=4 ,      lowerCamelCase   :Dict=37 ,      lowerCamelCase   :int="gelu" ,      lowerCamelCase   :Optional[Any]=0.1 ,      lowerCamelCase   :Tuple=0.1 ,      lowerCamelCase   :Tuple=10 ,      lowerCamelCase   :str=0.02 ,      lowerCamelCase   :Any="divided_space_time" ,      lowerCamelCase   :Optional[Any]=None ,      )     ->			str:
       UpperCAmelCase__							 =						parent
       UpperCAmelCase__							 =						batch_size
       UpperCAmelCase__							 =						image_size
       UpperCAmelCase__							 =						num_channels
       UpperCAmelCase__							 =						patch_size
       UpperCAmelCase__							 =						num_frames
       UpperCAmelCase__							 =						is_training
       UpperCAmelCase__							 =						use_labels
       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__							 =						attention_type
       UpperCAmelCase__							 =						initializer_range
       UpperCAmelCase__							 =						scope
       UpperCAmelCase__							 =						num_labels

       # in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
       UpperCAmelCase__							 =						(image_size // patch_size) ** 2
       UpperCAmelCase__							 =						(num_frames) * self.num_patches_per_frame + 1
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			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   :Optional[Any]      )     ->			Any:
       UpperCAmelCase__							 =						TimesformerConfig(
           image_size=self.image_size ,      patch_size=self.patch_size ,      num_channels=self.num_channels ,      num_frames=self.num_frames ,      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 ,      attention_type=self.attention_type ,      )
       UpperCAmelCase__							 =						self.num_labels
       return config
      def 	UpperCAmelCase_						(			self   :Union[str, Any] ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Tuple ,      lowerCamelCase   :Optional[Any]      )     ->			Dict:
       UpperCAmelCase__							 =						TimesformerModel(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   :int ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Dict ,      lowerCamelCase   :Union[str, Any]      )     ->			Union[str, Any]:
       UpperCAmelCase__							 =						TimesformerForVideoClassification(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   :Union[str, Any]      )     ->			Optional[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	,       lowerCAmelCase	,       unittest.TestCase		):
      UpperCAmelCase_        =		(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
      UpperCAmelCase_        =		(
          {"""feature-extraction""": TimesformerModel, """video-classification""": TimesformerForVideoClassification}
          if is_torch_available()
          else {}
      )

      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      UpperCAmelCase_        =		False
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Optional[Any]:
       UpperCAmelCase__							 =						TimesformerModelTester(self      )
       UpperCAmelCase__							 =						ConfigTester(
           self ,      config_class=lowerCamelCase ,      has_text_modality=lowerCamelCase ,      hidden_size=37      )
      def 	UpperCAmelCase_						(			self   :Tuple ,      lowerCamelCase   :List[str] ,      lowerCamelCase   :Optional[int] ,      lowerCamelCase   :Any=False      )     ->			List[Any]:
       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   :str      )     ->			Tuple:
       self.config_tester.run_common_tests()
      @unittest.skip(reason="TimeSformer does not use inputs_embeds"      )
      def 	UpperCAmelCase_						(			self   :Dict      )     ->			int:
       pass
      def 	UpperCAmelCase_						(			self   :Optional[int]      )     ->			Dict:
       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   :Any      )     ->			Optional[int]:
       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"]
        self.assertListEqual(arg_names[:1] ,      lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			Dict:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :int      )     ->			Dict:
       UpperCAmelCase__							 =						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_video_classification(*lowerCamelCase      )
      @slow
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Tuple:
       for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							 =						TimesformerModel.from_pretrained(lowerCamelCase      )
        self.assertIsNotNone(lowerCamelCase      )
      def 	UpperCAmelCase_						(			self   :str      )     ->			Any:
       if not self.has_attentions:
        pass

       else:
        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__							 =						self.model_tester.num_frames

         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      )

         # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
         self.assertListEqual(
             list(attentions[0].shape[-3:]      ) ,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] ,      )
         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      )

         # attentions has shape (batch_size x num_frames) x num_heads x (num_patches per frame + 1) x (num_patches per frame + 1)
         self.assertListEqual(
             list(self_attentions[0].shape[-3:]      ) ,      [self.model_tester.num_attention_heads, seq_len // num_frames + 1, seq_len // num_frames + 1] ,      )







      def 	UpperCAmelCase_						(			self   :str      )     ->			Tuple:
       def check_hidden_states_output(lowerCamelCase   :Tuple ,      lowerCamelCase   :int ,      lowerCamelCase   :Optional[int]      ):
        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      )



def 						lowerCAmelCase		(		):




 """simple docstring"""







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


@require_torch
@require_vision
class 			_UpperCamelCase      (				unittest.TestCase		):
      @cached_property
      def 	UpperCAmelCase_						(			self   :Tuple      )     ->			Optional[int]:
       # logits were tested with a different mean and std, so we use the same here
       return (
           VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5] ,      image_std=[0.5, 0.5, 0.5]      )
           if is_vision_available()
           else None
       )







      @slow
      def 	UpperCAmelCase_						(			self   :Optional[Any]      )     ->			Optional[int]:
       UpperCAmelCase__							 =						TimesformerForVideoClassification.from_pretrained("facebook/timesformer-base-finetuned-k400"      ).to(
           lowerCamelCase      )

       UpperCAmelCase__							 =						self.default_image_processor
       UpperCAmelCase__							 =						prepare_video()
       UpperCAmelCase__							 =						image_processor(video[:8] ,      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      )

       UpperCAmelCase__							 =						torch.tensor([-0.30_16, -0.77_13, -0.42_05]      ).to(lowerCamelCase      )

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











import argparse

from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird
from transformers.utils import logging


logging.set_verbosity_info()



def 						lowerCAmelCase		(		_lowerCAmelCase							:   Any						,							_lowerCAmelCase							:   List[str]						,							_lowerCAmelCase							:   Optional[int]						,							_lowerCAmelCase							:   Union[str, Any]	):




 """simple docstring"""







 UpperCAmelCase__							 =						BigBirdConfig.from_json_file(_lowerCAmelCase	)
 print(F'''Building PyTorch model from configuration: {config}'''	)

 if is_trivia_qa:
  UpperCAmelCase__							 =						BigBirdForQuestionAnswering(_lowerCAmelCase	)
 else:
  UpperCAmelCase__							 =						BigBirdForPreTraining(_lowerCAmelCase	)

 # Load weights from tf checkpoint
 load_tf_weights_in_big_bird(_lowerCAmelCase						,							_lowerCAmelCase						,							is_trivia_qa=_lowerCAmelCase	)

 # Save pytorch-model
 print(F'''Save PyTorch model to {pytorch_dump_path}'''	)
 model.save_pretrained(_lowerCAmelCase	)


if __name__ == "__main__":
  _lowerCAmelCase						:   List[Any]		=   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(
      "--big_bird_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."
  )
  parser.add_argument(
      "--is_trivia_qa", action="store_true", help="Whether to convert a model with a trivia_qa head."
  )
  _lowerCAmelCase						:   Dict		=   parser.parse_args()
  convert_tf_checkpoint_to_pytorch(
      args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa
  )





