

'''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 			a       (	_a ):


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Optional[Any]							     =     "new-model"


if is_tf_available():



      class 			a       (	_a ):


           """simple docstring"""






           SCREAMING_SNAKE_CASE :				Optional[int]							     =     NewModelConfig



@require_tf
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     @slow
     def 				lowerCamelCase__				(       self  :	Dict    )		->							Dict:
          __UpperCAmelCase			:				Any													=    '''bert-base-cased'''
          __UpperCAmelCase			:				Union[str, Any]													=    AutoConfig.from_pretrained(snake_case    )
          self.assertIsNotNone(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )

          __UpperCAmelCase			:				Optional[int]													=    TFAutoModel.from_pretrained(snake_case    )
          self.assertIsNotNone(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     def 				lowerCamelCase__				(       self  :	List[str]    )		->							int:
          __UpperCAmelCase			:				List[str]													=    '''bert-base-cased'''
          __UpperCAmelCase			:				Dict													=    AutoConfig.from_pretrained(snake_case    )
          self.assertIsNotNone(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )

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

     @slow
     def 				lowerCamelCase__				(       self  :	Optional[Any]    )		->							List[Any]:
          for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase			:				int													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

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

     @slow
     def 				lowerCamelCase__				(       self  :	List[str]    )		->							Any:
          for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase			:				int													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

               __UpperCAmelCase			:				Optional[Any]													=    TFAutoModelWithLMHead.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     def 				lowerCamelCase__				(       self  :	Tuple    )		->							Union[str, Any]:
          for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase			:				Tuple													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

               __UpperCAmelCase			:				Union[str, Any]													=    TFAutoModelForMaskedLM.from_pretrained(snake_case    )
               __UpperCAmelCase			:				int													=    TFAutoModelForMaskedLM.from_pretrained(snake_case							,				output_loading_info=snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     def 				lowerCamelCase__				(       self  :	int    )		->							Dict:
          for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase			:				str													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

               __UpperCAmelCase			:				int													=    TFAutoModelForSeqaSeqLM.from_pretrained(snake_case    )
               __UpperCAmelCase			:				Tuple													=    TFAutoModelForSeqaSeqLM.from_pretrained(snake_case							,				output_loading_info=snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     def 				lowerCamelCase__				(       self  :	Dict    )		->							Optional[int]:
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
               __UpperCAmelCase			:				Tuple													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

               __UpperCAmelCase			:				List[str]													=    TFAutoModelForSequenceClassification.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     def 				lowerCamelCase__				(       self  :	Optional[Any]    )		->							Any:
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
               __UpperCAmelCase			:				Dict													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

               __UpperCAmelCase			:				Dict													=    TFAutoModelForQuestionAnswering.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

     @slow
     @require_tensorflow_probability
     def 				lowerCamelCase__				(       self  :	str    )		->							List[Any]:
          for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
               __UpperCAmelCase			:				Union[str, Any]													=    AutoConfig.from_pretrained(snake_case    )
               self.assertIsNotNone(snake_case    )
               self.assertIsInstance(snake_case							,				snake_case    )

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

     def 				lowerCamelCase__				(       self  :	Tuple    )		->							Dict:
          __UpperCAmelCase			:				int													=    TFAutoModelWithLMHead.from_pretrained(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )
          self.assertEqual(model.num_parameters()							,				1_4410    )
          self.assertEqual(model.num_parameters(only_trainable=snake_case    )							,				1_4410    )

     def 				lowerCamelCase__				(       self  :	List[str]    )		->							Optional[int]:
          __UpperCAmelCase			:				Optional[Any]													=    TFAutoModelWithLMHead.from_pretrained(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )
          self.assertEqual(model.num_parameters()							,				1_4410    )
          self.assertEqual(model.num_parameters(only_trainable=snake_case    )							,				1_4410    )

     def 				lowerCamelCase__				(       self  :	str    )		->							List[Any]:
          # For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
          __UpperCAmelCase			:				Any													=    TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''    )
          self.assertIsInstance(snake_case							,				snake_case    )

          __UpperCAmelCase			:				Tuple													=    copy.deepcopy(model.config    )
          __UpperCAmelCase			:				List[Any]													=    ['''FunnelBaseModel''']
          __UpperCAmelCase			:				Optional[Any]													=    TFAutoModel.from_config(snake_case    )
          self.assertIsInstance(snake_case							,				snake_case    )

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

     def 				lowerCamelCase__				(       self  :	Optional[Any]    )		->							Any:
          try:
               AutoConfig.register('''new-model'''							,				snake_case    )

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

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

                         with tempfile.TemporaryDirectory() as tmp_dir:
                              model.save_pretrained(snake_case    )
                              __UpperCAmelCase			:				Union[str, Any]													=    auto_class.from_pretrained(snake_case    )
                              self.assertIsInstance(snake_case							,				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 				lowerCamelCase__				(       self  :	Dict    )		->							List[str]:
          with self.assertRaisesRegex(
              snake_case							,				'''bert-base is not a local folder and is not a valid model identifier'''    ):
               __UpperCAmelCase			:				Optional[int]													=    TFAutoModel.from_pretrained('''bert-base'''    )

     def 				lowerCamelCase__				(       self  :	Union[str, Any]    )		->							str:
          with self.assertRaisesRegex(
              snake_case							,				r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''    ):
               __UpperCAmelCase			:				Dict													=    TFAutoModel.from_pretrained(snake_case							,				revision='''aaaaaa'''    )

     def 				lowerCamelCase__				(       self  :	int    )		->							List[str]:
          with self.assertRaisesRegex(
              snake_case							,				'''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'''							,				):
               __UpperCAmelCase			:				Dict													=    TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''    )

     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							List[Any]:
          with self.assertRaisesRegex(snake_case							,				'''Use `from_pt=True` to load this model'''    ):
               __UpperCAmelCase			:				Dict													=    TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''    )






     def 				lowerCamelCase__				(       self  :	List[str]    )		->							Dict:
          # Make sure we have cached the model.
          __UpperCAmelCase			:				Any													=    TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''    )
          with RequestCounter() as counter:
               __UpperCAmelCase			:				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
          __UpperCAmelCase			:				str													=    TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''    )
          with RequestCounter() as counter:
               __UpperCAmelCase			:				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'''



