



'''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  snake_case__						(    SCREAMING_SNAKE_CASE_				):
 A__				= '''new-model'''


if is_tf_available():



    class  snake_case__						(    SCREAMING_SNAKE_CASE_				):
     A__				= NewModelConfig



@require_tf
class  snake_case__						(    unittest.TestCase				):
 @slow
 def A_   (   self						:							str  )      ->   Any:







       '''simple docstring'''
       __snake_case :   Optional[int]													=    'bert-base-cased'
       __snake_case :   List[str]													=    AutoConfig.from_pretrained(__a  )
       self.assertIsNotNone(__a  )
       self.assertIsInstance(__a      ,     __a  )

       __snake_case :   Dict													=    TFAutoModel.from_pretrained(__a  )
       self.assertIsNotNone(__a  )
       self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							Union[str, Any]  )      ->   Dict:







       '''simple docstring'''
       __snake_case :   Dict													=    'bert-base-cased'
       __snake_case :   int													=    AutoConfig.from_pretrained(__a  )
       self.assertIsNotNone(__a  )
       self.assertIsInstance(__a      ,     __a  )

       __snake_case :   str													=    TFAutoModelForPreTraining.from_pretrained(__a  )
       self.assertIsNotNone(__a  )
       self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							int  )      ->   List[Any]:







       '''simple docstring'''
       for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __snake_case :   Dict													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   Optional[int]													=    TFAutoModelForCausalLM.from_pretrained(__a  )
             __snake_case							,     __snake_case :   Optional[int]													=    TFAutoModelForCausalLM.from_pretrained(__a      ,     output_loading_info=__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							List[Any]  )      ->   Any:







       '''simple docstring'''
       for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __snake_case :   Optional[Any]													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   Optional[int]													=    TFAutoModelWithLMHead.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							Union[str, Any]  )      ->   Tuple:







       '''simple docstring'''
       for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __snake_case :   Union[str, Any]													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   int													=    TFAutoModelForMaskedLM.from_pretrained(__a  )
             __snake_case							,     __snake_case :   Optional[int]													=    TFAutoModelForMaskedLM.from_pretrained(__a      ,     output_loading_info=__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							List[str]  )      ->   Tuple:







       '''simple docstring'''
       for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __snake_case :   Optional[int]													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   Optional[int]													=    TFAutoModelForSeqaSeqLM.from_pretrained(__a  )
             __snake_case							,     __snake_case :   Tuple													=    TFAutoModelForSeqaSeqLM.from_pretrained(__a      ,     output_loading_info=__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							Tuple  )      ->   int:







       '''simple docstring'''
       # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       for model_name in ["bert-base-uncased"]:
             __snake_case :   int													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   List[Any]													=    TFAutoModelForSequenceClassification.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 def A_   (   self						:							Union[str, Any]  )      ->   Tuple:







       '''simple docstring'''
       # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       for model_name in ["bert-base-uncased"]:
             __snake_case :   List[Any]													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   Tuple													=    TFAutoModelForQuestionAnswering.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 @slow
 @require_tensorflow_probability
 def A_   (   self						:							Optional[Any]  )      ->   List[str]:







       '''simple docstring'''
       for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
             __snake_case :   List[Any]													=    AutoConfig.from_pretrained(__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )

             __snake_case :   Tuple													=    TFAutoModelForTableQuestionAnswering.from_pretrained(__a  )
             __snake_case							,     __snake_case :   Any													=    TFAutoModelForTableQuestionAnswering.from_pretrained(
                 __a      ,     output_loading_info=__a  )
             self.assertIsNotNone(__a  )
             self.assertIsInstance(__a      ,     __a  )
 def A_   (   self						:							Tuple  )      ->   str:







       '''simple docstring'''
       __snake_case :   str													=    TFAutoModelWithLMHead.from_pretrained(__a  )
       self.assertIsInstance(__a      ,     __a  )
       self.assertEqual(model.num_parameters()      ,     14410  )
       self.assertEqual(model.num_parameters(only_trainable=__a  )      ,     14410  )
 def A_   (   self						:							List[str]  )      ->   List[str]:







       '''simple docstring'''
       __snake_case :   Optional[Any]													=    TFAutoModelWithLMHead.from_pretrained(__a  )
       self.assertIsInstance(__a      ,     __a  )
       self.assertEqual(model.num_parameters()      ,     14410  )
       self.assertEqual(model.num_parameters(only_trainable=__a  )      ,     14410  )
 def A_   (   self						:							int  )      ->   Optional[Any]:







       '''simple docstring'''
       # For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
       __snake_case :   Optional[int]													=    TFAutoModel.from_pretrained('sgugger/funnel-random-tiny'  )
       self.assertIsInstance(__a      ,     __a  )

       __snake_case :   Optional[Any]													=    copy.deepcopy(model.config  )
       __snake_case :   Any													=    ['FunnelBaseModel']
       __snake_case :   Any													=    TFAutoModel.from_config(__a  )
       self.assertIsInstance(__a      ,     __a  )

       with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(__a  )
             __snake_case :   Optional[int]													=    TFAutoModel.from_pretrained(__a  )
             self.assertIsInstance(__a      ,     __a  )
 def A_   (   self						:							List[Any]  )      ->   Union[str, Any]:







       '''simple docstring'''
       try:
             AutoConfig.register('new-model'      ,     __a  )

             __snake_case :   Tuple													=    [
                 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(__a  ):
                               auto_class.register(__a      ,     __a  )
                         auto_class.register(__a      ,     __a  )
                         # Trying to register something existing in the Transformers library will raise an error
                         with self.assertRaises(__a  ):
                               auto_class.register(__a      ,     __a  )

                         # Now that the config is registered, it can be used as any other config with the auto-API
                         __snake_case :   Union[str, Any]													=    BertModelTester(self  ).get_config()
                         __snake_case :   Dict													=    NewModelConfig(**tiny_config.to_dict()  )
                         __snake_case :   Union[str, Any]													=    auto_class.from_config(__a  )
                         self.assertIsInstance(__a      ,     __a  )

                         with tempfile.TemporaryDirectory() as tmp_dir:
                               model.save_pretrained(__a  )
                               __snake_case :   Any													=    auto_class.from_pretrained(__a  )
                               self.assertIsInstance(__a      ,     __a  )

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







       '''simple docstring'''
       with self.assertRaisesRegex(
           __a      ,     'bert-base is not a local folder and is not a valid model identifier'  ):
             __snake_case :   List[Any]													=    TFAutoModel.from_pretrained('bert-base'  )
 def A_   (   self						:							List[Any]  )      ->   List[Any]:







       '''simple docstring'''
       with self.assertRaisesRegex(
           __a      ,     r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'  ):
             __snake_case :   int													=    TFAutoModel.from_pretrained(__a      ,     revision='aaaaaa'  )
 def A_   (   self						:							int  )      ->   str:







       '''simple docstring'''
       with self.assertRaisesRegex(
           __a      ,     'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'      ,     ):
             __snake_case :   int													=    TFAutoModel.from_pretrained('hf-internal-testing/config-no-model'  )
 def A_   (   self						:							Union[str, Any]  )      ->   Dict:







       '''simple docstring'''
       with self.assertRaisesRegex(__a      ,     'Use `from_pt=True` to load this model'  ):
             __snake_case :   Any													=    TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'  )





 def A_   (   self						:							Dict  )      ->   Tuple:







       '''simple docstring'''
       # Make sure we have cached the model.
       __snake_case :   List[str]													=    TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert'  )
       with RequestCounter() as counter:
             __snake_case :   List[str]													=    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
       __snake_case :   Optional[int]													=    TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded'  )
       with RequestCounter() as counter:
             __snake_case :   int													=    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  )








'''simple docstring'''





from abc import ABC, abstractmethod
from argparse import ArgumentParser



class  snake_case__						(    SCREAMING_SNAKE_CASE_				):
 @staticmethod
 @abstractmethod
 def A_   (   __a						:							ArgumentParser  )      ->   List[str]:







       '''simple docstring'''
       raise NotImplementedError()





 @abstractmethod
 def A_   (   self						:							Optional[Any]  )      ->   Optional[Any]:







       '''simple docstring'''
       raise NotImplementedError()



