






'''simple docstring'''




from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
    from transformers import (
        TFAutoModel,
        TFAutoModelForCausalLM,
        TFAutoModelForMaskedLM,
        TFAutoModelForPreTraining,
        TFAutoModelForQuestionAnswering,
        TFAutoModelForSeqaSeqLM,
        TFAutoModelForSequenceClassification,
        TFAutoModelForTableQuestionAnswering,
        TFAutoModelForTokenClassification,
        TFAutoModelWithLMHead,
        TFBertForMaskedLM,
        TFBertForPreTraining,
        TFBertForQuestionAnswering,
        TFBertForSequenceClassification,
        TFBertModel,
        TFFunnelBaseModel,
        TFFunnelModel,
        TFGPTaLMHeadModel,
        TFRobertaForMaskedLM,
        TFTaForConditionalGeneration,
        TFTapasForQuestionAnswering,
    )
    from transformers.models.auto.modeling_tf_auto import (
        TF_MODEL_FOR_CAUSAL_LM_MAPPING,
        TF_MODEL_FOR_MASKED_LM_MAPPING,
        TF_MODEL_FOR_PRETRAINING_MAPPING,
        TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
        TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
        TF_MODEL_MAPPING,
    )
    from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
    from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
    from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST
    from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST

class    _UpperCAmelCase			(     snake_case_						):




   """simple docstring"""

   snake_case    				=						'''new-model'''


if is_tf_available():

    class    _UpperCAmelCase			(     snake_case_						):




       """simple docstring"""

       snake_case    				=						NewModelConfig

