
from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       TFConvBertForMaskedLM,
       TFConvBertForMultipleChoice,
       TFConvBertForQuestionAnswering,
       TFConvBertForSequenceClassification,
       TFConvBertForTokenClassification,
       TFConvBertModel,
   )






class     UpperCAmelCase						:



   def __init__(self						:   List[Any]       ,		snake_case__						:   int       ,		snake_case__						:   Tuple=13       ,		snake_case__						:   Union[str, Any]=7       ,		snake_case__						:   Union[str, Any]=True       ,		snake_case__						:   List[str]=True       ,		snake_case__						:   Optional[Any]=True       ,		snake_case__						:   int=True       ,		snake_case__						:   Dict=99       ,		snake_case__						:   Union[str, Any]=32       ,		snake_case__						:   Dict=2       ,		snake_case__						:   str=4       ,		snake_case__						:   str=37       ,		snake_case__						:   List[Any]="gelu"       ,		snake_case__						:   Any=0.1       ,		snake_case__						:   Any=0.1       ,		snake_case__						:   Dict=5_12       ,		snake_case__						:   Optional[Any]=16       ,		snake_case__						:   str=2       ,		snake_case__						:   Tuple=0.02       ,		snake_case__						:   Any=3       ,		snake_case__						:   Optional[Any]=4       ,		snake_case__						:   Dict=None       ,		)						->      Dict:


          '''simple docstring'''


          snake_case			:  Any     							= parent
          snake_case			:  Optional[int]     							= 13
          snake_case			:  str     							= 7
          snake_case			:  Tuple     							= True
          snake_case			:  Optional[int]     							= True
          snake_case			:  Union[str, Any]     							= True
          snake_case			:  Dict     							= True
          snake_case			:  Tuple     							= 99
          snake_case			:  List[str]     							= 3_84
          snake_case			:  Tuple     							= 2
          snake_case			:  Optional[int]     							= 4
          snake_case			:  Dict     							= 37
          snake_case			:  List[Any]     							= "gelu"
          snake_case			:  Dict     							= 0.1
          snake_case			:  List[Any]     							= 0.1
          snake_case			:  int     							= 5_12
          snake_case			:  Union[str, Any]     							= 16
          snake_case			:  Any     							= 2
          snake_case			:  List[Any]     							= 0.02
          snake_case			:  List[str]     							= 3
          snake_case			:  Dict     							= 4
          snake_case			:  str     							= 1_28
          snake_case			:  Tuple     							= 2
          snake_case			:  Tuple     							= 9
          snake_case			:  str     							= 1
          snake_case			:  Tuple     							= None



   def 							_SCREAMING_SNAKE_CASE  (self						:   Dict					)						->      Tuple:


          '''simple docstring'''


          snake_case			:  Any     							= ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size					)

          snake_case			:  List[Any]     							= None
          if self.use_input_mask:
                 snake_case			:  Dict     							= random_attention_mask([self.batch_size, self.seq_length]					)

          snake_case			:  Any     							= None
          if self.use_token_type_ids:
                 snake_case			:  Optional[int]     							= ids_tensor([self.batch_size, self.seq_length]       ,		self.type_vocab_size					)

          snake_case			:  int     							= None
          snake_case			:  Union[str, Any]     							= None
          snake_case			:  Tuple     							= None
          if self.use_labels:
                 snake_case			:  int     							= ids_tensor([self.batch_size]       ,		self.type_sequence_label_size					)
                 snake_case			:  Any     							= ids_tensor([self.batch_size, self.seq_length]       ,		self.num_labels					)
                 snake_case			:  Any     							= ids_tensor([self.batch_size]       ,		self.num_choices					)

          snake_case			:  List[Any]     							= ConvBertConfig(
              vocab_size=self.vocab_size       ,		hidden_size=self.hidden_size       ,		num_hidden_layers=self.num_hidden_layers       ,		num_attention_heads=self.num_attention_heads       ,		intermediate_size=self.intermediate_size       ,		hidden_act=self.hidden_act       ,		hidden_dropout_prob=self.hidden_dropout_prob       ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,		max_position_embeddings=self.max_position_embeddings       ,		type_vocab_size=self.type_vocab_size       ,		initializer_range=self.initializer_range       ,		return_dict=snake_case__       ,		)

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



   def 							_SCREAMING_SNAKE_CASE  (self						:   Union[str, Any]       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   List[str]       ,		snake_case__						:   Dict       ,		snake_case__						:   List[Any]       ,		snake_case__						:   List[str]       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   List[str]					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  List[Any]     							= TFConvBertModel(config=snake_case__					)
          snake_case			:  str     							= {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

          snake_case			:  List[str]     							= [input_ids, input_mask]
          snake_case			:  str     							= model(snake_case__					)

          snake_case			:  List[str]     							= model(snake_case__					)

          self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple       ,		snake_case__						:   int       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   Any       ,		snake_case__						:   List[Any]       ,		snake_case__						:   str       ,		snake_case__						:   Optional[int]       ,		snake_case__						:   Dict					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  str     							= TFConvBertForMaskedLM(config=snake_case__					)
          snake_case			:  Optional[int]     							= {
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
          }
          snake_case			:  Dict     							= model(snake_case__					)
          self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Optional[Any]       ,		snake_case__						:   str       ,		snake_case__						:   Dict       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   Dict       ,		snake_case__						:   Dict       ,		snake_case__						:   Optional[int]       ,		snake_case__						:   List[Any]					)						->      List[str]:


          '''simple docstring'''


          snake_case			:  List[Any]     							= self.num_labels
          snake_case			:  Optional[Any]     							= TFConvBertForSequenceClassification(config=snake_case__					)
          snake_case			:  str     							= {
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
          }

          snake_case			:  Union[str, Any]     							= model(snake_case__					)
          self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Union[str, Any]       ,		snake_case__						:   Dict       ,		snake_case__						:   int       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   Any					)						->      Tuple:


          '''simple docstring'''


          snake_case			:  Any     							= self.num_choices
          snake_case			:  Dict     							= TFConvBertForMultipleChoice(config=snake_case__					)
          snake_case			:  Union[str, Any]     							= tf.tile(tf.expand_dims(snake_case__       ,		1					)       ,		(1, self.num_choices, 1)					)
          snake_case			:  int     							= tf.tile(tf.expand_dims(snake_case__       ,		1					)       ,		(1, self.num_choices, 1)					)
          snake_case			:  Optional[Any]     							= tf.tile(tf.expand_dims(snake_case__       ,		1					)       ,		(1, self.num_choices, 1)					)
          snake_case			:  List[Any]     							= {
              "input_ids": multiple_choice_inputs_ids,
              "attention_mask": multiple_choice_input_mask,
              "token_type_ids": multiple_choice_token_type_ids,
          }
          snake_case			:  Optional[int]     							= model(snake_case__					)
          self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_choices)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple       ,		snake_case__						:   int       ,		snake_case__						:   str       ,		snake_case__						:   Union[str, Any]       ,		snake_case__						:   str       ,		snake_case__						:   Dict       ,		snake_case__						:   Any       ,		snake_case__						:   List[Any]					)						->      Union[str, Any]:


          '''simple docstring'''


          snake_case			:  Dict     							= self.num_labels
          snake_case			:  List[str]     							= TFConvBertForTokenClassification(config=snake_case__					)
          snake_case			:  Optional[int]     							= {
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
          }
          snake_case			:  Tuple     							= model(snake_case__					)
          self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.num_labels)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Any       ,		snake_case__						:   Tuple       ,		snake_case__						:   Dict       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   List[Any]       ,		snake_case__						:   Optional[Any]       ,		snake_case__						:   Any       ,		snake_case__						:   List[Any]					)						->      int:


          '''simple docstring'''


          snake_case			:  Tuple     							= TFConvBertForQuestionAnswering(config=snake_case__					)
          snake_case			:  Union[str, Any]     							= {
              "input_ids": input_ids,
              "attention_mask": input_mask,
              "token_type_ids": token_type_ids,
          }

          snake_case			:  Tuple     							= model(snake_case__					)
          self.parent.assertEqual(result.start_logits.shape       ,		(self.batch_size, self.seq_length)					)
          self.parent.assertEqual(result.end_logits.shape       ,		(self.batch_size, self.seq_length)					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      int:


          '''simple docstring'''


          snake_case			:  Any     							= self.prepare_config_and_inputs()
          (
              (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   (
              snake_case
          )							,   
          )			:  str     							= config_and_inputs
          snake_case			:  str     							= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict






@require_tf
class     UpperCAmelCase						(		A_			,A_			,unittest.TestCase					):
   A__   :						Optional[int]			=	(
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   A__   :						str			=	(
       {
           "feature-extraction": TFConvBertModel,
           "fill-mask": TFConvBertForMaskedLM,
           "question-answering": TFConvBertForQuestionAnswering,
           "text-classification": TFConvBertForSequenceClassification,
           "token-classification": TFConvBertForTokenClassification,
           "zero-shot": TFConvBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A__   :						Tuple			=	False
   A__   :						Any			=	False
   A__   :						List[Any]			=	False



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      str:


          '''simple docstring'''


          snake_case			:  Dict     							= TFConvBertModelTester(self					)
          snake_case			:  List[str]     							= ConfigTester(self       ,		config_class=snake_case__       ,		hidden_size=37					)



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


          '''simple docstring'''


          self.config_tester.run_common_tests()



   def 							_SCREAMING_SNAKE_CASE  (self						:   Optional[int]					)						->      Dict:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   int					)						->      str:


          '''simple docstring'''


          snake_case			:  List[Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*snake_case__					)



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


          '''simple docstring'''


          snake_case			:  Dict     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      int:


          '''simple docstring'''


          snake_case			:  Union[str, Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Tuple					)						->      int:


          '''simple docstring'''


          snake_case			:  Dict     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*snake_case__					)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Dict					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  Optional[Any]     							= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*snake_case__					)



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


          '''simple docstring'''


          snake_case							,   snake_case			:  Any     							= self.model_tester.prepare_config_and_inputs_for_common()
          snake_case			:  Tuple     							= True
          snake_case			:  int     							= True

          if hasattr(snake_case__       ,		"use_cache"					):
                 snake_case			:  Optional[Any]     							= True

          snake_case			:  Tuple     							= getattr(self.model_tester       ,		"encoder_seq_length"       ,		self.model_tester.seq_length					)
          snake_case			:  List[Any]     							= getattr(self.model_tester       ,		"key_length"       ,		snake_case__					)

          for model_class in self.all_model_classes:
                 snake_case			:  Union[str, Any]     							= self._prepare_for_class(snake_case__       ,		snake_case__					)
                 snake_case			:  Union[str, Any]     							= model_class(snake_case__					)
                 snake_case			:  Tuple     							= len(model(snake_case__					)					)

                 with tempfile.TemporaryDirectory() as tmpdirname:
                        model.save_pretrained(snake_case__       ,		saved_model=snake_case__					)
                        snake_case			:  Optional[Any]     							= os.path.join(snake_case__       ,		"saved_model"       ,		"1"					)
                        snake_case			:  Optional[int]     							= tf.keras.models.load_model(snake_case__					)
                        snake_case			:  Union[str, Any]     							= model(snake_case__					)

                        if self.is_encoder_decoder:
                               snake_case			:  List[str]     							= outputs["encoder_hidden_states"]
                               snake_case			:  Any     							= outputs["encoder_attentions"]
                        else:
                               snake_case			:  List[str]     							= outputs["hidden_states"]
                               snake_case			:  List[str]     							= outputs["attentions"]

                        self.assertEqual(len(snake_case__					)       ,		snake_case__					)

                        snake_case			:  Optional[Any]     							= getattr(
                            self.model_tester       ,		"expected_num_hidden_layers"       ,		self.model_tester.num_hidden_layers + 1					)

                        self.assertEqual(len(snake_case__					)       ,		snake_case__					)
                        self.assertListEqual(
                            list(output_hidden_states[0].shape[-2:]					)       ,		[self.model_tester.seq_length, self.model_tester.hidden_size]       ,		)

                        self.assertEqual(len(snake_case__					)       ,		self.model_tester.num_hidden_layers					)
                        self.assertListEqual(
                            list(output_attentions[0].shape[-3:]					)       ,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,		)



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


          '''simple docstring'''


          snake_case			:  List[Any]     							= TFConvBertModel.from_pretrained("YituTech/conv-bert-base"					)
          self.assertIsNotNone(snake_case__					)



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


          '''simple docstring'''


          snake_case							,   snake_case			:  Dict     							= self.model_tester.prepare_config_and_inputs_for_common()
          snake_case			:  Tuple     							= True
          snake_case			:  int     							= getattr(self.model_tester       ,		"decoder_seq_length"       ,		self.model_tester.seq_length					)
          snake_case			:  Any     							= getattr(self.model_tester       ,		"encoder_seq_length"       ,		self.model_tester.seq_length					)
          snake_case			:  List[Any]     							= getattr(self.model_tester       ,		"key_length"       ,		snake_case__					)
          snake_case			:  Dict     							= getattr(self.model_tester       ,		"key_length"       ,		snake_case__					)

          def check_decoder_attentions_output(snake_case__						:   Union[str, Any]					):
                 snake_case			:  Union[str, Any]     							= len(snake_case__					)
                 self.assertEqual(out_len % 2       ,		0					)
                 snake_case			:  Union[str, Any]     							= outputs.decoder_attentions
                 self.assertEqual(len(snake_case__					)       ,		self.model_tester.num_hidden_layers					)
                 self.assertListEqual(
                     list(decoder_attentions[0].shape[-3:]					)       ,		[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]       ,		)

          def check_encoder_attentions_output(snake_case__						:   Optional[Any]					):
                 snake_case			:  Any     							= [
                     t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
                 ]
                 self.assertEqual(len(snake_case__					)       ,		self.model_tester.num_hidden_layers					)
                 self.assertListEqual(
                     list(attentions[0].shape[-3:]					)       ,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]       ,		)

          for model_class in self.all_model_classes:
                 snake_case			:  int     							= True
                 snake_case			:  List[Any]     							= False
                 snake_case			:  List[Any]     							= model_class(snake_case__					)
                 snake_case			:  str     							= model(self._prepare_for_class(snake_case__       ,		snake_case__					)					)
                 snake_case			:  List[str]     							= len(snake_case__					)
                 self.assertEqual(config.output_hidden_states       ,		snake_case__					)
                 check_encoder_attentions_output(snake_case__					)

                 if self.is_encoder_decoder:
                        snake_case			:  Optional[Any]     							= model_class(snake_case__					)
                        snake_case			:  Union[str, Any]     							= model(self._prepare_for_class(snake_case__       ,		snake_case__					)					)
                        self.assertEqual(config.output_hidden_states       ,		snake_case__					)
                        check_decoder_attentions_output(snake_case__					)

                 # Check that output attentions can also be changed via the config
                 del inputs_dict["output_attentions"]
                 snake_case			:  List[str]     							= True
                 snake_case			:  Any     							= model_class(snake_case__					)
                 snake_case			:  List[Any]     							= model(self._prepare_for_class(snake_case__       ,		snake_case__					)					)
                 self.assertEqual(config.output_hidden_states       ,		snake_case__					)
                 check_encoder_attentions_output(snake_case__					)

                 # Check attention is always last and order is fine
                 snake_case			:  int     							= True
                 snake_case			:  Optional[Any]     							= True
                 snake_case			:  Tuple     							= model_class(snake_case__					)
                 snake_case			:  Dict     							= model(self._prepare_for_class(snake_case__       ,		snake_case__					)					)

                 self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)       ,		len(snake_case__					)					)
                 self.assertEqual(model.config.output_hidden_states       ,		snake_case__					)
                 check_encoder_attentions_output(snake_case__					)






