



"""simple docstring"""
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
			    OpenAIGPTConfig,
			    OpenAIGPTDoubleHeadsModel,
			    OpenAIGPTForSequenceClassification,
			    OpenAIGPTLMHeadModel,
			    OpenAIGPTModel,
			)


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=1_3				,       lowerCamelCase__=7				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=9_9				,       lowerCamelCase__=3_2				,       lowerCamelCase__=5				,       lowerCamelCase__=4				,       lowerCamelCase__=3_7				,       lowerCamelCase__="gelu"				,       lowerCamelCase__=0.1				,       lowerCamelCase__=0.1				,       lowerCamelCase__=5_1_2				,       lowerCamelCase__=1_6				,       lowerCamelCase__=2				,       lowerCamelCase__=0.0_2				,       lowerCamelCase__=3				,       lowerCamelCase__=4				,       lowerCamelCase__=None				,       ):
									_lowerCamelCase        =				parent
									_lowerCamelCase        =				batch_size
									_lowerCamelCase        =				seq_length
									_lowerCamelCase        =				is_training
									_lowerCamelCase        =				use_token_type_ids
									_lowerCamelCase        =				use_labels
									_lowerCamelCase        =				vocab_size
									_lowerCamelCase        =				hidden_size
									_lowerCamelCase        =				num_hidden_layers
									_lowerCamelCase        =				num_attention_heads
									_lowerCamelCase        =				intermediate_size
									_lowerCamelCase        =				hidden_act
									_lowerCamelCase        =				hidden_dropout_prob
									_lowerCamelCase        =				attention_probs_dropout_prob
									_lowerCamelCase        =				max_position_embeddings
									_lowerCamelCase        =				type_vocab_size
									_lowerCamelCase        =				type_sequence_label_size
									_lowerCamelCase        =				initializer_range
									_lowerCamelCase        =				num_labels
									_lowerCamelCase        =				num_choices
									_lowerCamelCase        =				scope
									_lowerCamelCase        =				self.vocab_size - 1

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size   )

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

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

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

									_lowerCamelCase        =				ids_tensor([self.num_hidden_layers, self.num_attention_heads]				,       2   )

									return (
									    config,
									    input_ids,
									    head_mask,
									    token_type_ids,
									    sequence_labels,
									    token_labels,
									    choice_labels,
									)

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       *lowerCamelCase__   ):
									_lowerCamelCase        =				OpenAIGPTModel(config=lowerCamelCase__   )
									model.to(lowerCamelCase__   )
									model.eval()

									_lowerCamelCase        =				model(lowerCamelCase__				,       token_type_ids=lowerCamelCase__				,       head_mask=lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       token_type_ids=lowerCamelCase__   )
									_lowerCamelCase        =				model(lowerCamelCase__   )

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

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       *lowerCamelCase__   ):
									_lowerCamelCase        =				OpenAIGPTLMHeadModel(lowerCamelCase__   )
									model.to(lowerCamelCase__   )
									model.eval()

									_lowerCamelCase        =				model(lowerCamelCase__				,       token_type_ids=lowerCamelCase__				,       labels=lowerCamelCase__   )
									self.parent.assertEqual(result.loss.shape				,       ()   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       *lowerCamelCase__   ):
									_lowerCamelCase        =				OpenAIGPTDoubleHeadsModel(lowerCamelCase__   )
									model.to(lowerCamelCase__   )
									model.eval()

									_lowerCamelCase        =				model(lowerCamelCase__				,       token_type_ids=lowerCamelCase__				,       labels=lowerCamelCase__   )
									self.parent.assertEqual(result.loss.shape				,       ()   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)   )

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       *lowerCamelCase__   ):
									_lowerCamelCase        =				self.num_labels
									_lowerCamelCase        =				OpenAIGPTForSequenceClassification(lowerCamelCase__   )
									model.to(lowerCamelCase__   )
									model.eval()

									_lowerCamelCase        =				ids_tensor([self.batch_size]				,       self.type_sequence_label_size   )
									_lowerCamelCase        =				model(lowerCamelCase__				,       token_type_ids=lowerCamelCase__				,       labels=lowerCamelCase__   )
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)   )





					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.prepare_config_and_inputs()
									(
									    (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     (
									    _lowerCamelCase
									)  ,     
									)        =				config_and_inputs
									_lowerCamelCase        =				{
									    '''input_ids''': input_ids,
									    '''token_type_ids''': token_type_ids,
									    '''head_mask''': head_mask,
									}

									return config, inputs_dict


@require_torch
class 							lowerCamelCase_(   A__,				A__,				A__,				unittest.TestCase    ):





					'''simple docstring'''



					lowercase__   :       Union[str, Any]										= (
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					lowercase__   :       List[Any]										= (
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					lowercase__   :       Union[str, Any]										= (
					    {
					        'feature-extraction': OpenAIGPTModel,
					        'text-classification': OpenAIGPTForSequenceClassification,
					        'text-generation': OpenAIGPTLMHeadModel,
					        'zero-shot': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					def 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__   ):
									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 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__=False   ):
									_lowerCamelCase        =				super()._prepare_for_class(lowerCamelCase__				,       lowerCamelCase__				,       return_labels=lowerCamelCase__   )

									if return_labels:
													if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																	_lowerCamelCase        =				torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)				,       dtype=torch.long				,       device=lowerCamelCase__				,       )
																	_lowerCamelCase        =				inputs_dict['''labels''']
																	_lowerCamelCase        =				inputs_dict['''labels''']
																	_lowerCamelCase        =				torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.num_choices)				,       dtype=torch.long				,       device=lowerCamelCase__				,       )
																	_lowerCamelCase        =				torch.zeros(
																	    self.model_tester.batch_size				,       dtype=torch.long				,       device=lowerCamelCase__   )
									return inputs_dict

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				OpenAIGPTModelTester(self   )
									_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__				,       n_embd=3_7   )

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

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__   )





					@slow
					def 			snake_case__      (    self   ):
									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowerCamelCase        =				OpenAIGPTModel.from_pretrained(lowerCamelCase__   )
													self.assertIsNotNone(lowerCamelCase__   )


