

from __future__ import annotations

import unittest

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

						from transformers.models.openai.modeling_tf_openai import (
						    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFOpenAIGPTDoubleHeadsModel,
						    TFOpenAIGPTForSequenceClassification,
						    TFOpenAIGPTLMHeadModel,
						    TFOpenAIGPTModel,
						)



class 						__lowercase       :




			"""simple docstring"""







			def __init__(				self						:  Any     ,		lowerCAmelCase__						:  int     ,		):
									SCREAMING_SNAKE_CASE_:				str							= parent
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= 13
									SCREAMING_SNAKE_CASE_:				List[str]							= 7
									SCREAMING_SNAKE_CASE_:				str							= True
									SCREAMING_SNAKE_CASE_:				Any							= True
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= True
									SCREAMING_SNAKE_CASE_:				List[Any]							= True
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= True
									SCREAMING_SNAKE_CASE_:				Tuple							= 99
									SCREAMING_SNAKE_CASE_:				Any							= 32
									SCREAMING_SNAKE_CASE_:				Tuple							= 2
									SCREAMING_SNAKE_CASE_:				List[Any]							= 4
									SCREAMING_SNAKE_CASE_:				str							= 37
									SCREAMING_SNAKE_CASE_:				str							= "gelu"
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= 0.1
									SCREAMING_SNAKE_CASE_:				List[Any]							= 0.1
									SCREAMING_SNAKE_CASE_:				Tuple							= 512
									SCREAMING_SNAKE_CASE_:				Dict							= 16
									SCREAMING_SNAKE_CASE_:				Any							= 2
									SCREAMING_SNAKE_CASE_:				Tuple							= 0.02
									SCREAMING_SNAKE_CASE_:				int							= 3
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= 4
									SCREAMING_SNAKE_CASE_:				Tuple							= None
									SCREAMING_SNAKE_CASE_:				str							= self.vocab_size - 1



			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				str							= ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size)

									SCREAMING_SNAKE_CASE_:				int							= None
									if self.use_input_mask:
															SCREAMING_SNAKE_CASE_:				Any							= random_attention_mask([self.batch_size, self.seq_length])

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

									SCREAMING_SNAKE_CASE_:				List[Any]							= None
									if self.use_mc_token_ids:
															SCREAMING_SNAKE_CASE_:				Optional[int]							= ids_tensor([self.batch_size, self.num_choices]     ,		self.seq_length)

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

									SCREAMING_SNAKE_CASE_:				str							= 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     ,		)

									SCREAMING_SNAKE_CASE_:				str							= ids_tensor([self.num_hidden_layers, self.num_attention_heads]     ,		2)

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



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]     ,		lowerCAmelCase__						:  List[str]     ,		lowerCAmelCase__						:  str     ,		lowerCAmelCase__						:  Union[str, Any]     ,		lowerCAmelCase__						:  List[Any]     ,		lowerCAmelCase__						:  Tuple     ,		*lowerCAmelCase__						:  Any):
									SCREAMING_SNAKE_CASE_:				int							= TFOpenAIGPTModel(config=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				int							= {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_:				List[Any]							= model(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				Optional[Any]							= [input_ids, input_mask]
									SCREAMING_SNAKE_CASE_:				int							= model(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				Dict							= model(lowerCAmelCase__)

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



			def  _SCREAMING_SNAKE_CASE    (				self						:  Dict     ,		lowerCAmelCase__						:  List[str]     ,		lowerCAmelCase__						:  Union[str, Any]     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  Any     ,		lowerCAmelCase__						:  str     ,		*lowerCAmelCase__						:  Union[str, Any]):
									SCREAMING_SNAKE_CASE_:				List[Any]							= TFOpenAIGPTLMHeadModel(config=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				int							= {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_:				Any							= model(lowerCAmelCase__)
									self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.vocab_size))



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[Any]     ,		lowerCAmelCase__						:  Tuple     ,		lowerCAmelCase__						:  Dict     ,		lowerCAmelCase__						:  int     ,		lowerCAmelCase__						:  Any     ,		lowerCAmelCase__						:  Optional[Any]     ,		lowerCAmelCase__						:  str     ,		*lowerCAmelCase__						:  Optional[int]):
									SCREAMING_SNAKE_CASE_:				str							= TFOpenAIGPTDoubleHeadsModel(config=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				Optional[Any]							= tf.tile(tf.expand_dims(lowerCAmelCase__     ,		1)     ,		(1, self.num_choices, 1))
									SCREAMING_SNAKE_CASE_:				str							= tf.tile(tf.expand_dims(lowerCAmelCase__     ,		1)     ,		(1, self.num_choices, 1))
									SCREAMING_SNAKE_CASE_:				List[Any]							= tf.tile(tf.expand_dims(lowerCAmelCase__     ,		1)     ,		(1, self.num_choices, 1))

									SCREAMING_SNAKE_CASE_:				int							= {
									    "input_ids": multiple_choice_inputs_ids,
									    "mc_token_ids": mc_token_ids,
									    "attention_mask": multiple_choice_input_mask,
									    "token_type_ids": multiple_choice_token_type_ids,
									}
									SCREAMING_SNAKE_CASE_:				List[Any]							= model(lowerCAmelCase__)
									self.parent.assertEqual(
									    result.logits.shape     ,		(self.batch_size, self.num_choices, self.seq_length, self.vocab_size))
									self.parent.assertEqual(result.mc_logits.shape     ,		(self.batch_size, self.num_choices))



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]     ,		lowerCAmelCase__						:  Dict     ,		lowerCAmelCase__						:  Union[str, Any]     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  Tuple     ,		lowerCAmelCase__						:  str     ,		*lowerCAmelCase__						:  Optional[Any]):
									SCREAMING_SNAKE_CASE_:				List[Any]							= self.num_labels
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= ids_tensor([self.batch_size]     ,		self.type_sequence_label_size)
									SCREAMING_SNAKE_CASE_:				Optional[int]							= {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									    "labels": sequence_labels,
									}
									SCREAMING_SNAKE_CASE_:				Dict							= TFOpenAIGPTForSequenceClassification(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Optional[int]							= model(lowerCAmelCase__)
									self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels))




			def  _SCREAMING_SNAKE_CASE    (				self						:  Any):
									SCREAMING_SNAKE_CASE_:				str							= 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_
									)	,					(
									    SCREAMING_SNAKE_CASE_
									)	,					(
									    SCREAMING_SNAKE_CASE_
									)	,					
									):				Optional[int]							= config_and_inputs

									SCREAMING_SNAKE_CASE_:				List[str]							= {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
									return config, inputs_dict







@require_tf
class 						__lowercase       (				UpperCAmelCase_	, UpperCAmelCase_	, unittest.TestCase			):




			"""simple docstring"""







			_UpperCAmelCase     :							Optional[int]    							=  (
			    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
			    if is_tf_available()
			    else ()
			)
			_UpperCAmelCase     :							Dict    							=  (
			    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			_UpperCAmelCase     :							List[str]    							=  (
			    {
			        '''feature-extraction''': TFOpenAIGPTModel,
			        '''text-classification''': TFOpenAIGPTForSequenceClassification,
			        '''text-generation''': TFOpenAIGPTLMHeadModel,
			        '''zero-shot''': TFOpenAIGPTForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			_UpperCAmelCase     :							Optional[Any]    							=  False
			_UpperCAmelCase     :							Tuple    							=  False



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]     ,		lowerCAmelCase__						:  int     ,		lowerCAmelCase__						:  Any     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  Dict     ,		lowerCAmelCase__						:  str):
									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  _SCREAMING_SNAKE_CASE    (				self						:  Dict):
									SCREAMING_SNAKE_CASE_:				int							= TFOpenAIGPTModelTester(self)
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= ConfigTester(self     ,		config_class=lowerCAmelCase__     ,		n_embd=37)



			def  _SCREAMING_SNAKE_CASE    (				self						:  str):
									self.config_tester.run_common_tests()



			def  _SCREAMING_SNAKE_CASE    (				self						:  Tuple):
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__)



			def  _SCREAMING_SNAKE_CASE    (				self						:  Any):
									SCREAMING_SNAKE_CASE_:				List[Any]							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCAmelCase__)



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[str]):
									SCREAMING_SNAKE_CASE_:				List[Any]							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_double_head(*lowerCAmelCase__)



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[Any]):
									SCREAMING_SNAKE_CASE_:				int							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__)




			@slow
			def  _SCREAMING_SNAKE_CASE    (				self						:  List[str]):
									for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															SCREAMING_SNAKE_CASE_:				List[Any]							= TFOpenAIGPTModel.from_pretrained(lowerCAmelCase__)
															self.assertIsNotNone(lowerCAmelCase__)







