




import copy

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


lowerCamelCase__			:       Optional[int]         =							logging.get_logger(__name__)

lowerCamelCase__			:       Optional[int]         =							{
    'ut/deta': 'https://huggingface.co/ut/deta/resolve/main/config.json',
}





class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE ):


 '''simple docstring'''







 lowercase_       =   "deta"
 lowercase_       =   {
     "hidden_size": "d_model",
     "num_attention_heads": "encoder_attention_heads",
 }



 def __init__(							self  :		str    ,    _lowerCAmelCase  :		List[Any]=None    ,    _lowerCAmelCase  :		List[Any]=900    ,    _lowerCAmelCase  :		Tuple=2_048    ,    _lowerCAmelCase  :		str=6    ,    _lowerCAmelCase  :		List[str]=2_048    ,    _lowerCAmelCase  :		Union[str, Any]=8    ,    _lowerCAmelCase  :		Union[str, Any]=6    ,    _lowerCAmelCase  :		str=1_024    ,    _lowerCAmelCase  :		str=8    ,    _lowerCAmelCase  :		int=0.0    ,    _lowerCAmelCase  :		Any=True    ,    _lowerCAmelCase  :		Tuple="relu"    ,    _lowerCAmelCase  :		List[str]=256    ,    _lowerCAmelCase  :		int=0.1    ,    _lowerCAmelCase  :		str=0.0    ,    _lowerCAmelCase  :		Union[str, Any]=0.0    ,    _lowerCAmelCase  :		List[Any]=0.02    ,    _lowerCAmelCase  :		Union[str, Any]=1.0    ,    _lowerCAmelCase  :		Dict=True    ,    _lowerCAmelCase  :		Any=False    ,    _lowerCAmelCase  :		List[str]="sine"    ,    _lowerCAmelCase  :		int=5    ,    _lowerCAmelCase  :		Union[str, Any]=4    ,    _lowerCAmelCase  :		Dict=4    ,    _lowerCAmelCase  :		List[str]=True    ,    _lowerCAmelCase  :		int=300    ,    _lowerCAmelCase  :		Any=True    ,    _lowerCAmelCase  :		Optional[Any]=True    ,    _lowerCAmelCase  :		Optional[int]=1    ,    _lowerCAmelCase  :		Optional[int]=5    ,    _lowerCAmelCase  :		Tuple=2    ,    _lowerCAmelCase  :		Tuple=1    ,    _lowerCAmelCase  :		Optional[Any]=1    ,    _lowerCAmelCase  :		int=5    ,    _lowerCAmelCase  :		Any=2    ,    _lowerCAmelCase  :		Any=0.1    ,    _lowerCAmelCase  :		int=0.25    ,    **_lowerCAmelCase  :		Any    ,    ):
       if backbone_config is None:
             logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'						)
             SCREAMING_SNAKE_CASE_     						=      CONFIG_MAPPING['resnet'](out_features=['stage2', 'stage3', 'stage4']						)
       else:
             if isinstance(_lowerCAmelCase    ,    _lowerCAmelCase						):
                   SCREAMING_SNAKE_CASE_     						=      backbone_config.pop('model_type'						)
                   SCREAMING_SNAKE_CASE_     						=      CONFIG_MAPPING[backbone_model_type]
                   SCREAMING_SNAKE_CASE_     						=      config_class.from_dict(_lowerCAmelCase						)

       SCREAMING_SNAKE_CASE_     						=      backbone_config
       SCREAMING_SNAKE_CASE_     						=      num_queries
       SCREAMING_SNAKE_CASE_     						=      max_position_embeddings
       SCREAMING_SNAKE_CASE_     						=      d_model
       SCREAMING_SNAKE_CASE_     						=      encoder_ffn_dim
       SCREAMING_SNAKE_CASE_     						=      encoder_layers
       SCREAMING_SNAKE_CASE_     						=      encoder_attention_heads
       SCREAMING_SNAKE_CASE_     						=      decoder_ffn_dim
       SCREAMING_SNAKE_CASE_     						=      decoder_layers
       SCREAMING_SNAKE_CASE_     						=      decoder_attention_heads
       SCREAMING_SNAKE_CASE_     						=      dropout
       SCREAMING_SNAKE_CASE_     						=      attention_dropout
       SCREAMING_SNAKE_CASE_     						=      activation_dropout
       SCREAMING_SNAKE_CASE_     						=      activation_function
       SCREAMING_SNAKE_CASE_     						=      init_std
       SCREAMING_SNAKE_CASE_     						=      init_xavier_std
       SCREAMING_SNAKE_CASE_     						=      encoder_layerdrop
       SCREAMING_SNAKE_CASE_     						=      auxiliary_loss
       SCREAMING_SNAKE_CASE_     						=      position_embedding_type
       # deformable attributes
       SCREAMING_SNAKE_CASE_     						=      num_feature_levels
       SCREAMING_SNAKE_CASE_     						=      encoder_n_points
       SCREAMING_SNAKE_CASE_     						=      decoder_n_points
       SCREAMING_SNAKE_CASE_     						=      two_stage
       SCREAMING_SNAKE_CASE_     						=      two_stage_num_proposals
       SCREAMING_SNAKE_CASE_     						=      with_box_refine
       SCREAMING_SNAKE_CASE_     						=      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
       SCREAMING_SNAKE_CASE_     						=      class_cost
       SCREAMING_SNAKE_CASE_     						=      bbox_cost
       SCREAMING_SNAKE_CASE_     						=      giou_cost
       # Loss coefficients
       SCREAMING_SNAKE_CASE_     						=      mask_loss_coefficient
       SCREAMING_SNAKE_CASE_     						=      dice_loss_coefficient
       SCREAMING_SNAKE_CASE_     						=      bbox_loss_coefficient
       SCREAMING_SNAKE_CASE_     						=      giou_loss_coefficient
       SCREAMING_SNAKE_CASE_     						=      eos_coefficient
       SCREAMING_SNAKE_CASE_     						=      focal_alpha
       super().__init__(is_encoder_decoder=_lowerCAmelCase    ,    **_lowerCAmelCase						)



 @property
 def 				lowerCAmelCase_  (							self  :		Dict						):
       return self.encoder_attention_heads



 @property
 def 				lowerCAmelCase_  (							self  :		int						):
       return self.d_model




 def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_     						=      copy.deepcopy(self.__dict__						)
       SCREAMING_SNAKE_CASE_     						=      self.backbone_config.to_dict()
       SCREAMING_SNAKE_CASE_     						=      self.__class__.model_type
       return output





