


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)

__a     =			[
    """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   __lowercase				(  unittest.TestCase  ):





      def        _lowercase				(		self						:					Dict					,       __lowerCamelCase						:					str					,       __lowerCamelCase						:					bool					,       __lowerCamelCase						:					str = None					,       __lowerCamelCase						:					list = None       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     None
         UpperCAmelCase							=     os.path.abspath(os.path.join("""examples"""					,       """by_feature"""       )       )
         UpperCAmelCase							=     os.path.abspath("""examples"""       )
         for item in os.listdir(__lowerCamelCase       ):
            if item not in EXCLUDE_EXAMPLES:
               UpperCAmelCase							=     os.path.join(__lowerCamelCase					,       __lowerCamelCase       )
               if os.path.isfile(__lowerCamelCase       ) and ".py" in item_path:
                  with self.subTest(
                      tested_script=__lowerCamelCase					,       feature_script=__lowerCamelCase					,       tested_section="""main()""" if parser_only else """training_function()"""					,       ):
                     UpperCAmelCase							=     compare_against_test(
                         os.path.join(__lowerCamelCase					,       __lowerCamelCase       )					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase       )
                     UpperCAmelCase							=     """\n""".join(__lowerCamelCase       )
                     if special_strings is not None:
                        for string in special_strings:
                           UpperCAmelCase							=     diff.replace(__lowerCamelCase					,       """"""       )
                     self.assertEqual(__lowerCamelCase					,       """"""       )





      def        _lowercase				(		self						:					str       )    ->   Optional[int]:






         """simple docstring"""







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





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






         """simple docstring"""







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






@mock.patch.dict(os.environ ,					{'''TESTING_MOCKED_DATALOADERS''': '''1'''}  )
class   __lowercase				(  __snake_case  ):
      UpperCamelCase								=       False





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






         """simple docstring"""







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

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





      @classmethod
      def        _lowercase				(		cls						:					Dict       )    ->   Optional[int]:






         """simple docstring"""







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





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






         """simple docstring"""







         UpperCAmelCase							=     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        _lowercase				(		self						:					str       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
         UpperCAmelCase							=     run_command(self._launch_args + testargs       )
         self.assertTrue(os.path.exists(os.path.join(self.tmpdir					,       """step_2"""       )       )       )





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






         """simple docstring"""







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





      def        _lowercase				(		self						:					int       )    ->   str:






         """simple docstring"""







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





      @slow
      def        _lowercase				(		self						:					Optional[int]       )    ->   Any:






         """simple docstring"""







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





      def        _lowercase				(		self						:					Optional[Any]       )    ->   List[Any]:






         """simple docstring"""







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





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






         """simple docstring"""







         with tempfile.TemporaryDirectory() as tmpdir:
            UpperCAmelCase							=     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(__lowerCamelCase					,       """tracking"""       )       )       )





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






         """simple docstring"""







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





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






         """simple docstring"""







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







def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_   )						->int:
   return int((input_a, input_a).count(0   ) == 0   )
def   _UpperCamelCase				(						)						->None:
   assert and_gate(0  ,						0   ) == 0
   assert and_gate(0  ,						1   ) == 0
   assert and_gate(1  ,						0   ) == 0
   assert and_gate(1  ,						1   ) == 1


if __name__ == "__main__":
 test_and_gate()
 print(and_gate(1, 0))
 print(and_gate(0, 0))
 print(and_gate(0, 1))
 print(and_gate(1, 1))



