






import faiss  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import requests  # noqa: F401 # Here to have a nice missing dependency error message early on
import sklearn  # noqa: F401 # Here to have a nice missing dependency error message early on
import tqdm  # noqa: F401 # Here to have a nice missing dependency error message early on
from mauve import compute_mauve  # From: mauve-text

import datasets


a_		:						int             =    """\
@inproceedings{pillutla-etal:mauve:neurips2021,
  title={MAUVE: Measuring the Gap Between Neural Text and Human Text using Divergence Frontiers},
  author={Pillutla, Krishna and Swayamdipta, Swabha and Zellers, Rowan and Thickstun, John and Welleck, Sean and Choi, Yejin and Harchaoui, Zaid},
  booktitle = {NeurIPS},
  year      = {2021}
}

"""

a_		:						List[Any]             =    """\
MAUVE is a library built on PyTorch and HuggingFace Transformers to measure the gap between neural text and human text with the eponymous MAUVE measure.

MAUVE summarizes both Type I and Type II errors measured softly using Kullback–Leibler (KL) divergences.

For details, see the MAUVE paper: https://arxiv.org/abs/2102.01454 (Neurips, 2021).

This metrics is a wrapper around the official implementation of MAUVE:
https://github.com/krishnap25/mauve
"""

a_		:						List[Any]             =    """
Calculates MAUVE scores between two lists of generated text and reference text.
Args:
    predictions: list of generated text to score. Each predictions
        should be a string with tokens separated by spaces.
    references: list of reference for each prediction. Each
        reference should be a string with tokens separated by spaces.
Optional Args:
    num_buckets: the size of the histogram to quantize P and Q. Options: 'auto' (default) or an integer
    pca_max_data: the number data points to use for PCA dimensionality reduction prior to clustering. If -1, use all the data. Default -1
    kmeans_explained_var: amount of variance of the data to keep in dimensionality reduction by PCA. Default 0.9
    kmeans_num_redo: number of times to redo k-means clustering (the best objective is kept). Default 5
    kmeans_max_iter: maximum number of k-means iterations. Default 500
    featurize_model_name: name of the model from which features are obtained. Default 'gpt2-large' Use one of ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'].
    device_id: Device for featurization. Supply a GPU id (e.g. 0 or 3) to use GPU. If no GPU with this id is found, use CPU
    max_text_length: maximum number of tokens to consider. Default 1024
    divergence_curve_discretization_size: Number of points to consider on the divergence curve. Default 25
    mauve_scaling_factor: \"c\" from the paper. Default 5.
    verbose: If True (default), print running time updates
    seed: random seed to initialize k-means cluster assignments.
Returns:
    mauve: MAUVE score, a number between 0 and 1. Larger values indicate that P and Q are closer,
    frontier_integral: Frontier Integral, a number between 0 and 1. Smaller values indicate that P and Q are closer,
    divergence_curve: a numpy.ndarray of shape (m, 2); plot it with matplotlib to view the divergence curve,
    p_hist: a discrete distribution, which is a quantized version of the text distribution p_text,
    q_hist: same as above, but with q_text.
Examples:

    >>> # faiss segfaults in doctest for some reason, so the .compute call is not tested with doctest
    >>> import datasets
    >>> mauve = datasets.load_metric('mauve')
    >>> predictions = [\"hello there\", \"general kenobi\"]
    >>> references = [\"hello there\", \"general kenobi\"]
    >>> out = mauve.compute(predictions=predictions, references=references) # doctest: +SKIP
    >>> print(out.mauve) # doctest: +SKIP
    1.0
"""

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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       str:
						return datasets.MetricInfo(
						    description=_DESCRIPTION		,					citation=_CITATION		,					homepage='https://github.com/krishnap25/mauve'		,					inputs_description=_KWARGS_DESCRIPTION		,					features=datasets.Features(
						        {
						            'predictions': datasets.Value('string'		,					id='sequence'),
						            'references': datasets.Value('string'		,					id='sequence'),
						        })		,					codebase_urls=['https://github.com/krishnap25/mauve']		,					reference_urls=[
						        'https://arxiv.org/abs/2102.01454',
						        'https://github.com/krishnap25/mauve',
						    ]		,					)







			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a=None		,					a=None		,					a=None		,					a=None		,					a="auto"		,					a=-1		,					a=0.9		,					a=5		,					a=500		,					a="gpt2-large"		,					a=-1		,					a=1024		,					a=25		,					a=5		,					a=True		,					a=25		,					)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= compute_mauve(
						    p_text=a		,					q_text=a		,					p_features=a		,					q_features=a		,					p_tokens=a		,					q_tokens=a		,					num_buckets=a		,					pca_max_data=a		,					kmeans_explained_var=a		,					kmeans_num_redo=a		,					kmeans_max_iter=a		,					featurize_model_name=a		,					device_id=a		,					max_text_length=a		,					divergence_curve_discretization_size=a		,					mauve_scaling_factor=a		,					verbose=a		,					seed=a		,					)
						return out












