


'''simple docstring'''


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 						lowerCAmelCase__      :




			def __init__(		self	:  int						,	lowerCamelCase__	:  Dict						,	) ->int:



					'''simple docstring'''







					_UpperCAmelCase		:							Union[str, Any]             =   parent
					_UpperCAmelCase		:							List[Any]             =   13
					_UpperCAmelCase		:							Dict             =   7
					_UpperCAmelCase		:							str             =   True
					_UpperCAmelCase		:							Any             =   True
					_UpperCAmelCase		:							Optional[int]             =   True
					_UpperCAmelCase		:							str             =   True
					_UpperCAmelCase		:							Union[str, Any]             =   True
					_UpperCAmelCase		:							Optional[int]             =   99
					_UpperCAmelCase		:							List[Any]             =   32
					_UpperCAmelCase		:							List[Any]             =   2
					_UpperCAmelCase		:							List[str]             =   4
					_UpperCAmelCase		:							List[Any]             =   37
					_UpperCAmelCase		:							Union[str, Any]             =   "gelu"
					_UpperCAmelCase		:							Dict             =   0.1
					_UpperCAmelCase		:							Optional[Any]             =   0.1
					_UpperCAmelCase		:							List[Any]             =   5_12
					_UpperCAmelCase		:							int             =   16
					_UpperCAmelCase		:							Any             =   2
					_UpperCAmelCase		:							List[Any]             =   0.0_2
					_UpperCAmelCase		:							Dict             =   3
					_UpperCAmelCase		:							int             =   4
					_UpperCAmelCase		:							Optional[Any]             =   None
					_UpperCAmelCase		:							Any             =   self.vocab_size - 1




			def 				lowerCAmelCase__     (		self	:  str			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							Union[str, Any]             =   ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size			)

					_UpperCAmelCase		:							str             =   None
					if self.use_input_mask:
							_UpperCAmelCase		:							Optional[Any]             =   random_attention_mask([self.batch_size, self.seq_length]			)

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

					_UpperCAmelCase		:							Dict             =   None
					if self.use_mc_token_ids:
							_UpperCAmelCase		:							List[str]             =   ids_tensor([self.batch_size, self.num_choices]						,	self.seq_length			)

					_UpperCAmelCase		:							Any             =   None
					_UpperCAmelCase		:							List[Any]             =   None
					_UpperCAmelCase		:							str             =   None
					if self.use_labels:
							_UpperCAmelCase		:							int             =   ids_tensor([self.batch_size]						,	self.type_sequence_label_size			)
							_UpperCAmelCase		:							List[Any]             =   ids_tensor([self.batch_size, self.seq_length]						,	self.num_labels			)
							_UpperCAmelCase		:							List[Any]             =   ids_tensor([self.batch_size]						,	self.num_choices			)

					_UpperCAmelCase		:							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						,	)

					_UpperCAmelCase		:							Union[str, Any]             =   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 				lowerCAmelCase__     (		self	:  Tuple						,	lowerCamelCase__	:  int						,	lowerCamelCase__	:  Tuple						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  int						,	*lowerCamelCase__	:  Dict			) ->int:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   TFOpenAIGPTModel(config=lowerCamelCase__			)
					_UpperCAmelCase		:							int             =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					_UpperCAmelCase		:							Optional[Any]             =   model(lowerCamelCase__			)

					_UpperCAmelCase		:							str             =   [input_ids, input_mask]
					_UpperCAmelCase		:							List[Any]             =   model(lowerCamelCase__			)

					_UpperCAmelCase		:							List[Any]             =   model(lowerCamelCase__			)

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




			def 				lowerCAmelCase__     (		self	:  List[str]						,	lowerCamelCase__	:  Optional[Any]						,	lowerCamelCase__	:  Dict						,	lowerCamelCase__	:  Tuple						,	lowerCamelCase__	:  int						,	lowerCamelCase__	:  List[Any]						,	*lowerCamelCase__	:  int			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   TFOpenAIGPTLMHeadModel(config=lowerCamelCase__			)
					_UpperCAmelCase		:							List[Any]             =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					_UpperCAmelCase		:							str             =   model(lowerCamelCase__			)
					self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)			)




			def 				lowerCAmelCase__     (		self	:  Any						,	lowerCamelCase__	:  Tuple						,	lowerCamelCase__	:  Tuple						,	lowerCamelCase__	:  str						,	lowerCamelCase__	:  Optional[Any]						,	lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  Tuple						,	*lowerCamelCase__	:  Dict			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   TFOpenAIGPTDoubleHeadsModel(config=lowerCamelCase__			)

					_UpperCAmelCase		:							List[str]             =   tf.tile(tf.expand_dims(lowerCamelCase__						,	1			)						,	(1, self.num_choices, 1)			)
					_UpperCAmelCase		:							Union[str, Any]             =   tf.tile(tf.expand_dims(lowerCamelCase__						,	1			)						,	(1, self.num_choices, 1)			)
					_UpperCAmelCase		:							Union[str, Any]             =   tf.tile(tf.expand_dims(lowerCamelCase__						,	1			)						,	(1, self.num_choices, 1)			)

					_UpperCAmelCase		:							List[Any]             =   {
					    "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,
					}
					_UpperCAmelCase		:							Tuple             =   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 				lowerCAmelCase__     (		self	:  List[Any]						,	lowerCamelCase__	:  Any						,	lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Tuple						,	lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  List[str]						,	*lowerCamelCase__	:  Dict			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   self.num_labels
					_UpperCAmelCase		:							Any             =   ids_tensor([self.batch_size]						,	self.type_sequence_label_size			)
					_UpperCAmelCase		:							List[Any]             =   {
					    "input_ids": input_ids,
					    "attention_mask": input_mask,
					    "token_type_ids": token_type_ids,
					    "labels": sequence_labels,
					}
					_UpperCAmelCase		:							str             =   TFOpenAIGPTForSequenceClassification(lowerCamelCase__			)
					_UpperCAmelCase		:							int             =   model(lowerCamelCase__			)
					self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)			)






			def 				lowerCAmelCase__     (		self	:  List[Any]			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   self.prepare_config_and_inputs()

					(
					    (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     
					)		:							List[Any]             =   config_and_inputs

					_UpperCAmelCase		:							Dict             =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict






@require_tf
class 						lowerCAmelCase__      (      UpperCAmelCase__     ,							UpperCAmelCase__     ,							unittest.TestCase						):
			lowerCAmelCase			:						Optional[Any]							 =  (
			    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
			    if is_tf_available()
			    else ()
			)
			lowerCAmelCase			:						Union[str, Any]							 =  (
			    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			lowerCAmelCase			:						int							 =  (
			    {
			        "feature-extraction": TFOpenAIGPTModel,
			        "text-classification": TFOpenAIGPTForSequenceClassification,
			        "text-generation": TFOpenAIGPTLMHeadModel,
			        "zero-shot": TFOpenAIGPTForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			lowerCAmelCase			:						int							 =  False
			lowerCAmelCase			:						int							 =  False




			def 				lowerCAmelCase__     (		self	:  Dict						,	lowerCamelCase__	:  int						,	lowerCamelCase__	:  Any						,	lowerCamelCase__	:  Union[str, Any]						,	lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Optional[Any]			) ->List[Any]:



					'''simple docstring'''







					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	:  List[Any]			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							str             =   TFOpenAIGPTModelTester(self			)
					_UpperCAmelCase		:							List[Any]             =   ConfigTester(self						,	config_class=lowerCamelCase__						,	n_embd=37			)




			def 				lowerCAmelCase__     (		self	:  Dict			) ->int:



					'''simple docstring'''







					self.config_tester.run_common_tests()




			def 				lowerCAmelCase__     (		self	:  Optional[int]			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  List[Any]			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  Optional[int]			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Union[str, Any]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_double_head(*lowerCamelCase__			)




			def 				lowerCAmelCase__     (		self	:  str			) ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__			)






			@slow
			def 				lowerCAmelCase__     (		self	:  Union[str, Any]			) ->Any:



					'''simple docstring'''







					for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_UpperCAmelCase		:							Optional[Any]             =   TFOpenAIGPTModel.from_pretrained(lowerCamelCase__			)
							self.assertIsNotNone(lowerCamelCase__			)






@require_tf
class 						lowerCAmelCase__      (      unittest.TestCase						):




			@slow
			def 				lowerCAmelCase__     (		self	:  int			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							Union[str, Any]             =   TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"			)
					_UpperCAmelCase		:							Union[str, Any]             =   tf.convert_to_tensor([[4_81, 47_35, 5_44]]						,	dtype=tf.intaa			)  # the president is
					_UpperCAmelCase		:							Any             =   [
					    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

					_UpperCAmelCase		:							Any             =   model.generate(lowerCamelCase__						,	do_sample=lowerCamelCase__			)
					self.assertListEqual(output_ids[0].numpy().tolist()						,	lowerCamelCase__			)







'''simple docstring'''


import io
import json
import subprocess


lowerCamelCase__     						=  [
    ['en', 'ru'],
    ['ru', 'en'],
    ['en', 'de'],
    ['de', 'en'],
]

lowerCamelCase__     						=  8






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


lowerCamelCase__     						=  get_all_data(pairs, n_objs)
lowerCamelCase__     						=  './fsmt_val_data.json'
with io.open(filename, 'w', encoding='utf-8') as f:
						lowerCamelCase__     						=  json.dump(text, f, indent=2, ensure_ascii=False)