@require_torch
class 							lowerCamelCase_(   unittest.TestCase    ):





					'''simple docstring'''



					@slow
					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
									model.to(lowerCamelCase__   )
									_lowerCamelCase        =				torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]				,       dtype=torch.long				,       device=lowerCamelCase__   )  # the president is
									_lowerCamelCase        =				[
									    4_8_1,
									    4_7_3_5,
									    5_4_4,
									    2_4_6,
									    9_6_3,
									    8_7_0,
									    7_6_2,
									    2_3_9,
									    2_4_4,
									    4_0_4_7_7,
									    2_4_4,
									    2_4_9,
									    7_1_9,
									    8_8_1,
									    4_8_7,
									    5_4_4,
									    2_4_0,
									    2_4_4,
									    6_0_3,
									    4_8_1,
									]  # the president is a very good man. " \n " i\'m sure he is, " said the

									_lowerCamelCase        =				model.generate(lowerCamelCase__				,       do_sample=lowerCamelCase__   )
									self.assertListEqual(output_ids[0].tolist()				,       lowerCamelCase__   )





"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE  :						Optional[Any]    							=      {
    '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig''']
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						str    							=      ['''VisionEncoderDecoderModel''']

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						Dict    							=      ['''TFVisionEncoderDecoderModel''']

try:
			if not is_flax_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						List[Any]    							=      ['''FlaxVisionEncoderDecoderModel''']

if TYPE_CHECKING:
			from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel

			try:
						if not is_flax_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel

else:
			import sys

			__SCREAMING_SNAKE_CASE  :						Union[str, Any]    							=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
