


import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

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


lowerCAmelCase__			:      Union[str, Any]								=logging.get_logger(__name__)

lowerCAmelCase__			:      List[str]								={
    'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json',
    # See all DETR models at https://huggingface.co/models?filter=detr
}







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							_UpperCAmelCase      =						"""detr"""
							_UpperCAmelCase      =						["""past_key_values"""]
							_UpperCAmelCase      =						{
							    """hidden_size""": """d_model""",
							    """num_attention_heads""": """encoder_attention_heads""",
							}






							def __init__(      self			,						lowerCAmelCase__=True			,						lowerCAmelCase__=None			,						lowerCAmelCase__=3			,						lowerCAmelCase__=1_0_0			,						lowerCAmelCase__=6			,						lowerCAmelCase__=2_0_4_8			,						lowerCAmelCase__=8			,						lowerCAmelCase__=6			,						lowerCAmelCase__=2_0_4_8			,						lowerCAmelCase__=8			,						lowerCAmelCase__=0.0			,						lowerCAmelCase__=0.0			,						lowerCAmelCase__=True			,						lowerCAmelCase__="relu"			,						lowerCAmelCase__=2_5_6			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=0.0			,						lowerCAmelCase__=0.0			,						lowerCAmelCase__=0.02			,						lowerCAmelCase__=1.0			,						lowerCAmelCase__=False			,						lowerCAmelCase__="sine"			,						lowerCAmelCase__="resnet50"			,						lowerCAmelCase__=True			,						lowerCAmelCase__=False			,						lowerCAmelCase__=1			,						lowerCAmelCase__=5			,						lowerCAmelCase__=2			,						lowerCAmelCase__=1			,						lowerCAmelCase__=1			,						lowerCAmelCase__=5			,						lowerCAmelCase__=2			,						lowerCAmelCase__=0.1			,						**lowerCAmelCase__			,						):





										"""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.'       )
																SCREAMING_SNAKE_CASE_		:     Dict					=					CONFIG_MAPPING['resnet'](out_features=['stage4']       )
													elif isinstance(lowerCAmelCase__			,						lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     Tuple					=					backbone_config.get('model_type'       )
																SCREAMING_SNAKE_CASE_		:     Optional[int]					=					CONFIG_MAPPING[backbone_model_type]
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					config_class.from_dict(lowerCAmelCase__       )
													# set timm attributes to None
													SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					None, None, None

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






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										return self.encoder_attention_heads






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										return self.d_model






							@classmethod
							def 					UpperCamelCase__     (      cls			,						lowerCAmelCase__			,						**lowerCAmelCase__       ):





										"""simple docstring"""




										return cls(backbone_config=lowerCAmelCase__			,						**lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					copy.deepcopy(self.__dict__       )
										if output["backbone_config"] is not None:
													SCREAMING_SNAKE_CASE_		:     str					=					self.backbone_config.to_dict()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					self.__class__.model_type
										return output







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							_UpperCAmelCase      =						version.parse("""1.11"""  )






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										return 1E-5






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										return 1_2












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





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





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





      def        _snake_case     (			self					)						->	Any:
            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""" ,		lowercase ,		lowercase ,		lowercase					)
            self.one_complete_example("""complete_cv_example.py""" ,		lowercase ,		lowercase ,		lowercase					)







@mock.patch.dict(os.environ	,    {'TESTING_MOCKED_DATALOADERS': '1'}   )
class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = False





      @classmethod
      def        _snake_case     (			cls					)						->	Optional[int]:
            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					)						->	Optional[int]:
            super().tearDownClass()
            shutil.rmtree(cls._tmpdir					)





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			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        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.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'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir ,		"epoch_0"					)}\n        '.split()
            lowerCAmelCase    		=			run_command(self._launch_args + testargs ,		return_stdout=lowercase					)
            self.assertNotIn("""epoch 0:""" ,		lowercase					)
            self.assertIn("""epoch 1:""" ,		lowercase					)





      def        _snake_case     (			self					)						->	Union[str, Any]:
            lowerCAmelCase    		=			f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir ,		"step_2"					)}\n        '.split()
            lowerCAmelCase    		=			run_command(self._launch_args + testargs ,		return_stdout=lowercase					)
            if torch.cuda.is_available():
                  lowerCAmelCase    		=			torch.cuda.device_count()
            else:
                  lowerCAmelCase    		=			1
            if num_processes > 1:
                  self.assertNotIn("""epoch 0:""" ,		lowercase					)
                  self.assertIn("""epoch 1:""" ,		lowercase					)
            else:
                  self.assertIn("""epoch 0:""" ,		lowercase					)
                  self.assertIn("""epoch 1:""" ,		lowercase					)





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





      def        _snake_case     (			self					)						->	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					)						->	Any:
            with tempfile.TemporaryDirectory() as tmpdir:
                  lowerCAmelCase    		=			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(lowercase ,		"""tracking"""					)					)					)





      def        _snake_case     (			self					)						->	Union[str, Any]:
            lowerCAmelCase    		=			["""examples/by_feature/gradient_accumulation.py"""]
            run_command(self._launch_args + testargs					)





      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			["""examples/by_feature/local_sgd.py"""]
            run_command(self._launch_args + testargs					)