import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
				import torch

				from transformers import (
				    DebertaVaForMaskedLM,
				    DebertaVaForMultipleChoice,
				    DebertaVaForQuestionAnswering,
				    DebertaVaForSequenceClassification,
				    DebertaVaForTokenClassification,
				    DebertaVaModel,
				)
				from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST

class 		_snake_case  (  A__					):






			def __init__(       self		,					a		,					a=13		,					a=7		,					a=True		,					a=True		,					a=True		,					a=True		,					a=99		,					a=32		,					a=5		,					a=4		,					a=37		,					a="gelu"		,					a=0.1		,					a=0.1		,					a=512		,					a=16		,					a=2		,					a=0.02		,					a=False		,					a=True		,					a="None"		,					a=3		,					a=4		,					a=None		,					)      ->       int:
						SCREAMING_SNAKE_CASE						= parent
						SCREAMING_SNAKE_CASE						= batch_size
						SCREAMING_SNAKE_CASE						= seq_length
						SCREAMING_SNAKE_CASE						= is_training
						SCREAMING_SNAKE_CASE						= use_input_mask
						SCREAMING_SNAKE_CASE						= use_token_type_ids
						SCREAMING_SNAKE_CASE						= use_labels
						SCREAMING_SNAKE_CASE						= vocab_size
						SCREAMING_SNAKE_CASE						= hidden_size
						SCREAMING_SNAKE_CASE						= num_hidden_layers
						SCREAMING_SNAKE_CASE						= num_attention_heads
						SCREAMING_SNAKE_CASE						= intermediate_size
						SCREAMING_SNAKE_CASE						= hidden_act
						SCREAMING_SNAKE_CASE						= hidden_dropout_prob
						SCREAMING_SNAKE_CASE						= attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE						= max_position_embeddings
						SCREAMING_SNAKE_CASE						= type_vocab_size
						SCREAMING_SNAKE_CASE						= type_sequence_label_size
						SCREAMING_SNAKE_CASE						= initializer_range
						SCREAMING_SNAKE_CASE						= num_labels
						SCREAMING_SNAKE_CASE						= num_choices
						SCREAMING_SNAKE_CASE						= relative_attention
						SCREAMING_SNAKE_CASE						= position_biased_input
						SCREAMING_SNAKE_CASE						= pos_att_type
						SCREAMING_SNAKE_CASE						= scope






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[str]:
						SCREAMING_SNAKE_CASE						= ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size)

						SCREAMING_SNAKE_CASE						= None
						if self.use_input_mask:
									SCREAMING_SNAKE_CASE						= ids_tensor([self.batch_size, self.seq_length]		,					vocab_size=2)

						SCREAMING_SNAKE_CASE						= None
						if self.use_token_type_ids:
									SCREAMING_SNAKE_CASE						= ids_tensor([self.batch_size, self.seq_length]		,					self.type_vocab_size)

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

						SCREAMING_SNAKE_CASE						= self.get_config()

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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						return DebertaVaConfig(
						    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		,					relative_attention=self.relative_attention		,					position_biased_input=self.position_biased_input		,					pos_att_type=self.pos_att_type		,					)






			def    SCREAMING_SNAKE_CASE__    (       self		,					a)      ->       int:
						self.parent.assertListEqual(list(result.loss.size())		,					[])






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       int:
						SCREAMING_SNAKE_CASE						= DebertaVaModel(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a		,					attention_mask=a		,					token_type_ids=a)[0]
						SCREAMING_SNAKE_CASE						= model(a		,					token_type_ids=a)[0]
						SCREAMING_SNAKE_CASE						= model(a)[0]

						self.parent.assertListEqual(list(sequence_output.size())		,					[self.batch_size, self.seq_length, self.hidden_size])






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Dict:
						SCREAMING_SNAKE_CASE						= DebertaVaForMaskedLM(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a		,					attention_mask=a		,					token_type_ids=a		,					labels=a)

						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       str:
						SCREAMING_SNAKE_CASE						= self.num_labels
						SCREAMING_SNAKE_CASE						= DebertaVaForSequenceClassification(a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a		,					attention_mask=a		,					token_type_ids=a		,					labels=a)
						self.parent.assertListEqual(list(result.logits.size())		,					[self.batch_size, self.num_labels])
						self.check_loss_output(a)






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       int:
						SCREAMING_SNAKE_CASE						= self.num_labels
						SCREAMING_SNAKE_CASE						= DebertaVaForTokenClassification(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a		,					attention_mask=a		,					token_type_ids=a		,					labels=a)
						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.num_labels))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Optional[int]:
						SCREAMING_SNAKE_CASE						= DebertaVaForQuestionAnswering(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(
						    a		,					attention_mask=a		,					token_type_ids=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    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Any:
						SCREAMING_SNAKE_CASE						= DebertaVaForMultipleChoice(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= input_ids.unsqueeze(1).expand(-1		,					self.num_choices		,					-1).contiguous()
						SCREAMING_SNAKE_CASE						= token_type_ids.unsqueeze(1).expand(-1		,					self.num_choices		,					-1).contiguous()
						SCREAMING_SNAKE_CASE						= input_mask.unsqueeze(1).expand(-1		,					self.num_choices		,					-1).contiguous()
						SCREAMING_SNAKE_CASE						= model(
						    a		,					attention_mask=a		,					token_type_ids=a		,					labels=a		,					)
						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_choices))







			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
						SCREAMING_SNAKE_CASE						= self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						
						)						= config_and_inputs
						SCREAMING_SNAKE_CASE						= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict



