






import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
lowercase  							=					logging.getLogger()

lowercase  							=					logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class  UpperCamelCase_      (			snake_case_				):



	'''simple docstring'''




	def 							_UpperCamelCase			(       self       ,			a					)       ->    Union[str, Any]:
								os.makedirs(a       ,			exist_ok=a					)
								snake_case_				=							{'source': 'What is love ?', 'target': 'life'}
								snake_case_				=							{'train': 12, 'val': 2, 'test': 2}
								for split in ["train", "test", "val"]:
															for field in ["source", "target"]:
																						snake_case_				=							'\n'.join([contents[field]] * n_lines[split]					)
																						with open(os.path.join(a       ,			F'''{split}.{field}'''					)       ,			'w'					) as f:
																													f.write(a					)




	def 							_UpperCamelCase			(       self       ,			a       ,			a = "pytorch"					)       ->    Optional[int]:
								snake_case_				=							self.get_auto_remove_tmp_dir()
								snake_case_				=							os.path.join(a       ,			'output'					)
								snake_case_				=							os.path.join(a       ,			'data'					)
								self._create_dummy_data(data_dir=a					)

								snake_case_				=							F'''
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            '''.split()

								if gpus > 0:
															testargs.append(F'''--gpus={gpus}'''					)
															if is_apex_available():
																						testargs.append('--fp16'					)
								else:
															testargs.append('--gpus=0'					)
															testargs.append('--distributed_backend=ddp_cpu'					)
															testargs.append('--num_processes=2'					)

								snake_case_				=							[sys.executable, str(Path(finetune_rag.__file__					).resolve()					)] + testargs
								execute_subprocess_async(a       ,			env=self.get_env()					)

								snake_case_				=							os.path.join(a       ,			'metrics.json'					)
								with open(a					) as f:
															snake_case_				=							json.load(a					)
								return result




	@require_torch_gpu
	def 							_UpperCamelCase			(       self					)       ->    List[Any]:
								snake_case_				=							self._run_finetune(gpus=1					)
								self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,			0.2					)




	@require_torch_multi_gpu
	def 							_UpperCamelCase			(       self					)       ->    Optional[Any]:
								snake_case_				=							self._run_finetune(gpus=2					)
								self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,			0.2					)




	@require_torch_gpu
	@require_ray
	def 							_UpperCamelCase			(       self					)       ->    List[str]:
								snake_case_				=							self._run_finetune(gpus=1       ,			distributed_retriever='ray'					)
								self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,			0.2					)






	@require_torch_multi_gpu
	@require_ray
	def 							_UpperCamelCase			(       self					)       ->    List[str]:
								snake_case_				=							self._run_finetune(gpus=1       ,			distributed_retriever='ray'					)
								self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,			0.2					)













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


lowercase  							=					{
    "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 						__UpperCAmelCase			(     a_							,     a_							,     a_							,     a_=None):
							# Initialise PyTorch model
							snake_case_				=							XLNetConfig.from_json_file(a_)

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

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

							# Save pytorch-model
							snake_case_				=							os.path.join(a_							,     a_)
							snake_case_				=							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__":
	lowercase  							=					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",
	)
	lowercase  							=					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
	)





