






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





     """simple docstring"""






     def __init__( self					:  Dict							,		__A					:  Union[str, Any]							,		__A					:  Union[str, Any]=1_3							,		__A					:  str=7							,		__A					:  List[Any]=True							,		__A					:  Dict=True							,		__A					:  int=True							,		__A					:  int=9_9							,		__A					:  Tuple=3_2							,		__A					:  int=5							,		__A					:  str=4							,		__A					:  List[Any]=3_7							,		__A					:  Optional[Any]="gelu"							,		__A					:  Union[str, Any]=0.1							,		__A					:  Union[str, Any]=0.1							,		__A					:  List[Any]=5_1_2							,		__A					:  Optional[Any]=1_6							,		__A					:  int=2							,		__A					:  int=0.0_2							,		__A					:  Tuple=3							,		__A					:  int=4							,		__A					:  Optional[int]=None							,		):
      snake_case__			:				Union[str, Any]       =  parent
      snake_case__			:				str       =  batch_size
      snake_case__			:				Any       =  seq_length
      snake_case__			:				int       =  is_training
      snake_case__			:				Any       =  use_token_type_ids
      snake_case__			:				Optional[int]       =  use_labels
      snake_case__			:				Optional[Any]       =  vocab_size
      snake_case__			:				Tuple       =  hidden_size
      snake_case__			:				Tuple       =  num_hidden_layers
      snake_case__			:				List[str]       =  num_attention_heads
      snake_case__			:				List[Any]       =  intermediate_size
      snake_case__			:				List[Any]       =  hidden_act
      snake_case__			:				Dict       =  hidden_dropout_prob
      snake_case__			:				int       =  attention_probs_dropout_prob
      snake_case__			:				Tuple       =  max_position_embeddings
      snake_case__			:				int       =  type_vocab_size
      snake_case__			:				int       =  type_sequence_label_size
      snake_case__			:				Dict       =  initializer_range
      snake_case__			:				Tuple       =  num_labels
      snake_case__			:				int       =  num_choices
      snake_case__			:				List[Any]       =  scope
      snake_case__			:				Optional[int]       =  self.vocab_size - 1



     def 	_lowercase					( self					:  Optional[Any]	):
      snake_case__			:				int       =  ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size	)

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

      snake_case__			:				str       =  None
      snake_case__			:				List[Any]       =  None
      snake_case__			:				Optional[int]       =  None
      if self.use_labels:
       snake_case__			:				Tuple       =  ids_tensor([self.batch_size]							,		self.type_sequence_label_size	)
       snake_case__			:				Any       =  ids_tensor([self.batch_size, self.seq_length]							,		self.num_labels	)
       snake_case__			:				Optional[Any]       =  ids_tensor([self.batch_size]							,		self.num_choices	)

      snake_case__			:				int       =  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__			:				List[Any]       =  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 	_lowercase					( self					:  str							,		__A					:  Optional[Any]							,		__A					:  Union[str, Any]							,		__A					:  str							,		__A					:  Tuple							,		*__A					:  str	):
      snake_case__			:				Optional[Any]       =  OpenAIGPTModel(config=__A	)
      model.to(__A	)
      model.eval()

      snake_case__			:				Tuple       =  model(__A							,		token_type_ids=__A							,		head_mask=__A	)
      snake_case__			:				Optional[int]       =  model(__A							,		token_type_ids=__A	)
      snake_case__			:				List[Any]       =  model(__A	)

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



     def 	_lowercase					( self					:  Any							,		__A					:  Any							,		__A					:  Tuple							,		__A					:  List[Any]							,		__A					:  Dict							,		*__A					:  Tuple	):
      snake_case__			:				int       =  OpenAIGPTLMHeadModel(__A	)
      model.to(__A	)
      model.eval()

      snake_case__			:				Any       =  model(__A							,		token_type_ids=__A							,		labels=__A	)
      self.parent.assertEqual(result.loss.shape							,		()	)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)	)



     def 	_lowercase					( self					:  int							,		__A					:  Dict							,		__A					:  int							,		__A					:  Any							,		__A					:  Dict							,		*__A					:  Optional[Any]	):
      snake_case__			:				int       =  OpenAIGPTDoubleHeadsModel(__A	)
      model.to(__A	)
      model.eval()

      snake_case__			:				int       =  model(__A							,		token_type_ids=__A							,		labels=__A	)
      self.parent.assertEqual(result.loss.shape							,		()	)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)	)



     def 	_lowercase					( self					:  Union[str, Any]							,		__A					:  List[str]							,		__A					:  List[Any]							,		__A					:  Union[str, Any]							,		__A					:  Any							,		*__A					:  List[str]	):
      snake_case__			:				Any       =  self.num_labels
      snake_case__			:				Tuple       =  OpenAIGPTForSequenceClassification(__A	)
      model.to(__A	)
      model.eval()

      snake_case__			:				Optional[Any]       =  ids_tensor([self.batch_size]							,		self.type_sequence_label_size	)
      snake_case__			:				Optional[int]       =  model(__A							,		token_type_ids=__A							,		labels=__A	)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)	)






     def 	_lowercase					( self					:  int	):
      snake_case__			:				Optional[Any]       =  self.prepare_config_and_inputs()
      (
          snake_case__						
      )			:				Union[str, Any]       =  config_and_inputs
      snake_case__			:				Any       =  {
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "head_mask": head_mask,
      }

      return config, inputs_dict



