




"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
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 (
						    TF_MODEL_FOR_PRETRAINING_MAPPING,
						    TFMobileBertForMaskedLM,
						    TFMobileBertForMultipleChoice,
						    TFMobileBertForNextSentencePrediction,
						    TFMobileBertForPreTraining,
						    TFMobileBertForQuestionAnswering,
						    TFMobileBertForSequenceClassification,
						    TFMobileBertForTokenClassification,
						    TFMobileBertModel,
						)




@require_tf
class       lowerCAmelCase__       (			lowerCAmelCase_  ,			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     Dict       =      (
				    (
				        TFMobileBertModel,
				        TFMobileBertForMaskedLM,
				        TFMobileBertForNextSentencePrediction,
				        TFMobileBertForPreTraining,
				        TFMobileBertForQuestionAnswering,
				        TFMobileBertForSequenceClassification,
				        TFMobileBertForTokenClassification,
				        TFMobileBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				__UpperCAmelCase :     Optional[int]       =      (
				    {
				        "feature-extraction": TFMobileBertModel,
				        "fill-mask": TFMobileBertForMaskedLM,
				        "question-answering": TFMobileBertForQuestionAnswering,
				        "text-classification": TFMobileBertForSequenceClassification,
				        "token-classification": TFMobileBertForTokenClassification,
				        "zero-shot": TFMobileBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__UpperCAmelCase :     Optional[Any]       =      False
				__UpperCAmelCase :     Tuple       =      False



				def       snake_case    (     self					:	List[str]    ,   lowercase__					:	Tuple    ,   lowercase__					:	int    ,   lowercase__					:	List[str]=False						):
						__lowercase				:		List[str]												=				super()._prepare_for_class(lowercase__    ,   lowercase__    ,   return_labels=lowercase__						)

						if return_labels:
								if model_class in get_values(lowercase__						):
										__lowercase				:		List[str]												=				tf.zeros(self.model_tester.batch_size    ,   dtype=tf.intaa						)

						return inputs_dict




				class       lowerCAmelCase__       (			lowerCAmelCase_    ):


								"""simple docstring"""





								def __init__(     self					:	List[Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Any=1_3    ,   lowercase__					:	Union[str, Any]=7    ,   lowercase__					:	List[str]=True    ,   lowercase__					:	Any=True    ,   lowercase__					:	str=True    ,   lowercase__					:	Optional[int]=True    ,   lowercase__					:	int=9_9    ,   lowercase__					:	str=3_2    ,   lowercase__					:	str=3_2    ,   lowercase__					:	Union[str, Any]=2    ,   lowercase__					:	Union[str, Any]=4    ,   lowercase__					:	Union[str, Any]=3_7    ,   lowercase__					:	str="gelu"    ,   lowercase__					:	Any=0.1    ,   lowercase__					:	int=0.1    ,   lowercase__					:	List[str]=5_1_2    ,   lowercase__					:	Union[str, Any]=1_6    ,   lowercase__					:	Optional[int]=2    ,   lowercase__					:	Union[str, Any]=0.0_2    ,   lowercase__					:	Tuple=3    ,   lowercase__					:	List[Any]=4    ,   lowercase__					:	Any=None    ,   ):
										__lowercase				:		List[Any]												=				parent
										__lowercase				:		Any												=				batch_size
										__lowercase				:		str												=				seq_length
										__lowercase				:		Union[str, Any]												=				is_training
										__lowercase				:		str												=				use_input_mask
										__lowercase				:		List[str]												=				use_token_type_ids
										__lowercase				:		int												=				use_labels
										__lowercase				:		Tuple												=				vocab_size
										__lowercase				:		Optional[int]												=				hidden_size
										__lowercase				:		Any												=				num_hidden_layers
										__lowercase				:		Dict												=				num_attention_heads
										__lowercase				:		str												=				intermediate_size
										__lowercase				:		Union[str, Any]												=				hidden_act
										__lowercase				:		List[str]												=				hidden_dropout_prob
										__lowercase				:		int												=				attention_probs_dropout_prob
										__lowercase				:		Optional[Any]												=				max_position_embeddings
										__lowercase				:		List[Any]												=				type_vocab_size
										__lowercase				:		str												=				type_sequence_label_size
										__lowercase				:		Tuple												=				initializer_range
										__lowercase				:		Optional[Any]												=				num_labels
										__lowercase				:		Union[str, Any]												=				num_choices
										__lowercase				:		Optional[int]												=				scope
										__lowercase				:		str												=				embedding_size



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

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

										__lowercase				:		List[str]												=				None
										if self.use_token_type_ids:
												__lowercase				:		Any												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.type_vocab_size						)

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

										__lowercase				:		Tuple												=				MobileBertConfig(
										    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    ,   embedding_size=self.embedding_size    ,   )

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



								def       snake_case    (     self					:	Union[str, Any]    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	int    ,   lowercase__					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Dict						):
										__lowercase				:		Union[str, Any]												=				TFMobileBertModel(config=lowercase__						)
										__lowercase				:		Optional[Any]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Any												=				model(lowercase__						)

										__lowercase				:		Optional[Any]												=				[input_ids, input_mask]
										__lowercase				:		Any												=				model(lowercase__						)

										__lowercase				:		List[str]												=				model(lowercase__						)

										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       snake_case    (     self					:	Any    ,   lowercase__					:	Any    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	int    ,   lowercase__					:	int    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Any						):
										__lowercase				:		str												=				TFMobileBertForMaskedLM(config=lowercase__						)
										__lowercase				:		str												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		str												=				model(lowercase__						)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length, self.vocab_size)						)



								def       snake_case    (     self					:	int    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Union[str, Any]						):
										__lowercase				:		Union[str, Any]												=				TFMobileBertForNextSentencePrediction(config=lowercase__						)
										__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Union[str, Any]												=				model(lowercase__						)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, 2)						)



								def       snake_case    (     self					:	Dict    ,   lowercase__					:	Dict    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Tuple						):
										__lowercase				:		Any												=				TFMobileBertForPreTraining(config=lowercase__						)
										__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Any												=				model(lowercase__						)
										self.parent.assertEqual(
										    result.prediction_logits.shape    ,   (self.batch_size, self.seq_length, self.vocab_size)						)
										self.parent.assertEqual(result.seq_relationship_logits.shape    ,   (self.batch_size, 2)						)



								def       snake_case    (     self					:	int    ,   lowercase__					:	int    ,   lowercase__					:	List[str]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	str    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[str]						):
										__lowercase				:		List[Any]												=				self.num_labels
										__lowercase				:		str												=				TFMobileBertForSequenceClassification(config=lowercase__						)
										__lowercase				:		Any												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Optional[Any]												=				model(lowercase__						)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_labels)						)



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



								def       snake_case    (     self					:	List[Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[Any]    ,   lowercase__					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	int						):
										__lowercase				:		Union[str, Any]												=				self.num_labels
										__lowercase				:		Tuple												=				TFMobileBertForTokenClassification(config=lowercase__						)
										__lowercase				:		Optional[Any]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Tuple												=				model(lowercase__						)
										self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length, self.num_labels)						)



								def       snake_case    (     self					:	str    ,   lowercase__					:	List[str]    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Any    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	List[str]						):
										__lowercase				:		List[str]												=				TFMobileBertForQuestionAnswering(config=lowercase__						)
										__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
										__lowercase				:		Optional[int]												=				model(lowercase__						)
										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       snake_case    (     self					:	Tuple						):
										__lowercase				:		List[Any]												=				self.prepare_config_and_inputs()
										(
										    __lowercase
										)				:		Optional[int]												=				config_and_inputs
										__lowercase				:		Union[str, Any]												=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
										return config, inputs_dict





				def       snake_case    (     self					:	List[str]						):
						__lowercase				:		Optional[int]												=				TFMobileBertModelTest.TFMobileBertModelTester(self						)
						__lowercase				:		Union[str, Any]												=				ConfigTester(self    ,   config_class=lowercase__    ,   hidden_size=3_7						)



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



				def       snake_case    (     self					:	int						):
						__lowercase				:		Tuple												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*lowercase__						)



				def       snake_case    (     self					:	Any						):
						__lowercase				:		int												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase__						)



				def       snake_case    (     self					:	int						):
						__lowercase				:		Optional[Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase__						)



				def       snake_case    (     self					:	Tuple						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase__						)



				def       snake_case    (     self					:	Optional[Any]						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Any												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase__						)



				def       snake_case    (     self					:	int						):
						__lowercase				:		Union[str, Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase__						)



				@slow
				def       snake_case    (     self					:	int						):
						# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["google/mobilebert-uncased"]:
								__lowercase				:		List[Any]												=				TFMobileBertModel.from_pretrained(lowercase__						)
								self.assertIsNotNone(lowercase__						)





