from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def 			lowercase(	UpperCamelCase_       )		->	Union[str, Any]:







 '''simple docstring'''
 return ConvertCommand(
     args.model_type							,					args.tf_checkpoint							,					args.pytorch_dump_output							,					args.config							,					args.finetuning_task_name       )


_SCREAMING_SNAKE_CASE 		=      """
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
"""


class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):







   @staticmethod
   def 						lowerCamelCase_	(     lowerCamelCase_						:						ArgumentParser				):





    """simple docstring"""


    UpperCamelCase			  =      parser.add_parser(
        """convert"""   ,   help="""CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."""   ,   )
    train_parser.add_argument("""--model_type"""   ,   type=lowerCamelCase_   ,   required=lowerCamelCase_   ,   help="""Model's type."""				)
    train_parser.add_argument(
        """--tf_checkpoint"""   ,   type=lowerCamelCase_   ,   required=lowerCamelCase_   ,   help="""TensorFlow checkpoint path or folder."""				)
    train_parser.add_argument(
        """--pytorch_dump_output"""   ,   type=lowerCamelCase_   ,   required=lowerCamelCase_   ,   help="""Path to the PyTorch saved model output."""				)
    train_parser.add_argument("""--config"""   ,   type=lowerCamelCase_   ,   default=""""""   ,   help="""Configuration file path or folder."""				)
    train_parser.add_argument(
        """--finetuning_task_name"""   ,   type=lowerCamelCase_   ,   default=lowerCamelCase_   ,   help="""Optional fine-tuning task name if the TF model was a finetuned model."""   ,   )
    train_parser.set_defaults(func=lowerCamelCase_				)







   def __init__(     self						:						Union[str, Any]   ,   lowerCamelCase_						:						str   ,   lowerCamelCase_						:						str   ,   lowerCamelCase_						:						str   ,   lowerCamelCase_						:						str   ,   lowerCamelCase_						:						str   ,   *lowerCamelCase_						:						Any   ,   ):





    """simple docstring"""


    UpperCamelCase			  =      logging.get_logger("""transformers-cli/converting"""				)

    self._logger.info(f"""Loading model {model_type}"""				)
    UpperCamelCase			  =      model_type
    UpperCamelCase			  =      tf_checkpoint
    UpperCamelCase			  =      pytorch_dump_output
    UpperCamelCase			  =      config
    UpperCamelCase			  =      finetuning_task_name







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    if self._model_type == "albert":
     try:
      from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
          convert_tf_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "bert":
     try:
      from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
          convert_tf_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "funnel":
     try:
      from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
          convert_tf_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "t5":
     try:
      from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_tf_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "gpt":
     from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
         convert_openai_checkpoint_to_pytorch,
     )

     convert_openai_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "transfo_xl":
     try:
      from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
          convert_transfo_xl_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     if "ckpt" in self._tf_checkpoint.lower():
      UpperCamelCase			  =      self._tf_checkpoint
      UpperCamelCase			  =      """"""
     else:
      UpperCamelCase			  =      self._tf_checkpoint
      UpperCamelCase			  =      """"""
     convert_transfo_xl_checkpoint_to_pytorch(
         lowerCamelCase_   ,   self._config   ,   self._pytorch_dump_output   ,   lowerCamelCase_				)
    elif self._model_type == "gpt2":
     try:
      from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
          convert_gpta_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    elif self._model_type == "xlnet":
     try:
      from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
          convert_xlnet_checkpoint_to_pytorch,
      )
     except ImportError:
      raise ImportError(lowerCamelCase_				)

     convert_xlnet_checkpoint_to_pytorch(
         self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output   ,   self._finetuning_task_name				)
    elif self._model_type == "xlm":
     from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
         convert_xlm_checkpoint_to_pytorch,
     )

     convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._pytorch_dump_output				)
    elif self._model_type == "lxmert":
     from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
         convert_lxmert_checkpoint_to_pytorch,
     )

     convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._pytorch_dump_output				)
    elif self._model_type == "rembert":
     from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
         convert_rembert_tf_checkpoint_to_pytorch,
     )

     convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint   ,   self._config   ,   self._pytorch_dump_output				)
    else:
     raise ValueError(
         """--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]"""				)

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

_SCREAMING_SNAKE_CASE 		=      logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase		):







   def 						lowerCamelCase_	(     self						:						Union[str, Any]   ,   lowerCamelCase_						:						List[str]				):





    """simple docstring"""


    os.makedirs(lowerCamelCase_   ,   exist_ok=lowerCamelCase_				)
    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(lowerCamelCase_   ,   f"""{split}.{field}"""				)   ,   """w"""				) as f:
       f.write(lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Tuple   ,   lowerCamelCase_						:						int   ,   lowerCamelCase_						:						str = "pytorch"				):





    """simple docstring"""


    UpperCamelCase			  =      self.get_auto_remove_tmp_dir()
    UpperCamelCase			  =      os.path.join(lowerCamelCase_   ,   """output"""				)
    UpperCamelCase			  =      os.path.join(lowerCamelCase_   ,   """data"""				)
    self._create_dummy_data(data_dir=lowerCamelCase_				)

    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(lowerCamelCase_   ,   env=self.get_env()				)

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







   @require_torch_gpu
   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


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







   @require_torch_multi_gpu
   def 						lowerCamelCase_	(     self						:						List[Any]				):





    """simple docstring"""


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







   @require_torch_gpu
   @require_ray
   def 						lowerCamelCase_	(     self						:						Union[str, Any]				):





    """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 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


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