



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     :





							def __init__(				self						:						Optional[int] ,			__lowercase						:						int ,			):

											'''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						:						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						:						Union[str, Any] ,			__lowercase						:						List[Any] ,			__lowercase						:						Dict ,			__lowercase						:						Optional[Any] ,			__lowercase						:						str ,			__lowercase						:						Dict ,			*__lowercase						:						Union[str, Any]		):

											'''simple docstring'''
											__a					       =						TFOpenAIGPTModel(config=__snake_case		)
											__a					       =						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
											__a					       =						model(__snake_case		)

											__a					       =						[input_ids, input_mask]
											__a					       =						model(__snake_case		)

											__a					       =						model(__snake_case		)

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





							def   UpperCamelCase_   (				self						:						Optional[Any] ,			__lowercase						:						str ,			__lowercase						:						str ,			__lowercase						:						Union[str, Any] ,			__lowercase						:						Optional[int] ,			__lowercase						:						Tuple ,			*__lowercase						:						List[Any]		):

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





							def   UpperCamelCase_   (				self						:						Union[str, Any] ,			__lowercase						:						Union[str, Any] ,			__lowercase						:						str ,			__lowercase						:						str ,			__lowercase						:						Union[str, Any] ,			__lowercase						:						str ,			__lowercase						:						List[Any] ,			*__lowercase						:						Optional[Any]		):

											'''simple docstring'''
											__a					       =						TFOpenAIGPTDoubleHeadsModel(config=__snake_case		)

											__a					       =						tf.tile(tf.expand_dims(__snake_case ,			1		) ,			(1, self.num_choices, 1)		)
											__a					       =						tf.tile(tf.expand_dims(__snake_case ,			1		) ,			(1, self.num_choices, 1)		)
											__a					       =						tf.tile(tf.expand_dims(__snake_case ,			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(__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   UpperCamelCase_   (				self						:						str ,			__lowercase						:						Dict ,			__lowercase						:						Dict ,			__lowercase						:						Optional[Any] ,			__lowercase						:						Tuple ,			__lowercase						:						Any ,			*__lowercase						:						Optional[int]		):

											'''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(__snake_case		)
											__a					       =						model(__snake_case		)
											self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)		)







							def   UpperCamelCase_   (				self						:						int		):

											'''simple docstring'''
											__a					       =						self.prepare_config_and_inputs()

											(
											    __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     (							A__     , A__     , unittest.TestCase   ):
							__lowerCamelCase		:		List[str]      =(
							    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
							    if is_tf_available()
							    else ()
							)
							__lowerCamelCase		:		Tuple      =(
							    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							__lowerCamelCase		:		Union[str, Any]      =(
							    {
							        'feature-extraction': TFOpenAIGPTModel,
							        'text-classification': TFOpenAIGPTForSequenceClassification,
							        'text-generation': TFOpenAIGPTLMHeadModel,
							        'zero-shot': TFOpenAIGPTForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							__lowerCamelCase		:		Optional[int]      =False
							__lowerCamelCase		:		Union[str, Any]      =False





							def   UpperCamelCase_   (				self						:						Any ,			__lowercase						:						int ,			__lowercase						:						Tuple ,			__lowercase						:						Optional[int] ,			__lowercase						:						List[Any] ,			__lowercase						:						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   UpperCamelCase_   (				self						:						Tuple		):

											'''simple docstring'''
											__a					       =						TFOpenAIGPTModelTester(self		)
											__a					       =						ConfigTester(self ,			config_class=__snake_case ,			n_embd=37		)





							def   UpperCamelCase_   (				self						:						Optional[int]		):

											'''simple docstring'''
											self.config_tester.run_common_tests()





							def   UpperCamelCase_   (				self						:						Dict		):

											'''simple docstring'''
											__a					       =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*__snake_case		)





							def   UpperCamelCase_   (				self						:						Optional[Any]		):

											'''simple docstring'''
											__a					       =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_lm_head(*__snake_case		)





							def   UpperCamelCase_   (				self						:						Dict		):

											'''simple docstring'''
											__a					       =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case		)





							def   UpperCamelCase_   (				self						:						Optional[int]		):

											'''simple docstring'''
											__a					       =						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case		)







							@slow
							def   UpperCamelCase_   (				self						:						str		):

											'''simple docstring'''
											for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															__a					       =						TFOpenAIGPTModel.from_pretrained(__snake_case		)
															self.assertIsNotNone(__snake_case		)






@require_tf
class 				SCREAMING_SNAKE_CASE     (							unittest.TestCase   ):





							@slow
							def   UpperCamelCase_   (				self						:						Optional[Any]		):

											'''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(__snake_case ,			do_sample=__snake_case		)
											self.assertListEqual(output_ids[0].numpy().tolist() ,			__snake_case		)





from typing import TYPE_CHECKING

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


lowerCamelCase__     =	{
    """configuration_oneformer""": ["""ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """OneFormerConfig"""],
    """processing_oneformer""": ["""OneFormerProcessor"""],
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__     =	["""OneFormerImageProcessor"""]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__     =	[
						    """ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """OneFormerForUniversalSegmentation""",
						    """OneFormerModel""",
						    """OneFormerPreTrainedModel""",
						]

if TYPE_CHECKING:
						from .configuration_oneformer import ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, OneFormerConfig
						from .processing_oneformer import OneFormerProcessor

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_oneformer import OneFormerImageProcessor
						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_oneformer import (
												    ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    OneFormerForUniversalSegmentation,
												    OneFormerModel,
												    OneFormerPreTrainedModel,
												)


else:
						import sys

						lowerCamelCase__     =	_LazyModule(__name__, globals()["""__file__"""], _import_structure)
