

from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand



def   __UpperCAmelCase					(      __a :     Namespace      )     ->	Dict:

					"""simple docstring"""

					return ConvertCommand(
					    args.model_type						,args.tf_checkpoint						,args.pytorch_dump_output						,args.config						,args.finetuning_task_name      )


a__												=						'''
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
'''







class 							UpperCAmelCase_						(     __lowercase  ):
							"""simple docstring"""







							@staticmethod
							def 							__lowercase							(     _a			)       ->       Optional[Any]:
												_a      :       Union[str, Any]   =						parser.add_parser(
												    '''convert'''			,				help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'''			,				)
												train_parser.add_argument('''--model_type'''			,				type=_a			,				required=_a			,				help='''Model\'s type.'''			)
												train_parser.add_argument(
												    '''--tf_checkpoint'''			,				type=_a			,				required=_a			,				help='''TensorFlow checkpoint path or folder.'''			)
												train_parser.add_argument(
												    '''--pytorch_dump_output'''			,				type=_a			,				required=_a			,				help='''Path to the PyTorch saved model output.'''			)
												train_parser.add_argument('''--config'''			,				type=_a			,				default=''''''			,				help='''Configuration file path or folder.'''			)
												train_parser.add_argument(
												    '''--finetuning_task_name'''			,				type=_a			,				default=_a			,				help='''Optional fine-tuning task name if the TF model was a finetuned model.'''			,				)
												train_parser.set_defaults(func=_a			)


							def __init__(     self			,				_a			,				_a			,				_a			,				_a			,				_a			,				*_a			,				)       ->       Tuple:
												_a      :       str   =						logging.get_logger('''transformers-cli/converting'''			)

												self._logger.info(F"""Loading model {model_type}"""			)
												_a      :       int   =						model_type
												_a      :       Optional[Any]   =						tf_checkpoint
												_a      :       Union[str, Any]   =						pytorch_dump_output
												_a      :       Tuple   =						config
												_a      :       str   =						finetuning_task_name




							def 							__lowercase							(     self			)       ->       str:
												if self._model_type == "albert":
																	try:
																						from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
																						    convert_tf_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	convert_tf_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "bert":
																	try:
																						from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
																						    convert_tf_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	convert_tf_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "funnel":
																	try:
																						from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
																						    convert_tf_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	convert_tf_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "t5":
																	try:
																						from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
																	except ImportError:
																						raise ImportError(_a			)

																	convert_tf_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "gpt":
																	from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
																	    convert_openai_checkpoint_to_pytorch,
																	)

																	convert_openai_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "transfo_xl":
																	try:
																						from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
																						    convert_transfo_xl_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	if "ckpt" in self._tf_checkpoint.lower():
																						_a      :       Union[str, Any]   =						self._tf_checkpoint
																						_a      :       str   =						''''''
																	else:
																						_a      :       int   =						self._tf_checkpoint
																						_a      :       str   =						''''''
																	convert_transfo_xl_checkpoint_to_pytorch(
																	    _a			,				self._config			,				self._pytorch_dump_output			,				_a			)
												elif self._model_type == "gpt2":
																	try:
																						from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
																						    convert_gpta_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												elif self._model_type == "xlnet":
																	try:
																						from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
																						    convert_xlnet_checkpoint_to_pytorch,
																						)
																	except ImportError:
																						raise ImportError(_a			)

																	convert_xlnet_checkpoint_to_pytorch(
																	    self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			,				self._finetuning_task_name			)
												elif self._model_type == "xlm":
																	from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
																	    convert_xlm_checkpoint_to_pytorch,
																	)

																	convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint			,				self._pytorch_dump_output			)
												elif self._model_type == "lxmert":
																	from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
																	    convert_lxmert_checkpoint_to_pytorch,
																	)

																	convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint			,				self._pytorch_dump_output			)
												elif self._model_type == "rembert":
																	from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
																	    convert_rembert_tf_checkpoint_to_pytorch,
																	)

																	convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint			,				self._config			,				self._pytorch_dump_output			)
												else:
																	raise ValueError(
																	    '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'''			)






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
			from transformers import TvltImageProcessor

