





from collections import OrderedDict
from typing import Mapping

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


__lowerCamelCase       :     List[Any]   =     logging.get_logger(__name__)

__lowerCamelCase       :     str   =     {
    """roberta-base""": """https://huggingface.co/roberta-base/resolve/main/config.json""",
    """roberta-large""": """https://huggingface.co/roberta-large/resolve/main/config.json""",
    """roberta-large-mnli""": """https://huggingface.co/roberta-large-mnli/resolve/main/config.json""",
    """distilroberta-base""": """https://huggingface.co/distilroberta-base/resolve/main/config.json""",
    """roberta-base-openai-detector""": """https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json""",
    """roberta-large-openai-detector""": """https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json""",
}



class 					A__   (       __snake_case			):
      _UpperCAmelCase	:Union[str, Any]						    =   'roberta'





      def __init__( self       ,							A_=5_0265       ,							A_=768       ,							A_=12       ,							A_=12       ,							A_=3072       ,							A_="gelu"       ,							A_=0.1       ,							A_=0.1       ,							A_=512       ,							A_=2       ,							A_=0.02       ,							A_=1e-12       ,							A_=1       ,							A_=0       ,							A_=2       ,							A_="absolute"       ,							A_=True       ,							A_=None       ,							**A_       ,							):
       '''simple docstring'''




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

       UpperCamelCase	:					Optional[int]       	=   vocab_size
       UpperCamelCase	:					Dict       	=   hidden_size
       UpperCamelCase	:					str       	=   num_hidden_layers
       UpperCamelCase	:					Any       	=   num_attention_heads
       UpperCamelCase	:					List[str]       	=   hidden_act
       UpperCamelCase	:					Optional[Any]       	=   intermediate_size
       UpperCamelCase	:					Tuple       	=   hidden_dropout_prob
       UpperCamelCase	:					Tuple       	=   attention_probs_dropout_prob
       UpperCamelCase	:					Tuple       	=   max_position_embeddings
       UpperCamelCase	:					Any       	=   type_vocab_size
       UpperCamelCase	:					int       	=   initializer_range
       UpperCamelCase	:					str       	=   layer_norm_eps
       UpperCamelCase	:					Dict       	=   position_embedding_type
       UpperCamelCase	:					Any       	=   use_cache
       UpperCamelCase	:					Union[str, Any]       	=   classifier_dropout



class 					A__   (       __snake_case			):





      @property
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       if self.task == "multiple-choice":
        UpperCamelCase	:					Optional[int]       	=   {0: "batch", 1: "choice", 2: "sequence"}
       else:
        UpperCamelCase	:					Optional[int]       	=   {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
           ]   )








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

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





class __lowerCamelCase (      snake_case__):





	"""simple docstring"""






	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							os.makedirs(UpperCAmelCase       ,    exist_ok=UpperCAmelCase    )
							_UpperCAmelCase									=			{'source': 'What is love ?', 'target': 'life'}
							_UpperCAmelCase									=			{'train': 12, 'val': 2, 'test': 2}
							for split in ["train", "test", "val"]:
													for field in ["source", "target"]:
																			_UpperCAmelCase									=			'\n'.join([contents[field]] * n_lines[split]    )
																			with open(os.path.join(UpperCAmelCase       ,    F"""{split}.{field}"""    )       ,    'w'    ) as f:
																									f.write(UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase = "pytorch"    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.get_auto_remove_tmp_dir()
							_UpperCAmelCase									=			os.path.join(UpperCAmelCase       ,    'output'    )
							_UpperCAmelCase									=			os.path.join(UpperCAmelCase       ,    'data'    )
							self._create_dummy_data(data_dir=UpperCAmelCase    )

							_UpperCAmelCase									=			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'    )

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

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

	@require_torch_gpu
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=1    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )

	@require_torch_multi_gpu
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=2    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )

	@require_torch_gpu
	@require_ray
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			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    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=1       ,    distributed_retriever='ray'    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )
