
'''simple docstring'''
import unittest

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

from ...generation.test_utils import GenerationTesterMixin
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 (
		    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    OpenAIGPTConfig,
		    OpenAIGPTDoubleHeadsModel,
		    OpenAIGPTForSequenceClassification,
		    OpenAIGPTLMHeadModel,
		    OpenAIGPTModel,
		)







class   a_ :




			def __init__(       self					,   UpperCAmelCase					,   UpperCAmelCase=13					,   UpperCAmelCase=7					,   UpperCAmelCase=True					,   UpperCAmelCase=True					,   UpperCAmelCase=True					,   UpperCAmelCase=99					,   UpperCAmelCase=32					,   UpperCAmelCase=5					,   UpperCAmelCase=4					,   UpperCAmelCase=37					,   UpperCAmelCase="gelu"					,   UpperCAmelCase=0.1					,   UpperCAmelCase=0.1					,   UpperCAmelCase=5_12					,   UpperCAmelCase=16					,   UpperCAmelCase=2					,   UpperCAmelCase=0.02					,   UpperCAmelCase=3					,   UpperCAmelCase=4					,   UpperCAmelCase=None					,   ):
				a_      				=						parent
				a_      				=						batch_size
				a_      				=						seq_length
				a_      				=						is_training
				a_      				=						use_token_type_ids
				a_      				=						use_labels
				a_      				=						vocab_size
				a_      				=						hidden_size
				a_      				=						num_hidden_layers
				a_      				=						num_attention_heads
				a_      				=						intermediate_size
				a_      				=						hidden_act
				a_      				=						hidden_dropout_prob
				a_      				=						attention_probs_dropout_prob
				a_      				=						max_position_embeddings
				a_      				=						type_vocab_size
				a_      				=						type_sequence_label_size
				a_      				=						initializer_range
				a_      				=						num_labels
				a_      				=						num_choices
				a_      				=						scope
				a_      				=						self.vocab_size - 1




			def 					lowerCAmelCase__					(       self	):
				a_      				=						ids_tensor([self.batch_size, self.seq_length]					,   self.vocab_size	)

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

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

				a_      				=						OpenAIGPTConfig(
				    vocab_size=self.vocab_size					,   n_embd=self.hidden_size					,   n_layer=self.num_hidden_layers					,   n_head=self.num_attention_heads					,   n_positions=self.max_position_embeddings					,   pad_token_id=self.pad_token_id					,   )

				a_      				=						ids_tensor([self.num_hidden_layers, self.num_attention_heads]					,   2	)

				return (
				    config,
				    input_ids,
				    head_mask,
				    token_type_ids,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				)




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   *UpperCAmelCase	):
				a_      				=						OpenAIGPTModel(config=UpperCAmelCase	)
				model.to(UpperCAmelCase	)
				model.eval()

				a_      				=						model(UpperCAmelCase					,   token_type_ids=UpperCAmelCase					,   head_mask=UpperCAmelCase	)
				a_      				=						model(UpperCAmelCase					,   token_type_ids=UpperCAmelCase	)
				a_      				=						model(UpperCAmelCase	)

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




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   *UpperCAmelCase	):
				a_      				=						OpenAIGPTLMHeadModel(UpperCAmelCase	)
				model.to(UpperCAmelCase	)
				model.eval()

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




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   *UpperCAmelCase	):
				a_      				=						OpenAIGPTDoubleHeadsModel(UpperCAmelCase	)
				model.to(UpperCAmelCase	)
				model.eval()

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




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   *UpperCAmelCase	):
				a_      				=						self.num_labels
				a_      				=						OpenAIGPTForSequenceClassification(UpperCAmelCase	)
				model.to(UpperCAmelCase	)
				model.eval()

				a_      				=						ids_tensor([self.batch_size]					,   self.type_sequence_label_size	)
				a_      				=						model(UpperCAmelCase					,   token_type_ids=UpperCAmelCase					,   labels=UpperCAmelCase	)
				self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_labels)	)






			def 					lowerCAmelCase__					(       self	):
				a_      				=						self.prepare_config_and_inputs()
				(
				    (
				    a_
				)				,  (
				    a_
				)				,  (
				    a_
				)				,  (
				    a_
				)				,  (
				    a_
				)				,  (
				    a_
				)				,  (
				    a_
				)				,  
				)      				=						config_and_inputs
				a_      				=						{
				    """input_ids""": input_ids,
				    """token_type_ids""": token_type_ids,
				    """head_mask""": head_mask,
				}

				return config, inputs_dict







@require_torch
class   a_ (      UpperCamelCase__		,  UpperCamelCase__		,  UpperCamelCase__		,  unittest.TestCase					):
			lowerCamelCase__	:			str       =    (
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__	:			Union[str, Any]       =    (
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			lowerCamelCase__	:			str       =    (
			    {
			        'feature-extraction': OpenAIGPTModel,
			        'text-classification': OpenAIGPTForSequenceClassification,
			        'text-generation': OpenAIGPTLMHeadModel,
			        'zero-shot': OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase	):
				if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
					# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
					# `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
					# tiny config could not be created.
					return True

				return False




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase=False	):
				a_      				=						super()._prepare_for_class(UpperCAmelCase					,   UpperCAmelCase					,   return_labels=UpperCAmelCase	)

				if return_labels:
					if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
						a_      				=						torch.zeros(
						    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)					,   dtype=torch.long					,   device=UpperCAmelCase					,   )
						a_      				=						inputs_dict["""labels"""]
						a_      				=						inputs_dict["""labels"""]
						a_      				=						torch.zeros(
						    (self.model_tester.batch_size, self.model_tester.num_choices)					,   dtype=torch.long					,   device=UpperCAmelCase					,   )
						a_      				=						torch.zeros(
						    self.model_tester.batch_size					,   dtype=torch.long					,   device=UpperCAmelCase	)
				return inputs_dict




			def 					lowerCAmelCase__					(       self	):
				a_      				=						OpenAIGPTModelTester(self	)
				a_      				=						ConfigTester(self					,   config_class=UpperCAmelCase					,   n_embd=37	)




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




			def 					lowerCAmelCase__					(       self	):
				a_      				=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase	)




			def 					lowerCAmelCase__					(       self	):
				a_      				=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase	)




			def 					lowerCAmelCase__					(       self	):
				a_      				=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase	)




			def 					lowerCAmelCase__					(       self	):
				a_      				=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase	)






			@slow
			def 					lowerCAmelCase__					(       self	):
				for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					a_      				=						OpenAIGPTModel.from_pretrained(UpperCAmelCase	)
					self.assertIsNotNone(UpperCAmelCase	)







@require_torch
class   a_ (      unittest.TestCase					):




			@slow
			def 					lowerCAmelCase__					(       self	):
				a_      				=						OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""	)
				model.to(UpperCAmelCase	)
				a_      				=						torch.tensor([[4_81, 47_35, 5_44]]					,   dtype=torch.long					,   device=UpperCAmelCase	)  # the president is
				a_      				=						[
				    4_81,
				    47_35,
				    5_44,
				    2_46,
				    9_63,
				    8_70,
				    7_62,
				    2_39,
				    2_44,
				    4_04_77,
				    2_44,
				    2_49,
				    7_19,
				    8_81,
				    4_87,
				    5_44,
				    2_40,
				    2_44,
				    6_03,
				    4_81,
				]  # the president is a very good man. " \n " i\'m sure he is, " said the

				a_      				=						model.generate(UpperCAmelCase					,   do_sample=UpperCAmelCase	)
				self.assertListEqual(output_ids[0].tolist()					,   UpperCAmelCase	)


'''simple docstring'''
import comet  # From: unbabel-comet
import torch

import datasets


lowercase__											=datasets.logging.get_logger(__name__)

