






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






 """simple docstring"""





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





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   str					    =							os.path.abspath(os.path.join("""examples"""			,     """cv_example.py""" ) )
       UpperCamelCase_				:   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     A    (      SCREAMING_SNAKE_CASE__						):






 """simple docstring"""





 __a			:						Dict      =						False
 @classmethod
 def   _UpperCAmelCase       (							cls ):
       super().setUpClass()
       UpperCamelCase_				:   List[Any]					    =							tempfile.mkdtemp()
       UpperCamelCase_				:   Dict					    =							os.path.join(cls._tmpdir			,     """default_config.yml""" )

       write_basic_config(save_location=cls.configPath )
       UpperCamelCase_				:   List[str]					    =							["""accelerate""", """launch""", """--config_file""", cls.configPath]
 @classmethod
 def   _UpperCAmelCase       (							cls ):
       super().tearDownClass()
       shutil.rmtree(cls._tmpdir )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[int]					    =							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   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[Any]					    =							F"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
       UpperCamelCase_				:   Union[str, Any]					    =							run_command(self._launch_args + testargs )
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir			,     """step_2""" ) ) )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[int]					    =							F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir			,     'epoch_0' )}\n        ".split()
       UpperCamelCase_				:   Optional[Any]					    =							run_command(self._launch_args + testargs			,     return_stdout=__lowerCAmelCase )
       self.assertNotIn("""epoch 0:"""			,     __lowerCAmelCase )
       self.assertIn("""epoch 1:"""			,     __lowerCAmelCase )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   List[Any]					    =							F"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir			,     'step_2' )}\n        ".split()
       UpperCamelCase_				:   Optional[int]					    =							run_command(self._launch_args + testargs			,     return_stdout=__lowerCAmelCase )
       if torch.cuda.is_available():
             UpperCamelCase_				:   int					    =							torch.cuda.device_count()
       else:
             UpperCamelCase_				:   List[Any]					    =							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   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[Any]					    =							"""
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
       with mock.patch.dict(os.environ			,     {"""TESTING_MOCKED_DATALOADERS""": """0"""} ):
             UpperCamelCase_				:   str					    =							run_command(self._launch_args + testargs			,     return_stdout=__lowerCAmelCase )
             UpperCamelCase_				:   Any					    =							re.findall("""({.+})"""			,     __lowerCAmelCase )
             UpperCamelCase_				:   List[str]					    =							[r for r in results if """accuracy""" in r][-1]
             UpperCamelCase_				:   str					    =							ast.literal_eval(__lowerCAmelCase )
             self.assertGreaterEqual(results["""accuracy"""]			,     0.75 )
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[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   _UpperCAmelCase       (							self ):
       with tempfile.TemporaryDirectory() as tmpdir:
             UpperCamelCase_				:   Optional[int]					    =							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   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   int					    =							["""examples/by_feature/gradient_accumulation.py"""]
       run_command(self._launch_args + testargs )





 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Tuple					    =							["""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_tf_available, is_torch_available


UpperCamelCase							={
    "configuration_data2vec_audio": ["DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP", "Data2VecAudioConfig"],
    "configuration_data2vec_text": [
        "DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Data2VecTextConfig",
        "Data2VecTextOnnxConfig",
    ],
    "configuration_data2vec_vision": [
        "DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Data2VecVisionConfig",
        "Data2VecVisionOnnxConfig",
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase							=[
          "DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST",
          "Data2VecAudioForAudioFrameClassification",
          "Data2VecAudioForCTC",
          "Data2VecAudioForSequenceClassification",
          "Data2VecAudioForXVector",
          "Data2VecAudioModel",
          "Data2VecAudioPreTrainedModel",
      ]
      UpperCamelCase							=[
          "DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "Data2VecTextForCausalLM",
          "Data2VecTextForMaskedLM",
          "Data2VecTextForMultipleChoice",
          "Data2VecTextForQuestionAnswering",
          "Data2VecTextForSequenceClassification",
          "Data2VecTextForTokenClassification",
          "Data2VecTextModel",
          "Data2VecTextPreTrainedModel",
      ]
      UpperCamelCase							=[
          "DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST",
          "Data2VecVisionForImageClassification",
          "Data2VecVisionForMaskedImageModeling",
          "Data2VecVisionForSemanticSegmentation",
          "Data2VecVisionModel",
          "Data2VecVisionPreTrainedModel",
      ]

if is_tf_available():
      UpperCamelCase							=[
          "TFData2VecVisionForImageClassification",
          "TFData2VecVisionForSemanticSegmentation",
          "TFData2VecVisionModel",
          "TFData2VecVisionPreTrainedModel",
      ]

if TYPE_CHECKING:
      from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig
      from .configuration_dataavec_text import (
          DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP,
          DataaVecTextConfig,
          DataaVecTextOnnxConfig,
      )
      from .configuration_dataavec_vision import (
          DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP,
          DataaVecVisionConfig,
          DataaVecVisionOnnxConfig,
      )

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_dataavec_audio import (
                DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecAudioForAudioFrameClassification,
                DataaVecAudioForCTC,
                DataaVecAudioForSequenceClassification,
                DataaVecAudioForXVector,
                DataaVecAudioModel,
                DataaVecAudioPreTrainedModel,
            )
            from .modeling_dataavec_text import (
                DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecTextForCausalLM,
                DataaVecTextForMaskedLM,
                DataaVecTextForMultipleChoice,
                DataaVecTextForQuestionAnswering,
                DataaVecTextForSequenceClassification,
                DataaVecTextForTokenClassification,
                DataaVecTextModel,
                DataaVecTextPreTrainedModel,
            )
            from .modeling_dataavec_vision import (
                DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST,
                DataaVecVisionForImageClassification,
                DataaVecVisionForMaskedImageModeling,
                DataaVecVisionForSemanticSegmentation,
                DataaVecVisionModel,
                DataaVecVisionPreTrainedModel,
            )
      if is_tf_available():
            from .modeling_tf_dataavec_vision import (
                TFDataaVecVisionForImageClassification,
                TFDataaVecVisionForSemanticSegmentation,
                TFDataaVecVisionModel,
                TFDataaVecVisionPreTrainedModel,
            )

else:
      import sys

      UpperCamelCase							=_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