@require_tf
class 						__lowercase       (				unittest.TestCase			):




			"""simple docstring"""







			@slow
			def  _SCREAMING_SNAKE_CASE    (				self						:  Tuple):
									SCREAMING_SNAKE_CASE_:				Any							= TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt")
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= tf.convert_to_tensor([[481, 4735, 544]]     ,		dtype=tf.intaa)  # the president is
									SCREAMING_SNAKE_CASE_:				List[Any]							= [
									    481,
									    4735,
									    544,
									    246,
									    963,
									    870,
									    762,
									    239,
									    244,
									    4_0477,
									    244,
									    249,
									    719,
									    881,
									    487,
									    544,
									    240,
									    244,
									    603,
									    481,
									]  # the president is a very good man. " \n " i\'m sure he is, " said the

									SCREAMING_SNAKE_CASE_:				Tuple							= model.generate(lowerCAmelCase__     ,		do_sample=lowerCAmelCase__)
									self.assertListEqual(output_ids[0].numpy().tolist()     ,		lowerCAmelCase__)









import io
import json
import subprocess


lowerCAmelCase							:						Dict     			=		[
    ["""en""", """ru"""],
    ["""ru""", """en"""],
    ["""en""", """de"""],
    ["""de""", """en"""],
]

lowerCAmelCase							:						List[str]     			=		8







