






'''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 UpperCAmelCase_					(    unittest.TestCase				):




     """simple docstring"""




     def 				A__      ( self			:		Union[str, Any]     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		List[Any] = None     ,      UpperCAmelCase			:		Dict = None		)  ->  Optional[Any]:


      '''simple docstring'''

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






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


      '''simple docstring'''

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






     def 				A__      ( self			:		Any		)  ->  int:


      '''simple docstring'''

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





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




     """simple docstring"""




     UpperCamelCase_								=			False






     @classmethod
     def 				A__      ( cls			:		List[Any]		)  ->  Union[str, Any]:


      '''simple docstring'''

      super().setUpClass()
      lowercase					:   List[Any]				  =tempfile.mkdtemp()
      lowercase					:   str				  =os.path.join(cls._tmpdir     ,      '''default_config.yml'''		)

      write_basic_config(save_location=cls.configPath		)
      lowercase					:   Optional[int]				  =['''accelerate''', '''launch''', '''--config_file''', cls.configPath]






     @classmethod
     def 				A__      ( cls			:		Tuple		)  ->  List[str]:


      '''simple docstring'''

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






     def 				A__      ( self			:		List[Any]		)  ->  Dict:


      '''simple docstring'''

      lowercase					:   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			:		List[Any]		)  ->  Dict:


      '''simple docstring'''

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






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


      '''simple docstring'''

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






     def 				A__      ( self			:		str		)  ->  List[str]:


      '''simple docstring'''

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


      '''simple docstring'''

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






     def 				A__      ( self			:		Any		)  ->  Dict:


      '''simple docstring'''

      lowercase					:   List[str]				  =['''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			:		Union[str, Any]		)  ->  Optional[Any]:


      '''simple docstring'''

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






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


      '''simple docstring'''

      lowercase					:   Dict				  =['''examples/by_feature/gradient_accumulation.py''']
      run_command(self._launch_args + testargs		)






     def 				A__      ( self			:		str		)  ->  Tuple:


      '''simple docstring'''

      lowercase					:   Any				  =['''examples/by_feature/local_sgd.py''']
      run_command(self._launch_args + testargs		)











'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


SCREAMING_SNAKE_CASE						=	{
    'configuration_mega': ['MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegaConfig', 'MegaOnnxConfig'],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'MEGA_PRETRAINED_MODEL_ARCHIVE_LIST',
          'MegaForCausalLM',
          'MegaForMaskedLM',
          'MegaForMultipleChoice',
          'MegaForQuestionAnswering',
          'MegaForSequenceClassification',
          'MegaForTokenClassification',
          'MegaModel',
          'MegaPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mega import (
                MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
                MegaForCausalLM,
                MegaForMaskedLM,
                MegaForMultipleChoice,
                MegaForQuestionAnswering,
                MegaForSequenceClassification,
                MegaForTokenClassification,
                MegaModel,
                MegaPreTrainedModel,
            )

else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



