

'''simple docstring'''





import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
							import jax.numpy as jnp

							from transformers.models.distilbert.modeling_flax_distilbert import (
							    FlaxDistilBertForMaskedLM,
							    FlaxDistilBertForMultipleChoice,
							    FlaxDistilBertForQuestionAnswering,
							    FlaxDistilBertForSequenceClassification,
							    FlaxDistilBertForTokenClassification,
							    FlaxDistilBertModel,
							)





class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				def __init__(       self     :		List[str]		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		List[str]=13		,					UpperCAmelCase__     :		str=7		,					UpperCAmelCase__     :		int=True		,					UpperCAmelCase__     :		Union[str, Any]=True		,					UpperCAmelCase__     :		List[str]=True		,					UpperCAmelCase__     :		Optional[int]=True		,					UpperCAmelCase__     :		Optional[Any]=99		,					UpperCAmelCase__     :		Dict=32		,					UpperCAmelCase__     :		List[Any]=5		,					UpperCAmelCase__     :		Any=4		,					UpperCAmelCase__     :		List[Any]=37		,					UpperCAmelCase__     :		Dict="gelu"		,					UpperCAmelCase__     :		Dict=0.1		,					UpperCAmelCase__     :		Any=0.1		,					UpperCAmelCase__     :		Any=512		,					UpperCAmelCase__     :		Dict=16		,					UpperCAmelCase__     :		str=2		,					UpperCAmelCase__     :		str=0.02		,					UpperCAmelCase__     :		Any=4		,					):
										'''simple docstring'''
										lowercase :							int    		=parent
										lowercase :							Dict    		=batch_size
										lowercase :							Dict    		=seq_length
										lowercase :							Optional[Any]    		=is_training
										lowercase :							Optional[Any]    		=use_attention_mask
										lowercase :							Union[str, Any]    		=use_token_type_ids
										lowercase :							Tuple    		=use_labels
										lowercase :							Any    		=vocab_size
										lowercase :							str    		=hidden_size
										lowercase :							List[str]    		=num_hidden_layers
										lowercase :							List[str]    		=num_attention_heads
										lowercase :							Dict    		=intermediate_size
										lowercase :							Any    		=hidden_act
										lowercase :							Tuple    		=hidden_dropout_prob
										lowercase :							str    		=attention_probs_dropout_prob
										lowercase :							Dict    		=max_position_embeddings
										lowercase :							Any    		=type_vocab_size
										lowercase :							List[str]    		=type_sequence_label_size
										lowercase :							Optional[int]    		=initializer_range
										lowercase :							str    		=num_choices







				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size   )

										lowercase :							Any    		=None
										if self.use_attention_mask:
																lowercase :							int    		=random_attention_mask([self.batch_size, self.seq_length]   )

										lowercase :							List[str]    		=DistilBertConfig(
										    vocab_size=self.vocab_size		,					dim=self.hidden_size		,					n_layers=self.num_hidden_layers		,					n_heads=self.num_attention_heads		,					hidden_dim=self.intermediate_size		,					hidden_act=self.hidden_act		,					dropout=self.hidden_dropout_prob		,					attention_dropout=self.attention_probs_dropout_prob		,					max_position_embeddings=self.max_position_embeddings		,					initializer_range=self.initializer_range		,					tie_weights_=__A		,					)

										return config, input_ids, attention_mask







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							Dict    		=self.prepare_config_and_inputs()
										lowercase :							str    		=config_and_inputs
										lowercase :							Dict    		={'''input_ids''': input_ids, '''attention_mask''': attention_mask}
										return config, inputs_dict





@require_flax
class 		__SCREAMING_SNAKE_CASE							(			lowercase__						,							unittest.TestCase      ):
				lowerCamelCase_		       =    (
				    (
				        FlaxDistilBertModel,
				        FlaxDistilBertForMaskedLM,
				        FlaxDistilBertForMultipleChoice,
				        FlaxDistilBertForQuestionAnswering,
				        FlaxDistilBertForSequenceClassification,
				        FlaxDistilBertForTokenClassification,
				        FlaxDistilBertForQuestionAnswering,
				    )
				    if is_flax_available()
				    else ()
				)







				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										lowercase :							Tuple    		=FlaxDistilBertModelTester(self   )







				@slow
				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										for model_class_name in self.all_model_classes:
																lowercase :							List[Any]    		=model_class_name.from_pretrained('''distilbert-base-uncased'''   )
																lowercase :							Tuple    		=model(np.ones((1, 1)   )   )
																self.assertIsNotNone(__A   )





@require_flax
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				@slow
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''   )
										lowercase :							Tuple    		=np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
										lowercase :							str    		=np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
										lowercase :							int    		=model(__A		,					attention_mask=__A   )[0]
										lowercase :							Optional[int]    		=(1, 11, 768)
										self.assertEqual(output.shape		,					__A   )
										lowercase :							Optional[Any]    		=np.array([[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]   )

										self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]		,					__A		,					atol=1E-4   )   )







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

import datasets


_lowercase											=    """\
@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}
}
"""

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

_lowercase											=    """
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 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     List[Any]			,							UpperCAmelCase_	:     Union[str, Any]				)			->						int:
       return float((preds == labels).mean()				)
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     int			,							UpperCAmelCase_	:     str				)			->						Union[str, Any]:
       SCREAMING_SNAKE_CASE_							:				Tuple		=simple_accuracy(UpperCAmelCase_			,							UpperCAmelCase_				)
       SCREAMING_SNAKE_CASE_							:				Union[str, Any]		=float(fa_score(y_true=UpperCAmelCase_			,							y_pred=UpperCAmelCase_				)				)
       return {
           "accuracy": acc,
           "f1": fa,
       }
def 							SCREAMING_SNAKE_CASE_	(					UpperCAmelCase_	:     Optional[int]			,							UpperCAmelCase_	:     Dict				)			->						List[str]:
       SCREAMING_SNAKE_CASE_							:				str		=float(pearsonr(UpperCAmelCase_			,							UpperCAmelCase_				)[0]				)
       SCREAMING_SNAKE_CASE_							:				List[str]		=float(spearmanr(UpperCAmelCase_			,							UpperCAmelCase_				)[0]				)
       return {
           "pearson": pearson_corr,
           "spearmanr": spearman_corr,
       }


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


       def 	_snake_case				(    self				)						->    str:
              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 	_snake_case				(    self						,       __A						,       __A				)						->    List[Any]:
              if self.config_name == "cola":
                     return {"matthews_correlation": matthews_corrcoef(__A						,       __A				)}
              elif self.config_name == "stsb":
                     return pearson_and_spearman(__A						,       __A				)
              elif self.config_name in ["mrpc", "qqp"]:
                     return acc_and_fa(__A						,       __A				)
              elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
                     return {"accuracy": simple_accuracy(__A						,       __A				)}
              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"]'''				)
