
import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

lowerCAmelCase			:							List[str]     =[
    "cross_validation.py",
    "gradient_accumulation.py",
    "local_sgd.py",
    "multi_process_metrics.py",
    "memory.py",
    "automatic_gradient_accumulation.py",
    "fsdp_with_peak_mem_tracking.py",
    "deepspeed_with_config_support.py",
    "megatron_lm_gpt_pretraining.py",
]




class 	__snake_case	( unittest.TestCase		):





      '''simple docstring'''






      def 	_SCREAMING_SNAKE_CASE    (					self     :				int    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				bool    ,  _UpperCamelCase     :				str = None    ,  _UpperCamelCase     :				list = None)				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						None
          _lowerCamelCase     :		str 						=						os.path.abspath(os.path.join("""examples"""    ,  """by_feature"""))
          _lowerCamelCase     :		Any 						=						os.path.abspath("""examples""")
          for item in os.listdir(_UpperCamelCase):
              if item not in EXCLUDE_EXAMPLES:
                  _lowerCamelCase     :		int 						=						os.path.join(_UpperCamelCase    ,  _UpperCamelCase)
                  if os.path.isfile(_UpperCamelCase) and ".py" in item_path:
                      with self.subTest(
                          tested_script=_UpperCamelCase    ,  feature_script=_UpperCamelCase    ,  tested_section="""main()""" if parser_only else """training_function()"""    ,  ):
                          _lowerCamelCase     :		Dict 						=						compare_against_test(
                              os.path.join(_UpperCamelCase    ,  _UpperCamelCase)    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)
                          _lowerCamelCase     :		Optional[Any] 						=						"""\n""".join(_UpperCamelCase)
                          if special_strings is not None:
                              for string in special_strings:
                                  _lowerCamelCase     :		Union[str, Any] 						=						diff.replace(_UpperCamelCase    ,  """""")
                          self.assertEqual(_UpperCamelCase    ,  """""")



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->Any:


          """simple docstring"""


          self.one_complete_example("""complete_nlp_example.py"""    ,  _UpperCamelCase)
          self.one_complete_example("""complete_nlp_example.py"""    ,  _UpperCamelCase)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->str:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						os.path.abspath(os.path.join("""examples"""    ,  """cv_example.py"""))
          _lowerCamelCase     :		Dict 						=						[
              """ """ * 16 + """{\n\n""",
              """ """ * 20 + """\"accuracy\": eval_metric[\"accuracy\"],\n\n""",
              """ """ * 20 + """\"f1\": eval_metric[\"f1\"],\n\n""",
              """ """ * 20 + """\"train_loss\": total_loss.item() / len(train_dataloader),\n\n""",
              """ """ * 20 + """\"epoch\": epoch,\n\n""",
              """ """ * 16 + """},\n\n""",
              """ """ * 16 + """step=epoch,\n""",
              """ """ * 12,
              """ """ * 8 + """for step, batch in enumerate(active_dataloader):\n""",
          ]
          self.one_complete_example("""complete_cv_example.py"""    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)
          self.one_complete_example("""complete_cv_example.py"""    ,  _UpperCamelCase    ,  _UpperCamelCase    ,  _UpperCamelCase)





