


"""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 _lowerCAmelCase				(      snake_case_	):
   __UpperCAmelCase :       Tuple							  =  '''new-model'''


if is_tf_available():







  class _lowerCAmelCase				(      snake_case_	):
     __UpperCAmelCase :       Optional[int]							  =  NewModelConfig







@require_tf
class _lowerCAmelCase				(      unittest.TestCase	):







   @slow
   def  lowerCamelCase		(	self						)	->		Union[str, Any]:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    "bert-base-cased"
       snake_case    :      Dict      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
       self.assertIsNotNone(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

       snake_case    :      Tuple      					=    TFAutoModel.from_pretrained(UpperCamelCase__						)
       self.assertIsNotNone(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    "bert-base-cased"
       snake_case    :      int      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
       self.assertIsNotNone(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

       snake_case    :      Union[str, Any]      					=    TFAutoModelForPreTraining.from_pretrained(UpperCamelCase__						)
       self.assertIsNotNone(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case    :      Optional[int]      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      List[Any]      					=    TFAutoModelForCausalLM.from_pretrained(UpperCamelCase__						)
           snake_case					,snake_case    :      Dict      					=    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]:
           snake_case    :      Tuple      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      Any      					=    TFAutoModelWithLMHead.from_pretrained(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]:
           snake_case    :      Optional[int]      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      Dict      					=    TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase__						)
           snake_case					,snake_case    :      str      					=    TFAutoModelForMaskedLM.from_pretrained(UpperCamelCase__						,	output_loading_info=UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




       for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case    :      List[Any]      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      str      					=    TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__						)
           snake_case					,snake_case    :      Optional[Any]      					=    TFAutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__						,	output_loading_info=UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




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

           snake_case    :      Any      					=    TFAutoModelForSequenceClassification.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       for model_name in ["bert-base-uncased"]:
           snake_case    :      List[Any]      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      str      					=    TFAutoModelForQuestionAnswering.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




       for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
           snake_case    :      Any      					=    AutoConfig.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

           snake_case    :      Optional[Any]      					=    TFAutoModelForTableQuestionAnswering.from_pretrained(UpperCamelCase__						)
           snake_case					,snake_case    :      Any      					=    TFAutoModelForTableQuestionAnswering.from_pretrained(
               UpperCamelCase__						,	output_loading_info=UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)
           self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case    :      List[Any]      					=    TFAutoModelWithLMHead.from_pretrained(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)
       self.assertEqual(model.num_parameters()						,	1_4410						)
       self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase__						)						,	1_4410						)







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      List[str]      					=    TFAutoModelWithLMHead.from_pretrained(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)
       self.assertEqual(model.num_parameters()						,	1_4410						)
       self.assertEqual(model.num_parameters(only_trainable=UpperCamelCase__						)						,	1_4410						)







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

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

       snake_case    :      Tuple      					=    copy.deepcopy(model.config						)
       snake_case    :      str      					=    ["FunnelBaseModel"]
       snake_case    :      int      					=    TFAutoModel.from_config(UpperCamelCase__						)
       self.assertIsInstance(UpperCamelCase__						,	UpperCamelCase__						)

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







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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

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

                   with tempfile.TemporaryDirectory() as tmp_dir:
                       model.save_pretrained(UpperCamelCase__						)
                       snake_case    :      str      					=    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						)	->		str:

       '''simple docstring'''




       with self.assertRaisesRegex(
           UpperCamelCase__						,	"bert-base is not a local folder and is not a valid model identifier"						):
           snake_case    :      Union[str, Any]      					=    TFAutoModel.from_pretrained("bert-base"						)







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       with self.assertRaisesRegex(
           UpperCamelCase__						,	r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"						):
           snake_case    :      Union[str, Any]      					=    TFAutoModel.from_pretrained(UpperCamelCase__						,	revision="aaaaaa"						)







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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







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

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




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

from .base import GenericTensor, Pipeline







class _lowerCAmelCase				(      snake_case_	):







   def  lowerCamelCase		(	self						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	**UpperCamelCase__						)	->		Union[str, Any]:

       '''simple docstring'''




       if tokenize_kwargs is None:
           snake_case    :      Optional[Any]      					=    {}

       if truncation is not None:
           if "truncation" in tokenize_kwargs:
               raise ValueError(
                   "truncation parameter defined twice (given as keyword argument as well as in tokenize_kwargs)"						)
           snake_case    :      List[str]      					=    truncation

       snake_case    :      Union[str, Any]      					=    tokenize_kwargs

       snake_case    :      List[Any]      					=    {}
       if return_tensors is not None:
           snake_case    :      Tuple      					=    return_tensors

       return preprocess_params, {}, postprocess_params







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	**UpperCamelCase__						)	->		Dict[str, GenericTensor]:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    self.framework
       snake_case    :      str      					=    self.tokenizer(UpperCamelCase__						,	return_tensors=UpperCamelCase__						,	**UpperCamelCase__						)
       return model_inputs







   def  lowerCamelCase		(	self						,	UpperCamelCase__						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      int      					=    self.model(**UpperCamelCase__						)
       return model_outputs







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__=False						)	->		Union[str, Any]:

       '''simple docstring'''




       if return_tensors:
           return model_outputs[0]
       if self.framework == "pt":
           return model_outputs[0].tolist()
       elif self.framework == "tf":
           return model_outputs[0].numpy().tolist()







   def __call__(	self						,	*UpperCamelCase__						,	**UpperCamelCase__						)	->		Union[str, Any]:

       '''simple docstring'''




       return super().__call__(*UpperCamelCase__						,	**UpperCamelCase__						)





