


'''simple docstring'''
from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

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


if is_tf_available():
  import tensorflow as tf

  from transformers import TFBlipTextModel
  from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class      lowerCamelCase_  :






      def __init__(   self							:       Optional[Any]		,		_A							:       List[Any]		,		_A							:       Tuple=12		,		_A							:       Tuple=7		,		_A							:       str=True		,		_A							:       List[Any]=True		,		_A							:       Any=True		,		_A							:       Optional[Any]=99		,		_A							:       Tuple=32		,		_A							:       Dict=32		,		_A							:       Tuple=2		,		_A							:       Any=4		,		_A							:       Dict=37		,		_A							:       int=0.1		,		_A							:       List[str]=0.1		,		_A							:       Optional[int]=512		,		_A							:       int=0.0_2		,		_A							:       List[Any]=0		,		_A							:       Union[str, Any]=None		,		):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							parent
          UpperCAmelCase__     :						Union[str, Any]						=							batch_size
          UpperCAmelCase__     :						Tuple						=							seq_length
          UpperCAmelCase__     :						Optional[int]						=							is_training
          UpperCAmelCase__     :						Optional[int]						=							use_input_mask
          UpperCAmelCase__     :						Tuple						=							use_labels
          UpperCAmelCase__     :						Dict						=							vocab_size
          UpperCAmelCase__     :						str						=							hidden_size
          UpperCAmelCase__     :						List[str]						=							projection_dim
          UpperCAmelCase__     :						Tuple						=							num_hidden_layers
          UpperCAmelCase__     :						List[Any]						=							num_attention_heads
          UpperCAmelCase__     :						List[Any]						=							intermediate_size
          UpperCAmelCase__     :						List[Any]						=							dropout
          UpperCAmelCase__     :						int						=							attention_dropout
          UpperCAmelCase__     :						Optional[int]						=							max_position_embeddings
          UpperCAmelCase__     :						List[Any]						=							initializer_range
          UpperCAmelCase__     :						List[str]						=							scope
          UpperCAmelCase__     :						Union[str, Any]						=							bos_token_id






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						Union[str, Any]						=							ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size					)

          UpperCAmelCase__     :						Tuple						=							None
          if self.use_input_mask:
              UpperCAmelCase__     :						str						=							random_attention_mask([self.batch_size, self.seq_length]					)

          if input_mask is not None:
              UpperCAmelCase__     :						Optional[int]						=							input_mask.numpy()
              UpperCAmelCase__		,		UpperCAmelCase__     :						List[Any]						=							input_mask.shape
              UpperCAmelCase__     :						Dict						=							np.random.randint(1		,		seq_length - 1		,		size=(batch_size,)					)
              for batch_idx, start_index in enumerate(_A					):
                  UpperCAmelCase__     :						str						=							1
                  UpperCAmelCase__     :						List[Any]						=							0

          UpperCAmelCase__     :						Optional[int]						=							self.get_config()

          return config, input_ids, tf.convert_to_tensor(_A					)






      def lowercase_				(   self							:       Optional[int]					):


          '''simple docstring'''


          return BlipTextConfig(
              vocab_size=self.vocab_size		,		hidden_size=self.hidden_size		,		projection_dim=self.projection_dim		,		num_hidden_layers=self.num_hidden_layers		,		num_attention_heads=self.num_attention_heads		,		intermediate_size=self.intermediate_size		,		dropout=self.dropout		,		attention_dropout=self.attention_dropout		,		max_position_embeddings=self.max_position_embeddings		,		initializer_range=self.initializer_range		,		bos_token_id=self.bos_token_id		,		)






      def lowercase_				(   self							:       Optional[Any]		,		_A							:       int		,		_A							:       Dict		,		_A							:       List[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						str						=							TFBlipTextModel(config=_A					)
          UpperCAmelCase__     :						Optional[int]						=							model(_A		,		attention_mask=_A		,		training=_A					)
          UpperCAmelCase__     :						List[Any]						=							model(_A		,		training=_A					)
          self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size)					)
          self.parent.assertEqual(result.pooler_output.shape		,		(self.batch_size, self.hidden_size)					)






      def lowercase_				(   self							:       Dict					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							self.prepare_config_and_inputs()
          UpperCAmelCase__		,		UpperCAmelCase__		,		UpperCAmelCase__     :						int						=							config_and_inputs
          UpperCAmelCase__     :						Tuple						=							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
          return config, inputs_dict




@require_tf
class      lowerCamelCase_  (      __a	,			unittest.TestCase ):
      lowerCAmelCase__							=	(TFBlipTextModel,) if is_tf_available() else ()
      lowerCAmelCase__							=	False
      lowerCAmelCase__							=	False
      lowerCAmelCase__							=	False






      def lowercase_				(   self							:       List[Any]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							BlipTextModelTester(self					)
          UpperCAmelCase__     :						str						=							ConfigTester(self		,		config_class=_A		,		hidden_size=37					)






      def lowercase_				(   self							:       List[str]					):


          '''simple docstring'''


          self.config_tester.run_common_tests()






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[int]						=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_A					)






      def lowercase_				(   self							:       Any					):


          '''simple docstring'''


          pass






      def lowercase_				(   self							:       int					):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''Blip does not use inputs_embeds'''					)
      def lowercase_				(   self							:       str					):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''					)
      def lowercase_				(   self							:       List[Any]					):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''					)
      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          pass






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


          '''simple docstring'''


          for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__     :						Any						=							TFBlipTextModel.from_pretrained(_A					)
              self.assertIsNotNone(_A					)






      def lowercase_				(   self							:       Any		,		_A							:       Tuple=True					):


          '''simple docstring'''


          super().test_pt_tf_model_equivalence(allow_missing_keys=_A					)








'''simple docstring'''



from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


__UpperCamelCase   :							Optional[int]         =   """\
@inproceedings{wang2019glue,
  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},
  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},
  note={In the Proceedings of ICLR.},
  year={2019}
}
"""

__UpperCamelCase   :							Tuple         =   """\
GLUE, the General Language Understanding Evaluation benchmark
(https://gluebenchmark.com/) is a collection of resources for training,
evaluating, and analyzing natural language understanding systems.
"""

__UpperCamelCase   :							Optional[int]         =   """
Compute GLUE evaluation metric associated to each GLUE dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
Returns: depending on the GLUE subset, one or several of:
    \"accuracy\": Accuracy
    \"f1\": F1 score
    \"pearson\": Pearson Correlation
    \"spearmanr\": Spearman Correlation
    \"matthews_correlation\": Matthew Correlation
Examples:

    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'stsb')
    >>> references = [0., 1., 2., 3., 4., 5.]
    >>> predictions = [0., 1., 2., 3., 4., 5.]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})
    {'pearson': 1.0, 'spearmanr': 1.0}

    >>> glue_metric = datasets.load_metric('glue', 'cola')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'matthews_correlation': 1.0}
"""



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						Optional[int],       SCREAMING_SNAKE_CASE__:						str     )	->      List[Any]:

   """simple docstring"""
   return float((preds == labels).mean()     )



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						Optional[Any],       SCREAMING_SNAKE_CASE__:						List[str]     )	->      Optional[Any]:

   """simple docstring"""
   __a					  =     simple_accuracy(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )
   __a					  =     float(fa_score(y_true=SCREAMING_SNAKE_CASE__,       y_pred=SCREAMING_SNAKE_CASE__     )     )
   return {
       "accuracy": acc,
       "f1": fa,
   }



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						int,       SCREAMING_SNAKE_CASE__:						Optional[Any]     )	->      int:

   """simple docstring"""
   __a					  =     float(pearsonr(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )[0]     )
   __a					  =     float(spearmanr(SCREAMING_SNAKE_CASE__,       SCREAMING_SNAKE_CASE__     )[0]     )
   return {
       "pearson": pearson_corr,
       "spearmanr": spearman_corr,
   }



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   ,   _KWARGS_DESCRIPTION  )
class 				__SCREAMING_SNAKE_CASE							(				datasets.Metric  ):




      def 			__UpperCamelCase				(	self   )				->int:



         '''simple docstring'''
         if self.config_name not in [
             "sst2",
             "mnli",
             "mnli_mismatched",
             "mnli_matched",
             "cola",
             "stsb",
             "mrpc",
             "qqp",
             "qnli",
             "rte",
             "wnli",
             "hans",
         ]:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
                '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'   )
         return datasets.MetricInfo(
             description=_DESCRIPTION			, citation=_CITATION			, inputs_description=_KWARGS_DESCRIPTION			, features=datasets.Features(
                 {
                     'predictions': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'   ),
                     'references': datasets.Value('int64' if self.config_name != 'stsb' else 'float32'   ),
                 }   )			, codebase_urls=[]			, reference_urls=[]			, format='numpy'			, )






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase   )				->Tuple:



         '''simple docstring'''
         if self.config_name == "cola":
            return {"matthews_correlation": matthews_corrcoef(lowerCamelCase			, lowerCamelCase   )}
         elif self.config_name == "stsb":
            return pearson_and_spearman(lowerCamelCase			, lowerCamelCase   )
         elif self.config_name in ["mrpc", "qqp"]:
            return acc_and_fa(lowerCamelCase			, lowerCamelCase   )
         elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
            return {"accuracy": simple_accuracy(lowerCamelCase			, lowerCamelCase   )}
         else:
            raise KeyError(
                'You should supply a configuration name selected in '
                '["sst2", "mnli", "mnli_mismatched", "mnli_matched", '
                '"cola", "stsb", "mrpc", "qqp", "qnli", "rte", "wnli", "hans"]'   )