




"""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 					lowercase(     __a ):







 '''simple docstring'''





 lowercase__ 					=					"new-model"


if is_tf_available():






     class 					lowercase(     __a ):







      '''simple docstring'''





      lowercase__ 					=					NewModelConfig






@require_tf
class 					lowercase(     unittest.TestCase ):







 '''simple docstring'''





 @slow
 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		Dict								=  """bert-base-cased"""
   _snake_case							:		Tuple								=  AutoConfig.from_pretrained(a_		)
   self.assertIsNotNone(a_		)
   self.assertIsInstance(a_,      a_		)

   _snake_case							:		int								=  TFAutoModel.from_pretrained(a_		)
   self.assertIsNotNone(a_		)
   self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  """bert-base-cased"""
   _snake_case							:		Any								=  AutoConfig.from_pretrained(a_		)
   self.assertIsNotNone(a_		)
   self.assertIsInstance(a_,      a_		)

   _snake_case							:		Tuple								=  TFAutoModelForPreTraining.from_pretrained(a_		)
   self.assertIsNotNone(a_		)
   self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		str								=  AutoConfig.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)

     _snake_case							:		Optional[Any]								=  TFAutoModelForCausalLM.from_pretrained(a_		)
     _snake_case		,					_snake_case							:		Dict								=  TFAutoModelForCausalLM.from_pretrained(a_,      output_loading_info=a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		Tuple								=  AutoConfig.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)

     _snake_case							:		Union[str, Any]								=  TFAutoModelWithLMHead.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		int								=  AutoConfig.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)

     _snake_case							:		Optional[Any]								=  TFAutoModelForMaskedLM.from_pretrained(a_		)
     _snake_case		,					_snake_case							:		Optional[Any]								=  TFAutoModelForMaskedLM.from_pretrained(a_,      output_loading_info=a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		int								=  AutoConfig.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)

     _snake_case							:		List[Any]								=  TFAutoModelForSeqaSeqLM.from_pretrained(a_		)
     _snake_case		,					_snake_case							:		List[str]								=  TFAutoModelForSeqaSeqLM.from_pretrained(a_,      output_loading_info=a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


 @slow
 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



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

     _snake_case							:		Optional[Any]								=  TFAutoModelForSequenceClassification.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


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




   '''simple docstring'''



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

     _snake_case							:		List[Any]								=  TFAutoModelForQuestionAnswering.from_pretrained(a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


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




   '''simple docstring'''



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

     _snake_case							:		Dict								=  TFAutoModelForTableQuestionAnswering.from_pretrained(a_		)
     _snake_case		,					_snake_case							:		List[Any]								=  TFAutoModelForTableQuestionAnswering.from_pretrained(
         a_,      output_loading_info=a_		)
     self.assertIsNotNone(a_		)
     self.assertIsInstance(a_,      a_		)


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		Tuple								=  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  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  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  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		Any								=  TFAutoModel.from_pretrained("""sgugger/funnel-random-tiny"""		)
   self.assertIsInstance(a_,      a_		)

   _snake_case							:		Any								=  copy.deepcopy(model.config		)
   _snake_case							:		Dict								=  ["""FunnelBaseModel"""]
   _snake_case							:		Optional[Any]								=  TFAutoModel.from_config(a_		)
   self.assertIsInstance(a_,      a_		)

   with tempfile.TemporaryDirectory() as tmp_dir:
     model.save_pretrained(a_		)
     _snake_case							:		List[str]								=  TFAutoModel.from_pretrained(a_		)
     self.assertIsInstance(a_,      a_		)


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



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

     _snake_case							:		Union[str, Any]								=  [
         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							:		List[Any]								=  BertModelTester(self		).get_config()
         _snake_case							:		Optional[int]								=  NewModelConfig(**tiny_config.to_dict()		)
         _snake_case							:		List[str]								=  auto_class.from_config(a_		)
         self.assertIsInstance(a_,      a_		)

         with tempfile.TemporaryDirectory() as tmp_dir:
           model.save_pretrained(a_		)
           _snake_case							:		Optional[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  UpperCamelCase_   (				self:		Dict		):




   '''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  UpperCamelCase_   (				self:		Optional[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  UpperCamelCase_   (				self:		Optional[int]		):




   '''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							:		Optional[int]								=  TFAutoModel.from_pretrained("""hf-internal-testing/config-no-model"""		)


 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   with self.assertRaisesRegex(a_,      """Use `from_pt=True` to load this model"""		):
     _snake_case							:		Optional[int]								=  TFAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only"""		)


 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		Any								=  TFAutoModel.from_pretrained("""hf-internal-testing/tiny-random-bert"""		)
   with RequestCounter() as counter:
     _snake_case							:		Any								=  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							:		Dict								=  TFAutoModel.from_pretrained("""ArthurZ/tiny-random-bert-sharded"""		)
   with RequestCounter() as counter:
     _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		)



"""simple docstring"""


UpperCAmelCase__     						=  {
    """meter""": """m""",
    """kilometer""": """km""",
    """megametre""": """Mm""",
    """gigametre""": """Gm""",
    """terametre""": """Tm""",
    """petametre""": """Pm""",
    """exametre""": """Em""",
    """zettametre""": """Zm""",
    """yottametre""": """Ym""",
}
# Exponent of the factor(meter)
UpperCAmelCase__     						=  {
    """m""": 0,
    """km""": 3,
    """Mm""": 6,
    """Gm""": 9,
    """Tm""": 1_2,
    """Pm""": 1_5,
    """Em""": 1_8,
    """Zm""": 2_1,
    """Ym""": 2_4,
}






def 			__UpperCAmelCase    (	lowercase      ,lowercase      ,lowercase						):







  """simple docstring"""
  _UpperCAmelCase         =					from_type.lower().strip("""s"""						)
  _UpperCAmelCase         =					to_type.lower().strip("""s"""						)

  _UpperCAmelCase         =					UNIT_SYMBOL.get(lowercase      ,lowercase						)
  _UpperCAmelCase         =					UNIT_SYMBOL.get(lowercase      ,lowercase						)

  if from_sanitized not in METRIC_CONVERSION:
    _UpperCAmelCase         =					(
        f'''Invalid \'from_type\' value: {from_type!r}.\n'''
        f'''Conversion abbreviations are: {", ".join(lowercase						)}'''
    )
    raise ValueError(lowercase						)
  if to_sanitized not in METRIC_CONVERSION:
    _UpperCAmelCase         =					(
        f'''Invalid \'to_type\' value: {to_type!r}.\n'''
        f'''Conversion abbreviations are: {", ".join(lowercase						)}'''
    )
    raise ValueError(lowercase						)
  _UpperCAmelCase         =					METRIC_CONVERSION[from_sanitized]
  _UpperCAmelCase         =					METRIC_CONVERSION[to_sanitized]
  _UpperCAmelCase         =					1

  if from_exponent > to_exponent:
    _UpperCAmelCase         =					from_exponent - to_exponent
  else:
    _UpperCAmelCase         =					-(to_exponent - from_exponent)

  return value * pow(10      ,lowercase						)


if __name__ == "__main__":
    from doctest import testmod

    testmod()
