





# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import platform

import numpy as np
import psutil
import torch

from accelerate import __version__ as version
from accelerate.commands.config import default_config_file, load_config_from_file

from ..utils import is_npu_available, is_xpu_available






def 	lowerCamelCase_		(							_UpperCamelCase=None					)						->		Optional[int]:






				"""simple docstring"""


				if subparsers is not None:
								snake_case_		:       Union[str, Any]           =    subparsers.add_parser('''env'''					)
				else:
								snake_case_		:       Optional[int]           =    argparse.ArgumentParser('''Accelerate env command'''					)

				parser.add_argument(
				    '''--config_file'''					,							default=_UpperCamelCase					,							help='''The config file to use for the default values in the launching script.'''					)

				if subparsers is not None:
								parser.set_defaults(func=_UpperCamelCase					)
				return parser






def 	lowerCamelCase_		(							_UpperCamelCase					)						->		Any:






				"""simple docstring"""


				snake_case_		:       Tuple           =    torch.__version__
				snake_case_		:       List[Any]           =    torch.cuda.is_available()
				snake_case_		:       str           =    is_xpu_available()
				snake_case_		:       List[Any]           =    is_npu_available()

				snake_case_		:       List[Any]           =    '''Not found'''
				# Get the default from the config file.
				if args.config_file is not None or os.path.isfile(_UpperCamelCase					):
								snake_case_		:       Any           =    load_config_from_file(args.config_file					).to_dict()

				snake_case_		:       Dict           =    {
				    '''`Accelerate` version''': version,
				    '''Platform''': platform.platform(),
				    '''Python version''': platform.python_version(),
				    '''Numpy version''': np.__version__,
				    '''PyTorch version (GPU?)''': f'''{pt_version} ({pt_cuda_available})''',
				    '''PyTorch XPU available''': str(_UpperCamelCase					),
				    '''PyTorch NPU available''': str(_UpperCamelCase					),
				    '''System RAM''': f'''{psutil.virtual_memory().total / 1_024 ** 3:.2f} GB''',
				}
				if pt_cuda_available:
								snake_case_		:       List[str]           =    torch.cuda.get_device_name()

				print('''\nCopy-and-paste the text below in your GitHub issue\n'''					)
				print('''\n'''.join([f'''- {prop}: {val}''' for prop, val in info.items()]					)					)

				print('''- `Accelerate` default config:''' if args.config_file is None else '''- `Accelerate` config passed:'''					)
				snake_case_		:       Union[str, Any]           =    (
				    '''\n'''.join([f'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()]					)
				    if isinstance(_UpperCamelCase					,							_UpperCamelCase					)
				    else f'''\t{accelerate_config}'''
				)
				print(_UpperCamelCase					)

				snake_case_		:       Dict           =    accelerate_config

				return info






def 	lowerCamelCase_		(							)						->		int:






				"""simple docstring"""


				snake_case_		:       List[Any]           =    env_command_parser()
				snake_case_		:       int           =    parser.parse_args()
				env_command(_UpperCamelCase					)
				return 0


if __name__ == "__main__":
	raise SystemExit(main())

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)
SCREAMING_SNAKE_CASE			:						List[Any]				  =		logging.getLogger()

SCREAMING_SNAKE_CASE			:						List[str]				  =		logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
class  A_			(							a_			):

  def 		_UpperCAmelCase (		self			:   str  ,				__SCREAMING_SNAKE_CASE			:   int							):
   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 		_UpperCAmelCase (		self			:   str  ,				__SCREAMING_SNAKE_CASE			:   int  ,				__SCREAMING_SNAKE_CASE			:   str = "pytorch"							):
   __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"""
                --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							=	[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 		_UpperCAmelCase (		self			:   Dict							):
   __a							=	self._run_finetune(gpus=1							)
   self.assertGreaterEqual(result["test"][0]["test_avg_em"]  ,				0.2							)

  @require_torch_multi_gpu
  def 		_UpperCAmelCase (		self			:   Optional[int]							):
   __a							=	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]							):
   __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 		_UpperCAmelCase (		self			:   Any							):
   __a							=	self._run_finetune(gpus=1  ,				distributed_retriever="ray"							)
   self.assertGreaterEqual(result["test"][0]["test_avg_em"]  ,				0.2							)