@require_tf
class    _UpperCAmelCase			(     unittest.TestCase						):




   """simple docstring"""

   @slow
   def 						lowerCAmelCase (		self :	Union[str, Any]		):





         '''simple docstring'''





         _A			     =	"bert-base-cased"
         _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
         self.assertIsNotNone(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

         _A			     =	TFAutoModel.from_pretrained(__UpperCAmelCase		)
         self.assertIsNotNone(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Any		):





         '''simple docstring'''





         _A			     =	"bert-base-cased"
         _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
         self.assertIsNotNone(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

         _A			     =	TFAutoModelForPreTraining.from_pretrained(__UpperCAmelCase		)
         self.assertIsNotNone(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForCausalLM.from_pretrained(__UpperCAmelCase		)
               _A     ,	_A			     =	TFAutoModelForCausalLM.from_pretrained(__UpperCAmelCase      ,      output_loading_info=__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Any		):





         '''simple docstring'''





         for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelWithLMHead.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForMaskedLM.from_pretrained(__UpperCAmelCase		)
               _A     ,	_A			     =	TFAutoModelForMaskedLM.from_pretrained(__UpperCAmelCase      ,      output_loading_info=__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase		)
               _A     ,	_A			     =	TFAutoModelForSeqaSeqLM.from_pretrained(__UpperCAmelCase      ,      output_loading_info=__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Dict		):





         '''simple docstring'''





         for model_name in ["bert-base-uncased"]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForSequenceClassification.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   def 						lowerCAmelCase (		self :	Tuple		):





         '''simple docstring'''





         for model_name in ["bert-base-uncased"]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForQuestionAnswering.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   @slow
   @require_tensorflow_probability
   def 						lowerCAmelCase (		self :	List[Any]		):





         '''simple docstring'''





         for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
               _A			     =	AutoConfig.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

               _A			     =	TFAutoModelForTableQuestionAnswering.from_pretrained(__UpperCAmelCase		)
               _A     ,	_A			     =	TFAutoModelForTableQuestionAnswering.from_pretrained(
                   __UpperCAmelCase      ,      output_loading_info=__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         _A			     =	TFAutoModelWithLMHead.from_pretrained(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)
         self.assertEqual(model.num_parameters()      ,      14410		)
         self.assertEqual(model.num_parameters(only_trainable=__UpperCAmelCase		)      ,      14410		)

   def 						lowerCAmelCase (		self :	Tuple		):





         '''simple docstring'''





         _A			     =	TFAutoModelWithLMHead.from_pretrained(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)
         self.assertEqual(model.num_parameters()      ,      14410		)
         self.assertEqual(model.num_parameters(only_trainable=__UpperCAmelCase		)      ,      14410		)

   def 						lowerCAmelCase (		self :	int		):





         '''simple docstring'''





         _A			     =	TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

         _A			     =	copy.deepcopy(model.config		)
         _A			     =	["FunnelBaseModel"]
         _A			     =	TFAutoModel.from_config(__UpperCAmelCase		)
         self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

         with tempfile.TemporaryDirectory() as tmp_dir:
               model.save_pretrained(__UpperCAmelCase		)
               _A			     =	TFAutoModel.from_pretrained(__UpperCAmelCase		)
               self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         try:
               AutoConfig.register("new-model"      ,      __UpperCAmelCase		)

               _A			     =	[
                   TFAutoModel,
                   TFAutoModelForCausalLM,
                   TFAutoModelForMaskedLM,
                   TFAutoModelForPreTraining,
                   TFAutoModelForQuestionAnswering,
                   TFAutoModelForSequenceClassification,
                   TFAutoModelForTokenClassification,
               ]

               for auto_class in auto_classes:
                     with self.subTest(auto_class.__name__		):
                           # Wrong config class will raise an error
                           with self.assertRaises(__UpperCAmelCase		):
                                 auto_class.register(__UpperCAmelCase      ,      __UpperCAmelCase		)
                           auto_class.register(__UpperCAmelCase      ,      __UpperCAmelCase		)
                           # Trying to register something existing in the Transformers library will raise an error
                           with self.assertRaises(__UpperCAmelCase		):
                                 auto_class.register(__UpperCAmelCase      ,      __UpperCAmelCase		)

                           # Now that the config is registered, it can be used as any other config with the auto-API
                           _A			     =	BertModelTester(self		).get_config()
                           _A			     =	NewModelConfig(**tiny_config.to_dict()		)
                           _A			     =	auto_class.from_config(__UpperCAmelCase		)
                           self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

                           with tempfile.TemporaryDirectory() as tmp_dir:
                                 model.save_pretrained(__UpperCAmelCase		)
                                 _A			     =	auto_class.from_pretrained(__UpperCAmelCase		)
                                 self.assertIsInstance(__UpperCAmelCase      ,      __UpperCAmelCase		)

         finally:
               if "new-model" in CONFIG_MAPPING._extra_content:
                     del CONFIG_MAPPING._extra_content["new-model"]
               for mapping in (
                   TF_MODEL_MAPPING,
                   TF_MODEL_FOR_PRETRAINING_MAPPING,
                   TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
                   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
                   TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
                   TF_MODEL_FOR_CAUSAL_LM_MAPPING,
                   TF_MODEL_FOR_MASKED_LM_MAPPING,
               ):
                     if NewModelConfig in mapping._extra_content:
                           del mapping._extra_content[NewModelConfig]

   def 						lowerCAmelCase (		self :	Dict		):





         '''simple docstring'''





         with self.assertRaisesRegex(
             __UpperCAmelCase      ,      "bert-base is not a local folder and is not a valid model identifier"		):
               _A			     =	TFAutoModel.from_pretrained("bert-base"		)

   def 						lowerCAmelCase (		self :	str		):





         '''simple docstring'''





         with self.assertRaisesRegex(
             __UpperCAmelCase      ,      R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"		):
               _A			     =	TFAutoModel.from_pretrained(__UpperCAmelCase      ,      revision="aaaaaa"		)

   def 						lowerCAmelCase (		self :	Any		):





         '''simple docstring'''





         with self.assertRaisesRegex(
             __UpperCAmelCase      ,      "hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin"      ,      ):
               _A			     =	TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"		)

   def 						lowerCAmelCase (		self :	Any		):





         '''simple docstring'''





         with self.assertRaisesRegex(__UpperCAmelCase      ,      "Use `from_pt=True` to load this model"		):
               _A			     =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"		)

   def 						lowerCAmelCase (		self :	List[str]		):





         '''simple docstring'''





         _A			     =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"		)
         with RequestCounter() as counter:
               _A			     =	TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"		)
               self.assertEqual(counter.get_request_count      ,      0		)
               self.assertEqual(counter.head_request_count      ,      1		)
               self.assertEqual(counter.other_request_count      ,      0		)

         # With a sharded checkpoint
         _A			     =	TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"		)
         with RequestCounter() as counter:
               _A			     =	TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"		)
               self.assertEqual(counter.get_request_count      ,      0		)
               self.assertEqual(counter.head_request_count      ,      1		)
               self.assertEqual(counter.other_request_count      ,      0		)









from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase								=	logging.get_logger(__name__)

UpperCamelCase								=	{
    """vinvino02/glpn-kitti""": """https://huggingface.co/vinvino02/glpn-kitti/resolve/main/config.json""",
    # See all GLPN models at https://huggingface.co/models?filter=glpn
}






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "glpn"



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=3				,       _SCREAMING_SNAKE_CASE=4				,       _SCREAMING_SNAKE_CASE=[2, 2, 2, 2]				,       _SCREAMING_SNAKE_CASE=[8, 4, 2, 1]				,       _SCREAMING_SNAKE_CASE=[32, 64, 160, 256]				,       _SCREAMING_SNAKE_CASE=[7, 3, 3, 3]				,       _SCREAMING_SNAKE_CASE=[4, 2, 2, 2]				,       _SCREAMING_SNAKE_CASE=[1, 2, 5, 8]				,       _SCREAMING_SNAKE_CASE=[4, 4, 4, 4]				,       _SCREAMING_SNAKE_CASE="gelu"				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=0.0_2				,       _SCREAMING_SNAKE_CASE=0.1				,       _SCREAMING_SNAKE_CASE=1e-6				,       _SCREAMING_SNAKE_CASE=64				,       _SCREAMING_SNAKE_CASE=10				,       _SCREAMING_SNAKE_CASE=-1				,       **_SCREAMING_SNAKE_CASE				,       )->Any:




         '''simple docstring'''





         super().__init__(**_SCREAMING_SNAKE_CASE		)

         A_      :				Optional[int]		     =					num_channels
         A_      :				Union[str, Any]		     =					num_encoder_blocks
         A_      :				int		     =					depths
         A_      :				Dict		     =					sr_ratios
         A_      :				Any		     =					hidden_sizes
         A_      :				int		     =					patch_sizes
         A_      :				Optional[int]		     =					strides
         A_      :				str		     =					mlp_ratios
         A_      :				List[str]		     =					num_attention_heads
         A_      :				str		     =					hidden_act
         A_      :				int		     =					hidden_dropout_prob
         A_      :				List[Any]		     =					attention_probs_dropout_prob
         A_      :				Optional[Any]		     =					initializer_range
         A_      :				Tuple		     =					drop_path_rate
         A_      :				Optional[int]		     =					layer_norm_eps
         A_      :				List[str]		     =					decoder_hidden_size
         A_      :				List[Any]		     =					max_depth
         A_      :				List[Any]		     =					head_in_index