import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCamelCase__			:       str         =							get_tests_dir('fixtures/dummy-config.json')





class 	lowerCamelCase_							(				unittest.TestCase ):


 '''simple docstring'''







 def 				lowerCAmelCase_  (							self  :		Dict						):
       SCREAMING_SNAKE_CASE_     						=      0



 def 				lowerCAmelCase_  (							self  :		Optional[int]						):
       self.assertIsNotNone(transformers.models.auto.__spec__						)
       self.assertIsNotNone(importlib.util.find_spec('transformers.models.auto'						)						)



 def 				lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('bert-base-uncased'						)
       self.assertIsInstance(_lowerCAmelCase    ,    _lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase						)
       self.assertIsInstance(_lowerCAmelCase    ,    _lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Optional[Any]						):
       SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase						)
       self.assertIsInstance(_lowerCAmelCase    ,    _lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Dict						):
       SCREAMING_SNAKE_CASE_     						=      AutoConfig.for_model('roberta'						)
       self.assertIsInstance(_lowerCAmelCase    ,    _lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Dict						):
       with tempfile.TemporaryDirectory() as tmp_dir:
             # This model name contains bert and roberta, but roberta ends up being picked.
             SCREAMING_SNAKE_CASE_     						=      os.path.join(_lowerCAmelCase    ,    'fake-roberta'						)
             os.makedirs(_lowerCAmelCase    ,    exist_ok=_lowerCAmelCase						)
             with open(os.path.join(_lowerCAmelCase    ,    'config.json'						)    ,    'w'						) as f:
                   f.write(json.dumps({}						)						)
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase						)
             self.assertEqual(type(_lowerCAmelCase						)    ,    _lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Optional[Any]						):
       try:
             AutoConfig.register('custom'    ,    _lowerCAmelCase						)
             # Wrong model type will raise an error
             with self.assertRaises(_lowerCAmelCase						):
                   AutoConfig.register('model'    ,    _lowerCAmelCase						)
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(_lowerCAmelCase						):
                   AutoConfig.register('bert'    ,    _lowerCAmelCase						)

             # Now that the config is registered, it can be used as any other config with the auto-API
             SCREAMING_SNAKE_CASE_     						=      CustomConfig()
             with tempfile.TemporaryDirectory() as tmp_dir:
                   config.save_pretrained(_lowerCAmelCase						)
                   SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase						)
                   self.assertIsInstance(_lowerCAmelCase    ,    _lowerCAmelCase						)

       finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                   del CONFIG_MAPPING._extra_content["custom"]



 def 				lowerCAmelCase_  (							self  :		Optional[int]						):
       with self.assertRaisesRegex(
           _lowerCAmelCase    ,    'bert-base is not a local folder and is not a valid model identifier'						):
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('bert-base'						)



 def 				lowerCAmelCase_  (							self  :		int						):
       with self.assertRaisesRegex(
           _lowerCAmelCase    ,    R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'						):
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase    ,    revision='aaaaaa'						)



 def 				lowerCAmelCase_  (							self  :		Tuple						):
       with self.assertRaisesRegex(
           _lowerCAmelCase    ,    'hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'    ,    ):
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/no-config-test-repo'						)



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(_lowerCAmelCase						):
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'						)
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(_lowerCAmelCase						):
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'    ,    trust_remote_code=_lowerCAmelCase						)

       SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'    ,    trust_remote_code=_lowerCAmelCase						)
       self.assertEqual(config.__class__.__name__    ,    'NewModelConfig'						)

       # Test config can be reloaded.
       with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(_lowerCAmelCase						)
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained(_lowerCAmelCase    ,    trust_remote_code=_lowerCAmelCase						)
       self.assertEqual(reloaded_config.__class__.__name__    ,    'NewModelConfig'						)




 def 				lowerCAmelCase_  (							self  :		Any						):





       class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE ):


        '''simple docstring'''







        lowercase_       =   "new-model"

       try:
             AutoConfig.register('new-model'    ,    _lowerCAmelCase						)
             # If remote code is not set, the default is to use local
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'						)
             self.assertEqual(config.__class__.__name__    ,    'NewModelConfigLocal'						)

             # If remote code is disabled, we load the local one.
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'    ,    trust_remote_code=_lowerCAmelCase						)
             self.assertEqual(config.__class__.__name__    ,    'NewModelConfigLocal'						)

             # If remote is enabled, we load from the Hub
             SCREAMING_SNAKE_CASE_     						=      AutoConfig.from_pretrained('hf-internal-testing/test_dynamic_model'    ,    trust_remote_code=_lowerCAmelCase						)
             self.assertEqual(config.__class__.__name__    ,    'NewModelConfig'						)

       finally:
             if "new-model" in CONFIG_MAPPING._extra_content:
                   del CONFIG_MAPPING._extra_content["new-model"]