





"""simple docstring"""
import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
  from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
  from tensorflow.keras.optimizers import Adam






class 					_SCREAMING_SNAKE_CASE(   tf.keras.optimizers.schedules.LearningRateSchedule  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__ = 1.0		,SCREAMING_SNAKE_CASE__ = None		,)			->      Dict:





             """simple docstring"""






             super().__init__()
             __SCREAMING_SNAKE_CASE   :List[str]						=  initial_learning_rate
             __SCREAMING_SNAKE_CASE   :List[Any]						=  warmup_steps
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  power
             __SCREAMING_SNAKE_CASE   :int						=  decay_schedule_fn
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  name




       def __call__(						self		,SCREAMING_SNAKE_CASE__							)			->      Any:





             """simple docstring"""






             with tf.name_scope(self.name or '''WarmUp'''							) as name:
                   # Implements polynomial warmup. i.e., if global_step < warmup_steps, the
                   # learning rate will be `global_step/num_warmup_steps * init_lr`.
                   __SCREAMING_SNAKE_CASE   :List[str]						=  tf.cast(SCREAMING_SNAKE_CASE__		,tf.floataa							)
                   __SCREAMING_SNAKE_CASE   :str						=  tf.cast(self.warmup_steps		,tf.floataa							)
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  global_step_float / warmup_steps_float
                   __SCREAMING_SNAKE_CASE   :List[Any]						=  self.initial_learning_rate * tf.math.pow(SCREAMING_SNAKE_CASE__		,self.power							)
                   return tf.cond(
                       global_step_float < warmup_steps_float		,lambda: warmup_learning_rate		,lambda: self.decay_schedule_fn(step - self.warmup_steps							)		,name=SCREAMING_SNAKE_CASE__		,)




       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             return {
                 "initial_learning_rate": self.initial_learning_rate,
                 "decay_schedule_fn": self.decay_schedule_fn,
                 "warmup_steps": self.warmup_steps,
                 "power": self.power,
                 "name": self.name,
             }




def      __lowerCamelCase   (							a_     :		float ,   a_     :		int ,   a_     :		int ,   a_     :		float = 0.0 ,   a_     :		float = 0.9 ,   a_     :		float = 0.999 ,   a_     :		float = 1e-8 ,   a_     :		Optional[float] = None ,   a_     :		Optional[float] = None ,   a_     :		float = 0.0 ,   a_     :		float = 1.0 ,   a_     :		Optional[List[str]] = None ,   )			->      Optional[Any]:
      __SCREAMING_SNAKE_CASE   :List[Any]						=  tf.keras.optimizers.schedules.PolynomialDecay(
          initial_learning_rate=a_ ,   decay_steps=num_train_steps - num_warmup_steps ,   end_learning_rate=init_lr * min_lr_ratio ,   power=a_ ,   )
      if num_warmup_steps:
            __SCREAMING_SNAKE_CASE   :Optional[Any]						=  WarmUp(
                initial_learning_rate=a_ ,   decay_schedule_fn=a_ ,   warmup_steps=a_ ,   )
      if weight_decay_rate > 0.0:
            __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  AdamWeightDecay(
                learning_rate=a_ ,   weight_decay_rate=a_ ,   beta_a=a_ ,   beta_a=a_ ,   epsilon=a_ ,   clipnorm=a_ ,   global_clipnorm=a_ ,   exclude_from_weight_decay=['''LayerNorm''', '''layer_norm''', '''bias'''] ,   include_in_weight_decay=a_ ,   )
      else:
            __SCREAMING_SNAKE_CASE   :int						=  tf.keras.optimizers.Adam(
                learning_rate=a_ ,   beta_a=a_ ,   beta_a=a_ ,   epsilon=a_ ,   clipnorm=a_ ,   global_clipnorm=a_ ,   )
      # We return the optimizer and the LR scheduler in order to better track the
      # evolution of the LR independently of the optimizer.
      return optimizer, lr_schedule






