
'''simple docstring'''







from __future__ import annotations

import unittest

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

					from transformers.models.esm.modeling_tf_esm import (
					    TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFEsmForMaskedLM,
					    TFEsmForSequenceClassification,
					    TFEsmForTokenClassification,
					    TFEsmModel,
					)


class 		__UpperCAmelCase   :


							def __init__( self					,     lowerCAmelCase_					,     ):





								"""simple docstring"""






								_snake_case     		=	parent
								_snake_case     		=	13
								_snake_case     		=	7
								_snake_case     		=	True
								_snake_case     		=	True
								_snake_case     		=	True
								_snake_case     		=	99
								_snake_case     		=	32
								_snake_case     		=	2
								_snake_case     		=	4
								_snake_case     		=	37
								_snake_case     		=	'gelu'
								_snake_case     		=	0.1
								_snake_case     		=	0.1
								_snake_case     		=	5_12
								_snake_case     		=	16
								_snake_case     		=	2
								_snake_case     		=	0.02
								_snake_case     		=	3
								_snake_case     		=	4
								_snake_case     		=	None


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size						)

								_snake_case     		=	None
								if self.use_input_mask:
									_snake_case     		=	random_attention_mask([self.batch_size, self.seq_length]						)

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

								_snake_case     		=	EsmConfig(
								    vocab_size=self.vocab_size					,     hidden_size=self.hidden_size					,     num_hidden_layers=self.num_hidden_layers					,     pad_token_id=1					,     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					,     )

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


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								(
								    (
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,					
								)     		=	self.prepare_config_and_inputs()

								_snake_case     		=	True
								_snake_case     		=	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
								_snake_case     		=	ids_tensor([self.batch_size, self.seq_length]					,     vocab_size=2						)

								return (
								    config,
								    input_ids,
								    input_mask,
								    sequence_labels,
								    token_labels,
								    choice_labels,
								    encoder_hidden_states,
								    encoder_attention_mask,
								)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	TFEsmModel(config=_lowerCamelCase						)
								_snake_case     		=	{'input_ids': input_ids, 'attention_mask': input_mask}
								_snake_case     		=	model(_lowerCamelCase						)

								_snake_case     		=	[input_ids, input_mask]
								_snake_case     		=	model(_lowerCamelCase						)

								_snake_case     		=	model(_lowerCamelCase						)

								self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     ):





								"""simple docstring"""






								_snake_case     		=	True

								_snake_case     		=	TFEsmModel(config=_lowerCamelCase						)
								_snake_case     		=	{
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'encoder_hidden_states': encoder_hidden_states,
								    'encoder_attention_mask': encoder_attention_mask,
								}
								_snake_case     		=	model(_lowerCamelCase						)

								_snake_case     		=	[input_ids, input_mask]
								_snake_case     		=	model(_lowerCamelCase					,     encoder_hidden_states=_lowerCamelCase						)

								# Also check the case where encoder outputs are not passed
								_snake_case     		=	model(_lowerCamelCase					,     attention_mask=_lowerCamelCase						)

								self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	TFEsmForMaskedLM(config=_lowerCamelCase						)
								_snake_case     		=	model([input_ids, input_mask]						)
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)						)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	self.num_labels
								_snake_case     		=	TFEsmForTokenClassification(config=_lowerCamelCase						)
								_snake_case     		=	{'input_ids': input_ids, 'attention_mask': input_mask}
								_snake_case     		=	model(_lowerCamelCase						)
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.num_labels)						)




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.prepare_config_and_inputs()
								(
								    (
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,				(
								    _snake_case	
								)		,					
								)     		=	config_and_inputs
								_snake_case     		=	{'input_ids': input_ids, 'attention_mask': input_mask}
								return config, inputs_dict


@require_tf
class 		__UpperCAmelCase   (      __UpperCAmelCase		,     __UpperCAmelCase		,     unittest.TestCase		):
							__lowercase   	= (
							    (
							        TFEsmModel,
							        TFEsmForMaskedLM,
							        TFEsmForSequenceClassification,
							        TFEsmForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							__lowercase   	= (
							    {
							        "feature-extraction": TFEsmModel,
							        "fill-mask": TFEsmForMaskedLM,
							        "text-classification": TFEsmForSequenceClassification,
							        "token-classification": TFEsmForTokenClassification,
							        "zero-shot": TFEsmForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							__lowercase   	= False
							__lowercase   	= False


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	TFEsmModelTester(self						)
								_snake_case     		=	ConfigTester(self					,     config_class=_lowerCamelCase					,     hidden_size=37						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								self.config_tester.run_common_tests()


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowerCamelCase						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(*_lowerCamelCase						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase						)


							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case     		=	TFEsmModel.from_pretrained(_lowerCamelCase						)
									self.assertIsNotNone(_lowerCamelCase						)


							@unittest.skip('Protein models do not support embedding resizing.'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							@unittest.skip('Protein models do not support embedding resizing.'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(_lowerCamelCase						)
									assert isinstance(model.get_input_embeddings()					,     tf.keras.layers.Layer						)
									if model_class is TFEsmForMaskedLM:
										# Output embedding test differs from the main test because they're a matrix, not a layer
										_snake_case     		=	model.get_bias()
										assert isinstance(_lowerCamelCase					,     _lowerCamelCase						)
										for k, v in name.items():
											assert isinstance(_lowerCamelCase					,     tf.Variable						)
									else:
										_snake_case     		=	model.get_output_embeddings()
										assert x is None
										_snake_case     		=	model.get_bias()
										assert name is None


@require_tf
class 		__UpperCAmelCase   (      unittest.TestCase		):


							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	TFEsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'						)

								_snake_case     		=	tf.constant([[0, 1, 2, 3, 4, 5]]						)
								_snake_case     		=	model(_lowerCamelCase						)[0]
								_snake_case     		=	[1, 6, 33]
								self.assertEqual(list(output.numpy().shape						)					,     _lowerCamelCase						)
								# compare the actual values for a slice.
								_snake_case     		=	tf.constant(
								    [
								        [
								            [8.921518, -10.589814, -6.4671307],
								            [-6.3967156, -13.911377, -1.1211915],
								            [-7.781247, -13.951557, -3.740592],
								        ]
								    ]						)
								self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()					,     expected_slice.numpy()					,     atol=1E-2						)						)




							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	TFEsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'						)

								_snake_case     		=	tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]						)
								_snake_case     		=	model(_lowerCamelCase						)[0]
								# compare the actual values for a slice.
								_snake_case     		=	tf.constant(
								    [
								        [
								            [0.14443092, 0.54125327, 0.3247739],
								            [0.30340484, 0.00526676, 0.31077722],
								            [0.32278043, -0.24987096, 0.3414628],
								        ]
								    ]						)
								self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()					,     expected_slice.numpy()					,     atol=1E-4						)						)



'''simple docstring'''







import numpy as np
from nltk.translate import meteor_score

import datasets
from datasets.config import importlib_metadata, version


lowercase					:							str   =      version.parse(importlib_metadata.version("nltk"))
if NLTK_VERSION >= version.Version("3.6.4"):
					from nltk import word_tokenize


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

lowercase					:							Any   =      "\\nMETEOR, an automatic metric for machine translation evaluation\nthat is based on a generalized concept of unigram matching between the\nmachine-produced translation and human-produced reference translations.\nUnigrams can be matched based on their surface forms, stemmed forms,\nand meanings; furthermore, METEOR can be easily extended to include more\nadvanced matching strategies. Once all generalized unigram matches\nbetween the two strings have been found, METEOR computes a score for\nthis matching using a combination of unigram-precision, unigram-recall, and\na measure of fragmentation that is designed to directly capture how\nwell-ordered the matched words in the machine translation are in relation\nto the reference.\n\nMETEOR gets an R correlation value of 0.347 with human evaluation on the Arabic\ndata and 0.331 on the Chinese data. This is shown to be an improvement on\nusing simply unigram-precision, unigram-recall and their harmonic F1\ncombination.\n"

lowercase					:							Optional[Any]   =      "\nComputes METEOR score of translated segments against one or more references.\nArgs:\n    predictions: list of predictions to score. Each prediction\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\n    alpha: Parameter for controlling relative weights of precision and recall. default: 0.9\n    beta: Parameter for controlling shape of penalty as a function of fragmentation. default: 3\n    gamma: Relative weight assigned to fragmentation penalty. default: 0.5\nReturns:\n    'meteor': meteor score.\nExamples:\n\n    >>> meteor = datasets.load_metric('meteor')\n    >>> predictions = [\"It is a guide to action which ensures that the military always obeys the commands of the party\"]\n    >>> references = [\"It is a guide to action that ensures that the military will forever heed Party commands\"]\n    >>> results = meteor.compute(predictions=predictions, references=references)\n    >>> print(round(results[\"meteor\"], 4))\n    0.6944\n"


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


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								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 				lowerCamelCase       ( self					,     lowerCAmelCase_						):





								"""simple docstring"""






								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 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_=0.9					,     lowerCAmelCase_=3					,     lowerCAmelCase_=0.5						):





								"""simple docstring"""






								if NLTK_VERSION >= version.Version('3.6.5'						):
									_snake_case     		=	[
									    meteor_score.single_meteor_score(
									        word_tokenize(lowerCAmelCase_						)					,     word_tokenize(lowerCAmelCase_						)					,     alpha=lowerCAmelCase_					,     beta=lowerCAmelCase_					,     gamma=lowerCAmelCase_						)
									    for ref, pred in zip(lowerCAmelCase_					,     lowerCAmelCase_						)
									]
								else:
									_snake_case     		=	[
									    meteor_score.single_meteor_score(lowerCAmelCase_					,     lowerCAmelCase_					,     alpha=lowerCAmelCase_					,     beta=lowerCAmelCase_					,     gamma=lowerCAmelCase_						)
									    for ref, pred in zip(lowerCAmelCase_					,     lowerCAmelCase_						)
									]

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

