
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)

lowerCamelCase__	=							[
    '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  A_				(			self       ,							a       ,							a       ,							a = None       ,							a = None				)					->      Optional[Any]:


								'''simple docstring'''

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

						def  A_				(			self				)					->      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				)					->      int:


								'''simple docstring'''

								_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"""       ,							__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__      (   lowerCamelCase__	):
						UpperCamelCase_							: Tuple	=		False

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


								'''simple docstring'''

								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				)					->      List[str]:


								'''simple docstring'''

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

						def  A_				(			self				)					->      Dict:


								'''simple docstring'''

								_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				)					->      Dict:


								'''simple docstring'''

								_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				)					->      List[str]:


								'''simple docstring'''

								_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=__lowerCamelCase				)
								self.assertNotIn("""epoch 0:"""       ,							__lowerCamelCase				)
								self.assertIn("""epoch 1:"""       ,							__lowerCamelCase				)

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


								'''simple docstring'''

								_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=__lowerCamelCase				)
								if torch.cuda.is_available():
										_UpperCamelCase						    =	torch.cuda.device_count()
								else:
										_UpperCamelCase						    =	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				)					->      str:


								'''simple docstring'''

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

						def  A_				(			self				)					->      Dict:


								'''simple docstring'''

								_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				)					->      Optional[Any]:


								'''simple docstring'''

								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(__lowerCamelCase       ,							"""tracking"""				)				)				)

						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

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







						def  A_				(			self				)					->      Tuple:


								'''simple docstring'''

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





import copy

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


lowerCamelCase__	=							logging.get_logger(__name__)

lowerCamelCase__	=							{
    "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json",
}







class        lowerCAmelCase__      (   __lowercase	):
						UpperCamelCase_							: int	=		"deta"
						UpperCamelCase_							: Union[str, Any]	=		{
						    "hidden_size": "d_model",
						    "num_attention_heads": "encoder_attention_heads",
						}

						def __init__(			self       ,							a=None       ,							a=9_00       ,							a=20_48       ,							a=6       ,							a=20_48       ,							a=8       ,							a=6       ,							a=10_24       ,							a=8       ,							a=0.0       ,							a=True       ,							a="relu"       ,							a=2_56       ,							a=0.1       ,							a=0.0       ,							a=0.0       ,							a=0.02       ,							a=1.0       ,							a=True       ,							a=False       ,							a="sine"       ,							a=5       ,							a=4       ,							a=4       ,							a=True       ,							a=3_00       ,							a=True       ,							a=True       ,							a=1       ,							a=5       ,							a=2       ,							a=1       ,							a=1       ,							a=5       ,							a=2       ,							a=0.1       ,							a=0.25       ,							**a       ,							)					->      Dict:


								'''simple docstring'''

								if backbone_config is None:
										logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""				)
										_UpperCamelCase						    =	CONFIG_MAPPING["""resnet"""](out_features=["""stage2""", """stage3""", """stage4"""]				)
								else:
										if isinstance(a       ,							a				):
												_UpperCamelCase						    =	backbone_config.pop("""model_type"""				)
												_UpperCamelCase						    =	CONFIG_MAPPING[backbone_model_type]
												_UpperCamelCase						    =	config_class.from_dict(a				)

								_UpperCamelCase						    =	backbone_config
								_UpperCamelCase						    =	num_queries
								_UpperCamelCase						    =	max_position_embeddings
								_UpperCamelCase						    =	d_model
								_UpperCamelCase						    =	encoder_ffn_dim
								_UpperCamelCase						    =	encoder_layers
								_UpperCamelCase						    =	encoder_attention_heads
								_UpperCamelCase						    =	decoder_ffn_dim
								_UpperCamelCase						    =	decoder_layers
								_UpperCamelCase						    =	decoder_attention_heads
								_UpperCamelCase						    =	dropout
								_UpperCamelCase						    =	attention_dropout
								_UpperCamelCase						    =	activation_dropout
								_UpperCamelCase						    =	activation_function
								_UpperCamelCase						    =	init_std
								_UpperCamelCase						    =	init_xavier_std
								_UpperCamelCase						    =	encoder_layerdrop
								_UpperCamelCase						    =	auxiliary_loss
								_UpperCamelCase						    =	position_embedding_type
								# deformable attributes
								_UpperCamelCase						    =	num_feature_levels
								_UpperCamelCase						    =	encoder_n_points
								_UpperCamelCase						    =	decoder_n_points
								_UpperCamelCase						    =	two_stage
								_UpperCamelCase						    =	two_stage_num_proposals
								_UpperCamelCase						    =	with_box_refine
								_UpperCamelCase						    =	assign_first_stage
								if two_stage is True and with_box_refine is False:
										raise ValueError("""If two_stage is True, with_box_refine must be True."""				)
								# Hungarian matcher
								_UpperCamelCase						    =	class_cost
								_UpperCamelCase						    =	bbox_cost
								_UpperCamelCase						    =	giou_cost
								# Loss coefficients
								_UpperCamelCase						    =	mask_loss_coefficient
								_UpperCamelCase						    =	dice_loss_coefficient
								_UpperCamelCase						    =	bbox_loss_coefficient
								_UpperCamelCase						    =	giou_loss_coefficient
								_UpperCamelCase						    =	eos_coefficient
								_UpperCamelCase						    =	focal_alpha
								super().__init__(is_encoder_decoder=a       ,							**a				)

						@property
						def  A_				(			self				)					->      int:


								'''simple docstring'''

								return self.encoder_attention_heads

						@property
						def  A_				(			self				)					->      int:


								'''simple docstring'''

								return self.d_model







						def  A_				(			self				)					->      Tuple:


								'''simple docstring'''

								_UpperCamelCase						    =	copy.deepcopy(self.__dict__				)
								_UpperCamelCase						    =	self.backbone_config.to_dict()
								_UpperCamelCase						    =	self.__class__.model_type
								return output



