
"""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 SCREAMING_SNAKE_CASE       :






	'''simple docstring'''







	def __init__(  self :Dict       ,    __magic_name__ :Dict       ,    )			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     parent
			a__   		=     13
			a__   		=     7
			a__   		=     True
			a__   		=     True
			a__   		=     True
			a__   		=     True
			a__   		=     True
			a__   		=     99
			a__   		=     32
			a__   		=     2
			a__   		=     4
			a__   		=     37
			a__   		=     '''gelu'''
			a__   		=     0.1
			a__   		=     0.1
			a__   		=     512
			a__   		=     16
			a__   		=     2
			a__   		=     0.02
			a__   		=     3
			a__   		=     4
			a__   		=     None
			a__   		=     self.vocab_size - 1


	def    _UpperCamelCase							(  self :Dict			)			->   Optional[int]:

			'''simple docstring'''






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

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

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

			a__   		=     None
			if self.use_mc_token_ids:
					a__   		=     ids_tensor([self.batch_size, self.num_choices]       ,    self.seq_length			)

			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,
			    input_mask,
			    head_mask,
			    token_type_ids,
			    mc_token_ids,
			    sequence_labels,
			    token_labels,
			    choice_labels,
			)


	def    _UpperCamelCase							(  self :Optional[int]       ,    __magic_name__ :Union[str, Any]       ,    __magic_name__ :Union[str, Any]       ,    __magic_name__ :Optional[Any]       ,    __magic_name__ :str       ,    __magic_name__ :Any       ,    *__magic_name__ :str			)			->   str:

			'''simple docstring'''






			a__   		=     TFOpenAIGPTModel(config=__magic_name__			)
			a__   		=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
			a__   		=     model(__magic_name__			)

			a__   		=     [input_ids, input_mask]
			a__   		=     model(__magic_name__			)

			a__   		=     model(__magic_name__			)

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


	def    _UpperCamelCase							(  self :Tuple       ,    __magic_name__ :str       ,    __magic_name__ :Optional[int]       ,    __magic_name__ :Optional[Any]       ,    __magic_name__ :List[Any]       ,    __magic_name__ :Union[str, Any]       ,    *__magic_name__ :str			)			->   List[Any]:

			'''simple docstring'''






			a__   		=     TFOpenAIGPTLMHeadModel(config=__magic_name__			)
			a__   		=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
			a__   		=     model(__magic_name__			)
			self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size)			)


	def    _UpperCamelCase							(  self :str       ,    __magic_name__ :Dict       ,    __magic_name__ :Optional[int]       ,    __magic_name__ :int       ,    __magic_name__ :Any       ,    __magic_name__ :List[Any]       ,    __magic_name__ :Optional[int]       ,    *__magic_name__ :Dict			)			->   int:

			'''simple docstring'''






			a__   		=     TFOpenAIGPTDoubleHeadsModel(config=__magic_name__			)

			a__   		=     tf.tile(tf.expand_dims(__magic_name__       ,    1			)       ,    (1, self.num_choices, 1)			)
			a__   		=     tf.tile(tf.expand_dims(__magic_name__       ,    1			)       ,    (1, self.num_choices, 1)			)
			a__   		=     tf.tile(tf.expand_dims(__magic_name__       ,    1			)       ,    (1, self.num_choices, 1)			)

			a__   		=     {
			    '''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,
			}
			a__   		=     model(__magic_name__			)
			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    _UpperCamelCase							(  self :Optional[Any]       ,    __magic_name__ :Any       ,    __magic_name__ :Tuple       ,    __magic_name__ :List[str]       ,    __magic_name__ :str       ,    __magic_name__ :str       ,    *__magic_name__ :int			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     self.num_labels
			a__   		=     ids_tensor([self.batch_size]       ,    self.type_sequence_label_size			)
			a__   		=     {
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			    '''token_type_ids''': token_type_ids,
			    '''labels''': sequence_labels,
			}
			a__   		=     TFOpenAIGPTForSequenceClassification(__magic_name__			)
			a__   		=     model(__magic_name__			)
			self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_labels)			)






	def    _UpperCamelCase							(  self :Optional[int]			)			->   Any:

			'''simple docstring'''






			a__   		=     self.prepare_config_and_inputs()

			(
			    (
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	(
			    a__
			)		,	
			)   		=     config_and_inputs

			a__   		=     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
			return config, inputs_dict

@require_tf
class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					,   lowerCAmelCase					,   unittest.TestCase					):






	'''simple docstring'''







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


	def    _UpperCamelCase							(  self :List[str]       ,    __magic_name__ :List[Any]       ,    __magic_name__ :Optional[int]       ,    __magic_name__ :List[str]       ,    __magic_name__ :int       ,    __magic_name__ :int			)			->   Optional[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    _UpperCamelCase							(  self :Union[str, Any]			)			->   Optional[int]:

			'''simple docstring'''






			a__   		=     TFOpenAIGPTModelTester(self			)
			a__   		=     ConfigTester(self       ,    config_class=__magic_name__       ,    n_embd=37			)


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

			'''simple docstring'''






			self.config_tester.run_common_tests()


	def    _UpperCamelCase							(  self :List[Any]			)			->   Any:

			'''simple docstring'''






			a__   		=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_model(*__magic_name__			)


	def    _UpperCamelCase							(  self :str			)			->   str:

			'''simple docstring'''






			a__   		=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_lm_head(*__magic_name__			)


	def    _UpperCamelCase							(  self :Optional[Any]			)			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_double_head(*__magic_name__			)


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

			'''simple docstring'''






			a__   		=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__magic_name__			)






	@slow
	def    _UpperCamelCase							(  self :Dict			)			->   str:

			'''simple docstring'''






			for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					a__   		=     TFOpenAIGPTModel.from_pretrained(__magic_name__			)
					self.assertIsNotNone(__magic_name__			)

@require_tf
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	@slow
	def    _UpperCamelCase							(  self :List[str]			)			->   Dict:

			'''simple docstring'''






			a__   		=     TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''			)
			a__   		=     tf.convert_to_tensor([[481, 4735, 544]]       ,    dtype=tf.intaa			)  # the president is
			a__   		=     [
			    481,
			    4735,
			    544,
			    246,
			    963,
			    870,
			    762,
			    239,
			    244,
			    40477,
			    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

			a__   		=     model.generate(__magic_name__       ,    do_sample=__magic_name__			)
			self.assertListEqual(output_ids[0].numpy().tolist()       ,    __magic_name__			)






"""simple docstring"""




import argparse
import logging
import sys
from unittest.mock import patch

import run_glue_with_pabee

from transformers.testing_utils import TestCasePlus


logging.basicConfig(level=logging.DEBUG)

__lowerCAmelCase							:  Dict 				=		logging.getLogger()

def __snake_case (      )       ->    Optional[int]:


		"""simple docstring"""





		a__   		=     argparse.ArgumentParser()
		parser.add_argument('''-f'''    )
		a__   		=     parser.parse_args()
		return args.f




class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					):






	'''simple docstring'''







	def    _UpperCamelCase							(  self :Tuple			)			->   Union[str, Any]:

			'''simple docstring'''






			a__   		=     logging.StreamHandler(sys.stdout			)
			logger.addHandler(__magic_name__			)

			a__   		=     self.get_auto_remove_tmp_dir()
			a__   		=     F"\n            run_glue_with_pabee.py\n            --model_type albert\n            --model_name_or_path albert-base-v2\n            --data_dir ./tests/fixtures/tests_samples/MRPC/\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --task_name mrpc\n            --do_train\n            --do_eval\n            --per_gpu_train_batch_size=2\n            --per_gpu_eval_batch_size=1\n            --learning_rate=2e-5\n            --max_steps=50\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

			with patch.object(__magic_name__       ,    '''argv'''       ,    __magic_name__			):
					a__   		=     run_glue_with_pabee.main()
					for value in result.values():
							self.assertGreaterEqual(__magic_name__       ,    0.75			)




