





'''simple docstring'''






import importlib.util
import os
import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import (
    is_accelerate_available,
    is_flax_available,
    is_safetensors_available,
    is_tf_available,
    is_torch_available,
)
from . import BaseTransformersCLICommand
def        UpperCamelCase__				(		lowerCAmelCase  ):





      """simple docstring"""


      return EnvironmentCommand()
def        UpperCamelCase__				(		lowerCAmelCase  ):





      """simple docstring"""


      return EnvironmentCommand(args.accelerate_config_file  )





class 			UpperCAmelCase					(      snake_case_       ):




      @staticmethod
      def     lowercase__   (      __snake_case      :			ArgumentParser  )    ->				str:
            _lowerCAmelCase												=							parser.add_parser("""env"""  )
            download_parser.set_defaults(func=__snake_case  )
            download_parser.add_argument(
                """--accelerate-config_file"""      ,      default=__snake_case      ,      help="""The accelerate config file to use for the default values in the launching script."""      ,      )
            download_parser.set_defaults(func=__snake_case  )




      def __init__(      self      :			Optional[Any]      ,      __snake_case      :			str      ,      *__snake_case      :			Optional[Any]  )    ->				None:
            _lowerCAmelCase												=							accelerate_config_file




      def     lowercase__   (      self      :			int  )    ->				Optional[int]:
            _lowerCAmelCase												=							"""not installed"""
            if is_safetensors_available():
                  import safetensors

                  _lowerCAmelCase												=							safetensors.__version__
            elif importlib.util.find_spec("""safetensors"""  ) is not None:
                  import safetensors

                  _lowerCAmelCase												=							f"{safetensors.__version__} but is ignored because of PyTorch version too old."

            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							_lowerCAmelCase												=							"""not found"""
            if is_accelerate_available():
                  import accelerate
                  from accelerate.commands.config import default_config_file, load_config_from_file

                  _lowerCAmelCase												=							accelerate.__version__
                  # Get the default from the config file.
                  if self._accelerate_config_file is not None or os.path.isfile(__snake_case  ):
                        _lowerCAmelCase												=							load_config_from_file(self._accelerate_config_file  ).to_dict()

                  _lowerCAmelCase												=							(
                      """\n""".join([f"\t- {prop}: {val}" for prop, val in accelerate_config.items()]  )
                      if isinstance(__snake_case      ,      __snake_case  )
                      else f"\t{accelerate_config}"
                  )

            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							"""NA"""
            if is_torch_available():
                  import torch

                  _lowerCAmelCase												=							torch.__version__
                  _lowerCAmelCase												=							torch.cuda.is_available()

            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							"""NA"""
            if is_tf_available():
                  import tensorflow as tf

                  _lowerCAmelCase												=							tf.__version__
                  try:
                        # deprecated in v2.1
                        _lowerCAmelCase												=							tf.test.is_gpu_available()
                  except AttributeError:
                        # returns list of devices, convert to bool
                        _lowerCAmelCase												=							bool(tf.config.list_physical_devices("""GPU"""  )  )

            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							"""not installed"""
            _lowerCAmelCase												=							"""NA"""
            if is_flax_available():
                  import flax
                  import jax
                  import jaxlib

                  _lowerCAmelCase												=							flax.__version__
                  _lowerCAmelCase												=							jax.__version__
                  _lowerCAmelCase												=							jaxlib.__version__
                  _lowerCAmelCase												=							jax.lib.xla_bridge.get_backend().platform

            _lowerCAmelCase												=							{
                """`transformers` version""": version,
                """Platform""": platform.platform(),
                """Python version""": platform.python_version(),
                """Huggingface_hub version""": huggingface_hub.__version__,
                """Safetensors version""": f"{safetensors_version}",
                """Accelerate version""": f"{accelerate_version}",
                """Accelerate config""": f"{accelerate_config_str}",
                """PyTorch version (GPU?)""": f"{pt_version} ({pt_cuda_available})",
                """Tensorflow version (GPU?)""": f"{tf_version} ({tf_cuda_available})",
                """Flax version (CPU?/GPU?/TPU?)""": f"{flax_version} ({jax_backend})",
                """Jax version""": f"{jax_version}",
                """JaxLib version""": f"{jaxlib_version}",
                """Using GPU in script?""": """<fill in>""",
                """Using distributed or parallel set-up in script?""": """<fill in>""",
            }

            print("""\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"""  )
            print(self.format_dict(__snake_case  )  )

            return info




      @staticmethod
      def     lowercase__   (      __snake_case      :			str  )    ->				Optional[int]:
            return "\n".join([f"- {prop}: {val}" for prop, val in d.items()]  ) + "\n"













'''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__    :       Any              =logging.getLogger()

A__    :       int              =logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class 			UpperCAmelCase					(      snake_case_       ):




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




      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			int      ,      __snake_case      :			str = "pytorch"  )    ->				int:
            _lowerCAmelCase												=							self.get_auto_remove_tmp_dir()
            _lowerCAmelCase												=							os.path.join(__snake_case      ,      """output"""  )
            _lowerCAmelCase												=							os.path.join(__snake_case      ,      """data"""  )
            self._create_dummy_data(data_dir=__snake_case  )

            _lowerCAmelCase												=							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												=							[sys.executable, str(Path(finetune_rag.__file__  ).resolve()  )] + testargs
            execute_subprocess_async(__snake_case      ,      env=self.get_env()  )

            _lowerCAmelCase												=							os.path.join(__snake_case      ,      """metrics.json"""  )
            with open(__snake_case  ) as f:
                  _lowerCAmelCase												=							json.load(__snake_case  )
            return result




      @require_torch_gpu
      def     lowercase__   (      self      :			Dict  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self._run_finetune(gpus=1  )
            self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]      ,      0.2  )




      @require_torch_multi_gpu
      def     lowercase__   (      self      :			int  )    ->				Dict:
            _lowerCAmelCase												=							self._run_finetune(gpus=2  )
            self.assertGreaterEqual(result["""test"""][0]["""test_avg_em"""]      ,      0.2  )




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