lowercase__											='\\n@inproceedings{rei-EtAl:2020:WMT,\n   author    = {Rei, Ricardo  and  Stewart, Craig  and  Farinha, Ana C  and  Lavie, Alon},\n   title     = {Unbabel\'s Participation in the WMT20 Metrics Shared Task},\n   booktitle      = {Proceedings of the Fifth Conference on Machine Translation},\n   month          = {November},\n   year           = {2020},\n   address        = {Online},\n   publisher      = {Association for Computational Linguistics},\n   pages     = {909--918},\n}\n@inproceedings{rei-etal-2020-comet,\n   title = "{COMET}: A Neural Framework for {MT} Evaluation",\n   author = "Rei, Ricardo  and\n      Stewart, Craig  and\n      Farinha, Ana C  and\n      Lavie, Alon",\n   booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)",\n   month = nov,\n   year = "2020",\n   address = "Online",\n   publisher = "Association for Computational Linguistics",\n   url = "https://www.aclweb.org/anthology/2020.emnlp-main.213",\n   pages = "2685--2702",\n}\n'

lowercase__											='\\nCrosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA\'s or MQM).\nWith the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition.\n\nSee the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information.\n'

lowercase__											='\nCOMET score.\n\nArgs:\n\n`sources` (list of str): Source sentences\n`predictions` (list of str): candidate translations\n`references` (list of str): reference translations\n`cuda` (bool): If set to True, runs COMET using GPU\n`show_progress` (bool): Shows progress\n`model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None.\n\nReturns:\n    `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`.\n    `scores`: List of scores.\n\nExamples:\n\n    >>> comet_metric = datasets.load_metric(\'comet\')\n    >>> # comet_metric = load_metric(\'comet\', \'wmt20-comet-da\')  # you can also choose which model to use\n    >>> source = ["Dem Feuer konnte Einhalt geboten werden", "Schulen und Kindergärten wurden eröffnet."]\n    >>> hypothesis = ["The fire could be stopped", "Schools and kindergartens were open"]\n    >>> reference = ["They were able to control the fire.", "Schools and kindergartens opened"]\n    >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source)\n    >>> print([round(v, 2) for v in results["scores"]])\n    [0.19, 0.92]\n'







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




			def 					lowerCAmelCase__					(       self	):
				return datasets.MetricInfo(
				    description=_DESCRIPTION					,   citation=_CITATION					,   homepage="""https://unbabel.github.io/COMET/html/index.html"""					,   inputs_description=_KWARGS_DESCRIPTION					,   features=datasets.Features(
				        {
				            """sources""": datasets.Value("""string"""					,   id="""sequence"""	),
				            """predictions""": datasets.Value("""string"""					,   id="""sequence"""	),
				            """references""": datasets.Value("""string"""					,   id="""sequence"""	),
				        }	)					,   codebase_urls=["""https://github.com/Unbabel/COMET"""]					,   reference_urls=[
				        """https://github.com/Unbabel/COMET""",
				        """https://www.aclweb.org/anthology/2020.emnlp-main.213/""",
				        """http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6""",
				    ]					,   )




			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase	):
				if self.config_name == "default":
					a_      				=						comet.load_from_checkpoint(comet.download_model("""wmt20-comet-da"""	)	)
				else:
					a_      				=						comet.load_from_checkpoint(comet.download_model(self.config_name	)	)






			def 					lowerCAmelCase__					(       self					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase					,   UpperCAmelCase=None					,   UpperCAmelCase=False	):
				if gpus is None:
					a_      				=						1 if torch.cuda.is_available() else 0
				a_      				=						{"""src""": sources, """mt""": predictions, """ref""": references}
				a_      				=						[dict(zip(UpperCAmelCase					,   UpperCAmelCase	)	) for t in zip(*data.values()	)]
				a_				,  a_      				=						self.scorer.predict(UpperCAmelCase					,   gpus=UpperCAmelCase					,   progress_bar=UpperCAmelCase	)
				return {"mean_score": mean_score, "scores": scores}
