
from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
							from transformers import (
							    AutoConfig,
							    BertConfig,
							    GPTaConfig,
							    TaConfig,
							    TFAutoModel,
							    TFAutoModelForCausalLM,
							    TFAutoModelForMaskedLM,
							    TFAutoModelForPreTraining,
							    TFAutoModelForQuestionAnswering,
							    TFAutoModelForSeqaSeqLM,
							    TFAutoModelForSequenceClassification,
							    TFAutoModelWithLMHead,
							    TFBertForMaskedLM,
							    TFBertForPreTraining,
							    TFBertForQuestionAnswering,
							    TFBertForSequenceClassification,
							    TFBertModel,
							    TFGPTaLMHeadModel,
							    TFRobertaForMaskedLM,
							    TFTaForConditionalGeneration,
							)
							from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
							from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
							from transformers import (
							    AutoModel,
							    AutoModelForCausalLM,
							    AutoModelForMaskedLM,
							    AutoModelForPreTraining,
							    AutoModelForQuestionAnswering,
							    AutoModelForSeqaSeqLM,
							    AutoModelForSequenceClassification,
							    AutoModelWithLMHead,
							    BertForMaskedLM,
							    BertForPreTraining,
							    BertForQuestionAnswering,
							    BertForSequenceClassification,
							    BertModel,
							    GPTaLMHeadModel,
							    RobertaForMaskedLM,
							    TaForConditionalGeneration,
							)





@is_pt_tf_cross_test
class 							lowerCAmelCase_						(    unittest.TestCase							):




			'''simple docstring'''


			@slow
			def      __lowerCamelCase    (	self  ):
										# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["bert-base-uncased"]:
																	SCREAMING_SNAKE_CASE_	:							Optional[Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							List[Any]											=TFAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							str											=AutoModel.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["bert-base-uncased"]:
																	SCREAMING_SNAKE_CASE_	:							Union[str, Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Optional[int]											=TFAutoModelForPreTraining.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Any											=AutoModelForPreTraining.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE_	:							Optional[Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Optional[int]											=TFAutoModelForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							int											=TFAutoModelForCausalLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Union[str, Any]											=AutoModelForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							int											=AutoModelForCausalLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE_	:							Union[str, Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Optional[Any]											=TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							List[str]											=AutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE_	:							Optional[int]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							int											=TFAutoModelForMaskedLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							Tuple											=TFAutoModelForMaskedLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							List[str]											=AutoModelForMaskedLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							Any											=AutoModelForMaskedLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	SCREAMING_SNAKE_CASE_	:							Optional[Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Any											=TFAutoModelForSeqaSeqLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							Union[str, Any]											=TFAutoModelForSeqaSeqLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Union[str, Any]											=AutoModelForSeqaSeqLM.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_	:							Optional[int]											=AutoModelForSeqaSeqLM.from_pretrained(
																	    __SCREAMING_SNAKE_CASE    ,							output_loading_info=__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["bert-base-uncased"]:
																	SCREAMING_SNAKE_CASE_	:							List[Any]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							List[Any]											=TFAutoModelForSequenceClassification.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Optional[Any]											=AutoModelForSequenceClassification.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			@slow
			def      __lowerCamelCase    (	self  ):
										# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["bert-base-uncased"]:
																	SCREAMING_SNAKE_CASE_	:							Optional[int]											=AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							str											=TFAutoModelForQuestionAnswering.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )

																	SCREAMING_SNAKE_CASE_	:							Dict											=AutoModelForQuestionAnswering.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )







			def      __lowerCamelCase    (	self  ):
										SCREAMING_SNAKE_CASE_	:							int											=TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
										self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )
										self.assertEqual(model.num_parameters()    ,							14_410  )
										self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE  )    ,							14_410  )

										SCREAMING_SNAKE_CASE_	:							List[str]											=AutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
										self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )
										self.assertEqual(model.num_parameters()    ,							14_410  )
										self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE  )    ,							14_410  )







			def      __lowerCamelCase    (	self  ):
										SCREAMING_SNAKE_CASE_	:							Optional[int]											=TFAutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_pt=__SCREAMING_SNAKE_CASE  )
										self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )
										self.assertEqual(model.num_parameters()    ,							14_410  )
										self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE  )    ,							14_410  )

										SCREAMING_SNAKE_CASE_	:							Dict											=AutoModelWithLMHead.from_pretrained(__SCREAMING_SNAKE_CASE    ,							from_tf=__SCREAMING_SNAKE_CASE  )
										self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE  )
										self.assertEqual(model.num_parameters()    ,							14_410  )
										self.assertEqual(model.num_parameters(only_trainable=__SCREAMING_SNAKE_CASE  )    ,							14_410  )






import argparse
import json

import numpy
import torch

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()



def 	__A		(      _A				,					_A       ):



 """simple docstring"""


 __a							=	torch.load(_A				,					map_location="cpu"       )

 __a							=	chkpt["model"]

 # We have the base model one level deeper than the original XLM repository
 __a							=	{}
 for k, v in state_dict.items():
  if "pred_layer" in k:
   __a							=	v
  else:
   __a							=	v

 __a							=	chkpt["params"]
 __a							=	{n: v for n, v in config.items() if not isinstance(_A				,					(torch.FloatTensor, numpy.ndarray)       )}

 __a							=	chkpt["dico_word2id"]
 __a							=	{s + "</w>" if s.find("@@"       ) == -1 and i > 13 else s.replace("@@"				,					""       ): i for s, i in vocab.items()}

 # Save pytorch-model
 __a							=	pytorch_dump_folder_path + "/" + WEIGHTS_NAME
 __a							=	pytorch_dump_folder_path + "/" + CONFIG_NAME
 __a							=	pytorch_dump_folder_path + "/" + VOCAB_FILES_NAMES["vocab_file"]

 print(f"""Save PyTorch model to {pytorch_weights_dump_path}"""       )
 torch.save(_A				,					_A       )

 print(f"""Save configuration file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )

 print(f"""Save vocab file to {pytorch_config_dump_path}"""       )
 with open(_A				,					"w"				,					encoding="utf-8"       ) as f:
  f.write(json.dumps(_A				,					indent=2       ) + "\n"       )


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE			:						Union[str, Any]				  =		argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--xlm_checkpoint_path""", default=None, type=str, required=True, help="""Path the official PyTorch dump."""
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  SCREAMING_SNAKE_CASE			:						Any				  =		parser.parse_args()
  convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)


