


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



    """simple docstring"""






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






    def _a					(			self    )  ->       Dict:
         self.one_complete_example('complete_nlp_example.py'					,   A_    )
         self.one_complete_example('complete_nlp_example.py'					,   A_    )






    def _a					(			self    )  ->       Dict:
         __UpperCamelCase    							=os.path.abspath(os.path.join('examples'					,   'cv_example.py'    )    )
         __UpperCamelCase    							=[
             ' ' * 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     UpperCAmelCase__       (					A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Tuple      =  False






    @classmethod
    def _a					(			cls    )  ->       Union[str, Any]:
         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 _a					(			cls    )  ->       Union[str, Any]:
         super().tearDownClass()
         shutil.rmtree(cls._tmpdir    )






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






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






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






    def _a					(			self    )  ->       str:
         __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 _a					(			self    )  ->       List[str]:
         with tempfile.TemporaryDirectory() as tmpdir:
              __UpperCamelCase    							=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    )  ->       Optional[int]:
         __UpperCamelCase    							=['examples/by_feature/gradient_accumulation.py']
         run_command(self._launch_args + testargs    )






    def _a					(			self    )  ->       List[Any]:
         __UpperCamelCase    							=['examples/by_feature/local_sgd.py']
         run_command(self._launch_args + testargs    )










"""simple docstring"""
import copy
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


A:   List[str]								=		logging.get_logger(__name__)

A:   Dict								=		{
    "microsoft/conditional-detr-resnet-50": (
        "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"
    ),
}



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):
   __lowerCAmelCase  :   Any						=						'conditional_detr'
   __lowerCAmelCase  :   Union[str, Any]						=						['past_key_values']
   __lowerCAmelCase  :   int						=						{
       'hidden_size': 'd_model',
       'num_attention_heads': 'encoder_attention_heads',
   }

   def __init__(   self      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=None      ,     _SCREAMING_SNAKE_CASE=3      ,     _SCREAMING_SNAKE_CASE=300      ,     _SCREAMING_SNAKE_CASE=6      ,     _SCREAMING_SNAKE_CASE=2048      ,     _SCREAMING_SNAKE_CASE=8      ,     _SCREAMING_SNAKE_CASE=6      ,     _SCREAMING_SNAKE_CASE=2048      ,     _SCREAMING_SNAKE_CASE=8      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE="relu"      ,     _SCREAMING_SNAKE_CASE=256      ,     _SCREAMING_SNAKE_CASE=0.1      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.0      ,     _SCREAMING_SNAKE_CASE=0.02      ,     _SCREAMING_SNAKE_CASE=1.0      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE="sine"      ,     _SCREAMING_SNAKE_CASE="resnet50"      ,     _SCREAMING_SNAKE_CASE=True      ,     _SCREAMING_SNAKE_CASE=False      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=5      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=1      ,     _SCREAMING_SNAKE_CASE=1      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=5      ,     _SCREAMING_SNAKE_CASE=2      ,     _SCREAMING_SNAKE_CASE=0.25      ,     **_SCREAMING_SNAKE_CASE      ,     )  ->      Tuple:





    '''simple docstring'''




    if backbone_config is not None and use_timm_backbone:
     raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""						)

    if not use_timm_backbone:
     if backbone_config is None:
      logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""						)
      UpperCAmelCase	:					Tuple	=	CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]						)
     elif isinstance(_SCREAMING_SNAKE_CASE      ,     _SCREAMING_SNAKE_CASE						):
      UpperCAmelCase	:					str	=	backbone_config.get("""model_type"""						)
      UpperCAmelCase	:					int	=	CONFIG_MAPPING[backbone_model_type]
      UpperCAmelCase	:					Union[str, Any]	=	config_class.from_dict(_SCREAMING_SNAKE_CASE						)

    UpperCAmelCase	:					Union[str, Any]	=	use_timm_backbone
    UpperCAmelCase	:					Optional[int]	=	backbone_config
    UpperCAmelCase	:					List[str]	=	num_channels
    UpperCAmelCase	:					Any	=	num_queries
    UpperCAmelCase	:					Union[str, Any]	=	d_model
    UpperCAmelCase	:					List[str]	=	encoder_ffn_dim
    UpperCAmelCase	:					Optional[int]	=	encoder_layers
    UpperCAmelCase	:					Union[str, Any]	=	encoder_attention_heads
    UpperCAmelCase	:					Optional[Any]	=	decoder_ffn_dim
    UpperCAmelCase	:					Any	=	decoder_layers
    UpperCAmelCase	:					Optional[int]	=	decoder_attention_heads
    UpperCAmelCase	:					Optional[int]	=	dropout
    UpperCAmelCase	:					Dict	=	attention_dropout
    UpperCAmelCase	:					Dict	=	activation_dropout
    UpperCAmelCase	:					Any	=	activation_function
    UpperCAmelCase	:					Any	=	init_std
    UpperCAmelCase	:					Tuple	=	init_xavier_std
    UpperCAmelCase	:					Optional[int]	=	encoder_layerdrop
    UpperCAmelCase	:					Any	=	decoder_layerdrop
    UpperCAmelCase	:					Any	=	encoder_layers
    UpperCAmelCase	:					Optional[Any]	=	auxiliary_loss
    UpperCAmelCase	:					List[Any]	=	position_embedding_type
    UpperCAmelCase	:					Union[str, Any]	=	backbone
    UpperCAmelCase	:					List[Any]	=	use_pretrained_backbone
    UpperCAmelCase	:					Dict	=	dilation
    # Hungarian matcher
    UpperCAmelCase	:					Optional[int]	=	class_cost
    UpperCAmelCase	:					List[str]	=	bbox_cost
    UpperCAmelCase	:					List[str]	=	giou_cost
    # Loss coefficients
    UpperCAmelCase	:					List[Any]	=	mask_loss_coefficient
    UpperCAmelCase	:					List[str]	=	dice_loss_coefficient
    UpperCAmelCase	:					Optional[int]	=	cls_loss_coefficient
    UpperCAmelCase	:					Union[str, Any]	=	bbox_loss_coefficient
    UpperCAmelCase	:					Union[str, Any]	=	giou_loss_coefficient
    UpperCAmelCase	:					int	=	focal_alpha
    super().__init__(is_encoder_decoder=_SCREAMING_SNAKE_CASE      ,     **_SCREAMING_SNAKE_CASE						)

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return self.encoder_attention_heads

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return self.d_model




   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Any:





    '''simple docstring'''




    UpperCAmelCase	:					List[Any]	=	copy.deepcopy(self.__dict__						)
    if self.backbone_config is not None:
     UpperCAmelCase	:					Union[str, Any]	=	self.backbone_config.to_dict()
    UpperCAmelCase	:					Dict	=	self.__class__.model_type
    return output



class       SCREAMING_SNAKE_CASE__  (					UpperCAmelCase__ ):
   __lowerCAmelCase  :   Any						=						version.parse('1.11' )

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      Mapping[str, Mapping[int, str]]:





    '''simple docstring'''




    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ("""pixel_mask""", {0: """batch"""}),
        ]						)

   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      float:





    '''simple docstring'''




    return 1E-5




   @property
   def 	SCREAMING_SNAKE_CASE     (   self						)  ->      int:





    '''simple docstring'''




    return 12
