


import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command



class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     Dict							=					inspect.getfile(accelerate.test_utils)
       __UpperCamelCase      :     Optional[Any]							=					os.path.sep.join(mod_file.split(os.path.sep)[:-1] + ["scripts", "test_cli.py"])

       __UpperCamelCase      :     Tuple							=					["accelerate", "launch"]
       __UpperCamelCase      :     str							=					Path.home() / ".cache/huggingface/accelerate"
       __UpperCamelCase      :     List[Any]							=					"default_config.yaml"
       __UpperCamelCase      :     Dict							=					config_folder / config_file
       __UpperCamelCase      :     Any							=					config_folder / "_default_config.yaml"

       __UpperCamelCase      :     Union[str, Any]							=					Path("tests/test_configs")






       @classmethod
       def  _lowercase   (     cls						):




              """simple docstring"""




              if cls.config_path.is_file():
                     cls.config_path.rename(cls.changed_path						)






       @classmethod
       def  _lowercase   (     cls						):




              """simple docstring"""




              if cls.changed_path.is_file():
                     cls.changed_path.rename(cls.config_path						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		self.base_cmd
              if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
                     cmd += ["--multi_gpu"]
              execute_subprocess_async(cmd + [self.test_file_path] ,					env=os.environ.copy()						)






       def  _lowercase   (     self						):




              """simple docstring"""




              for config in sorted(self.test_config_path.glob('''**/*.yaml'''						)						):
                     with self.subTest(config_file=__SCREAMING_SNAKE_CASE						):
                            execute_subprocess_async(
                                self.base_cmd + ['''--config_file''', str(__SCREAMING_SNAKE_CASE						), self.test_file_path] ,					env=os.environ.copy()						)






       def  _lowercase   (     self						):




              """simple docstring"""




              execute_subprocess_async(['''accelerate''', '''test'''] ,					env=os.environ.copy()						)




class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     List[Any]							=					"test-tpu"
       __UpperCamelCase      :     Tuple							=					"us-central1-a"
       __UpperCamelCase      :     Any							=					"ls"
       __UpperCamelCase      :     Union[str, Any]							=					["accelerate", "tpu-config"]
       __UpperCamelCase      :     str							=					"cd /usr/share"
       __UpperCamelCase      :     Optional[int]							=					"tests/test_samples/test_command_file.sh"
       __UpperCamelCase      :     List[str]							=					"Running gcloud compute tpus tpu-vm ssh"






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		run_command(
                  self.cmd
                  + ['''--command''', self.command, '''--tpu_zone''', self.tpu_zone, '''--tpu_name''', self.tpu_name, '''--debug'''] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		run_command(
                  self.cmd
                  + [
                      '''--config_file''',
                      '''tests/test_configs/0_12_0.yaml''',
                      '''--command''',
                      self.command,
                      '''--tpu_zone''',
                      self.tpu_zone,
                      '''--tpu_name''',
                      self.tpu_name,
                      '''--debug''',
                  ] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		run_command(
                  self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--debug'''] ,					return_stdout=__SCREAMING_SNAKE_CASE						)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		run_command(
                  self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command''', self.command, '''--debug'''] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		run_command(
                  self.cmd
                  + [
                      '''--config_file''',
                      '''tests/test_configs/latest.yaml''',
                      '''--command''',
                      self.command,
                      '''--command''',
                      '''echo "Hello World"''',
                      '''--debug''',
                  ] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		run_command(
                  self.cmd
                  + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--command_file''', self.command_file, '''--debug'''] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		run_command(
                  self.cmd
                  + [
                      '''--config_file''',
                      '''tests/test_configs/0_12_0.yaml''',
                      '''--command_file''',
                      self.command_file,
                      '''--tpu_zone''',
                      self.tpu_zone,
                      '''--tpu_name''',
                      self.tpu_name,
                      '''--debug''',
                  ] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		run_command(
                  self.cmd + ['''--config_file''', '''tests/test_configs/latest.yaml''', '''--install_accelerate''', '''--debug'''] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		run_command(
                  self.cmd
                  + [
                      '''--config_file''',
                      '''tests/test_configs/latest.yaml''',
                      '''--install_accelerate''',
                      '''--accelerate_version''',
                      '''12.0.0''',
                      '''--debug''',
                  ] ,					return_stdout=__SCREAMING_SNAKE_CASE ,					)
              self.assertIn(
                  f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all""" ,					__SCREAMING_SNAKE_CASE ,					)





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


__UpperCAmelCase : Optional[Any] 			=				logging.get_logger(__name__)

__UpperCAmelCase : List[str] 			=				{
    "microsoft/conditional-detr-resnet-50": (
        "https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json"
    ),
}



class    UpperCAmelCase_	(							_a):
       '''simple docstring'''
       __UpperCamelCase      :     Optional[int]							=					"conditional_detr"
       __UpperCamelCase      :     Optional[Any]							=					["past_key_values"]
       __UpperCamelCase      :     Union[str, Any]							=					{
           "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=2_048 ,					__SCREAMING_SNAKE_CASE=8 ,					__SCREAMING_SNAKE_CASE=6 ,					__SCREAMING_SNAKE_CASE=2_048 ,					__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 ,					):




              """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    :	str   	=		CONFIG_MAPPING['''resnet'''](out_features=['''stage4''']						)
                     elif isinstance(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):
                            UpperCamelCase    :	Tuple   	=		backbone_config.get('''model_type'''						)
                            UpperCamelCase    :	Optional[Any]   	=		CONFIG_MAPPING[backbone_model_type]
                            UpperCamelCase    :	Any   	=		config_class.from_dict(__SCREAMING_SNAKE_CASE						)

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






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return self.encoder_attention_heads






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return self.d_model






       def  _lowercase   (     self						):




              """simple docstring"""




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




class    UpperCAmelCase_	(							_a):
       '''simple docstring'''
       __UpperCamelCase      :     Dict							=					version.parse("1.11")






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




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






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return 1e-5






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              return 12

