





"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
		import tensorflow as tf

		from transformers.models.xglm.modeling_tf_xglm import (
		    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXGLMForCausalLM,
		    TFXGLMModel,
		)


@require_tf
class        __UpperCamelCase				:
							lowerCamelCase   :					Any      					=XGLMConfig
							lowerCamelCase   :					List[Any]      					={}
							lowerCamelCase   :					Optional[int]      					="gelu"






							def __init__(   self      ,					lowerCAmelCase__      ,					lowerCAmelCase__=14      ,					lowerCAmelCase__=7      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=True      ,					lowerCAmelCase__=99      ,					lowerCAmelCase__=32      ,					lowerCAmelCase__=2      ,					lowerCAmelCase__=4      ,					lowerCAmelCase__=37      ,					lowerCAmelCase__="gelu"      ,					lowerCAmelCase__=0.1      ,					lowerCAmelCase__=0.1      ,					lowerCAmelCase__=512      ,					lowerCAmelCase__=0.02      ,					)	-> str:
								a      :		Optional[int]	=				parent
								a      :		int	=				batch_size
								a      :		Optional[Any]	=				seq_length
								a      :		Any	=				is_training
								a      :		Optional[int]	=				use_input_mask
								a      :		str	=				use_labels
								a      :		Any	=				vocab_size
								a      :		Optional[int]	=				d_model
								a      :		int	=				num_hidden_layers
								a      :		Union[str, Any]	=				num_attention_heads
								a      :		Union[str, Any]	=				ffn_dim
								a      :		Any	=				activation_function
								a      :		Tuple	=				activation_dropout
								a      :		int	=				attention_dropout
								a      :		Optional[int]	=				max_position_embeddings
								a      :		str	=				initializer_range
								a      :		List[Any]	=				None
								a      :		Tuple	=				0
								a      :		str	=				2
								a      :		Optional[int]	=				1






							def     __a   (   self      )	-> Any:
								return XGLMConfig.from_pretrained("facebook/xglm-564M"      )






							def     __a   (   self      )	-> Optional[int]:
								a      :		Dict	=				tf.clip_by_value(
								    ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size      )      ,					clip_value_min=0      ,					clip_value_max=3      )

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

								a      :		int	=				self.get_config()

								a      :		int	=				floats_tensor([self.num_hidden_layers, self.num_attention_heads]      ,					2      )

								return (
								    config,
								    input_ids,
								    input_mask,
								    head_mask,
								)






							def     __a   (   self      )	-> Union[str, Any]:
								return XGLMConfig(
								    vocab_size=self.vocab_size      ,					d_model=self.hidden_size      ,					num_layers=self.num_hidden_layers      ,					attention_heads=self.num_attention_heads      ,					ffn_dim=self.ffn_dim      ,					activation_function=self.activation_function      ,					activation_dropout=self.activation_dropout      ,					attention_dropout=self.attention_dropout      ,					max_position_embeddings=self.max_position_embeddings      ,					initializer_range=self.initializer_range      ,					use_cache=a__      ,					bos_token_id=self.bos_token_id      ,					eos_token_id=self.eos_token_id      ,					pad_token_id=self.pad_token_id      ,					return_dict=a__      ,					)






							def     __a   (   self      )	-> Optional[int]:
								a      :		Tuple	=				self.prepare_config_and_inputs()

								(
								    a	
								)      :		List[str]	=				config_and_inputs

								a      :		Tuple	=				{
								    """input_ids""": input_ids,
								    """head_mask""": head_mask,
								}

								return config, inputs_dict


