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






			def __init__(			self							, __UpperCamelCase							, ):
									_lowercase									=				parent
									_lowercase									=				13
									_lowercase									=				7
									_lowercase									=				True
									_lowercase									=				True
									_lowercase									=				True
									_lowercase									=				True
									_lowercase									=				True
									_lowercase									=				99
									_lowercase									=				32
									_lowercase									=				2
									_lowercase									=				4
									_lowercase									=				37
									_lowercase									=				"""gelu"""
									_lowercase									=				0.1
									_lowercase									=				0.1
									_lowercase									=				512
									_lowercase									=				16
									_lowercase									=				2
									_lowercase									=				0.02
									_lowercase									=				3
									_lowercase									=				4
									_lowercase									=				None
									_lowercase									=				self.vocab_size - 1






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size     )

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

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

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

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

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

									_lowercase									=				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							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, *__UpperCamelCase     ):
									_lowercase									=				TFOpenAIGPTModel(config=__UpperCamelCase     )
									_lowercase									=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
									_lowercase									=				model(__UpperCamelCase     )

									_lowercase									=				[input_ids, input_mask]
									_lowercase									=				model(__UpperCamelCase     )

									_lowercase									=				model(__UpperCamelCase     )

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






			def       UpperCamelCase_							(			self							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, *__UpperCamelCase     ):
									_lowercase									=				TFOpenAIGPTLMHeadModel(config=__UpperCamelCase     )
									_lowercase									=				{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
									_lowercase									=				model(__UpperCamelCase     )
									self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)     )






			def       UpperCamelCase_							(			self							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, *__UpperCamelCase     ):
									_lowercase									=				TFOpenAIGPTDoubleHeadsModel(config=__UpperCamelCase     )

									_lowercase									=				tf.tile(tf.expand_dims(__UpperCamelCase							, 1     )							, (1, self.num_choices, 1)     )
									_lowercase									=				tf.tile(tf.expand_dims(__UpperCamelCase							, 1     )							, (1, self.num_choices, 1)     )
									_lowercase									=				tf.tile(tf.expand_dims(__UpperCamelCase							, 1     )							, (1, self.num_choices, 1)     )

									_lowercase									=				{
									    """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,
									}
									_lowercase									=				model(__UpperCamelCase     )
									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							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, *__UpperCamelCase     ):
									_lowercase									=				self.num_labels
									_lowercase									=				ids_tensor([self.batch_size]							, self.type_sequence_label_size     )
									_lowercase									=				{
									    """input_ids""": input_ids,
									    """attention_mask""": input_mask,
									    """token_type_ids""": token_type_ids,
									    """labels""": sequence_labels,
									}
									_lowercase									=				TFOpenAIGPTForSequenceClassification(__UpperCamelCase     )
									_lowercase									=				model(__UpperCamelCase     )
									self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)     )






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.prepare_config_and_inputs()

									(
									    (
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	(
									    _lowercase
									)					,	
									)									=				config_and_inputs

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







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






			def       UpperCamelCase_							(			self							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase							, __UpperCamelCase     ):
									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     ):
									_lowercase									=				TFOpenAIGPTModelTester(self     )
									_lowercase									=				ConfigTester(self							, config_class=__UpperCamelCase							, n_embd=37     )






			def       UpperCamelCase_							(			self     ):
									self.config_tester.run_common_tests()






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase     )






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCamelCase     )






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCamelCase     )






			def       UpperCamelCase_							(			self     ):
									_lowercase									=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase     )






			@slow
			def       UpperCamelCase_							(			self     ):
									for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_lowercase									=				TFOpenAIGPTModel.from_pretrained(__UpperCamelCase     )
															self.assertIsNotNone(__UpperCamelCase     )







@require_tf
class       a_   ( unittest.TestCase						):






			@slow
			def       UpperCamelCase_							(			self     ):
									_lowercase									=				TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""     )
									_lowercase									=				tf.convert_to_tensor([[481, 4_735, 544]]							, dtype=tf.intaa     )  # the president is
									_lowercase									=				[
									    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

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

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


A		:   int         =    {'''configuration_vilt''': ['''VILT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViltConfig''']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   int         =    ['''ViltFeatureExtractor''']
		A		:   List[Any]         =    ['''ViltImageProcessor''']
		A		:   Dict         =    ['''ViltProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   Tuple         =    [
		    '''VILT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ViltForImageAndTextRetrieval''',
		    '''ViltForImagesAndTextClassification''',
		    '''ViltForTokenClassification''',
		    '''ViltForMaskedLM''',
		    '''ViltForQuestionAnswering''',
		    '''ViltLayer''',
		    '''ViltModel''',
		    '''ViltPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_vilt import ViltFeatureExtractor
				from .image_processing_vilt import ViltImageProcessor
				from .processing_vilt import ViltProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vilt import (
				    VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViltForImageAndTextRetrieval,
				    ViltForImagesAndTextClassification,
				    ViltForMaskedLM,
				    ViltForQuestionAnswering,
				    ViltForTokenClassification,
				    ViltLayer,
				    ViltModel,
				    ViltPreTrainedModel,
				)


else:
		import sys

		A		:   Dict         =    _LazyModule(__name__, globals()['''__file__'''], _import_structure)