





import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


_SCREAMING_SNAKE_CASE      	=		{
    """cola""": 2,
    """mnli""": 3,
    """mrpc""": 2,
    """sst-2""": 2,
    """sts-b""": 1,
    """qqp""": 2,
    """qnli""": 2,
    """rte""": 2,
    """wnli""": 2,
}


logging.set_verbosity_info()


def 							SCREAMING_SNAKE_CASE__						(    __a   , __a   , __a   , __a=None   ):
	# Initialise PyTorch model
	snake_case_		:			Dict											=	XLNetConfig.from_json_file(__a   )

	snake_case_		:			List[Any]											=	finetuning_task.lower() if finetuning_task is not None else ''
	if finetuning_task in GLUE_TASKS_NUM_LABELS:
		print(f"""Building PyTorch XLNetForSequenceClassification model from configuration: {config}"""   )
		snake_case_		:			List[str]											=	finetuning_task
		snake_case_		:			Optional[Any]											=	GLUE_TASKS_NUM_LABELS[finetuning_task]
		snake_case_		:			List[Any]											=	XLNetForSequenceClassification(__a   )
	elif "squad" in finetuning_task:
		snake_case_		:			Optional[int]											=	finetuning_task
		snake_case_		:			Union[str, Any]											=	XLNetForQuestionAnswering(__a   )
	else:
		snake_case_		:			List[str]											=	XLNetLMHeadModel(__a   )

	# Load weights from tf checkpoint
	load_tf_weights_in_xlnet(__a   , __a   , __a   )

	# Save pytorch-model
	snake_case_		:			Tuple											=	os.path.join(__a   , __a   )
	snake_case_		:			Union[str, Any]											=	os.path.join(__a   , __a   )
	print(f"""Save PyTorch model to {os.path.abspath(__a   )}"""   )
	torch.save(model.state_dict()   , __a   )
	print(f"""Save configuration file to {os.path.abspath(__a   )}"""   )
	with open(__a   , 'w'   , encoding='utf-8'   ) as f:
		f.write(config.to_json_string()   )


if __name__ == "__main__":
						_SCREAMING_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(
						    """--xlnet_config_file""",
						    default=None,
						    type=str,
						    required=True,
						    help=(
						        """The config json file corresponding to the pre-trained XLNet model. \n"""
						        """This specifies the model architecture."""
						    ),
						)
						parser.add_argument(
						    """--pytorch_dump_folder_path""",
						    default=None,
						    type=str,
						    required=True,
						    help="""Path to the folder to store the PyTorch model or dataset/vocab.""",
						)
						parser.add_argument(
						    """--finetuning_task""",
						    default=None,
						    type=str,
						    help="""Name of a task on which the XLNet TensorFlow model was fine-tuned""",
						)
						_SCREAMING_SNAKE_CASE      	=		parser.parse_args()
						print(args)

						convert_xlnet_checkpoint_to_pytorch(
						    args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
						)








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 							SCREAMING_SNAKE_CASE__						(    __a   , __a   ):
	# Load checkpoint
	snake_case_		:			Union[str, Any]											=	torch.load(__a   , map_location='cpu'   )

	snake_case_		:			Union[str, Any]											=	chkpt['model']

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

	snake_case_		:			Tuple											=	chkpt['params']
	snake_case_		:			List[Any]											=	{n: v for n, v in config.items() if not isinstance(__a   , (torch.FloatTensor, numpy.ndarray)   )}

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

	# Save pytorch-model
	snake_case_		:			List[str]											=	pytorch_dump_folder_path + '/' + WEIGHTS_NAME
	snake_case_		:			Dict											=	pytorch_dump_folder_path + '/' + CONFIG_NAME
	snake_case_		:			Any											=	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      	=		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      	=		parser.parse_args()
						convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)

