

"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import DistilBertConfig, 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.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )




class 					_lowerCAmelCase  :







 """simple docstring"""







 def __init__(			self							:			List[str],					UpperCAmelCase__							:			Tuple,					):
    __lowercase								=						parent
    __lowercase								=						1_3
    __lowercase								=						7
    __lowercase								=						True
    __lowercase								=						True
    __lowercase								=						False
    __lowercase								=						True
    __lowercase								=						9_9
    __lowercase								=						3_2
    __lowercase								=						2
    __lowercase								=						4
    __lowercase								=						3_7
    __lowercase								=						"gelu"
    __lowercase								=						0.1
    __lowercase								=						0.1
    __lowercase								=						5_1_2
    __lowercase								=						1_6
    __lowercase								=						2
    __lowercase								=						0.02
    __lowercase								=						3
    __lowercase								=						4
    __lowercase								=						None


 def 					_lowercase (			self							:			int	):
    __lowercase								=						ids_tensor([self.batch_size, self.seq_length],					self.vocab_size	)

    __lowercase								=						None
    if self.use_input_mask:
       __lowercase								=						random_attention_mask([self.batch_size, self.seq_length]	)

    __lowercase								=						None
    __lowercase								=						None
    __lowercase								=						None
    if self.use_labels:
       __lowercase								=						ids_tensor([self.batch_size],					self.type_sequence_label_size	)
       __lowercase								=						ids_tensor([self.batch_size, self.seq_length],					self.num_labels	)
       __lowercase								=						ids_tensor([self.batch_size],					self.num_choices	)

    __lowercase								=						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,					)

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


 def 					_lowercase (			self							:			Union[str, Any],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int]	):
    __lowercase								=						TFDistilBertModel(config=UpperCAmelCase__	)
    __lowercase								=						{"input_ids": input_ids, "attention_mask": input_mask}

    __lowercase								=						model(UpperCAmelCase__	)

    __lowercase								=						[input_ids, input_mask]

    __lowercase								=						model(UpperCAmelCase__	)

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


 def 					_lowercase (			self							:			Tuple,					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Tuple,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			List[str]	):
    __lowercase								=						TFDistilBertForMaskedLM(config=UpperCAmelCase__	)
    __lowercase								=						{"input_ids": input_ids, "attention_mask": input_mask}
    __lowercase								=						model(UpperCAmelCase__	)
    self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.vocab_size)	)


 def 					_lowercase (			self							:			int,					UpperCAmelCase__							:			Optional[Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			Any	):
    __lowercase								=						TFDistilBertForQuestionAnswering(config=UpperCAmelCase__	)
    __lowercase								=						{
        "input_ids": input_ids,
        "attention_mask": input_mask,
    }
    __lowercase								=						model(UpperCAmelCase__	)
    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 					_lowercase (			self							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Any	):
    __lowercase								=						self.num_labels
    __lowercase								=						TFDistilBertForSequenceClassification(UpperCAmelCase__	)
    __lowercase								=						{"input_ids": input_ids, "attention_mask": input_mask}
    __lowercase								=						model(UpperCAmelCase__	)
    self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_labels)	)


 def 					_lowercase (			self							:			Optional[int],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			Dict,					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			List[Any],					UpperCAmelCase__							:			Tuple	):
    __lowercase								=						self.num_choices
    __lowercase								=						TFDistilBertForMultipleChoice(UpperCAmelCase__	)
    __lowercase								=						tf.tile(tf.expand_dims(UpperCAmelCase__,					1	),					(1, self.num_choices, 1)	)
    __lowercase								=						tf.tile(tf.expand_dims(UpperCAmelCase__,					1	),					(1, self.num_choices, 1)	)
    __lowercase								=						{
        "input_ids": multiple_choice_inputs_ids,
        "attention_mask": multiple_choice_input_mask,
    }
    __lowercase								=						model(UpperCAmelCase__	)
    self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_choices)	)


 def 					_lowercase (			self							:			str,					UpperCAmelCase__							:			Union[str, Any],					UpperCAmelCase__							:			str,					UpperCAmelCase__							:			Any,					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			List[Any]	):
    __lowercase								=						self.num_labels
    __lowercase								=						TFDistilBertForTokenClassification(UpperCAmelCase__	)
    __lowercase								=						{"input_ids": input_ids, "attention_mask": input_mask}
    __lowercase								=						model(UpperCAmelCase__	)
    self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.num_labels)	)



 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.prepare_config_and_inputs()
    ((__lowercase)		,(__lowercase)		,(__lowercase)		,(__lowercase)		,(__lowercase)		,(__lowercase))								=						config_and_inputs
    __lowercase								=						{"input_ids": input_ids, "attention_mask": input_mask}
    return config, inputs_dict







