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




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




 def 							_snake_case   (	self		)    ->							List[str]:
     self.one_complete_example("complete_nlp_example.py"			,     _lowerCAmelCase		)
     self.one_complete_example("complete_nlp_example.py"			,     _lowerCAmelCase		)





 def 							_snake_case   (	self		)    ->							int:
     _lowerCAmelCase				=      os.path.abspath(os.path.join("examples"			,     "cv_example.py"		)		)
     _lowerCAmelCase				=      [
         " " * 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    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							str         =					False




 @classmethod
 def 							_snake_case   (	cls		)    ->							Dict:
     super().setUpClass()
     _lowerCAmelCase				=      tempfile.mkdtemp()
     _lowerCAmelCase				=      os.path.join(cls._tmpdir			,     "default_config.yml"		)

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




 @classmethod
 def 							_snake_case   (	cls		)    ->							List[Any]:
     super().tearDownClass()
     shutil.rmtree(cls._tmpdir		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      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		)    ->							Any:
     _lowerCAmelCase				=      f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        '''.split()
     _lowerCAmelCase				=      run_command(self._launch_args + testargs		)
     self.assertTrue(os.path.exists(os.path.join(self.tmpdir			,     "step_2"		)		)		)




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




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      f'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir			,     'step_2'		)}
        '''.split()
     _lowerCAmelCase				=      run_command(self._launch_args + testargs			,     return_stdout=_lowerCAmelCase		)
     if torch.cuda.is_available():
         _lowerCAmelCase				=      torch.cuda.device_count()
     else:
         _lowerCAmelCase				=      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 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      "\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
     with mock.patch.dict(os.environ			,     {"TESTING_MOCKED_DATALOADERS": "0"}		):
         _lowerCAmelCase				=      run_command(self._launch_args + testargs			,     return_stdout=_lowerCAmelCase		)
         _lowerCAmelCase				=      re.findall("({.+})"			,     _lowerCAmelCase		)
         _lowerCAmelCase				=      [r for r in results if "accuracy" in r][-1]
         _lowerCAmelCase				=      ast.literal_eval(_lowerCAmelCase		)
         self.assertGreaterEqual(results["accuracy"]			,     0.75		)




 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      ["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		)    ->							Tuple:
     with tempfile.TemporaryDirectory() as tmpdir:
         _lowerCAmelCase				=      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 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      ["examples/by_feature/gradient_accumulation.py"]
     run_command(self._launch_args + testargs		)





 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      ["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_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE	 =							{"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _SCREAMING_SNAKE_CASE	 =							["XGLMTokenizer"]

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _SCREAMING_SNAKE_CASE	 =							["XGLMTokenizerFast"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _SCREAMING_SNAKE_CASE	 =							[
           "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "XGLMForCausalLM",
           "XGLMModel",
           "XGLMPreTrainedModel",
       ]


try:
       if not is_flax_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _SCREAMING_SNAKE_CASE	 =							[
           "FlaxXGLMForCausalLM",
           "FlaxXGLMModel",
           "FlaxXGLMPreTrainedModel",
       ]


try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       _SCREAMING_SNAKE_CASE	 =							[
           "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFXGLMForCausalLM",
           "TFXGLMModel",
           "TFXGLMPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_xglm import XGLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_xglm_fast import XGLMTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

       try:
              if not is_flax_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_xglm import (
                  TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFXGLMForCausalLM,
                  TFXGLMModel,
                  TFXGLMPreTrainedModel,
              )


else:
       import sys

       _SCREAMING_SNAKE_CASE	 =							_LazyModule(__name__, globals()["__file__"], _import_structure)