if is_speech_available():
			from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class 							UpperCAmelCase_						(     unittest.TestCase  ):
							"""simple docstring"""







							def 							__lowercase							(     self			)       ->       int:
												_a      :       Dict   =						'''ZinengTang/tvlt-base'''
												_a      :       List[str]   =						tempfile.mkdtemp()


							def 							__lowercase							(     self			,				**_a			)       ->       int:
												return TvltImageProcessor.from_pretrained(self.checkpoint			,				**_a			)


							def 							__lowercase							(     self			,				**_a			)       ->       List[Any]:
												return TvltFeatureExtractor.from_pretrained(self.checkpoint			,				**_a			)


							def 							__lowercase							(     self			)       ->       Optional[int]:
												shutil.rmtree(self.tmpdirname			)


							def 							__lowercase							(     self			)       ->       Dict:
												_a      :       Union[str, Any]   =						self.get_image_processor()
												_a      :       Dict   =						self.get_feature_extractor()

												_a      :       Optional[int]   =						TvltProcessor(image_processor=_a			,				feature_extractor=_a			)
												processor.save_pretrained(self.tmpdirname			)
												_a      :       Any   =						TvltProcessor.from_pretrained(self.tmpdirname			)

												self.assertIsInstance(processor.feature_extractor			,				_a			)
												self.assertIsInstance(processor.image_processor			,				_a			)


							def 							__lowercase							(     self			)       ->       Any:
												_a      :       Optional[Any]   =						self.get_image_processor()
												_a      :       Dict   =						self.get_feature_extractor()

												_a      :       Dict   =						TvltProcessor(image_processor=_a			,				feature_extractor=_a			)

												_a      :       Union[str, Any]   =						np.ones([1_2_0_0_0]			)

												_a      :       Dict   =						feature_extractor(_a			,				return_tensors='''np'''			)
												_a      :       Tuple   =						processor(audio=_a			,				return_tensors='''np'''			)

												for key in audio_dict.keys():
																	self.assertAlmostEqual(audio_dict[key].sum()			,				input_processor[key].sum()			,				delta=1e-2			)


							def 							__lowercase							(     self			)       ->       int:
												_a      :       Optional[Any]   =						self.get_image_processor()
												_a      :       Union[str, Any]   =						self.get_feature_extractor()

												_a      :       Optional[Any]   =						TvltProcessor(image_processor=_a			,				feature_extractor=_a			)

												_a      :       List[Any]   =						np.ones([3, 2_2_4, 2_2_4]			)

												_a      :       int   =						image_processor(_a			,				return_tensors='''np'''			)
												_a      :       Optional[int]   =						processor(images=_a			,				return_tensors='''np'''			)

												for key in image_dict.keys():
																	self.assertAlmostEqual(image_dict[key].sum()			,				input_processor[key].sum()			,				delta=1e-2			)


							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												_a      :       int   =						self.get_image_processor()
												_a      :       Union[str, Any]   =						self.get_feature_extractor()

												_a      :       Any   =						TvltProcessor(image_processor=_a			,				feature_extractor=_a			)

												_a      :       List[str]   =						np.ones([1_2_0_0_0]			)
												_a      :       Optional[int]   =						np.ones([3, 2_2_4, 2_2_4]			)

												_a      :       int   =						processor(audio=_a			,				images=_a			)

												self.assertListEqual(list(inputs.keys()			)			,				['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']			)

												# test if it raises when no input is passed
												with pytest.raises(_a			):
																	processor()




							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												_a      :       str   =						self.get_image_processor()
												_a      :       Union[str, Any]   =						self.get_feature_extractor()

												_a      :       Dict   =						TvltProcessor(image_processor=_a			,				feature_extractor=_a			)

												self.assertListEqual(
												    processor.model_input_names			,				image_processor.model_input_names + feature_extractor.model_input_names			,				msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''			,				)



