

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

			"""simple docstring"""

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






			def    _lowerCAmelCase			(							self     :					Tuple						):
								UpperCamelCase__		:    Tuple         =ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size						)

								UpperCamelCase__		:    str         =None
								if self.use_input_mask:
													UpperCamelCase__		:    Tuple         =random_attention_mask([self.batch_size, self.seq_length]						)

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

								UpperCamelCase__		:    str         =None
								if self.use_mc_token_ids:
													UpperCamelCase__		:    Optional[Any]         =ids_tensor([self.batch_size, self.num_choices]						,  self.seq_length						)

								UpperCamelCase__		:    List[str]         =None
								UpperCamelCase__		:    Optional[int]         =None
								UpperCamelCase__		:    List[str]         =None
								if self.use_labels:
													UpperCamelCase__		:    str         =ids_tensor([self.batch_size]						,  self.type_sequence_label_size						)
													UpperCamelCase__		:    Union[str, Any]         =ids_tensor([self.batch_size, self.seq_length]						,  self.num_labels						)
													UpperCamelCase__		:    Dict         =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     :					int						,  lowercase_     :					int						,  lowercase_     :					int						,  lowercase_     :					Union[str, Any]						,  lowercase_     :					int						,  lowercase_     :					Optional[Any]						,  *lowercase_     :					Any						):
								UpperCamelCase__		:    Optional[Any]         =TFOpenAIGPTModel(config=__a						)
								UpperCamelCase__		:    Dict         ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								UpperCamelCase__		:    int         =model(__a						)

								UpperCamelCase__		:    int         =[input_ids, input_mask]
								UpperCamelCase__		:    List[Any]         =model(__a						)

								UpperCamelCase__		:    Tuple         =model(__a						)

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






			def    _lowerCAmelCase			(							self     :					List[Any]						,  lowercase_     :					Optional[int]						,  lowercase_     :					Optional[Any]						,  lowercase_     :					List[str]						,  lowercase_     :					Union[str, Any]						,  lowercase_     :					Union[str, Any]						,  *lowercase_     :					Optional[Any]						):
								UpperCamelCase__		:    Dict         =TFOpenAIGPTLMHeadModel(config=__a						)
								UpperCamelCase__		:    Tuple         ={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								UpperCamelCase__		:    int         =model(__a						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.vocab_size)						)






			def    _lowerCAmelCase			(							self     :					Optional[int]						,  lowercase_     :					Any						,  lowercase_     :					Dict						,  lowercase_     :					Optional[int]						,  lowercase_     :					int						,  lowercase_     :					str						,  lowercase_     :					Union[str, Any]						,  *lowercase_     :					int						):
								UpperCamelCase__		:    Optional[Any]         =TFOpenAIGPTDoubleHeadsModel(config=__a						)

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

								UpperCamelCase__		:    Union[str, 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__		:    int         =model(__a						)
								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     :					str						,  lowercase_     :					Tuple						,  lowercase_     :					Tuple						,  lowercase_     :					List[str]						,  lowercase_     :					List[Any]						,  lowercase_     :					Any						,  *lowercase_     :					str						):
								UpperCamelCase__		:    Tuple         =self.num_labels
								UpperCamelCase__		:    int         =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__		:    Optional[int]         =TFOpenAIGPTForSequenceClassification(__a						)
								UpperCamelCase__		:    List[str]         =model(__a						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_labels)						)






			def    _lowerCAmelCase			(							self     :					Tuple						):
								UpperCamelCase__		:    List[str]         =self.prepare_config_and_inputs()

								(
								    UpperCamelCase__       
								)		:    Optional[int]         =config_and_inputs

								UpperCamelCase__		:    Union[str, Any]         ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict



@require_tf
class     __a   (      lowerCAmelCase_,    lowerCAmelCase_,    unittest.TestCase    ):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_						     =						(
			    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
			    if is_tf_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE_						     =						(
			    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			SCREAMING_SNAKE_CASE_						     =						(
			    {
			        'feature-extraction': TFOpenAIGPTModel,
			        'text-classification': TFOpenAIGPTForSequenceClassification,
			        'text-generation': TFOpenAIGPTLMHeadModel,
			        'zero-shot': TFOpenAIGPTForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			SCREAMING_SNAKE_CASE_						     =						False
			SCREAMING_SNAKE_CASE_						     =						False






			def    _lowerCAmelCase			(							self     :					Optional[int]						,  lowercase_     :					Tuple						,  lowercase_     :					Optional[Any]						,  lowercase_     :					List[Any]						,  lowercase_     :					int						,  lowercase_     :					Optional[Any]						):





								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     :					str						):
								UpperCamelCase__		:    Optional[Any]         =TFOpenAIGPTModelTester(self						)
								UpperCamelCase__		:    Optional[Any]         =ConfigTester(self						,  config_class=__a						,  n_embd=37						)






			def    _lowerCAmelCase			(							self     :					List[str]						):
								self.config_tester.run_common_tests()






			def    _lowerCAmelCase			(							self     :					int						):
								UpperCamelCase__		:    Union[str, Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*__a						)






			def    _lowerCAmelCase			(							self     :					List[Any]						):
								UpperCamelCase__		:    Tuple         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_lm_head(*__a						)






			def    _lowerCAmelCase			(							self     :					Union[str, Any]						):
								UpperCamelCase__		:    List[str]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_double_head(*__a						)






			def    _lowerCAmelCase			(							self     :					Union[str, Any]						):
								UpperCamelCase__		:    str         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__a						)






			@slow
			def    _lowerCAmelCase			(							self     :					List[Any]						):





								for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCamelCase__		:    List[str]         =TFOpenAIGPTModel.from_pretrained(__a						)
													self.assertIsNotNone(__a						)



@require_tf
class     __a   (      unittest.TestCase    ):

			"""simple docstring"""

			@slow
			def    _lowerCAmelCase			(							self     :					Tuple						):
								UpperCamelCase__		:    Optional[int]         =TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''						)
								UpperCamelCase__		:    str         =tf.convert_to_tensor([[481, 4735, 544]]						,  dtype=tf.intaa						)  # the president is
								UpperCamelCase__		:    Dict         =[
								    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

								UpperCamelCase__		:    List[Any]         =model.generate(__a						,  do_sample=__a						)
								self.assertListEqual(output_ids[0].numpy().tolist()						,  __a						)





"""simple docstring"""





import io
import json
import subprocess


_SCREAMING_SNAKE_CASE       :  List[Any]        = [
    ["""en""", """ru"""],
    ["""ru""", """en"""],
    ["""en""", """de"""],
    ["""de""", """en"""],
]

_SCREAMING_SNAKE_CASE       :  str        = 8



def        _lowerCAmelCase      (						UpperCAmelCase     :							Tuple      ,							UpperCAmelCase     :							List[Any]				):


					'''simple docstring'''






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


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


