





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     UpperCamelCase_   (			UpperCAmelCase__					):

    '''simple docstring'''






    UpperCAmelCase__							  =				'''new-model'''


if is_tf_available():


  class     UpperCamelCase_   (			UpperCAmelCase__					):

      '''simple docstring'''






      UpperCAmelCase__							  =				NewModelConfig


@require_tf
class     UpperCamelCase_   (			unittest.TestCase					):

    '''simple docstring'''






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





           '''simple docstring'''

           A__							=   '''bert-base-cased'''
           A__							=   AutoConfig.from_pretrained(UpperCAmelCase__)
           self.assertIsNotNone(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)

           A__							=   TFAutoModel.from_pretrained(UpperCAmelCase__)
           self.assertIsNotNone(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

           A__							=   '''bert-base-cased'''
           A__							=   AutoConfig.from_pretrained(UpperCAmelCase__)
           self.assertIsNotNone(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)

           A__							=   TFAutoModelForPreTraining.from_pretrained(UpperCAmelCase__)
           self.assertIsNotNone(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelForCausalLM.from_pretrained(UpperCAmelCase__)
                  A__			,							A__							=   TFAutoModelForCausalLM.from_pretrained(UpperCAmelCase__ ,			output_loading_info=UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelWithLMHead.from_pretrained(UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelForMaskedLM.from_pretrained(UpperCAmelCase__)
                  A__			,							A__							=   TFAutoModelForMaskedLM.from_pretrained(UpperCAmelCase__ ,			output_loading_info=UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase__)
                  A__			,							A__							=   TFAutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase__ ,			output_loading_info=UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelForSequenceClassification.from_pretrained(UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


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





           '''simple docstring'''

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

                  A__							=   TFAutoModelForQuestionAnswering.from_pretrained(UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


    @slow
    @require_tensorflow_probability
    def 	SCREAMING_SNAKE_CASE							(      self				:     List[Any])						->int:





           '''simple docstring'''

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

                  A__							=   TFAutoModelForTableQuestionAnswering.from_pretrained(UpperCAmelCase__)
                  A__			,							A__							=   TFAutoModelForTableQuestionAnswering.from_pretrained(
                      UpperCAmelCase__ ,			output_loading_info=UpperCAmelCase__)
                  self.assertIsNotNone(UpperCAmelCase__)
                  self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)


    def 	SCREAMING_SNAKE_CASE							(      self				:     Dict)						->str:





           '''simple docstring'''

           A__							=   TFAutoModelWithLMHead.from_pretrained(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)
           self.assertEqual(model.num_parameters() ,			14_410)
           self.assertEqual(model.num_parameters(only_trainable=UpperCAmelCase__) ,			14_410)


    def 	SCREAMING_SNAKE_CASE							(      self				:     Optional[int])						->Any:





           '''simple docstring'''

           A__							=   TFAutoModelWithLMHead.from_pretrained(UpperCAmelCase__)
           self.assertIsInstance(UpperCAmelCase__ ,			UpperCAmelCase__)
           self.assertEqual(model.num_parameters() ,			14_410)
           self.assertEqual(model.num_parameters(only_trainable=UpperCAmelCase__) ,			14_410)


    def 	SCREAMING_SNAKE_CASE							(      self				:     Any)						->Dict:





           '''simple docstring'''

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

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

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     str)						->Dict:





           '''simple docstring'''

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

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

                                with tempfile.TemporaryDirectory() as tmp_dir:
                                       model.save_pretrained(UpperCAmelCase__)
                                       A__							=   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 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->int:





           '''simple docstring'''

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     str)						->Tuple:





           '''simple docstring'''

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[Any])						->Tuple:





           '''simple docstring'''

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


    def 	SCREAMING_SNAKE_CASE							(      self				:     str)						->Optional[Any]:





           '''simple docstring'''

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





    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->Tuple:





           '''simple docstring'''

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

from typing import Any


class     UpperCamelCase_   :

    '''simple docstring'''






    def __init__(      self				:     str ,			UpperCAmelCase__				:     int ,			UpperCAmelCase__				:     int ,			UpperCAmelCase__				:     float = 0)						->None:





           '''simple docstring'''

           A__			,							A__							=   row, column
           A__							=   [[default_value for c in range(UpperCAmelCase__)] for r in range(UpperCAmelCase__)]


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





           '''simple docstring'''

           A__							=   f"""Matrix consist of {self.row} rows and {self.column} columns\n"""

           # Make string identifier
           A__							=   0
           for row_vector in self.array:
                  for obj in row_vector:
                         A__							=   max(UpperCAmelCase__ ,			len(str(UpperCAmelCase__)))
           A__							=   f"""%{max_element_length}s"""

           # Make string and return
           def single_line(UpperCAmelCase__				:     list[float]) -> str:
                  nonlocal string_format_identifier
                  A__							=   '''['''
                  line += ", ".join(string_format_identifier % (obj,) for obj in row_vector)
                  line += "]"
                  return line

           s += "\n".join(single_line(UpperCAmelCase__) for row_vector in self.array)
           return s


    def __repr__(      self				:     Tuple)						->str:





           '''simple docstring'''

           return str(self)


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str] ,			UpperCAmelCase__				:     tuple[int, int])						->bool:





           '''simple docstring'''

           if not (isinstance(UpperCAmelCase__ ,			(list, tuple)) and len(UpperCAmelCase__) == 2):
                  return False
           elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column):
                  return False
           else:
                  return True


    def __getitem__(      self				:     List[Any] ,			UpperCAmelCase__				:     tuple[int, int])						->Any:





           '''simple docstring'''

           assert self.validate_indicies(UpperCAmelCase__)
           return self.array[loc[0]][loc[1]]


    def __setitem__(      self				:     List[Any] ,			UpperCAmelCase__				:     tuple[int, int] ,			UpperCAmelCase__				:     float)						->None:





           '''simple docstring'''

           assert self.validate_indicies(UpperCAmelCase__)
           A__							=   value


    def __add__(      self				:     Optional[int] ,			UpperCAmelCase__				:     Matrix)						->Matrix:





           '''simple docstring'''

           assert isinstance(UpperCAmelCase__ ,			UpperCAmelCase__)
           assert self.row == another.row and self.column == another.column

           # Add
           A__							=   Matrix(self.row ,			self.column)
           for r in range(self.row):
                  for c in range(self.column):
                         A__							=   self[r, c] + another[r, c]
           return result


    def __neg__(      self				:     str)						->Matrix:





           '''simple docstring'''

           A__							=   Matrix(self.row ,			self.column)
           for r in range(self.row):
                  for c in range(self.column):
                         A__							=   -self[r, c]
           return result


    def __sub__(      self				:     str ,			UpperCAmelCase__				:     Matrix)						->Matrix:





           '''simple docstring'''

           return self + (-another)


    def __mul__(      self				:     Union[str, Any] ,			UpperCAmelCase__				:     int | float | Matrix)						->Matrix:





           '''simple docstring'''

           if isinstance(UpperCAmelCase__ ,			(int, float)):  # Scalar multiplication
                  A__							=   Matrix(self.row ,			self.column)
                  for r in range(self.row):
                         for c in range(self.column):
                                A__							=   self[r, c] * another
                  return result
           elif isinstance(UpperCAmelCase__ ,			UpperCAmelCase__):  # Matrix multiplication
                  assert self.column == another.row
                  A__							=   Matrix(self.row ,			another.column)
                  for r in range(self.row):
                         for c in range(another.column):
                                for i in range(self.column):
                                       result[r, c] += self[r, i] * another[i, c]
                  return result
           else:
                  A__							=   f"""Unsupported type given for another ({type(UpperCAmelCase__)})"""
                  raise TypeError(UpperCAmelCase__)


    def 	SCREAMING_SNAKE_CASE							(      self				:     List[str])						->Matrix:





           '''simple docstring'''

           A__							=   Matrix(self.column ,			self.row)
           for r in range(self.row):
                  for c in range(self.column):
                         A__							=   self[r, c]
           return result





    def 	SCREAMING_SNAKE_CASE							(      self				:     Tuple ,			UpperCAmelCase__				:     Matrix ,			UpperCAmelCase__				:     Matrix)						->Any:





           '''simple docstring'''

           assert isinstance(UpperCAmelCase__ ,			UpperCAmelCase__) and isinstance(UpperCAmelCase__ ,			UpperCAmelCase__)
           assert self.row == self.column == u.row == v.row  # u, v should be column vector
           assert u.column == v.column == 1  # u, v should be column vector

           # Calculate
           A__							=   v.transpose()
           A__							=   (v_t * self * u)[0, 0] + 1
           if numerator_factor == 0:
                  return None  # It's not invertable
           return self - ((self * u) * (v_t * self) * (1.0 / numerator_factor))


# Testing
if __name__ == "__main__":







  def SCREAMING_SNAKE_CASE    (	)       ->	None:



         """simple docstring"""

         A__							=   Matrix(3			,    3			,    0						)
         for i in range(3						):
                A__							=   1
         print(f"""a^(-1) is {ainv}"""						)
         # u, v
         A__							=   Matrix(3			,    1			,    0						)
         A__			,							A__			,							A__							=   1, 2, -3
         A__							=   Matrix(3			,    1			,    0						)
         A__			,							A__			,							A__							=   4, -2, 5
         print(f"""u is {u}"""						)
         print(f"""v is {v}"""						)
         print(f"""uv^T is {u * v.transpose()}"""						)
         # Sherman Morrison
         print(f"""(a + uv^T)^(-1) is {ainv.sherman_morrison(lowercase_			,    lowercase_						)}"""						)







  def SCREAMING_SNAKE_CASE    (	)       ->	None:



         """simple docstring"""

         import doctest

         doctest.testmod()
  testa()


