






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


a_		:    Union[str, Any]   							=     {
    '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 					lowerCamelCase__					(_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase=None):
			# Initialise PyTorch model
			SCREAMING_SNAKE_CASE							=			XLNetConfig.from_json_file(_UpperCAmelCase)

			SCREAMING_SNAKE_CASE							=			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}''')
						SCREAMING_SNAKE_CASE							=			finetuning_task
						SCREAMING_SNAKE_CASE							=			GLUE_TASKS_NUM_LABELS[finetuning_task]
						SCREAMING_SNAKE_CASE							=			XLNetForSequenceClassification(_UpperCAmelCase)
			elif "squad" in finetuning_task:
						SCREAMING_SNAKE_CASE							=			finetuning_task
						SCREAMING_SNAKE_CASE							=			XLNetForQuestionAnswering(_UpperCAmelCase)
			else:
						SCREAMING_SNAKE_CASE							=			XLNetLMHeadModel(_UpperCAmelCase)

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

			# Save pytorch-model
			SCREAMING_SNAKE_CASE							=			os.path.join(_UpperCAmelCase				,							_UpperCAmelCase)
			SCREAMING_SNAKE_CASE							=			os.path.join(_UpperCAmelCase				,							_UpperCAmelCase)
			print(F'''Save PyTorch model to {os.path.abspath(_UpperCAmelCase)}''')
			torch.save(model.state_dict()				,							_UpperCAmelCase)
			print(F'''Save configuration file to {os.path.abspath(_UpperCAmelCase)}''')
			with open(_UpperCAmelCase				,							'w'				,							encoding='utf-8') as f:
						f.write(config.to_json_string())


if __name__ == "__main__":
				a_		:    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(
				    '--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',
				)
				a_		:    Dict   							=     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
				)












from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a   :							Optional[Any]						= logging.get_logger(__name__)

__a   :							Dict						= {
    "google/realm-cc-news-pretrained-embedder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-encoder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-scorer": (
        "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-openqa": (
        "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"
    ),
    "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json",
    "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json",
    "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json",
    "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json",
    # See all REALM models at https://huggingface.co/models?filter=realm
}


class    __lowercase   (						lowercase_	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					"realm"




     def __init__(					self							:      str			,						UpperCamelCase_							:      List[Any]=30_522			,						UpperCamelCase_							:      Dict=768			,						UpperCamelCase_							:      Union[str, Any]=128			,						UpperCamelCase_							:      Tuple=12			,						UpperCamelCase_							:      Optional[Any]=12			,						UpperCamelCase_							:      Optional[int]=8			,						UpperCamelCase_							:      str=3_072			,						UpperCamelCase_							:      List[str]="gelu_new"			,						UpperCamelCase_							:      Optional[int]=0.1			,						UpperCamelCase_							:      Optional[int]=0.1			,						UpperCamelCase_							:      str=512			,						UpperCamelCase_							:      int=2			,						UpperCamelCase_							:      str=0.02			,						UpperCamelCase_							:      List[Any]=1e-12			,						UpperCamelCase_							:      str=256			,						UpperCamelCase_							:      List[str]=10			,						UpperCamelCase_							:      int=1e-3			,						UpperCamelCase_							:      List[str]=5			,						UpperCamelCase_							:      Tuple=320			,						UpperCamelCase_							:      List[str]=13_353_718			,						UpperCamelCase_							:      Tuple=5_000			,						UpperCamelCase_							:      Optional[int]=1			,						UpperCamelCase_							:      List[str]=0			,						UpperCamelCase_							:      Union[str, Any]=2			,						**UpperCamelCase_							:      List[Any]			,						):


         """simple docstring"""



         super().__init__(pad_token_id=UpperCamelCase_			,						bos_token_id=UpperCamelCase_			,						eos_token_id=UpperCamelCase_			,						**UpperCamelCase_					)

         # Common config
         __A       =						vocab_size
         __A       =						max_position_embeddings
         __A       =						hidden_size
         __A       =						retriever_proj_size
         __A       =						num_hidden_layers
         __A       =						num_attention_heads
         __A       =						num_candidates
         __A       =						intermediate_size
         __A       =						hidden_act
         __A       =						hidden_dropout_prob
         __A       =						attention_probs_dropout_prob
         __A       =						initializer_range
         __A       =						type_vocab_size
         __A       =						layer_norm_eps

         # Reader config
         __A       =						span_hidden_size
         __A       =						max_span_width
         __A       =						reader_layer_norm_eps
         __A       =						reader_beam_size
         __A       =						reader_seq_len

         # Retrieval config
         __A       =						num_block_records
         __A       =						searcher_beam_size




