






'''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)

UpperCAmelCase__						:Tuple          =			[
    """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 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):




       def 					a_					(					self      : List[Any]	,    A__      : str	,    A__      : bool	,    A__      : str = None	,    A__      : list = None					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				None
            __lowerCamelCase						:       int				   =				os.path.abspath(os.path.join("""examples"""	,    """by_feature"""					)					)
            __lowerCamelCase						:       Union[str, Any]				   =				os.path.abspath("""examples"""					)
            for item in os.listdir(A__					):
                 if item not in EXCLUDE_EXAMPLES:
                      __lowerCamelCase						:       str				   =				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()"""	,    ):
                                __lowerCamelCase						:       Any				   =				compare_against_test(
                                    os.path.join(A__	,    A__					)	,    A__	,    A__	,    A__					)
                                __lowerCamelCase						:       Union[str, Any]				   =				"""\n""".join(A__					)
                                if special_strings is not None:
                                     for string in special_strings:
                                          __lowerCamelCase						:       Optional[Any]				   =				diff.replace(A__	,    """"""					)
                                self.assertEqual(A__	,    """"""					)




       def 					a_					(					self      : Tuple					):


            """simple docstring"""







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




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       str				   =				os.path.abspath(os.path.join("""examples"""	,    """cv_example.py"""					)					)
            __lowerCamelCase						:       Any				   =				[
                """ """ * 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"""	,    A__	,    A__	,    A__					)
            self.one_complete_example("""complete_cv_example.py"""	,    A__	,    A__	,    A__					)







@mock.patch.dict(os.environ    ,   {'TESTING_MOCKED_DATALOADERS': '1'}  )
class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_  ):
       snake_case__    :     List[str]   		=			False




       @classmethod
       def 					a_					(					cls      : str					):


            """simple docstring"""







            super().setUpClass()
            __lowerCamelCase						:       Dict				   =				tempfile.mkdtemp()
            __lowerCamelCase						:       Dict				   =				os.path.join(cls._tmpdir	,    """default_config.yml"""					)

            write_basic_config(save_location=cls.configPath					)
            __lowerCamelCase						:       Dict				   =				["""accelerate""", """launch""", """--config_file""", cls.configPath]




       @classmethod
       def 					a_					(					cls      : Optional[Any]					):


            """simple docstring"""







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




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				f"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        ".split()
            run_command(self._launch_args + testargs					)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir	,    """epoch_0"""					)					)					)




       def 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				f"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
            __lowerCamelCase						:       Optional[int]				   =				run_command(self._launch_args + testargs					)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir	,    """step_2"""					)					)					)




       def 					a_					(					self      : Any					):


            """simple docstring"""







            __lowerCamelCase						:       Tuple				   =				f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir	,    'epoch_0'					)}\n        ".split()
            __lowerCamelCase						:       str				   =				run_command(self._launch_args + testargs	,    return_stdout=A__					)
            self.assertNotIn("""epoch 0:"""	,    A__					)
            self.assertIn("""epoch 1:"""	,    A__					)




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir	,    'step_2'					)}\n        ".split()
            __lowerCamelCase						:       List[Any]				   =				run_command(self._launch_args + testargs	,    return_stdout=A__					)
            if torch.cuda.is_available():
                 __lowerCamelCase						:       Tuple				   =				torch.cuda.device_count()
            else:
                 __lowerCamelCase						:       int				   =				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 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







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




       def 					a_					(					self      : int					):


            """simple docstring"""







            __lowerCamelCase						:       int				   =				["""examples/by_feature/multi_process_metrics.py"""]
            run_command(self._launch_args + testargs					)




       @require_trackers
       @mock.patch.dict(os.environ	,    {"""WANDB_MODE""": """offline"""}					)
       def 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







            with tempfile.TemporaryDirectory() as tmpdir:
                 __lowerCamelCase						:       List[Any]				   =				f"\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            ".split()
                 run_command(self._launch_args + testargs					)
                 self.assertTrue(os.path.exists(os.path.join(A__	,    """tracking"""					)					)					)




       def 					a_					(					self      : List[str]					):


            """simple docstring"""







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




       def 					a_					(					self      : List[str]					):


            """simple docstring"""







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











'''simple docstring'''

import argparse
import json

from tqdm import tqdm
def     __lowercase  ()   ->     Tuple:


     """simple docstring"""
     __lowerCamelCase						:       Union[str, Any]				   =				argparse.ArgumentParser()

     # Required parameters
     parser.add_argument(
         """--src_path""",						type=_lowercase,						default="""biencoder-nq-dev.json""",						help="""Path to raw DPR training data""",						)
     parser.add_argument(
         """--evaluation_set""",						type=_lowercase,						help="""where to store parsed evaluation_set file""",						)
     parser.add_argument(
         """--gold_data_path""",						type=_lowercase,						help="""where to store parsed gold_data_path file""",						)
     __lowerCamelCase						:       Tuple				   =				parser.parse_args()

     with open(args.src_path,						"""r"""     ) as src_file, open(args.evaluation_set,						"""w"""     ) as eval_file, open(
         args.gold_data_path,						"""w"""     ) as gold_file:
          __lowerCamelCase						:       int				   =				json.load(_lowercase     )
          for dpr_record in tqdm(_lowercase     ):
               __lowerCamelCase						:       List[Any]				   =				dpr_record["""question"""]
               __lowerCamelCase						:       List[str]				   =				[context["""title"""] for context in dpr_record["""positive_ctxs"""]]
               eval_file.write(question + """\n"""     )
               gold_file.write("""\t""".join(_lowercase     ) + """\n"""     )


if __name__ == "__main__":
   main()



