





"""simple docstring"""






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 					__lowercase      (      _UpperCamelCase				):







					'''simple docstring'''




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







					def 			_lowerCamelCase		(    self						,      _UpperCAmelCase						,      _UpperCAmelCase = "pytorch"  ):
									__a			:    Optional[int]      							=	self.get_auto_remove_tmp_dir()
									__a			:    Optional[int]      							=	os.path.join(_UpperCAmelCase						,      '''output'''  )
									__a			:    Optional[int]      							=	os.path.join(_UpperCAmelCase						,      '''data'''  )
									self._create_dummy_data(data_dir=_UpperCAmelCase  )

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

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

									__a			:    Optional[Any]      							=	os.path.join(_UpperCAmelCase						,      '''metrics.json'''  )
									with open(_UpperCAmelCase  ) as f:
													__a			:    Dict      							=	json.load(_UpperCAmelCase  )
									return result







					@require_torch_gpu
					def 			_lowerCamelCase		(    self  ):
									__a			:    Optional[int]      							=	self._run_finetune(gpus=1  )
									self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,      0.2  )







					@require_torch_multi_gpu
					def 			_lowerCamelCase		(    self  ):
									__a			:    Optional[int]      							=	self._run_finetune(gpus=2  )
									self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,      0.2  )







					@require_torch_gpu
					@require_ray
					def 			_lowerCamelCase		(    self  ):
									__a			:    Dict      							=	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  ):
									__a			:    Dict      							=	self._run_finetune(gpus=1						,      distributed_retriever='''ray'''  )
									self.assertGreaterEqual(result['''test'''][0]['''test_avg_em''']						,      0.2  )

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


lowerCamelCase  =logging.get_logger(__name__)

lowerCamelCase  ={
    "abeja/gpt-neox-japanese-2.7b": "https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json",
}




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	'''gpt_neox_japanese'''






     def __init__(				self    ,    __SCREAMING_SNAKE_CASE=3_2_0_0_0    ,    __SCREAMING_SNAKE_CASE=2_5_6_0    ,    __SCREAMING_SNAKE_CASE=3_2    ,    __SCREAMING_SNAKE_CASE=3_2    ,    __SCREAMING_SNAKE_CASE=4    ,    __SCREAMING_SNAKE_CASE="gelu"    ,    __SCREAMING_SNAKE_CASE=1.00    ,    __SCREAMING_SNAKE_CASE=1_0_0_0_0    ,    __SCREAMING_SNAKE_CASE=2_0_4_8    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=1e-5    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=3_1_9_9_6    ,    __SCREAMING_SNAKE_CASE=3_1_9_9_9    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.0    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> List[str]:




           """simple docstring"""







           super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE    ,    eos_token_id=__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)
           UpperCamelCase__     : List[str]											=					vocab_size
           UpperCamelCase__     : int											=					max_position_embeddings
           UpperCamelCase__     : Union[str, Any]											=					hidden_size
           UpperCamelCase__     : Any											=					num_hidden_layers
           UpperCamelCase__     : List[Any]											=					num_attention_heads
           UpperCamelCase__     : int											=					intermediate_multiple_size
           UpperCamelCase__     : List[Any]											=					hidden_act
           UpperCamelCase__     : Tuple											=					rotary_pct
           UpperCamelCase__     : Optional[int]											=					rotary_emb_base
           UpperCamelCase__     : Optional[Any]											=					initializer_range
           UpperCamelCase__     : Optional[int]											=					layer_norm_eps
           UpperCamelCase__     : Optional[Any]											=					use_cache
           UpperCamelCase__     : List[str]											=					attention_dropout
           UpperCamelCase__     : Optional[int]											=					hidden_dropout