@require_tf
class       lowerCAmelCase__       (			unittest.TestCase    ):


				"""simple docstring"""





				@slow
				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Any												=				TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"						)
						__lowercase				:		Dict												=				tf.constant([[0, 1, 2, 3, 4, 5]]						)
						__lowercase				:		Dict												=				model(lowercase__						)[0]

						__lowercase				:		Optional[int]												=				[1, 6, 3_0_5_2_2]
						self.assertEqual(output.shape    ,   lowercase__						)

						__lowercase				:		Any												=				tf.constant(
						    [
						        [
						            [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6],
						            [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7],
						            [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3],
						        ]
						    ]						)
						tf.debugging.assert_near(output[:, :3, :3]    ,   lowercase__    ,   atol=1e-4						)






"""simple docstring"""







from scipy.stats import spearmanr

import datasets


__A   :  Optional[int]					=      '\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   :  List[Any]					=      '\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   :  Optional[int]					=      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       snake_case    (     self					:	List[str]						):
						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       snake_case    (     self					:	Optional[int]    ,   lowercase__					:	str    ,   lowercase__					:	Any    ,   lowercase__					:	int=False						):
						__lowercase				:		Optional[int]												=				spearmanr(lowercase__    ,   lowercase__						)
						if return_pvalue:
								return {"spearmanr": results[0], "spearmanr_pvalue": results[1]}
						else:
								return {"spearmanr": results[0]}