@require_torch
class 		_snake_case  (  A__   ,    A__   ,    unittest.TestCase					):
			_lowercase		:    Dict			     =							(
			    (
			        DebertaVaModel,
			        DebertaVaForMaskedLM,
			        DebertaVaForSequenceClassification,
			        DebertaVaForTokenClassification,
			        DebertaVaForQuestionAnswering,
			        DebertaVaForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			_lowercase		:    Optional[int]			     =							(
			    {
			        '''feature-extraction''': DebertaVaModel,
			        '''fill-mask''': DebertaVaForMaskedLM,
			        '''question-answering''': DebertaVaForQuestionAnswering,
			        '''text-classification''': DebertaVaForSequenceClassification,
			        '''token-classification''': DebertaVaForTokenClassification,
			        '''zero-shot''': DebertaVaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			_lowercase		:    Optional[int]			     =							True
			_lowercase		:    int			     =							False
			_lowercase		:    Dict			     =							False
			_lowercase		:    Optional[int]			     =							False
			_lowercase		:    List[str]			     =							False






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
						SCREAMING_SNAKE_CASE						= DebertaVaModelTester(self)
						SCREAMING_SNAKE_CASE						= ConfigTester(self		,					config_class=a		,					hidden_size=37)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						self.config_tester.run_common_tests()






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Tuple:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[Any]:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*a)







			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       str:
						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE						= DebertaVaModel.from_pretrained(a)
									self.assertIsNotNone(a)



@require_torch
@require_sentencepiece
@require_tokenizers
class 		_snake_case  (  unittest.TestCase					):






			@unittest.skip(reason='Model not available yet')
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
						pass







			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge')

						SCREAMING_SNAKE_CASE						= torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]])
						SCREAMING_SNAKE_CASE						= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
						with torch.no_grad():
									SCREAMING_SNAKE_CASE						= model(a		,					attention_mask=a)[0]
						# compare the actual values for a slice.
						SCREAMING_SNAKE_CASE						= torch.tensor(
						    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]])
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]		,					a		,					atol=1E-4)		,					f'''{output[:, 1:4, 1:4]}''')