import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
      import tensorflow as tf

if is_tensorflow_text_available():
      from transformers.models.bert import TFBertTokenizer


__UpperCAmelCase    :Optional[int]				       =     ["bert-base-uncased", "bert-base-cased"]
__UpperCAmelCase    :str				       =     "hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():



      class 			a       (	tf.keras.Model ):


           """simple docstring"""






           def __init__(       self  :	List[str]							,				snake_case  :	List[str]    )		->							str:
                super().__init__()
                __UpperCAmelCase			:				List[str]													=    tokenizer
                __UpperCAmelCase			:				List[Any]													=    AutoConfig.from_pretrained(snake_case    )
                __UpperCAmelCase			:				int													=    TFAutoModel.from_config(snake_case    )






           def 				lowerCamelCase__				(       self  :	List[Any]							,				snake_case  :	Optional[int]    )		->							Optional[Any]:
                __UpperCAmelCase			:				List[Any]													=    self.tokenizer(snake_case    )
                __UpperCAmelCase			:				Optional[Any]													=    self.bert(**snake_case    )
                return out["pooler_output"]



@require_tf
@require_tensorflow_text
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     def 				lowerCamelCase__				(       self  :	Optional[int]    )		->							List[str]:
          super().setUp()

          __UpperCAmelCase			:				Tuple													=    [
              BertTokenizer.from_pretrained(snake_case    ) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
          ]  # repeat for when fast_bert_tokenizer=false
          __UpperCAmelCase			:				Any													=    [TFBertTokenizer.from_pretrained(snake_case    ) for checkpoint in TOKENIZER_CHECKPOINTS] + [
              TFBertTokenizer.from_pretrained(snake_case							,				use_fast_bert_tokenizer=snake_case    )
              for checkpoint in TOKENIZER_CHECKPOINTS
          ]
          assert len(self.tokenizers    ) == len(self.tf_tokenizers    )

          __UpperCAmelCase			:				Optional[int]													=    [
              '''This is a straightforward English test sentence.''',
              '''This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.''',
              '''Now we\'re going to add some Chinese: 一 二 三 一二三''',
              '''And some much more rare Chinese: 齉 堃 齉堃''',
              '''Je vais aussi écrire en français pour tester les accents''',
              '''Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ''',
          ]
          __UpperCAmelCase			:				Optional[int]													=    list(zip(self.test_sentences							,				self.test_sentences[::-1]    )    )

     def 				lowerCamelCase__				(       self  :	Optional[int]    )		->							Optional[Any]:
          for tokenizer, tf_tokenizer in zip(self.tokenizers							,				self.tf_tokenizers    ):
               for test_inputs in (self.test_sentences, self.paired_sentences):
                    __UpperCAmelCase			:				Any													=    tokenizer(snake_case							,				return_tensors='''tf'''							,				padding='''longest'''    )
                    __UpperCAmelCase			:				Optional[int]													=    tf_tokenizer(snake_case    )

                    for key in python_outputs.keys():
                         self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape    )    )
                         self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]							,				tf.intaa    ) == tf_outputs[key]    )    )

     @slow
     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							str:
          for tf_tokenizer in self.tf_tokenizers:
               __UpperCAmelCase			:				Any													=    tf_tokenizer(self.paired_sentences    )
               __UpperCAmelCase			:				Union[str, Any]													=    tf_tokenizer(
                   text=[sentence[0] for sentence in self.paired_sentences]							,				text_pair=[sentence[1] for sentence in self.paired_sentences]							,				)
               for key in merged_outputs.keys():
                    self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]							,				tf.intaa    ) == separated_outputs[key]    )    )

     @slow
     def 				lowerCamelCase__				(       self  :	str    )		->							Union[str, Any]:
          for tf_tokenizer in self.tf_tokenizers:
               __UpperCAmelCase			:				Optional[int]													=    tf.function(snake_case    )
               for test_inputs in (self.test_sentences, self.paired_sentences):
                    __UpperCAmelCase			:				int													=    tf.constant(snake_case    )
                    __UpperCAmelCase			:				Tuple													=    compiled_tokenizer(snake_case    )
                    __UpperCAmelCase			:				Optional[int]													=    tf_tokenizer(snake_case    )

                    for key in eager_outputs.keys():
                         self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]    )    )






     @slow
     def 				lowerCamelCase__				(       self  :	str    )		->							str:
          for tf_tokenizer in self.tf_tokenizers:
               __UpperCAmelCase			:				List[Any]													=    ModelToSave(tokenizer=snake_case    )
               __UpperCAmelCase			:				Union[str, Any]													=    tf.convert_to_tensor(self.test_sentences    )
               __UpperCAmelCase			:				Tuple													=    model(snake_case    )  # Build model with some sample inputs
               with TemporaryDirectory() as tempdir:
                    __UpperCAmelCase			:				Any													=    Path(snake_case    ) / '''saved.model'''
                    model.save(snake_case    )
                    __UpperCAmelCase			:				str													=    tf.keras.models.load_model(snake_case    )
               __UpperCAmelCase			:				Optional[int]													=    loaded_model(snake_case    )
               # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
               self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output    )    )							,				1E-5    )