



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, 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 numpy as np
				import tensorflow as tf

				from transformers import (
				    TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    FlaubertConfig,
				    TFFlaubertForMultipleChoice,
				    TFFlaubertForQuestionAnsweringSimple,
				    TFFlaubertForSequenceClassification,
				    TFFlaubertForTokenClassification,
				    TFFlaubertModel,
				    TFFlaubertWithLMHeadModel,
				)
class 				__A					:





			def __init__(self							,    __magic_name__							,    ):
						lowerCamelCase__ :      Union[str, Any]								=		parent
						lowerCamelCase__ :      List[str]								=		13
						lowerCamelCase__ :      str								=		7
						lowerCamelCase__ :      Any								=		True
						lowerCamelCase__ :      List[str]								=		True
						lowerCamelCase__ :      Optional[int]								=		True
						lowerCamelCase__ :      Union[str, Any]								=		True
						lowerCamelCase__ :      str								=		True
						lowerCamelCase__ :      List[Any]								=		False
						lowerCamelCase__ :      List[Any]								=		False
						lowerCamelCase__ :      Optional[Any]								=		False
						lowerCamelCase__ :      str								=		2
						lowerCamelCase__ :      Any								=		99
						lowerCamelCase__ :      Union[str, Any]								=		0
						lowerCamelCase__ :      List[Any]								=		32
						lowerCamelCase__ :      Tuple								=		2
						lowerCamelCase__ :      int								=		4
						lowerCamelCase__ :      Any								=		0.1
						lowerCamelCase__ :      str								=		0.1
						lowerCamelCase__ :      List[Any]								=		512
						lowerCamelCase__ :      List[str]								=		16
						lowerCamelCase__ :      Union[str, Any]								=		2
						lowerCamelCase__ :      Optional[Any]								=		0.02
						lowerCamelCase__ :      Optional[Any]								=		3
						lowerCamelCase__ :      str								=		4
						lowerCamelCase__ :      Any								=		"""last"""
						lowerCamelCase__ :      Optional[int]								=		True
						lowerCamelCase__ :      Dict								=		None
						lowerCamelCase__ :      List[Any]								=		0





			def        _snake_case    (self				):
						lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size				)
						lowerCamelCase__ :      List[str]								=		random_attention_mask([self.batch_size, self.seq_length]							,    dtype=tf.floataa				)

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

						lowerCamelCase__ :      List[str]								=		None
						if self.use_token_type_ids:
									lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.n_langs				)

						lowerCamelCase__ :      Optional[Any]								=		None
						lowerCamelCase__ :      Optional[Any]								=		None
						lowerCamelCase__ :      Union[str, Any]								=		None
						if self.use_labels:
									lowerCamelCase__ :      int								=		ids_tensor([self.batch_size]							,    self.type_sequence_label_size				)
									lowerCamelCase__ :      Any								=		ids_tensor([self.batch_size, self.seq_length]							,    self.num_labels				)
									lowerCamelCase__ :      Dict								=		ids_tensor([self.batch_size]							,    2							,    dtype=tf.floataa				)
									lowerCamelCase__ :      str								=		ids_tensor([self.batch_size]							,    self.num_choices				)

						lowerCamelCase__ :      Optional[int]								=		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							,    bos_token_id=self.bos_token_id							,    )

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





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
						lowerCamelCase__ :      Dict								=		TFFlaubertModel(config=__magic_name__				)
						lowerCamelCase__ :      Optional[int]								=		{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
						lowerCamelCase__ :      List[Any]								=		model(__magic_name__				)

						lowerCamelCase__ :      Dict								=		[input_ids, input_mask]
						lowerCamelCase__ :      Any								=		model(__magic_name__				)
						self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
						lowerCamelCase__ :      Any								=		TFFlaubertWithLMHeadModel(__magic_name__				)

						lowerCamelCase__ :      Optional[int]								=		{"""input_ids""": input_ids, """lengths""": input_lengths, """langs""": token_type_ids}
						lowerCamelCase__ :      Any								=		model(__magic_name__				)

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





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
						lowerCamelCase__ :      Any								=		TFFlaubertForQuestionAnsweringSimple(__magic_name__				)

						lowerCamelCase__ :      Union[str, Any]								=		{"""input_ids""": input_ids, """lengths""": input_lengths}

						lowerCamelCase__ :      Dict								=		model(__magic_name__				)

						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							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
						lowerCamelCase__ :      Union[str, Any]								=		TFFlaubertForSequenceClassification(__magic_name__				)

						lowerCamelCase__ :      Dict								=		{"""input_ids""": input_ids, """lengths""": input_lengths}

						lowerCamelCase__ :      Union[str, Any]								=		model(__magic_name__				)

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





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    ):
						lowerCamelCase__ :      Dict								=		self.num_labels
						lowerCamelCase__ :      List[str]								=		TFFlaubertForTokenClassification(config=__magic_name__				)
						lowerCamelCase__ :      Tuple								=		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
						lowerCamelCase__ :      Union[str, Any]								=		model(__magic_name__				)
						self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.num_labels)				)





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





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		self.prepare_config_and_inputs()
						(
						    (
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,
						) :      int								=		config_and_inputs
						lowerCamelCase__ :      Dict								=		{
						    """input_ids""": input_ids,
						    """token_type_ids""": token_type_ids,
						    """langs""": token_type_ids,
						    """lengths""": input_lengths,
						}
						return config, inputs_dict







@require_tf
class 				__A					( A_					, A_					, unittest.TestCase      ):
			UpperCamelCase					:str     =			(
			    (
			        TFFlaubertModel,
			        TFFlaubertWithLMHeadModel,
			        TFFlaubertForSequenceClassification,
			        TFFlaubertForQuestionAnsweringSimple,
			        TFFlaubertForTokenClassification,
			        TFFlaubertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			UpperCamelCase					:str     =			(
			    (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
			)  # TODO (PVP): Check other models whether language generation is also applicable
			UpperCamelCase					:Optional[Any]     =			(
			    {
			        '''feature-extraction''': TFFlaubertModel,
			        '''fill-mask''': TFFlaubertWithLMHeadModel,
			        '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
			        '''text-classification''': TFFlaubertForSequenceClassification,
			        '''token-classification''': TFFlaubertForTokenClassification,
			        '''zero-shot''': TFFlaubertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			UpperCamelCase					:Union[str, Any]     =			False
			UpperCamelCase					:Tuple     =			False





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						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        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		TFFlaubertModelTester(self				)
						lowerCamelCase__ :      Optional[int]								=		ConfigTester(self							,    config_class=__magic_name__							,    emb_dim=37				)





			def        _snake_case    (self				):
						self.config_tester.run_common_tests()





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_model(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_lm_head(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_qa(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_sequence_classif(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_for_token_classification(*__magic_name__				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_for_multiple_choice(*__magic_name__				)





			@slow
			def        _snake_case    (self				):
						for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      Any								=		TFFlaubertModel.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)







@require_tf
@require_sentencepiece
@require_tokenizers
class 				__A					( unittest.TestCase      ):





			@slow
			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		TFFlaubertModel.from_pretrained("""jplu/tf-flaubert-small-cased"""				)

						lowerCamelCase__ :      Tuple								=		tf.convert_to_tensor(
						    [[0, 158, 735, 2592, 1424, 6727, 82, 1]]							,    dtype=tf.intaa							,    )  # "J'aime flaubert !"

						lowerCamelCase__ :      Any								=		model(__magic_name__				)[0]
						lowerCamelCase__ :      Any								=		tf.TensorShape((1, 8, 512)				)
						self.assertEqual(output.shape							,    __magic_name__				)
						# compare the actual values for a slice.
						lowerCamelCase__ :      Optional[int]								=		tf.convert_to_tensor(
						    [
						        [
						            [-1.8_76_87_73, -1.56_65_55, 0.27_07_24_18],
						            [-1.6_92_00_38, -0.5_87_35_05, 1.9_32_95_99],
						            [-2.9_56_39_85, -1.6_99_38_35, 1.7_97_20_52],
						        ]
						    ]							,    dtype=tf.floataa							,    )

						self.assertTrue(np.allclose(output[:, :3, :3].numpy()							,    expected_slice.numpy()							,    atol=1E-4				)				)








import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


_lowercase     							=						version.parse(importlib_metadata.version('''nltk'''))
if NLTK_VERSION >= version.Version('''3.6.4'''):
				from nltk import word_tokenize


_lowercase     							=						'''\
@inproceedings{banarjee2005,
  title     = {{METEOR}: An Automatic Metric for {MT} Evaluation with Improved Correlation with Human Judgments},
  author    = {Banerjee, Satanjeev  and Lavie, Alon},
  booktitle = {Proceedings of the {ACL} Workshop on Intrinsic and Extrinsic Evaluation Measures for Machine Translation and/or Summarization},
  month     = jun,
  year      = {2005},
  address   = {Ann Arbor, Michigan},
  publisher = {Association for Computational Linguistics},
  url       = {https://www.aclweb.org/anthology/W05-0909},
  pages     = {65--72},
}
'''

_lowercase     							=						'''\
METEOR, an automatic metric for machine translation evaluation
that is based on a generalized concept of unigram matching between the
machine-produced translation and human-produced reference translations.
Unigrams can be matched based on their surface forms, stemmed forms,
and meanings; furthermore, METEOR can be easily extended to include more
advanced matching strategies. Once all generalized unigram matches
between the two strings have been found, METEOR computes a score for
this matching using a combination of unigram-precision, unigram-recall, and
a measure of fragmentation that is designed to directly capture how
well-ordered the matched words in the machine translation are in relation
to the reference.

METEOR gets an R correlation value of 0.347 with human evaluation on the Arabic
data and 0.331 on the Chinese data. This is shown to be an improvement on
using simply unigram-precision, unigram-recall and their harmonic F1
combination.
'''

_lowercase     							=						'''
Computes METEOR score of translated segments against one or more references.
Args:
    predictions: list of predictions to score. Each prediction
        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.
    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9
    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3
    gamma: Relative weight assigned to fragmentation penalty. default: 0.5
Returns:
    \'meteor\': meteor score.
Examples:

    >>> meteor = datasets.load_metric(\'meteor\')
    >>> predictions = ["It is a guide to action which ensures that the military always obeys the commands of the party"]
    >>> references = ["It is a guide to action that ensures that the military will forever heed Party commands"]
    >>> results = meteor.compute(predictions=predictions, references=references)
    >>> print(round(results["meteor"], 4))
    0.6944
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					, _KWARGS_DESCRIPTION      )
class 				__A					( datasets.Metric      ):





			def        _snake_case    (self				):
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,    citation=_CITATION							,    inputs_description=_KWARGS_DESCRIPTION							,    features=datasets.Features(
						        {
						            """predictions""": datasets.Value("""string"""							,    id="""sequence"""				),
						            """references""": datasets.Value("""string"""							,    id="""sequence"""				),
						        }				)							,    codebase_urls=["""https://github.com/nltk/nltk/blob/develop/nltk/translate/meteor_score.py"""]							,    reference_urls=[
						        """https://www.nltk.org/api/nltk.translate.html#module-nltk.translate.meteor_score""",
						        """https://en.wikipedia.org/wiki/METEOR""",
						    ]							,    )





			def        _snake_case    (self							,    __magic_name__				):
						import nltk

						nltk.download("""wordnet"""				)
						if NLTK_VERSION >= version.Version("""3.6.5"""				):
									nltk.download("""punkt"""				)
						if NLTK_VERSION >= version.Version("""3.6.6"""				):
									nltk.download("""omw-1.4"""				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__=0.9							,    __magic_name__=3							,    __magic_name__=0.5				):
						if NLTK_VERSION >= version.Version("""3.6.5"""				):
									lowerCamelCase__ :      List[str]								=		[
									    meteor_score.single_meteor_score(
									        word_tokenize(__magic_name__				)							,    word_tokenize(__magic_name__				)							,    alpha=__magic_name__							,    beta=__magic_name__							,    gamma=__magic_name__				)
									    for ref, pred in zip(__magic_name__							,    __magic_name__				)
									]
						else:
									lowerCamelCase__ :      int								=		[
									    meteor_score.single_meteor_score(__magic_name__							,    __magic_name__							,    alpha=__magic_name__							,    beta=__magic_name__							,    gamma=__magic_name__				)
									    for ref, pred in zip(__magic_name__							,    __magic_name__				)
									]

						return {"meteor": np.mean(__magic_name__				)}



