import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    FlaubertForMultipleChoice,
					    FlaubertForQuestionAnswering,
					    FlaubertForQuestionAnsweringSimple,
					    FlaubertForSequenceClassification,
					    FlaubertForTokenClassification,
					    FlaubertModel,
					    FlaubertWithLMHeadModel,
					)
					from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class     lowerCAmelCase_     (       __lowercase ):







							def __init__(   self      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     Any=13 ,			_A      :     Union[str, Any]=7 ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[Any]=True ,			_A      :     Optional[int]=False ,			_A      :     Any=False ,			_A      :     int=False ,			_A      :     Optional[Any]=2 ,			_A      :     Any=99 ,			_A      :     str=0 ,			_A      :     Union[str, Any]=32 ,			_A      :     List[Any]=5 ,			_A      :     Tuple=4 ,			_A      :     List[str]=0.1 ,			_A      :     Union[str, Any]=0.1 ,			_A      :     int=512 ,			_A      :     Union[str, Any]=12 ,			_A      :     List[str]=2 ,			_A      :     int=0.02 ,			_A      :     Optional[Any]=3 ,			_A      :     Any=4 ,			_A      :     Optional[int]="last" ,			_A      :     Any=None ,			_A      :     Dict=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     seq_length
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_input_lengths
												_UpperCamelCase			     =     use_token_type_ids
												_UpperCamelCase			     =     use_labels
												_UpperCamelCase			     =     gelu_activation
												_UpperCamelCase			     =     sinusoidal_embeddings
												_UpperCamelCase			     =     causal
												_UpperCamelCase			     =     asm
												_UpperCamelCase			     =     n_langs
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     n_special
												_UpperCamelCase			     =     hidden_size
												_UpperCamelCase			     =     num_hidden_layers
												_UpperCamelCase			     =     num_attention_heads
												_UpperCamelCase			     =     hidden_dropout_prob
												_UpperCamelCase			     =     attention_probs_dropout_prob
												_UpperCamelCase			     =     max_position_embeddings
												_UpperCamelCase			     =     type_vocab_size
												_UpperCamelCase			     =     type_sequence_label_size
												_UpperCamelCase			     =     initializer_range
												_UpperCamelCase			     =     num_labels
												_UpperCamelCase			     =     num_choices
												_UpperCamelCase			     =     summary_type
												_UpperCamelCase			     =     use_proj
												_UpperCamelCase			     =     scope







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size		)
												_UpperCamelCase			     =     random_attention_mask([self.batch_size, self.seq_length]		)

												_UpperCamelCase			     =     None
												if self.use_input_lengths:
																	_UpperCamelCase			     =     (
																	    ids_tensor([self.batch_size] ,			vocab_size=2		) + self.seq_length - 2
																	)  # small variation of seq_length

												_UpperCamelCase			     =     None
												if self.use_token_type_ids:
																	_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.n_langs		)

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

												_UpperCamelCase			     =     self.get_config()

												return (
												    config,
												    input_ids,
												    token_type_ids,
												    input_lengths,
												    sequence_labels,
												    token_labels,
												    is_impossible_labels,
												    choice_labels,
												    input_mask,
												)







							def  UpperCamelCase_						(   self      :     str		):
												return FlaubertConfig(
												    vocab_size=self.vocab_size ,			n_special=self.n_special ,			emb_dim=self.hidden_size ,			n_layers=self.num_hidden_layers ,			n_heads=self.num_attention_heads ,			dropout=self.hidden_dropout_prob ,			attention_dropout=self.attention_probs_dropout_prob ,			gelu_activation=self.gelu_activation ,			sinusoidal_embeddings=self.sinusoidal_embeddings ,			asm=self.asm ,			causal=self.causal ,			n_langs=self.n_langs ,			max_position_embeddings=self.max_position_embeddings ,			initializer_range=self.initializer_range ,			summary_type=self.summary_type ,			use_proj=self.use_proj ,			)







							def  UpperCamelCase_						(   self      :     str ,			_A      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Tuple ,			_A      :     List[str] ,			_A      :     List[Any] ,			_A      :     Any ,			_A      :     str ,			_A      :     Optional[int] ,			):
												_UpperCamelCase			     =     FlaubertModel(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     model(_A ,			lengths=_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A		)
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     List[Any] ,			_A      :     Any ,			):
												_UpperCamelCase			     =     FlaubertWithLMHeadModel(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A ,			token_type_ids=_A ,			labels=_A		)
												self.parent.assertEqual(result.loss.shape ,			()		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[str] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     Union[str, Any] ,			_A      :     str ,			_A      :     List[str] ,			_A      :     Tuple ,			_A      :     Optional[int] ,			_A      :     Dict ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnsweringSimple(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(_A ,			start_positions=_A ,			end_positions=_A		)
												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  UpperCamelCase_						(   self      :     Tuple ,			_A      :     str ,			_A      :     Tuple ,			_A      :     Tuple ,			_A      :     Union[str, Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     Dict ,			_A      :     List[Any] ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnswering(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			p_mask=_A ,			)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			)

												((_UpperCamelCase)     ,    )			     =     result_with_labels.to_tuple()

												_UpperCamelCase			     =     model(_A ,			start_positions=_A ,			end_positions=_A		)

												((_UpperCamelCase)     ,    )			     =     result_with_labels.to_tuple()

												self.parent.assertEqual(result_with_labels.loss.shape ,			()		)
												self.parent.assertEqual(result.start_top_log_probs.shape ,			(self.batch_size, model.config.start_n_top)		)
												self.parent.assertEqual(result.start_top_index.shape ,			(self.batch_size, model.config.start_n_top)		)
												self.parent.assertEqual(
												    result.end_top_log_probs.shape ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
												self.parent.assertEqual(
												    result.end_top_index.shape ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
												self.parent.assertEqual(result.cls_logits.shape ,			(self.batch_size,)		)







							def  UpperCamelCase_						(   self      :     List[Any] ,			_A      :     Union[str, Any] ,			_A      :     Tuple ,			_A      :     str ,			_A      :     int ,			_A      :     int ,			_A      :     Optional[int] ,			_A      :     Optional[int] ,			_A      :     int ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     FlaubertForSequenceClassification(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)
												_UpperCamelCase			     =     model(_A ,			labels=_A		)

												self.parent.assertEqual(result.loss.shape ,			()		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)







							def  UpperCamelCase_						(   self      :     Optional[int] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Union[str, Any] ,			_A      :     List[Any] ,			_A      :     int ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_labels
												_UpperCamelCase			     =     FlaubertForTokenClassification(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A ,			attention_mask=_A ,			labels=_A		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.num_labels)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     Dict ,			_A      :     str ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     Any ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[Any] ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_choices
												_UpperCamelCase			     =     FlaubertForMultipleChoice(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     input_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     token_type_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     input_mask.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     model(
												    _A ,			attention_mask=_A ,			token_type_ids=_A ,			labels=_A ,			)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)		)







							def  UpperCamelCase_						(   self      :     Tuple		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												(
												    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    
												)			     =     config_and_inputs
												_UpperCamelCase			     =     {
												    '''input_ids''': input_ids,
												    '''token_type_ids''': token_type_ids,
												    '''lengths''': input_lengths,
												    '''attention_mask''': input_mask,
												}
												return config, inputs_dict




@require_torch
class     lowerCAmelCase_     (       __lowercase,  __lowercase,  unittest.TestCase ):
							UpperCAmelCase				   =     (
							    (
							        FlaubertModel,
							        FlaubertWithLMHeadModel,
							        FlaubertForQuestionAnswering,
							        FlaubertForQuestionAnsweringSimple,
							        FlaubertForSequenceClassification,
							        FlaubertForTokenClassification,
							        FlaubertForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase				   =     (
							    {
							        "feature-extraction": FlaubertModel,
							        "fill-mask": FlaubertWithLMHeadModel,
							        "question-answering": FlaubertForQuestionAnsweringSimple,
							        "text-classification": FlaubertForSequenceClassification,
							        "token-classification": FlaubertForTokenClassification,
							        "zero-shot": FlaubertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)







							def  UpperCamelCase_						(   self      :     Union[str, Any] ,			_A      :     Dict ,			_A      :     Dict ,			_A      :     Tuple ,			_A      :     int ,			_A      :     Any		):
												if (
												    pipeline_test_casse_name == "QAPipelineTests"
												    and tokenizer_name is not None
												    and not tokenizer_name.endswith('''Fast'''		)
												):
																	# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																	# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																	# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																	return True

												return False







							def  UpperCamelCase_						(   self      :     str ,			_A      :     Any ,			_A      :     List[str] ,			_A      :     Optional[int]=False		):
												_UpperCamelCase			     =     super()._prepare_for_class(_A ,			_A ,			return_labels=_A		)

												if return_labels:
																	if model_class.__name__ == "FlaubertForQuestionAnswering":
																						_UpperCamelCase			     =     torch.zeros(
																						    self.model_tester.batch_size ,			dtype=torch.long ,			device=_A		)
																						_UpperCamelCase			     =     torch.zeros(
																						    self.model_tester.batch_size ,			dtype=torch.long ,			device=_A		)

												return inputs_dict







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     FlaubertModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			emb_dim=37		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												self.config_tester.run_common_tests()







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_simple_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_token_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_multiple_choice(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     str		):
												for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_UpperCamelCase			     =     FlaubertModel.from_pretrained(_A		)
																	self.assertIsNotNone(_A		)







							@slow
							@require_torch_gpu
							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()
												for model_class in self.all_model_classes:
																	# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
																	if model_class == FlaubertForMultipleChoice:
																						return

																	_UpperCamelCase			     =     True
																	_UpperCamelCase			     =     model_class(config=_A		)

																	_UpperCamelCase			     =     self._prepare_for_class(_A ,			_A		)
																	_UpperCamelCase			     =     torch.jit.trace(
																	    _A ,			(inputs_dict['''input_ids'''].to('''cpu'''		), inputs_dict['''attention_mask'''].to('''cpu'''		))		)

																	with tempfile.TemporaryDirectory() as tmp:
																						torch.jit.save(_A ,			os.path.join(_A ,			'''traced_model.pt'''		)		)
																						_UpperCamelCase			     =     torch.jit.load(os.path.join(_A ,			'''traced_model.pt'''		) ,			map_location=_A		)
																						loaded(inputs_dict['''input_ids'''].to(_A		) ,			inputs_dict['''attention_mask'''].to(_A		)		)




@require_torch
class     lowerCAmelCase_     (       unittest.TestCase ):







							@slow
							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''		)
												_UpperCamelCase			     =     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
												with torch.no_grad():
																	_UpperCamelCase			     =     model(_A		)[0]
												_UpperCamelCase			     =     torch.Size((1, 11, 768)		)
												self.assertEqual(output.shape ,			_A		)
												_UpperCamelCase			     =     torch.tensor(
												    [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]		)

												self.assertTrue(torch.allclose(output[:, :3, :3] ,			_A ,			atol=1e-4		)		)


"""simple docstring"""
from scipy.stats import pearsonr

import datasets


SCREAMING_SNAKE_CASE_					     =	'''
Pearson correlation coefficient and p-value for testing non-correlation.
The 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.
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.
'''


SCREAMING_SNAKE_CASE_					     =	'''
Args:
    predictions (`list` of `int`): Predicted class labels, as returned by a model.
    references (`list` of `int`): Ground truth labels.
    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.

Returns:
    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.
    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.

Examples:

    Example 1-A simple example using only predictions and references.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74

    Example 2-The same as Example 1, but that also returns the `p-value`.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)
        >>> print(sorted(list(results.keys())))
        [\'p-value\', \'pearsonr\']
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74
        >>> print(round(results[\'p-value\'], 2))
        0.15
'''


SCREAMING_SNAKE_CASE_					     =	'''
@article{2020SciPy-NMeth,
author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
      Haberland, Matt and Reddy, Tyler and Cournapeau, David and
      Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
      Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
      Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
      Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
      Kern, Robert and Larson, Eric and Carey, C J and
      Polat, Ilhan and Feng, Yu and Moore, Eric W. and
      {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
      Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
      Harris, Charles R. and Archibald, Anne M. and
      Ribeiro, Antonio H. and Pedregosa, Fabian and
      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
      Computing in Python}},
journal = {Nature Methods},
year    = {2020},
volume  = {17},
pages   = {261--272},
adsurl  = {https://rdcu.be/b08Wh},
doi = {10.1038/s41592-019-0686-2},
}
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,				_KWARGS_DESCRIPTION )
class       lowerCAmelCase_		(				datasets.Metric ):



							'''simple docstring'''





							def    A__						( self				)       ->  Union[str, Any]:
													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    ,				snake_case_    ,				snake_case_    ,				snake_case_=False				)       ->  Dict:
													if return_pvalue:
																			__lowerCAmelCase     =  pearsonr(snake_case_    ,				snake_case_				)
																			return {"pearsonr": results[0], "p-value": results[1]}
													else:
																			return {"pearsonr": float(pearsonr(snake_case_    ,				snake_case_				)[0]				)}