@require_tf
class     UpperCAmelCase						(		unittest.TestCase					):



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


          '''simple docstring'''


          snake_case			:  Dict     							= TFConvBertModel.from_pretrained("YituTech/conv-bert-base"					)
          snake_case			:  Optional[int]     							= tf.constant([[0, 1, 2, 3, 4, 5]]					)
          snake_case			:  List[str]     							= model(snake_case__					)[0]

          snake_case			:  str     							= [1, 6, 7_68]
          self.assertEqual(output.shape       ,		snake_case__					)

          snake_case			:  List[Any]     							= tf.constant(
              [
                  [
                      [-0.03475493, -0.4686034, -0.30638832],
                      [0.22637248, -0.26988646, -0.7423424],
                      [0.10324868, -0.45013508, -0.58280784],
                  ]
              ]					)
          tf.debugging.assert_near(output[:, :3, :3]       ,		snake_case__       ,		atol=1e-4					)







import math_equivalence  # From: git+https://github.com/hendrycks/math.git

import datasets


__lowerCamelCase				=			"""\
@article{hendrycksmath2021,
  title={Measuring Mathematical Problem Solving With the MATH Dataset},
  author={Dan Hendrycks
    and Collin Burns
    and Saurav Kadavath
    and Akul Arora
    and Steven Basart
    and Eric Tang
    and Dawn Song
    and Jacob Steinhardt},
  journal={arXiv preprint arXiv:2103.03874},
  year={2021}
}
"""