@require_tf
class        __UpperCamelCase				(						SCREAMING_SNAKE_CASE_		,       SCREAMING_SNAKE_CASE_		,       unittest.TestCase						):
							lowerCamelCase   :					Optional[Any]      					=(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							lowerCamelCase   :					List[str]      					=(TFXGLMForCausalLM,) if is_tf_available() else ()
							lowerCamelCase   :					Optional[Any]      					=(
							    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
							)
							lowerCamelCase   :					Any      					=False
							lowerCamelCase   :					int      					=False
							lowerCamelCase   :					Union[str, Any]      					=False






							def     __a   (   self      )	-> int:
								a      :		List[str]	=				TFXGLMModelTester(self      )
								a      :		Optional[Any]	=				ConfigTester(self      ,					config_class=a__      ,					n_embd=37      )






							def     __a   (   self      )	-> Union[str, Any]:
								self.config_tester.run_common_tests()






							@slow
							def     __a   (   self      )	-> int:
								for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a      :		Any	=				TFXGLMModel.from_pretrained(a__      )
									self.assertIsNotNone(a__      )






							@unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."      )
							def     __a   (   self      )	-> int:
								super().test_resize_token_embeddings()


@require_tf
class        __UpperCamelCase				(						unittest.TestCase						):






							@slow
							def     __a   (   self      ,					lowerCAmelCase__=True      )	-> Optional[int]:
								a      :		List[str]	=				TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"      )
								a      :		Tuple	=				tf.convert_to_tensor([[2, 268, 9865]]      ,					dtype=tf.intaa      )  # The dog
								# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
								# fmt: off
								a      :		Any	=				[2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581]
								# fmt: on
								a      :		str	=				model.generate(a__      ,					do_sample=a__      ,					num_beams=1      )
								if verify_outputs:
									self.assertListEqual(output_ids[0].numpy().tolist()      ,					a__      )






							@slow
							def     __a   (   self      )	-> int:
								a      :		str	=				XGLMTokenizer.from_pretrained("facebook/xglm-564M"      )
								a      :		Tuple	=				TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"      )

								tf.random.set_seed(0      )
								a      :		List[Any]	=				tokenizer("Today is a nice day and"      ,					return_tensors="tf"      )
								a      :		Optional[Any]	=				tokenized.input_ids
								# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
								with tf.device(":/CPU:0"      ):
									a      :		List[str]	=				model.generate(a__      ,					do_sample=a__      ,					seed=[7, 0]      )
								a      :		int	=				tokenizer.decode(output_ids[0]      ,					skip_special_tokens=a__      )

								a      :		Union[str, Any]	=				(
								    """Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"""
								)
								self.assertEqual(a__      ,					a__      )






							@slow
							def     __a   (   self      )	-> Any:
								a      :		Union[str, Any]	=				TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"      )
								a      :		List[Any]	=				XGLMTokenizer.from_pretrained("facebook/xglm-564M"      )

								a      :		Optional[Any]	=				"""left"""

								# use different length sentences to test batching
								a      :		List[Any]	=				[
								    """This is an extremelly long sentence that only exists to test the ability of the model to cope with """
								    """left-padding, such as in batched generation. The output for the sequence below should be the same """
								    """regardless of whether left padding is applied or not. When""",
								    """Hello, my dog is a little""",
								]

								a      :		int	=				tokenizer(a__      ,					return_tensors="tf"      ,					padding=a__      )
								a      :		Optional[int]	=				inputs["""input_ids"""]

								a      :		List[Any]	=				model.generate(input_ids=a__      ,					attention_mask=inputs["attention_mask"]      ,					max_new_tokens=12      )

								a      :		Any	=				tokenizer(sentences[0]      ,					return_tensors="tf"      ).input_ids
								a      :		List[Any]	=				model.generate(input_ids=a__      ,					max_new_tokens=12      )

								a      :		str	=				tokenizer(sentences[1]      ,					return_tensors="tf"      ).input_ids
								a      :		Optional[Any]	=				model.generate(input_ids=a__      ,					max_new_tokens=12      )

								a      :		Any	=				tokenizer.batch_decode(a__      ,					skip_special_tokens=a__      )
								a      :		Union[str, Any]	=				tokenizer.decode(output_non_padded[0]      ,					skip_special_tokens=a__      )
								a      :		Optional[int]	=				tokenizer.decode(output_padded[0]      ,					skip_special_tokens=a__      )

								a      :		Dict	=				[
								    """This is an extremelly long sentence that only exists to test the ability of the model to cope with """
								    """left-padding, such as in batched generation. The output for the sequence below should be the same """
								    """regardless of whether left padding is applied or not. When left padding is applied, the sequence will be """
								    """a single""",
								    """Hello, my dog is a little bit of a shy one, but he is very friendly""",
								]
								self.assertListEqual(a__      ,					a__      )
								self.assertListEqual(a__      ,					[non_padded_sentence, padded_sentence]      )



"""simple docstring"""




from scipy.stats import pearsonr

import datasets


_a      :		str           =   '\nPearson correlation coefficient and p-value for testing non-correlation.\nThe Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.\nThe p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.\n'


_a      :		List[str]           =   '\nArgs:\n    predictions (`list` of `int`): Predicted class labels, as returned by a model.\n    references (`list` of `int`): Ground truth labels.\n    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.\n\nReturns:\n    pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.\n    p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.\n\nExamples:\n\n    Example 1-A simple example using only predictions and references.\n        >>> pearsonr_metric = datasets.load_metric("pearsonr")\n        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])\n        >>> print(round(results[\'pearsonr\'], 2))\n        -0.74\n\n    Example 2-The same as Example 1, but that also returns the `p-value`.\n        >>> pearsonr_metric = datasets.load_metric("pearsonr")\n        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)\n        >>> print(sorted(list(results.keys())))\n        [\'p-value\', \'pearsonr\']\n        >>> print(round(results[\'pearsonr\'], 2))\n        -0.74\n        >>> print(round(results[\'p-value\'], 2))\n        0.15\n'


_a      :		List[Any]           =   '\n@article{2020SciPy-NMeth,\nauthor  = {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, Ilhan 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, Antonio H. and Pedregosa, Fabian and\n      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},\ntitle   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific\n      Computing in Python}},\njournal = {Nature Methods},\nyear    = {2020},\nvolume  = {17},\npages   = {261--272},\nadsurl  = {https://rdcu.be/b08Wh},\ndoi = {10.1038/s41592-019-0686-2},\n}\n'
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    ,	_KWARGS_DESCRIPTION		)
class 						__A							( datasets.Metric		):






     def     __A  (		self  ):
       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.pearsonr.html"""]				,     )






     def     __A  (		self				,     a__				,     a__				,     a__=False  ):
       if return_pvalue:
         _lowerCAmelCase						:       List[Any]  			=     pearsonr(a__				,     a__  )
         return {"pearsonr": results[0], "p-value": results[1]}
       else:
         return {"pearsonr": float(pearsonr(a__				,     a__  )[0]  )}