class 					_SCREAMING_SNAKE_CASE(   A  ):




       def __init__(						self		,SCREAMING_SNAKE_CASE__ = 0.0_0_1		,SCREAMING_SNAKE_CASE__ = 0.9		,SCREAMING_SNAKE_CASE__ = 0.9_9_9		,SCREAMING_SNAKE_CASE__ = 1E-7		,SCREAMING_SNAKE_CASE__ = False		,SCREAMING_SNAKE_CASE__ = 0.0		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = None		,SCREAMING_SNAKE_CASE__ = "AdamWeightDecay"		,**SCREAMING_SNAKE_CASE__		,)			->      Any:





             """simple docstring"""






             super().__init__(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :List[Any]						=  weight_decay_rate
             __SCREAMING_SNAKE_CASE   :Dict						=  include_in_weight_decay
             __SCREAMING_SNAKE_CASE   :Tuple						=  exclude_from_weight_decay




       @classmethod
       def     _UpperCamelCase     (						cls		,SCREAMING_SNAKE_CASE__							)			->      List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Any						=  {'''WarmUp''': WarmUp}
             return super(SCREAMING_SNAKE_CASE__		,cls							).from_config(SCREAMING_SNAKE_CASE__		,custom_objects=SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Dict:





             """simple docstring"""






             super(SCREAMING_SNAKE_CASE__		,self							)._prepare_local(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Dict						=  tf.constant(
                 self.weight_decay_rate		,name='''adam_weight_decay_rate'''							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  self._do_use_weight_decay(var.name							)
             if do_decay:
                   return var.assign_sub(
                       learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]['''weight_decay_rate''']		,use_locking=self._use_locking		,)
             return tf.no_op()




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None		,**SCREAMING_SNAKE_CASE__							)			->      Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :str						=  list(zip(*SCREAMING_SNAKE_CASE__							)							)
             return super(SCREAMING_SNAKE_CASE__		,self							).apply_gradients(zip(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)		,name=SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Optional[int]:





             """simple docstring"""






             if apply_state is None:
                   return self._decayed_lr_t[var_dtype], {}

             __SCREAMING_SNAKE_CASE   :Any						=  apply_state or {}
             __SCREAMING_SNAKE_CASE   :Any						=  apply_state.get((var_device, var_dtype)							)
             if coefficients is None:
                   __SCREAMING_SNAKE_CASE   :Dict						=  self._fallback_apply_state(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE   :List[str]						=  coefficients

             return coefficients["lr_t"], {"apply_state": apply_state}




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None							)			->      Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Optional[int]						=  self._get_lr(var.device		,var.dtype.base_dtype		,SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  self._decay_weights_op(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             with tf.control_dependencies([decay]							):
                   return super(SCREAMING_SNAKE_CASE__		,self							)._resource_apply_dense(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=None							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Optional[Any]						=  self._get_lr(var.device		,var.dtype.base_dtype		,SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :List[Any]						=  self._decay_weights_op(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             with tf.control_dependencies([decay]							):
                   return super(SCREAMING_SNAKE_CASE__		,self							)._resource_apply_sparse(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,**SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :int						=  super().get_config()
             config.update({'''weight_decay_rate''': self.weight_decay_rate}							)
             return config




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__							)			->      List[str]:





             """simple docstring"""






             if self.weight_decay_rate == 0:
                   return False

             if self._include_in_weight_decay:
                   for r in self._include_in_weight_decay:
                         if re.search(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							) is not None:
                               return True

             if self._exclude_from_weight_decay:
                   for r in self._exclude_from_weight_decay:
                         if re.search(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							) is not None:
                               return False
             return True






class 					_SCREAMING_SNAKE_CASE(   A  ):




       def __init__(						self							)			->      Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  []
             __SCREAMING_SNAKE_CASE   :Dict						=  None




       @property
       def     _UpperCamelCase     (						self							)			->      Optional[int]:





             """simple docstring"""






             if self._accum_steps is None:
                   __SCREAMING_SNAKE_CASE   :str						=  tf.Variable(
                       tf.constant(0		,dtype=tf.intaa							)		,trainable=SCREAMING_SNAKE_CASE__		,synchronization=tf.VariableSynchronization.ON_READ		,aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA		,)

             return self._accum_steps.value()




       @property
       def     _UpperCamelCase     (						self							)			->      Optional[Any]:





             """simple docstring"""






             if not self._gradients:
                   raise ValueError('''The accumulator should be called first to initialize the gradients'''							)
             return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]




       def __call__(						self		,SCREAMING_SNAKE_CASE__							)			->      str:





             """simple docstring"""






             if not self._gradients:
                   __SCREAMING_SNAKE_CASE   :Tuple						=  self.step  # Create the step variable.
                   self._gradients.extend(
                       [
                           tf.Variable(
                               tf.zeros_like(SCREAMING_SNAKE_CASE__							)		,trainable=SCREAMING_SNAKE_CASE__		,synchronization=tf.VariableSynchronization.ON_READ		,aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA		,)
                           if gradient is not None
                           else gradient
                           for gradient in gradients
                       ]							)
             if len(SCREAMING_SNAKE_CASE__							) != len(self._gradients							):
                   raise ValueError(f'''Expected {len(self._gradients							)} gradients, but got {len(SCREAMING_SNAKE_CASE__							)}'''							)

             for accum_gradient, gradient in zip(self._gradients		,SCREAMING_SNAKE_CASE__							):
                   if accum_gradient is not None and gradient is not None:
                         accum_gradient.assign_add(SCREAMING_SNAKE_CASE__							)

             self._accum_steps.assign_add(1							)




       def     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






             if not self._gradients:
                   return
             self._accum_steps.assign(0							)
             for gradient in self._gradients:
                   if gradient is not None:
                         gradient.assign(tf.zeros_like(SCREAMING_SNAKE_CASE__							)							)






"""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 					_SCREAMING_SNAKE_CASE(   A  ):
       SCREAMING_SNAKE_CASE_   :      int							=  '''new-model'''


if is_tf_available():






  class 					_SCREAMING_SNAKE_CASE(   A  ):
         SCREAMING_SNAKE_CASE_   :      Union[str, Any]							=  NewModelConfig






@require_tf
class 					_SCREAMING_SNAKE_CASE(   unittest.TestCase  ):




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Tuple						=  '''bert-base-cased'''
             __SCREAMING_SNAKE_CASE   :int						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :List[str]						=  TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Tuple						=  '''bert-base-cased'''
             __SCREAMING_SNAKE_CASE   :List[str]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :str						=  TFAutoModelForPreTraining.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Dict:





             """simple docstring"""






             for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Dict						=  TFAutoModelForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__		,output_loading_info=SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :Any						=  TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Optional[int]:





             """simple docstring"""






             for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :Tuple						=  TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Tuple						=  TFAutoModelForMaskedLM.from_pretrained(SCREAMING_SNAKE_CASE__		,output_loading_info=SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Dict:





             """simple docstring"""






             for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :str						=  TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Optional[int]						=  TFAutoModelForSeqaSeqLM.from_pretrained(SCREAMING_SNAKE_CASE__		,output_loading_info=SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






             for model_name in ["bert-base-uncased"]:
                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :List[str]						=  TFAutoModelForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






             for model_name in ["bert-base-uncased"]:
                   __SCREAMING_SNAKE_CASE   :Optional[int]						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  TFAutoModelForQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       @require_tensorflow_probability
       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
                   __SCREAMING_SNAKE_CASE   :int						=  AutoConfig.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFAutoModelForTableQuestionAnswering.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Optional[int]						=  TFAutoModelForTableQuestionAnswering.from_pretrained(
                       SCREAMING_SNAKE_CASE__		,output_loading_info=SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :int						=  TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             self.assertEqual(model.num_parameters()		,1_44_10							)
             self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__							)		,1_44_10							)




       def     _UpperCamelCase     (						self							)			->      str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  TFAutoModelWithLMHead.from_pretrained(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)
             self.assertEqual(model.num_parameters()		,1_44_10							)
             self.assertEqual(model.num_parameters(only_trainable=SCREAMING_SNAKE_CASE__							)		,1_44_10							)




       def     _UpperCamelCase     (						self							)			->      Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE   :str						=  copy.deepcopy(model.config							)
             __SCREAMING_SNAKE_CASE   :List[str]						=  ['''FunnelBaseModel''']
             __SCREAMING_SNAKE_CASE   :int						=  TFAutoModel.from_config(SCREAMING_SNAKE_CASE__							)
             self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

             with tempfile.TemporaryDirectory() as tmp_dir:
                   model.save_pretrained(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      Optional[Any]:





             """simple docstring"""






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

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

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

                               with tempfile.TemporaryDirectory() as tmp_dir:
                                     model.save_pretrained(SCREAMING_SNAKE_CASE__							)
                                     __SCREAMING_SNAKE_CASE   :str						=  auto_class.from_pretrained(SCREAMING_SNAKE_CASE__							)
                                     self.assertIsInstance(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)

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





             """simple docstring"""






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




       def     _UpperCamelCase     (						self							)			->      Any:





             """simple docstring"""






             with self.assertRaisesRegex(
                 SCREAMING_SNAKE_CASE__		,R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''							):
                   __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFAutoModel.from_pretrained(SCREAMING_SNAKE_CASE__		,revision='''aaaaaa'''							)




       def     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






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




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





             """simple docstring"""






             with self.assertRaisesRegex(SCREAMING_SNAKE_CASE__		,'''Use `from_pt=True` to load this model'''							):
                   __SCREAMING_SNAKE_CASE   :List[str]						=  TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''							)




       def     _UpperCamelCase     (						self							)			->      List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :int						=  TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''							)
             with RequestCounter() as counter:
                   __SCREAMING_SNAKE_CASE   :int						=  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
             __SCREAMING_SNAKE_CASE   :Dict						=  TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''							)
             with RequestCounter() as counter:
                   __SCREAMING_SNAKE_CASE   :Optional[Any]						=  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							)