@require_torch
class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_			,       UpperCamelCase_			,       UpperCamelCase_			,       unittest.TestCase						):





     """simple docstring"""






     a_     =		(
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     a_     =		(
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     a_     =		(
         {
             "feature-extraction": OpenAIGPTModel,
             "text-classification": OpenAIGPTForSequenceClassification,
             "text-generation": OpenAIGPTLMHeadModel,
             "zero-shot": OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )



     def 	_lowercase					( self					:  int							,		__A					:  Optional[int]							,		__A					:  Dict							,		__A					:  Union[str, Any]							,		__A					:  int							,		__A					:  int	):
      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 	_lowercase					( self					:  str							,		__A					:  Tuple							,		__A					:  Tuple							,		__A					:  Dict=False	):
      snake_case__			:				Union[str, Any]       =  super()._prepare_for_class(__A							,		__A							,		return_labels=__A	)

      if return_labels:
       if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
        snake_case__			:				int       =  torch.zeros(
            (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)							,		dtype=torch.long							,		device=__A							,		)
        snake_case__			:				Optional[int]       =  inputs_dict["labels"]
        snake_case__			:				Tuple       =  inputs_dict["labels"]
        snake_case__			:				Optional[int]       =  torch.zeros(
            (self.model_tester.batch_size, self.model_tester.num_choices)							,		dtype=torch.long							,		device=__A							,		)
        snake_case__			:				Union[str, Any]       =  torch.zeros(
            self.model_tester.batch_size							,		dtype=torch.long							,		device=__A	)
      return inputs_dict



     def 	_lowercase					( self					:  Optional[int]	):
      snake_case__			:				Optional[int]       =  OpenAIGPTModelTester(self	)
      snake_case__			:				List[Any]       =  ConfigTester(self							,		config_class=__A							,		n_embd=3_7	)



     def 	_lowercase					( self					:  Tuple	):
      self.config_tester.run_common_tests()



     def 	_lowercase					( self					:  Any	):
      snake_case__			:				List[Any]       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*__A	)



     def 	_lowercase					( self					:  Dict	):
      snake_case__			:				int       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*__A	)



     def 	_lowercase					( self					:  List[Any]	):
      snake_case__			:				str       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*__A	)



     def 	_lowercase					( self					:  Union[str, Any]	):
      snake_case__			:				int       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__A	)






     @slow
     def 	_lowercase					( self					:  List[Any]	):
      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       snake_case__			:				int       =  OpenAIGPTModel.from_pretrained(__A	)
       self.assertIsNotNone(__A	)



@require_torch
class 							SCREAMING_SNAKE_CASE__						(    unittest.TestCase						):





     """simple docstring"""






     @slow
     def 	_lowercase					( self					:  int	):
      snake_case__			:				List[str]       =  OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"	)
      model.to(__A	)
      snake_case__			:				List[str]       =  torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]							,		dtype=torch.long							,		device=__A	)  # the president is
      snake_case__			:				Tuple       =  [
          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

      snake_case__			:				Any       =  model.generate(__A							,		do_sample=__A	)
      self.assertListEqual(output_ids[0].tolist()							,		__A	)











from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCamelCase :      Any										=				{
    """configuration_instructblip""": [
        """INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """InstructBlipConfig""",
        """InstructBlipQFormerConfig""",
        """InstructBlipVisionConfig""",
    ],
    """processing_instructblip""": ["""InstructBlipProcessor"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase :      List[Any]										=				[
	    """INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """InstructBlipQFormerModel""",
	    """InstructBlipPreTrainedModel""",
	    """InstructBlipForConditionalGeneration""",
	    """InstructBlipVisionModel""",
	]

if TYPE_CHECKING:
	from .configuration_instructblip import (
	    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    InstructBlipConfig,
	    InstructBlipQFormerConfig,
	    InstructBlipVisionConfig,
	)
	from .processing_instructblip import InstructBlipProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_instructblip import (
		    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    InstructBlipForConditionalGeneration,
		    InstructBlipPreTrainedModel,
		    InstructBlipQFormerModel,
		    InstructBlipVisionModel,
		)

else:
	import sys

	__lowerCamelCase :      Any										=				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