@mock.patch.dict(os.environ ,  {'TESTING_MOCKED_DATALOADERS': '1'}		)
class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       False



      @classmethod
      def 	_SCREAMING_SNAKE_CASE    (					cls     :				Any)				->List[Any]:


          """simple docstring"""


          super().setUpClass()
          _lowerCamelCase     :		str 						=						tempfile.mkdtemp()
          _lowerCamelCase     :		Optional[Any] 						=						os.path.join(cls._tmpdir    ,  """default_config.yml""")

          write_basic_config(save_location=cls.configPath)
          _lowerCamelCase     :		List[str] 						=						["""accelerate""", """launch""", """--config_file""", cls.configPath]



      @classmethod
      def 	_SCREAMING_SNAKE_CASE    (					cls     :				Dict)				->Optional[Any]:


          """simple docstring"""


          super().tearDownClass()
          shutil.rmtree(cls._tmpdir)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        """.split()
          run_command(self._launch_args + testargs)
          self.assertTrue(os.path.exists(os.path.join(self.tmpdir    ,  """epoch_0""")))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
          _lowerCamelCase     :		Any 						=						run_command(self._launch_args + testargs)
          self.assertTrue(os.path.exists(os.path.join(self.tmpdir    ,  """step_2""")))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir    ,  'epoch_0')}
        """.split()
          _lowerCamelCase     :		Any 						=						run_command(self._launch_args + testargs    ,  return_stdout=_UpperCamelCase)
          self.assertNotIn("""epoch 0:"""    ,  _UpperCamelCase)
          self.assertIn("""epoch 1:"""    ,  _UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->int:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir    ,  'step_2')}
        """.split()
          _lowerCamelCase     :		Dict 						=						run_command(self._launch_args + testargs    ,  return_stdout=_UpperCamelCase)
          if torch.cuda.is_available():
              _lowerCamelCase     :		List[str] 						=						torch.cuda.device_count()
          else:
              _lowerCamelCase     :		Dict 						=						1
          if num_processes > 1:
              self.assertNotIn("""epoch 0:"""    ,  _UpperCamelCase)
              self.assertIn("""epoch 1:"""    ,  _UpperCamelCase)
          else:
              self.assertIn("""epoch 0:"""    ,  _UpperCamelCase)
              self.assertIn("""epoch 1:"""    ,  _UpperCamelCase)



      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str])				->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						"""
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
          with mock.patch.dict(os.environ    ,  {"""TESTING_MOCKED_DATALOADERS""": """0"""}):
              _lowerCamelCase     :		Any 						=						run_command(self._launch_args + testargs    ,  return_stdout=_UpperCamelCase)
              _lowerCamelCase     :		List[Any] 						=						re.findall("""({.+})"""    ,  _UpperCamelCase)
              _lowerCamelCase     :		List[Any] 						=						[r for r in results if """accuracy""" in r][-1]
              _lowerCamelCase     :		Optional[Any] 						=						ast.literal_eval(_UpperCamelCase)
              self.assertGreaterEqual(results["""accuracy"""]    ,  0.7_5)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						["""examples/by_feature/multi_process_metrics.py"""]
          run_command(self._launch_args + testargs)



      @require_trackers
      @mock.patch.dict(os.environ    ,  {"""WANDB_MODE""": """offline"""})
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any])				->Any:


          """simple docstring"""


          with tempfile.TemporaryDirectory() as tmpdir:
              _lowerCamelCase     :		int 						=						F"""
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            """.split()
              run_command(self._launch_args + testargs)
              self.assertTrue(os.path.exists(os.path.join(_UpperCamelCase    ,  """tracking""")))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						["""examples/by_feature/gradient_accumulation.py"""]
          run_command(self._launch_args + testargs)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						["""examples/by_feature/local_sgd.py"""]
          run_command(self._launch_args + testargs)





import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


lowerCAmelCase			:							int     ={
    "cola": 2,
    "mnli": 3,
    "mrpc": 2,
    "sst-2": 2,
    "sts-b": 1,
    "qqp": 2,
    "qnli": 2,
    "rte": 2,
    "wnli": 2,
}


logging.set_verbosity_info()







def 						A__  (					__A   , __A   , __A   , __A=None	):







    '''simple docstring'''
    # Initialise PyTorch model
    _lowerCamelCase     :		Tuple 						=						XLNetConfig.from_json_file(__A	)

    _lowerCamelCase     :		List[Any] 						=						finetuning_task.lower() if finetuning_task is not None else """"""
    if finetuning_task in GLUE_TASKS_NUM_LABELS:
        print(F"""Building PyTorch XLNetForSequenceClassification model from configuration: {config}"""	)
        _lowerCamelCase     :		int 						=						finetuning_task
        _lowerCamelCase     :		Union[str, Any] 						=						GLUE_TASKS_NUM_LABELS[finetuning_task]
        _lowerCamelCase     :		int 						=						XLNetForSequenceClassification(__A	)
    elif "squad" in finetuning_task:
        _lowerCamelCase     :		Dict 						=						finetuning_task
        _lowerCamelCase     :		Optional[Any] 						=						XLNetForQuestionAnswering(__A	)
    else:
        _lowerCamelCase     :		Any 						=						XLNetLMHeadModel(__A	)

    # Load weights from tf checkpoint
    load_tf_weights_in_xlnet(__A   , __A   , __A	)

    # Save pytorch-model
    _lowerCamelCase     :		Optional[Any] 						=						os.path.join(__A   , __A	)
    _lowerCamelCase     :		Any 						=						os.path.join(__A   , __A	)
    print(F"""Save PyTorch model to {os.path.abspath(__A	)}"""	)
    torch.save(model.state_dict()   , __A	)
    print(F"""Save configuration file to {os.path.abspath(__A	)}"""	)
    with open(__A   , """w"""   , encoding="""utf-8"""	) as f:
        f.write(config.to_json_string()	)


if __name__ == "__main__":
    lowerCAmelCase			:							Dict     =argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--xlnet_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained XLNet model. \n"
            "This specifies the model architecture."
        ),
    )
    parser.add_argument(
        "--pytorch_dump_folder_path",
        default=None,
        type=str,
        required=True,
        help="Path to the folder to store the PyTorch model or dataset/vocab.",
    )
    parser.add_argument(
        "--finetuning_task",
        default=None,
        type=str,
        help="Name of a task on which the XLNet TensorFlow model was fine-tuned",
    )
    lowerCAmelCase			:							Union[str, Any]     =parser.parse_args()
    print(args)

    convert_xlnet_checkpoint_to_pytorch(
        args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
    )




