





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        SCREAMING_SNAKE_CASE     (UpperCAmelCase   ):
   _UpperCamelCase					:	str				      = 'new-model'


if is_tf_available():
      class        SCREAMING_SNAKE_CASE     (UpperCAmelCase   ):
         _UpperCamelCase					:	Optional[Any]				      = NewModelConfig
@require_tf
class        SCREAMING_SNAKE_CASE     (unittest.TestCase   ):


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     int:





     """simple docstring"""







     lowercase__			    =						'bert-base-cased'
     lowercase__			    =						AutoConfig.from_pretrained(a )
     self.assertIsNotNone(a )
     self.assertIsInstance(a    ,  a )

     lowercase__			    =						TFAutoModel.from_pretrained(a )
     self.assertIsNotNone(a )
     self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     Dict:





     """simple docstring"""







     lowercase__			    =						'bert-base-cased'
     lowercase__			    =						AutoConfig.from_pretrained(a )
     self.assertIsNotNone(a )
     self.assertIsInstance(a    ,  a )

     lowercase__			    =						TFAutoModelForPreTraining.from_pretrained(a )
     self.assertIsNotNone(a )
     self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     List[str]:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForCausalLM.from_pretrained(a )
       lowercase__    ,					lowercase__			    =						TFAutoModelForCausalLM.from_pretrained(a    ,  output_loading_info=a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Optional[Any]:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelWithLMHead.from_pretrained(a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     str:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForMaskedLM.from_pretrained(a )
       lowercase__    ,					lowercase__			    =						TFAutoModelForMaskedLM.from_pretrained(a    ,  output_loading_info=a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     Dict:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForSeqaSeqLM.from_pretrained(a )
       lowercase__    ,					lowercase__			    =						TFAutoModelForSeqaSeqLM.from_pretrained(a    ,  output_loading_info=a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Tuple:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForSequenceClassification.from_pretrained(a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     Any:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForQuestionAnswering.from_pretrained(a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   @slow
   @require_tensorflow_probability
   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     List[Any]:





     """simple docstring"""







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

       lowercase__			    =						TFAutoModelForTableQuestionAnswering.from_pretrained(a )
       lowercase__    ,					lowercase__			    =						TFAutoModelForTableQuestionAnswering.from_pretrained(
           a    ,  output_loading_info=a )
       self.assertIsNotNone(a )
       self.assertIsInstance(a    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     str:





     """simple docstring"""







     lowercase__			    =						TFAutoModelWithLMHead.from_pretrained(a )
     self.assertIsInstance(a    ,  a )
     self.assertEqual(model.num_parameters()    ,  14_410 )
     self.assertEqual(model.num_parameters(only_trainable=a )    ,  14_410 )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Tuple:





     """simple docstring"""







     lowercase__			    =						TFAutoModelWithLMHead.from_pretrained(a )
     self.assertIsInstance(a    ,  a )
     self.assertEqual(model.num_parameters()    ,  14_410 )
     self.assertEqual(model.num_parameters(only_trainable=a )    ,  14_410 )


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





     """simple docstring"""







     lowercase__			    =						TFAutoModel.from_pretrained('sgugger/funnel-random-tiny' )
     self.assertIsInstance(a    ,  a )

     lowercase__			    =						copy.deepcopy(model.config )
     lowercase__			    =						['FunnelBaseModel']
     lowercase__			    =						TFAutoModel.from_config(a )
     self.assertIsInstance(a    ,  a )

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


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     List[Any]:





     """simple docstring"""







     try:
       AutoConfig.register('new-model'    ,  a )

       lowercase__			    =						[
           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
           lowercase__			    =						BertModelTester(self ).get_config()
           lowercase__			    =						NewModelConfig(**tiny_config.to_dict() )
           lowercase__			    =						auto_class.from_config(a )
           self.assertIsInstance(a    ,  a )

           with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(a )
             lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Any:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[int] )->     Optional[int]:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     Tuple:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     int:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Union[str, Any]:





     """simple docstring"""







     lowercase__			    =						TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert' )
     with RequestCounter() as counter:
       lowercase__			    =						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
     lowercase__			    =						TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded' )
     with RequestCounter() as counter:
       lowercase__			    =						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 __future__ import annotations

import math
from collections.abc import Callable




def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE = 100	,		)       ->     float:
  lowercase__			    =						x_start
  lowercase__			    =						fnc(_SCREAMING_SNAKE_CASE	)
  lowercase__			    =						0.0

  for _ in range(_SCREAMING_SNAKE_CASE	):
    # Approximates curve as a sequence of linear lines and sums their length
    lowercase__			    =						(x_end - x_start) / steps + xa
    lowercase__			    =						fnc(_SCREAMING_SNAKE_CASE	)
    length += math.hypot(xa - xa	,		fxa - fxa	)

    # Increment step
    lowercase__			    =						xa
    lowercase__			    =						fxa

  return length


if __name__ == "__main__":




      def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	)       ->     str:
        return math.sin(10 * x	)

      print("""f(x) = sin(10 * x)""")
      print("""The length of the curve from x = -10 to x = 10 is:""")
      lowercase_					     =	10
      while i <= 100_000:
            print(f'''With {i} steps: {line_length(f, -10, 10, i)}''')
            i *= 10


