



from __future__ import annotations

import unittest

from transformers import is_tf_available
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 numpy
				import tensorflow as tf

				from transformers import (
				    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BertConfig,
				    DPRConfig,
				    TFDPRContextEncoder,
				    TFDPRQuestionEncoder,
				    TFDPRReader,
				)
class 				__A					:





			def __init__(self							,    __magic_name__							,    __magic_name__=13							,    __magic_name__=7							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=99							,    __magic_name__=32							,    __magic_name__=2							,    __magic_name__=4							,    __magic_name__=37							,    __magic_name__="gelu"							,    __magic_name__=0.1							,    __magic_name__=0.1							,    __magic_name__=512							,    __magic_name__=16							,    __magic_name__=2							,    __magic_name__=0.02							,    __magic_name__=3							,    __magic_name__=4							,    __magic_name__=None							,    __magic_name__=0							,    ):
						lowerCamelCase__ :      str								=		parent
						lowerCamelCase__ :      Optional[Any]								=		batch_size
						lowerCamelCase__ :      Tuple								=		seq_length
						lowerCamelCase__ :      Any								=		is_training
						lowerCamelCase__ :      str								=		use_input_mask
						lowerCamelCase__ :      Tuple								=		use_token_type_ids
						lowerCamelCase__ :      Optional[Any]								=		use_labels
						lowerCamelCase__ :      Dict								=		vocab_size
						lowerCamelCase__ :      Any								=		hidden_size
						lowerCamelCase__ :      Dict								=		num_hidden_layers
						lowerCamelCase__ :      Union[str, Any]								=		num_attention_heads
						lowerCamelCase__ :      Union[str, Any]								=		intermediate_size
						lowerCamelCase__ :      Optional[Any]								=		hidden_act
						lowerCamelCase__ :      Union[str, Any]								=		hidden_dropout_prob
						lowerCamelCase__ :      Tuple								=		attention_probs_dropout_prob
						lowerCamelCase__ :      Optional[int]								=		max_position_embeddings
						lowerCamelCase__ :      Any								=		type_vocab_size
						lowerCamelCase__ :      Optional[int]								=		type_sequence_label_size
						lowerCamelCase__ :      Any								=		initializer_range
						lowerCamelCase__ :      int								=		num_labels
						lowerCamelCase__ :      Tuple								=		num_choices
						lowerCamelCase__ :      str								=		scope
						lowerCamelCase__ :      Union[str, Any]								=		projection_dim





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size				)

						lowerCamelCase__ :      Optional[int]								=		None
						if self.use_input_mask:
									# follow test_modeling_tf_ctrl.py
									lowerCamelCase__ :      List[str]								=		random_attention_mask([self.batch_size, self.seq_length]				)

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

						lowerCamelCase__ :      Union[str, Any]								=		None
						lowerCamelCase__ :      Dict								=		None
						lowerCamelCase__ :      Any								=		None
						if self.use_labels:
									lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size]							,    self.type_sequence_label_size				)
									lowerCamelCase__ :      Any								=		ids_tensor([self.batch_size, self.seq_length]							,    self.num_labels				)
									lowerCamelCase__ :      Any								=		ids_tensor([self.batch_size]							,    self.num_choices				)

						lowerCamelCase__ :      Dict								=		BertConfig(
						    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							,    is_decoder=__magic_name__							,    initializer_range=self.initializer_range							,    )
						lowerCamelCase__ :      Tuple								=		DPRConfig(projection_dim=self.projection_dim							,    **config.to_dict()				)

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





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Optional[Any]								=		TFDPRContextEncoder(config=__magic_name__				)
						lowerCamelCase__ :      Tuple								=		model(__magic_name__							,    attention_mask=__magic_name__							,    token_type_ids=__magic_name__				)
						lowerCamelCase__ :      Any								=		model(__magic_name__							,    token_type_ids=__magic_name__				)
						lowerCamelCase__ :      Optional[Any]								=		model(__magic_name__				)
						self.parent.assertEqual(result.pooler_output.shape							,    (self.batch_size, self.projection_dim or self.hidden_size)				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Optional[Any]								=		TFDPRQuestionEncoder(config=__magic_name__				)
						lowerCamelCase__ :      Optional[int]								=		model(__magic_name__							,    attention_mask=__magic_name__							,    token_type_ids=__magic_name__				)
						lowerCamelCase__ :      Union[str, Any]								=		model(__magic_name__							,    token_type_ids=__magic_name__				)
						lowerCamelCase__ :      str								=		model(__magic_name__				)
						self.parent.assertEqual(result.pooler_output.shape							,    (self.batch_size, self.projection_dim or self.hidden_size)				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Dict								=		TFDPRReader(config=__magic_name__				)
						lowerCamelCase__ :      Tuple								=		model(__magic_name__							,    attention_mask=__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)				)
						self.parent.assertEqual(result.relevance_logits.shape							,    (self.batch_size,)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		self.prepare_config_and_inputs()
						(
						    (
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,(
						    lowerCamelCase__
						)							,
						) :      List[Any]								=		config_and_inputs
						lowerCamelCase__ :      Optional[Any]								=		{"""input_ids""": input_ids}
						return config, inputs_dict







@require_tf
class 				__A					( A_					, A_					, unittest.TestCase      ):
			UpperCamelCase					:Union[str, Any]     =			(
			    (
			        TFDPRContextEncoder,
			        TFDPRQuestionEncoder,
			        TFDPRReader,
			    )
			    if is_tf_available()
			    else ()
			)
			UpperCamelCase					:Optional[int]     =			{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

			UpperCamelCase					:int     =			False
			UpperCamelCase					:Dict     =			False
			UpperCamelCase					:Tuple     =			False
			UpperCamelCase					:Optional[int]     =			False
			UpperCamelCase					:str     =			False





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		TFDPRModelTester(self				)
						lowerCamelCase__ :      List[Any]								=		ConfigTester(self							,    config_class=__magic_name__							,    hidden_size=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_dpr_context_encoder(*__magic_name__				)





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





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





			@slow
			def        _snake_case    (self				):
						for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      List[str]								=		TFDPRContextEncoder.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)

						for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      Dict								=		TFDPRContextEncoder.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)

						for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      int								=		TFDPRQuestionEncoder.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)

						for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      List[Any]								=		TFDPRReader.from_pretrained(__magic_name__				)
									self.assertIsNotNone(__magic_name__				)







@require_tf
class 				__A					( unittest.TestCase      ):





			@slow
			def        _snake_case    (self				):
						lowerCamelCase__ :      Any								=		TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""				)

						lowerCamelCase__ :      List[Any]								=		tf.constant(
						    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]				)  # [CLS] hello, is my dog cute? [SEP]
						lowerCamelCase__ :      Optional[Any]								=		model(__magic_name__				)[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						lowerCamelCase__ :      Union[str, Any]								=		tf.constant(
						    [
						        [
						            0.03_23_62_53,
						            0.12_75_33_35,
						            0.16_81_85_09,
						            0.00_27_97_86,
						            0.3_89_69_33,
						            0.24_26_49_45,
						            0.2_17_89_71,
						            -0.02_33_52_27,
						            -0.08_48_19_59,
						            -0.14_32_41_17,
						        ]
						    ]				)
						self.assertTrue(numpy.allclose(output[:, :10].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__				)}



