



"""simple docstring"""
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)

SCREAMING_SNAKE_CASE_				= [
    """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				):






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






							def snake_case_  (   self      ):
									self.one_complete_example("complete_nlp_example.py"			,     a_      )
									self.one_complete_example("complete_nlp_example.py"			,     a_      )






							def snake_case_  (   self      ):
									a_     :      Optional[int] 						=     os.path.abspath(os.path.join("examples"			,     "cv_example.py"      )      )
									a_     :      List[str] 						=     [
									    " " * 1_6 + "{\n\n",
									    " " * 2_0 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n",
									    " " * 2_0 + "\"f1\": eval_metric[\"f1\"],\n\n",
									    " " * 2_0 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n",
									    " " * 2_0 + "\"epoch\": epoch,\n\n",
									    " " * 1_6 + "},\n\n",
									    " " * 1_6 + "step=epoch,\n",
									    " " * 1_2,
									    " " * 8 + "for step, batch in enumerate(active_dataloader):\n",
									]
									self.one_complete_example("complete_cv_example.py"			,     a_			,     a_			,     a_      )
									self.one_complete_example("complete_cv_example.py"			,     a_			,     a_			,     a_      )




@mock.patch.dict(os.environ     ,{"TESTING_MOCKED_DATALOADERS": "1"}				)
class 			snake_case_						(						a_				):
							__lowerCAmelCase    					=					False






							@classmethod
							def snake_case_  (   cls      ):
									super().setUpClass()
									a_     :      Tuple 						=     tempfile.mkdtemp()
									a_     :      Optional[Any] 						=     os.path.join(cls._tmpdir			,     "default_config.yml"      )

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






							@classmethod
							def snake_case_  (   cls      ):
									super().tearDownClass()
									shutil.rmtree(cls._tmpdir      )






							def snake_case_  (   self      ):
									a_     :      Optional[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 snake_case_  (   self      ):
									a_     :      Tuple 						=     F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
									a_     :      Optional[int] 						=     run_command(self._launch_args + testargs      )
									self.assertTrue(os.path.exists(os.path.join(self.tmpdir			,     "step_2"      )      )      )






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






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






							@slow
							def snake_case_  (   self      ):
									a_     :      Dict 						=     "\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
									with mock.patch.dict(os.environ			,     {"TESTING_MOCKED_DATALOADERS": "0"}      ):
											a_     :      int 						=     run_command(self._launch_args + testargs			,     return_stdout=a_      )
											a_     :      Union[str, Any] 						=     re.findall("({.+})"			,     a_      )
											a_     :      Dict 						=     [r for r in results if "accuracy" in r][-1]
											a_     :      Dict 						=     ast.literal_eval(a_      )
											self.assertGreaterEqual(results["accuracy"]			,     0.75      )






							def snake_case_  (   self      ):
									a_     :      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 snake_case_  (   self      ):
									with tempfile.TemporaryDirectory() as tmpdir:
											a_     :      Dict 						=     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(a_			,     "tracking"      )      )      )






							def snake_case_  (   self      ):
									a_     :      int 						=     ["examples/by_feature/gradient_accumulation.py"]
									run_command(self._launch_args + testargs      )






							def snake_case_  (   self      ):
									a_     :      Dict 						=     ["examples/by_feature/local_sgd.py"]
									run_command(self._launch_args + testargs      )




"""simple docstring"""
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


SCREAMING_SNAKE_CASE_				= {
    """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        lowerCAmelCase_						(							SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__=None	)			->      int:
		# Initialise PyTorch model
		a_     :      Optional[int] 						=     XLNetConfig.from_json_file(SCREAMING_SNAKE_CASE__	)

		a_     :      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}"""	)
				a_     :      Tuple 						=     finetuning_task
				a_     :      Dict 						=     GLUE_TASKS_NUM_LABELS[finetuning_task]
				a_     :      Tuple 						=     XLNetForSequenceClassification(SCREAMING_SNAKE_CASE__	)
		elif "squad" in finetuning_task:
				a_     :      Tuple 						=     finetuning_task
				a_     :      List[Any] 						=     XLNetForQuestionAnswering(SCREAMING_SNAKE_CASE__	)
		else:
				a_     :      Any 						=     XLNetLMHeadModel(SCREAMING_SNAKE_CASE__	)

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

		# Save pytorch-model
		a_     :      Tuple 						=     os.path.join(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)
		a_     :      str 						=     os.path.join(SCREAMING_SNAKE_CASE__, SCREAMING_SNAKE_CASE__	)
		print(F"""Save PyTorch model to {os.path.abspath(SCREAMING_SNAKE_CASE__	)}"""	)
		torch.save(model.state_dict(), SCREAMING_SNAKE_CASE__	)
		print(F"""Save configuration file to {os.path.abspath(SCREAMING_SNAKE_CASE__	)}"""	)
		with open(SCREAMING_SNAKE_CASE__, "w", encoding="utf-8"	) as f:
				f.write(config.to_json_string()	)


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE_				= 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""",
					)
					SCREAMING_SNAKE_CASE_				= 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
					)