@require_tf
class 					_lowerCAmelCase  (	lowercase     ,lowercase     ,unittest.TestCase   ):







 """simple docstring"""







 __UpperCAmelCase	:					int   		=	(
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 __UpperCAmelCase	:					Union[str, Any]   		=	(
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __UpperCAmelCase	:					str   		=	False
 __UpperCAmelCase	:					List[str]   		=	False


 def 					_lowercase (			self							:			Optional[int]	):
    __lowercase								=						TFDistilBertModelTester(self	)
    __lowercase								=						ConfigTester(self,					config_class=UpperCAmelCase__,					dim=3_7	)


 def 					_lowercase (			self							:			str	):
    self.config_tester.run_common_tests()


 def 					_lowercase (			self							:			Optional[int]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[Any]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			Dict	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__	)


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__	)



 @slow
 def 					_lowercase (			self							:			str	):
    for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]	):
       __lowercase								=						TFDistilBertModel.from_pretrained(UpperCAmelCase__	)
       self.assertIsNotNone(UpperCAmelCase__	)







@require_tf
class 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 @slow
 def 					_lowercase (			self							:			Union[str, Any]	):
    __lowercase								=						TFDistilBertModel.from_pretrained("distilbert-base-uncased"	)
    __lowercase								=						tf.constant([[0, 1, 2, 3, 4, 5]]	)
    __lowercase								=						model(UpperCAmelCase__	)[0]

    __lowercase								=						[1, 6, 7_6_8]
    self.assertEqual(output.shape,					UpperCAmelCase__	)

    __lowercase								=						tf.constant(
        [
            [
                [0.19_261_885, -0.13_732_955, 0.4_119_799],
                [0.22_150_156, -0.07_422_661, 0.39_037_204],
                [0.22_756_018, -0.0_896_414, 0.3_701_467],
            ]
        ]	)
    tf.debugging.assert_near(output[:, :3, :3],					UpperCAmelCase__,					atol=1E-4	)







"""simple docstring"""





from scipy.stats import spearmanr

import datasets


_a       =							'\nThe Spearman rank-order correlation coefficient is a measure of the\nrelationship between two datasets. Like other correlation coefficients,\nthis one varies between -1 and +1 with 0 implying no correlation.\nPositive correlations imply that as data in dataset x increases, so\ndoes data in dataset y. Negative correlations imply that as x increases,\ny decreases. Correlations of -1 or +1 imply an exact monotonic relationship.\n\nUnlike the Pearson correlation, the Spearman correlation does not\nassume that both datasets are normally distributed.\n\nThe p-value roughly indicates the probability of an uncorrelated system\nproducing datasets that have a Spearman correlation at least as extreme\nas the one computed from these datasets. The p-values are not entirely\nreliable but are probably reasonable for datasets larger than 500 or so.\n'

_a       =							'\nArgs:\n    predictions (`List[float]`): Predicted labels, as returned by a model.\n    references (`List[float]`): Ground truth labels.\n    return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns\n            only the spearmanr score. Defaults to `False`.\nReturns:\n    spearmanr (`float`): Spearman correlation coefficient.\n    p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.\nExamples:\n    Example 1:\n        >>> spearmanr_metric = datasets.load_metric("spearmanr")\n        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])\n        >>> print(results)\n        {\'spearmanr\': -0.7}\n\n    Example 2:\n        >>> spearmanr_metric = datasets.load_metric("spearmanr")\n        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],\n        ...                                     predictions=[10, 9, 2.5, 6, 4],\n        ...                                     return_pvalue=True)\n        >>> print(results[\'spearmanr\'])\n        -0.7\n        >>> print(round(results[\'spearmanr_pvalue\'], 2))\n        0.19\n'

_a       =							r'\\n@book{kokoska2000crc,\n  title={CRC standard probability and statistics tables and formulae},\n  author={Kokoska, Stephen and Zwillinger, Daniel},\n  year={2000},\n  publisher={Crc Press}\n}\n@article{2020SciPy-NMeth,\n  author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and\n            Haberland, Matt and Reddy, Tyler and Cournapeau, David and\n            Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and\n            Bright, Jonathan and {van der Walt}, St{\'e}fan J. and\n            Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and\n            Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and\n            Kern, Robert and Larson, Eric and Carey, C J and\n            Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and\n            {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and\n            Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and\n            Harris, Charles R. and Archibald, Anne M. and\n            Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and\n            {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\n  title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n            Computing in Python}},\n  journal = {Nature Methods},\n  year    = {2020},\n  volume  = {17},\n  pages   = {261--272},\n  adsurl  = {https://rdcu.be/b08Wh},\n  doi     = {10.1038/s41592-019-0686-2},\n}\n'




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







 """simple docstring"""







 def 					_lowercase (			self							:			Dict	):
    return datasets.MetricInfo(
        description=_DESCRIPTION,					citation=_CITATION,					inputs_description=_KWARGS_DESCRIPTION,					features=datasets.Features(
            {
                "predictions": datasets.Value("float"	),
                "references": datasets.Value("float"	),
            }	),					reference_urls=["https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html"],					)



 def 					_lowercase (			self							:			Optional[Any],					UpperCAmelCase__							:			Optional[int],					UpperCAmelCase__							:			List[str],					UpperCAmelCase__							:			Optional[int]=False	):
    __lowercase								=						spearmanr(UpperCAmelCase__,					UpperCAmelCase__	)
    if return_pvalue:
       return {"spearmanr": results[0], "spearmanr_pvalue": results[1]}
    else:
       return {"spearmanr": results[0]}




