
'''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			:					List[str]    ,						lowercase			:					int    ,						):


				'''simple docstring'''


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



	def 				A							(						self			:					Optional[int]			):


				'''simple docstring'''


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

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

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

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

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

				_snake_case						=    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    ,						)

				_snake_case						=    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 				A							(						self			:					Tuple    ,						lowercase			:					Union[str, Any]    ,						lowercase			:					int    ,						lowercase			:					List[str]    ,						lowercase			:					int    ,						lowercase			:					str    ,						*lowercase			:					Optional[Any]			):


				'''simple docstring'''


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

				_snake_case						=    [input_ids, input_mask]
				_snake_case						=    model(_snake_case			)

				_snake_case						=    model(_snake_case			)

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



	def 				A							(						self			:					List[str]    ,						lowercase			:					Any    ,						lowercase			:					Optional[int]    ,						lowercase			:					str    ,						lowercase			:					Any    ,						lowercase			:					List[str]    ,						*lowercase			:					List[Any]			):


				'''simple docstring'''


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



	def 				A							(						self			:					Dict    ,						lowercase			:					Dict    ,						lowercase			:					str    ,						lowercase			:					Optional[int]    ,						lowercase			:					List[str]    ,						lowercase			:					str    ,						lowercase			:					str    ,						*lowercase			:					Tuple			):


				'''simple docstring'''


				_snake_case						=    TFOpenAIGPTDoubleHeadsModel(config=_snake_case			)

				_snake_case						=    tf.tile(tf.expand_dims(_snake_case    ,						1			)    ,						(1, self.num_choices, 1)			)
				_snake_case						=    tf.tile(tf.expand_dims(_snake_case    ,						1			)    ,						(1, self.num_choices, 1)			)
				_snake_case						=    tf.tile(tf.expand_dims(_snake_case    ,						1			)    ,						(1, self.num_choices, 1)			)

				_snake_case						=    {
				    '''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,
				}
				_snake_case						=    model(_snake_case			)
				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 				A							(						self			:					Any    ,						lowercase			:					Optional[Any]    ,						lowercase			:					str    ,						lowercase			:					Any    ,						lowercase			:					Union[str, Any]    ,						lowercase			:					List[Any]    ,						*lowercase			:					str			):


				'''simple docstring'''


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





	def 				A							(						self			:					Dict			):


				'''simple docstring'''


				_snake_case						=    self.prepare_config_and_inputs()

				(
				    _snake_case							
				)						=    config_and_inputs

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




@require_tf
class      SCREAMING_SNAKE_CASE__  (			a_							,a_							,unittest.TestCase      ):





	'''simple docstring'''



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



	def 				A							(						self			:					Union[str, Any]    ,						lowercase			:					int    ,						lowercase			:					List[str]    ,						lowercase			:					Optional[int]    ,						lowercase			:					Optional[Any]    ,						lowercase			:					Optional[int]			):


				'''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 				A							(						self			:					str			):


				'''simple docstring'''


				_snake_case						=    TFOpenAIGPTModelTester(self			)
				_snake_case						=    ConfigTester(self    ,						config_class=_snake_case    ,						n_embd=37			)



	def 				A							(						self			:					Optional[Any]			):


				'''simple docstring'''


				self.config_tester.run_common_tests()



	def 				A							(						self			:					str			):


				'''simple docstring'''


				_snake_case						=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_model(*_snake_case			)



	def 				A							(						self			:					Union[str, Any]			):


				'''simple docstring'''


				_snake_case						=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_lm_head(*_snake_case			)



	def 				A							(						self			:					str			):


				'''simple docstring'''


				_snake_case						=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_double_head(*_snake_case			)



	def 				A							(						self			:					Dict			):


				'''simple docstring'''


				_snake_case						=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case			)





	@slow
	def 				A							(						self			:					Tuple			):


				'''simple docstring'''


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




@require_tf
class      SCREAMING_SNAKE_CASE__  (			unittest.TestCase      ):





	'''simple docstring'''



	@slow
	def 				A							(						self			:					int			):


				'''simple docstring'''


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

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

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class      SCREAMING_SNAKE_CASE__  (			UpperCAmelCase      ):





	'''simple docstring'''



	_UpperCAmelCase :	int					     =      ["image_processor", "tokenizer"]
	_UpperCAmelCase :	Tuple					     =      "EfficientNetImageProcessor"
	_UpperCAmelCase :	Optional[Any]					     =      ("BertTokenizer", "BertTokenizerFast")



	def __init__(						self			:					Tuple    ,						lowercase			:					List[Any]    ,						lowercase			:					Optional[int]			):


				'''simple docstring'''


				super().__init__(lowercase    ,						lowercase			)



	def __call__(						self			:					Union[str, Any]    ,						lowercase			:					Dict=None    ,						lowercase			:					Optional[int]=None    ,						lowercase			:					List[Any]="max_length"    ,						lowercase			:					Optional[int]=64    ,						lowercase			:					int=None    ,						**lowercase			:					Tuple			):


				'''simple docstring'''


				if text is None and images is None:
							raise ValueError('You have to specify either text or images. Both cannot be none.'			)

				if text is not None:
							_snake_case						=    self.tokenizer(
							    lowercase    ,						padding=lowercase    ,						max_length=lowercase    ,						return_tensors=lowercase    ,						**lowercase			)

				if images is not None:
							_snake_case						=    self.image_processor(lowercase    ,						return_tensors=lowercase    ,						**lowercase			)

				if text is not None and images is not None:
							_snake_case						=    image_features.pixel_values
							return encoding
				elif text is not None:
							return encoding
				else:
							return BatchEncoding(data=dict(**lowercase			)    ,						tensor_type=lowercase			)



	def 				A							(						self			:					Any    ,						*lowercase			:					List[str]    ,						**lowercase			:					Optional[Any]			):


				'''simple docstring'''


				return self.tokenizer.batch_decode(*lowercase    ,						**lowercase			)



	def 				A							(						self			:					List[Any]    ,						*lowercase			:					Union[str, Any]    ,						**lowercase			:					Tuple			):


				'''simple docstring'''


				return self.tokenizer.decode(*lowercase    ,						**lowercase			)





	@property
	def 				A							(						self			:					List[str]			):


				'''simple docstring'''


				_snake_case						=    self.tokenizer.model_input_names
				_snake_case						=    self.image_processor.model_input_names
				return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names			)			)