



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)
__snake_case						:Tuple      		=     logging.getLogger()

__snake_case						:Optional[int]      		=     logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

class     _A       (	__UpperCAmelCase    ):



       def 		_lowerCamelCase			(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]):


           '''simple docstring'''

           os.makedirs(__SCREAMING_SNAKE_CASE     ,		exist_ok=__SCREAMING_SNAKE_CASE)
           __a						       =				{'''source''': '''What is love ?''', '''target''': '''life'''}
           __a						       =				{'''train''': 12, '''val''': 2, '''test''': 2}
           for split in ["train", "test", "val"]:
               for field in ["source", "target"]:
                   __a						       =				'''\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 		_lowerCamelCase			(		self							:     Optional[int]     ,		__SCREAMING_SNAKE_CASE							:     int     ,		__SCREAMING_SNAKE_CASE							:     str = "pytorch"):


           '''simple docstring'''

           __a						       =				self.get_auto_remove_tmp_dir()
           __a						       =				os.path.join(__SCREAMING_SNAKE_CASE     ,		'''output''')
           __a						       =				os.path.join(__SCREAMING_SNAKE_CASE     ,		'''data''')
           self._create_dummy_data(data_dir=__SCREAMING_SNAKE_CASE)

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

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

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



       @require_torch_gpu
       def 		_lowerCamelCase			(		self							:     str):


           '''simple docstring'''

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



       @require_torch_multi_gpu
       def 		_lowerCamelCase			(		self							:     Dict):


           '''simple docstring'''

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



       @require_torch_gpu
       @require_ray
       def 		_lowerCamelCase			(		self							:     Optional[int]):


           '''simple docstring'''

           __a						       =				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 		_lowerCamelCase			(		self							:     Tuple):


           '''simple docstring'''

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











import copy

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


_lowerCamelCase	:		Any							       =   logging.get_logger(__name__)


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      '''encoder-decoder'''
    UpperCAmelCase__							  =      True


    def __init__(			self      :	List[str]					, **UpperCAmelCase__      :	Union[str, Any])						->List[Any]:





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)
           assert (
               "encoder" in kwargs and "decoder" in kwargs
           ), "Config has to be initialized with encoder and decoder config"
           A__       =    kwargs.pop('''encoder''')
           A__       =    encoder_config.pop('''model_type''')
           A__       =    kwargs.pop('''decoder''')
           A__       =    decoder_config.pop('''model_type''')

           from ..auto.configuration_auto import AutoConfig

           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    True


    @classmethod
    def        SCREAMING_SNAKE_CASE							(			cls      :	Union[str, Any]					, UpperCAmelCase__      :	PretrainedConfig					, UpperCAmelCase__      :	PretrainedConfig					, **UpperCAmelCase__      :	Union[str, Any])						->PretrainedConfig:





           '''simple docstring'''

           logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config''')
           A__       =    True
           A__       =    True

           return cls(encoder=encoder_config.to_dict()					, decoder=decoder_config.to_dict()					, **UpperCAmelCase__)





    def        SCREAMING_SNAKE_CASE							(			self      :	str)						->Optional[Any]:





           '''simple docstring'''

           A__       =    copy.deepcopy(self.__dict__)
           A__       =    self.encoder.to_dict()
           A__       =    self.decoder.to_dict()
           A__       =    self.__class__.model_type
           return output


