





import argparse

import torch

from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel
from transformers.utils import logging


logging.set_verbosity_info()





def 						lowerCamelCase__     (     UpperCamelCase__			:       Optional[int]       ,    UpperCamelCase__			:       Optional[int]       ,    UpperCamelCase__			:       Dict       ,    UpperCamelCase__			:       Union[str, Any]						)			-> Union[str, Any]:



					'''simple docstring'''




					_snake_case									=				FunnelConfig.from_json_file(UpperCamelCase__						)
					print(F'''Building PyTorch model from configuration: {config}'''						)
					_snake_case									=				FunnelBaseModel(UpperCamelCase__						) if base_model else FunnelModel(UpperCamelCase__						)

					# Load weights from tf checkpoint
					load_tf_weights_in_funnel(UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__						)

					# Save pytorch-model
					print(F'''Save PyTorch model to {pytorch_dump_path}'''						)
					torch.save(model.state_dict()       ,    UpperCamelCase__						)


if __name__ == "__main__":
					UpperCAmelCase_      							=     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(
					    """--config_file""",
					    default=None,
					    type=str,
					    required=True,
					    help="""The config json file corresponding to the pre-trained model. \nThis 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(
					    """--base_model""", action="""store_true""", help="""Whether you want just the base model (no decoder) or not."""
					)
					UpperCAmelCase_      							=     parser.parse_args()
					convert_tf_checkpoint_to_pytorch(
					    args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model
					)













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				,						lowerCAmelCase_				,						lowerCAmelCase_=13				,						lowerCAmelCase_=10				,						lowerCAmelCase_=3				,						lowerCAmelCase_=2				,						lowerCAmelCase_=2				,						lowerCAmelCase_=True				,						lowerCAmelCase_=True				,						lowerCAmelCase_=32				,						lowerCAmelCase_=5				,						lowerCAmelCase_=4				,						lowerCAmelCase_=37				,						lowerCAmelCase_="gelu"				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=0.1				,						lowerCAmelCase_=10				,						lowerCAmelCase_=0.02				,						lowerCAmelCase_="divided_space_time"				,						lowerCAmelCase_=None				,						)		->							Optional[Any]:
										_snake_case									=				parent
										_snake_case									=				batch_size
										_snake_case									=				image_size
										_snake_case									=				num_channels
										_snake_case									=				patch_size
										_snake_case									=				num_frames
										_snake_case									=				is_training
										_snake_case									=				use_labels
										_snake_case									=				hidden_size
										_snake_case									=				num_hidden_layers
										_snake_case									=				num_attention_heads
										_snake_case									=				intermediate_size
										_snake_case									=				hidden_act
										_snake_case									=				hidden_dropout_prob
										_snake_case									=				attention_probs_dropout_prob
										_snake_case									=				attention_type
										_snake_case									=				initializer_range
										_snake_case									=				scope
										_snake_case									=				num_labels

										# in TimeSformer, the number of spatial tokens equals num_frames * num_patches per frame + 1 CLS token
										_snake_case									=				(image_size // patch_size) ** 2
										_snake_case									=				(num_frames) * self.num_patches_per_frame + 1




					def 	lowerCAmelCase			(		self  )		->							Optional[Any]:
										_snake_case									=				floats_tensor(
										    [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]  )

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

										_snake_case									=				self.get_config()

										return config, pixel_values, labels




					def 	lowerCAmelCase			(		self  )		->							str:
										_snake_case									=				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				,						)
										_snake_case									=				self.num_labels
										return config




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Dict:
										_snake_case									=				TimesformerModel(config=lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()
										_snake_case									=				model(lowerCAmelCase_  )
										self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )		->							Tuple:
										_snake_case									=				TimesformerForVideoClassification(lowerCAmelCase_  )
										model.to(lowerCAmelCase_  )
										model.eval()

										_snake_case									=				model(lowerCAmelCase_  )

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






					def 	lowerCAmelCase			(		self  )		->							Any:
										_snake_case									=				self.prepare_config_and_inputs()
										_snake_case							,     _snake_case							,     _snake_case									=				config_and_inputs
										_snake_case									=				{'pixel_values': pixel_values}
										return config, inputs_dict





@require_torch
class 			UpperCamelCase_		(  _lowerCamelCase							,  _lowerCamelCase							,  unittest.TestCase					):
					lowerCAmelCase_							=	(TimesformerModel, TimesformerForVideoClassification) if is_torch_available() else ()
					lowerCAmelCase_							=	(
					    {'''feature-extraction''': TimesformerModel, '''video-classification''': TimesformerForVideoClassification}
					    if is_torch_available()
					    else {}
					)

					lowerCAmelCase_							=	False
					lowerCAmelCase_							=	False
					lowerCAmelCase_							=	False
					lowerCAmelCase_							=	False




					def 	lowerCAmelCase			(		self  )		->							Optional[Any]:
										_snake_case									=				TimesformerModelTester(self  )
										_snake_case									=				ConfigTester(
										    self				,						config_class=lowerCAmelCase_				,						has_text_modality=lowerCAmelCase_				,						hidden_size=37  )




					def 	lowerCAmelCase			(		self				,						lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_=False  )		->							Any:
										_snake_case									=				copy.deepcopy(lowerCAmelCase_  )

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

										return inputs_dict




					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										self.config_tester.run_common_tests()




					@unittest.skip(reason='TimeSformer does not use inputs_embeds'  )
					def 	lowerCAmelCase			(		self  )		->							Tuple:
										pass




					def 	lowerCAmelCase			(		self  )		->							Optional[Any]:
										_snake_case							,     _snake_case									=				self.model_tester.prepare_config_and_inputs_for_common()

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




					def 	lowerCAmelCase			(		self  )		->							Union[str, Any]:
										_snake_case							,     _snake_case									=				self.model_tester.prepare_config_and_inputs_for_common()

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

															_snake_case									=				['pixel_values']
															self.assertListEqual(arg_names[:1]				,						lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							List[str]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Union[str, Any]:
										_snake_case									=				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_video_classification(*lowerCAmelCase_  )




					@slow
					def 	lowerCAmelCase			(		self  )		->							List[Any]:
										for model_name in TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_snake_case									=				TimesformerModel.from_pretrained(lowerCAmelCase_  )
															self.assertIsNotNone(lowerCAmelCase_  )




					def 	lowerCAmelCase			(		self  )		->							Union[str, Any]:
										if not self.has_attentions:
															pass

										else:
															_snake_case							,     _snake_case									=				self.model_tester.prepare_config_and_inputs_for_common()
															_snake_case									=				True

															for model_class in self.all_model_classes:
																				_snake_case									=				self.model_tester.seq_length
																				_snake_case									=				self.model_tester.num_frames

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

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

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

																				_snake_case									=				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 	lowerCAmelCase			(		self  )		->							Dict:
										def check_hidden_states_output(lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  ):
															_snake_case									=				model_class(lowerCAmelCase_  )
															model.to(lowerCAmelCase_  )
															model.eval()

															with torch.no_grad():
																				_snake_case									=				model(**self._prepare_for_class(lowerCAmelCase_				,						lowerCAmelCase_  )  )

															_snake_case									=				outputs.hidden_states
															_snake_case									=				self.model_tester.num_hidden_layers + 1
															self.assertEqual(len(lowerCAmelCase_  )				,						lowerCAmelCase_  )

															_snake_case									=				self.model_tester.seq_length

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

										_snake_case							,     _snake_case									=				self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
															_snake_case									=				True
															check_hidden_states_output(lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )

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

															check_hidden_states_output(lowerCAmelCase_				,						lowerCAmelCase_				,						lowerCAmelCase_  )





def 						lowerCamelCase__     (     )			-> Tuple:



					'''simple docstring'''




					_snake_case									=				hf_hub_download(
					    repo_id='hf-internal-testing/spaghetti-video'       ,    filename='eating_spaghetti.npy'       ,    repo_type='dataset'						)
					_snake_case									=				np.load(UpperCamelCase__						)
					return list(UpperCamelCase__						)




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




					@cached_property
					def 	lowerCAmelCase			(		self  )		->							Dict:
										# 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 	lowerCAmelCase			(		self  )		->							Optional[int]:
										_snake_case									=				TimesformerForVideoClassification.from_pretrained('facebook/timesformer-base-finetuned-k400'  ).to(
										    lowerCAmelCase_  )

										_snake_case									=				self.default_image_processor
										_snake_case									=				prepare_video()
										_snake_case									=				image_processor(video[:8]				,						return_tensors='pt'  ).to(lowerCAmelCase_  )

										# forward pass
										with torch.no_grad():
															_snake_case									=				model(**lowerCAmelCase_  )

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

										_snake_case									=				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  )  )