__lowerCamelCase				=			"""\
This metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.
It first canonicalizes the inputs (e.g., converting \"1/2\" to \"\\frac{1}{2}\") and then computes accuracy.
"""


__lowerCamelCase				=			R"""
Calculates accuracy after canonicalizing inputs.

Args:
    predictions: list of predictions to score. Each prediction
        is a string that contains natural language and LaTex.
    references: list of reference for each prediction. Each
        reference is a string that contains natural language
        and LaTex.
Returns:
    accuracy: accuracy after canonicalizing inputs
        (e.g., converting \"1/2\" to \"\\frac{1}{2}\")

Examples:
    >>> metric = datasets.load_metric(\"competition_math\")
    >>> results = metric.compute(references=[\"\\frac{1}{2}\"], predictions=[\"1/2\"])
    >>> print(results)
    {'accuracy': 1.0}
"""






@datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION			,_KWARGS_DESCRIPTION					)
class     UpperCAmelCase						(		datasets.Metric					):



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


          '''simple docstring'''


          return datasets.MetricInfo(
              description=_DESCRIPTION       ,		citation=_CITATION       ,		inputs_description=_KWARGS_DESCRIPTION       ,		features=datasets.Features(
                  {
                      "predictions": datasets.Value("string"					),
                      "references": datasets.Value("string"					),
                  }					)       ,		homepage="https://github.com/hendrycks/math"       ,		codebase_urls=["https://github.com/hendrycks/math"]       ,		)



   def 							_SCREAMING_SNAKE_CASE  (self						:   Optional[int]       ,		snake_case__						:   str       ,		snake_case__						:   List[Any]					)						->      Optional[Any]:


          '''simple docstring'''


          snake_case			:  Optional[int]     							= 0.0
          for i, j in zip(snake_case__       ,		snake_case__					):
                 n_correct += 1.0 if math_equivalence.is_equiv(snake_case__       ,		snake_case__					) else 0.0
          snake_case			:  Optional[int]     							= n_correct / len(snake_case__					)
          return {
              "accuracy": accuracy,
          }





