


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)
lowerCamelCase						=		logging.getLogger()

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





class  _a							(    _lowercase):




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




	def 							UpperCAmelCase__( self						:		Dict		, _SCREAMING_SNAKE_CASE						:		int		, _SCREAMING_SNAKE_CASE						:		str = "pytorch"		)->	Optional[int]:
			lowerCAmelCase__       :      Optional[int]   					= self.get_auto_remove_tmp_dir()
			lowerCAmelCase__       :      Optional[Any]   					= os.path.join(_SCREAMING_SNAKE_CASE		, '''output'''		)
			lowerCAmelCase__       :      Optional[int]   					= os.path.join(_SCREAMING_SNAKE_CASE		, '''data'''		)
			self._create_dummy_data(data_dir=_SCREAMING_SNAKE_CASE		)

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

			lowerCAmelCase__       :      List[Any]   					= [sys.executable, str(Path(finetune_rag.__file__		).resolve()		)] + testargs
			execute_subprocess_async(_SCREAMING_SNAKE_CASE		, env=self.get_env()		)

			lowerCAmelCase__       :      List[Any]   					= os.path.join(_SCREAMING_SNAKE_CASE		, '''metrics.json'''		)
			with open(_SCREAMING_SNAKE_CASE		) as f:
					lowerCAmelCase__       :      int   					= json.load(_SCREAMING_SNAKE_CASE		)
			return result




	@require_torch_gpu
	def 							UpperCAmelCase__( self						:		List[str]		)->	Dict:
			lowerCAmelCase__       :      int   					= self._run_finetune(gpus=1		)
			self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']		, 0.2		)




	@require_torch_multi_gpu
	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Dict:
			lowerCAmelCase__       :      Optional[int]   					= self._run_finetune(gpus=2		)
			self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']		, 0.2		)




	@require_torch_gpu
	@require_ray
	def 							UpperCAmelCase__( self						:		Optional[Any]		)->	List[Any]:
			lowerCAmelCase__       :      Union[str, Any]   					= 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						:		str		)->	Union[str, Any]:
			lowerCAmelCase__       :      Union[str, Any]   					= self._run_finetune(gpus=1		, distributed_retriever='''ray'''		)
			self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']		, 0.2		)








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase						=		logging.get_logger(__name__)

lowerCamelCase						=		{
    '''facebook/xlm-roberta-xl''': '''https://huggingface.co/facebook/xlm-roberta-xl/resolve/main/config.json''',
    '''facebook/xlm-roberta-xxl''': '''https://huggingface.co/facebook/xlm-roberta-xxl/resolve/main/config.json''',
    # See all XLM-RoBERTa-XL models at https://huggingface.co/models?filter=xlm-roberta-xl
}





class  _a							(    _lowercase):
	_a  :  Optional[int]				   =							'''xlm-roberta-xl'''




	def __init__( self						:		Any		, _SCREAMING_SNAKE_CASE						:		str=25_0880		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=2560		, _SCREAMING_SNAKE_CASE						:		int=36		, _SCREAMING_SNAKE_CASE						:		Optional[int]=32		, _SCREAMING_SNAKE_CASE						:		Any=1_0240		, _SCREAMING_SNAKE_CASE						:		List[str]="gelu"		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=0.1		, _SCREAMING_SNAKE_CASE						:		Dict=0.1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=514		, _SCREAMING_SNAKE_CASE						:		Optional[int]=1		, _SCREAMING_SNAKE_CASE						:		Tuple=0.02		, _SCREAMING_SNAKE_CASE						:		Dict=1E-05		, _SCREAMING_SNAKE_CASE						:		Tuple=1		, _SCREAMING_SNAKE_CASE						:		Optional[int]=0		, _SCREAMING_SNAKE_CASE						:		Tuple=2		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]="absolute"		, _SCREAMING_SNAKE_CASE						:		Any=True		, _SCREAMING_SNAKE_CASE						:		Any=None		, **_SCREAMING_SNAKE_CASE						:		Tuple		, )->	str:
			super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE		, bos_token_id=_SCREAMING_SNAKE_CASE		, eos_token_id=_SCREAMING_SNAKE_CASE		, **_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Any   					= vocab_size
			lowerCAmelCase__       :      List[Any]   					= hidden_size
			lowerCAmelCase__       :      int   					= num_hidden_layers
			lowerCAmelCase__       :      Dict   					= num_attention_heads
			lowerCAmelCase__       :      str   					= hidden_act
			lowerCAmelCase__       :      List[Any]   					= intermediate_size
			lowerCAmelCase__       :      Tuple   					= hidden_dropout_prob
			lowerCAmelCase__       :      str   					= attention_probs_dropout_prob
			lowerCAmelCase__       :      List[str]   					= max_position_embeddings
			lowerCAmelCase__       :      Any   					= type_vocab_size
			lowerCAmelCase__       :      Optional[Any]   					= initializer_range
			lowerCAmelCase__       :      Optional[int]   					= layer_norm_eps
			lowerCAmelCase__       :      Optional[int]   					= position_embedding_type
			lowerCAmelCase__       :      Any   					= use_cache
			lowerCAmelCase__       :      List[Any]   					= classifier_dropout





class  _a							(    _lowercase):




	@property
	def 							UpperCAmelCase__( self						:		Any		)->	Mapping[str, Mapping[int, str]]:
			if self.task == "multiple-choice":
					lowerCAmelCase__       :      int   					= {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
			else:
					lowerCAmelCase__       :      List[Any]   					= {0: '''batch''', 1: '''sequence'''}
			return OrderedDict(
			    [
			        ('''input_ids''', dynamic_axis),
			        ('''attention_mask''', dynamic_axis),
			    ]		)