def     A_   (					_UpperCAmelCase       ,			_UpperCAmelCase					):
						SCREAMING_SNAKE_CASE_:				Dict							= {}
						for src, tgt in pairs:
												SCREAMING_SNAKE_CASE_:				Tuple							= f"{src}-{tgt}"
												SCREAMING_SNAKE_CASE_:				List[str]							= f"sacrebleu -t wmt19 -l {pair} --echo src".split()
												SCREAMING_SNAKE_CASE_:				int							= subprocess.run(_UpperCAmelCase       ,			stdout=subprocess.PIPE					).stdout.decode("utf-8"					).splitlines()
												SCREAMING_SNAKE_CASE_:				Optional[Any]							= f"sacrebleu -t wmt19 -l {pair} --echo ref".split()
												SCREAMING_SNAKE_CASE_:				Dict							= subprocess.run(_UpperCAmelCase       ,			stdout=subprocess.PIPE					).stdout.decode("utf-8"					).splitlines()
												SCREAMING_SNAKE_CASE_:				Tuple							= {"src": src_lines[:n_objs], "tgt": tgt_lines[:n_objs]}
						return text


lowerCAmelCase							:						Tuple     			=		get_all_data(pairs, n_objs)
lowerCAmelCase							:						Optional[Any]     			=		"""./fsmt_val_data.json"""
with io.open(filename, """w""", encoding="""utf-8""") as f:
						lowerCAmelCase							:						List[str]     			=		json.dump(text, f, indent=2, ensure_ascii=False)






