
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					:




				def __init__(					self				,				_UpperCamelCase				,				_UpperCamelCase=13				,				_UpperCamelCase=7				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=99				,				_UpperCamelCase=32				,				_UpperCamelCase=5				,				_UpperCamelCase=4				,				_UpperCamelCase=37				,				_UpperCamelCase="gelu"				,				_UpperCamelCase=0.1				,				_UpperCamelCase=0.1				,				_UpperCamelCase=512				,				_UpperCamelCase=16				,				_UpperCamelCase=2				,				_UpperCamelCase=0.02				,				_UpperCamelCase=3				,				_UpperCamelCase=4				,				_UpperCamelCase=None				,				)				->      Dict:
							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      __a						(					self     )				->      List[str]:
							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      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				*_UpperCamelCase     )				->      Tuple:
							lowerCAmelCase_						=	OpenAIGPTModel(config=_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()

							lowerCAmelCase_						=	model(_UpperCamelCase				,				token_type_ids=_UpperCamelCase				,				head_mask=_UpperCamelCase     )
							lowerCAmelCase_						=	model(_UpperCamelCase				,				token_type_ids=_UpperCamelCase     )
							lowerCAmelCase_						=	model(_UpperCamelCase     )

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				*_UpperCamelCase     )				->      Union[str, Any]:
							lowerCAmelCase_						=	OpenAIGPTLMHeadModel(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				*_UpperCamelCase     )				->      Any:
							lowerCAmelCase_						=	OpenAIGPTDoubleHeadsModel(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				*_UpperCamelCase     )				->      Optional[Any]:
							lowerCAmelCase_						=	self.num_labels
							lowerCAmelCase_						=	OpenAIGPTForSequenceClassification(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()

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




				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	self.prepare_config_and_inputs()
							(
							    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					(	__SCREAMING_SNAKE_CASE					,      __SCREAMING_SNAKE_CASE					,      __SCREAMING_SNAKE_CASE					,      unittest.TestCase       ):
				_lowercase					=(
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				_lowercase					=(
				    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				_lowercase					=(
				    {
				        """feature-extraction""": OpenAIGPTModel,
				        """text-classification""": OpenAIGPTForSequenceClassification,
				        """text-generation""": OpenAIGPTLMHeadModel,
				        """zero-shot""": OpenAIGPTForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      List[Any]:
							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				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase=False     )				->      Union[str, Any]:
							lowerCAmelCase_						=	super()._prepare_for_class(_UpperCamelCase				,				_UpperCamelCase				,				return_labels=_UpperCamelCase     )

							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=_UpperCamelCase				,				)
													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=_UpperCamelCase				,				)
													lowerCAmelCase_						=	torch.zeros(
													    self.model_tester.batch_size				,				dtype=torch.long				,				device=_UpperCamelCase     )
							return inputs_dict




				def      __a						(					self     )				->      Union[str, Any]:
							lowerCAmelCase_						=	OpenAIGPTModelTester(self     )
							lowerCAmelCase_						=	ConfigTester(self				,				config_class=_UpperCamelCase				,				n_embd=37     )




				def      __a						(					self     )				->      Union[str, Any]:
							self.config_tester.run_common_tests()




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_double_lm_head_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_UpperCamelCase     )




				@slow
				def      __a						(					self     )				->      List[str]:
							for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase_						=	OpenAIGPTModel.from_pretrained(_UpperCamelCase     )
										self.assertIsNotNone(_UpperCamelCase     )





@require_torch
class 	_lowerCAmelCase					(	unittest.TestCase       ):




				@slow
				def      __a						(					self     )				->      str:
							lowerCAmelCase_						=	OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
							model.to(_UpperCamelCase     )
							lowerCAmelCase_						=	torch.tensor([[481, 4_735, 544]]				,				dtype=torch.long				,				device=_UpperCamelCase     )  # the president is
							lowerCAmelCase_						=	[
							    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

							lowerCAmelCase_						=	model.generate(_UpperCamelCase				,				do_sample=_UpperCamelCase     )
							self.assertListEqual(output_ids[0].tolist()				,				_UpperCamelCase     )





import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase					   =logging.get_logger(__name__)

_lowerCamelCase					   ={
    """BridgeTower/bridgetower-base""": """https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json""",
    """BridgeTower/bridgetower-base-itm-mlm""": (
        """https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json"""
    ),
}





class 							A__		(	__SCREAMING_SNAKE_CASE):
      _UpperCAmelCase				:    Dict           =			"""bridgetower_vision_model"""
      def __init__(      self    ,  __magic_name__=7_6_8    ,  __magic_name__=1_2    ,  __magic_name__=3    ,  __magic_name__=1_6    ,  __magic_name__=2_8_8    ,  __magic_name__=1    ,  __magic_name__=1e-05    ,  __magic_name__=False    ,  __magic_name__=True    ,  __magic_name__=False    ,  **__magic_name__    ,  ):
       super().__init__(**__magic_name__      )
       lowerCamelCase		:	Dict							=      hidden_size
       lowerCamelCase		:	str							=      num_hidden_layers
       lowerCamelCase		:	Optional[int]							=      num_channels
       lowerCamelCase		:	List[str]							=      patch_size
       lowerCamelCase		:	Tuple							=      image_size
       lowerCamelCase		:	Any							=      initializer_factor
       lowerCamelCase		:	Tuple							=      layer_norm_eps
       lowerCamelCase		:	Tuple							=      stop_gradient
       lowerCamelCase		:	Optional[int]							=      share_layernorm
       lowerCamelCase		:	str							=      remove_last_layer






      @classmethod
      def 						UpperCamelCase__							(      cls    ,  __magic_name__    ,  **__magic_name__      ):
       lowerCamelCase			,    lowerCamelCase		:	int							=      cls.get_config_dict(__magic_name__    ,  **__magic_name__      )

       if config_dict.get("""model_type"""      ) == "bridgetower":
        lowerCamelCase		:	str							=      config_dict["""text_config"""]

       if "model_type" in config_dict and hasattr(cls    ,  """model_type"""      ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
            F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''      )

       return cls.from_dict(__magic_name__    ,  **__magic_name__      )





class 							A__		(	__SCREAMING_SNAKE_CASE):
      _UpperCAmelCase				:    Union[str, Any]           =			"""bridgetower_text_model"""
      def __init__(      self    ,  __magic_name__=5_0_2_6_5    ,  __magic_name__=7_6_8    ,  __magic_name__=1_2    ,  __magic_name__=1_2    ,  __magic_name__=1    ,  __magic_name__=3_0_7_2    ,  __magic_name__="gelu"    ,  __magic_name__=0.1    ,  __magic_name__=0.1    ,  __magic_name__=5_1_4    ,  __magic_name__=1    ,  __magic_name__=1e-05    ,  __magic_name__=1    ,  __magic_name__=0    ,  __magic_name__=2    ,  __magic_name__="absolute"    ,  __magic_name__=True    ,  **__magic_name__    ,  ):
       super().__init__(**__magic_name__      )

       lowerCamelCase		:	int							=      vocab_size
       lowerCamelCase		:	int							=      hidden_size
       lowerCamelCase		:	Any							=      num_hidden_layers
       lowerCamelCase		:	Union[str, Any]							=      num_attention_heads
       lowerCamelCase		:	Tuple							=      hidden_act
       lowerCamelCase		:	Optional[int]							=      initializer_factor
       lowerCamelCase		:	Any							=      intermediate_size
       lowerCamelCase		:	List[str]							=      hidden_dropout_prob
       lowerCamelCase		:	Dict							=      attention_probs_dropout_prob
       lowerCamelCase		:	str							=      max_position_embeddings
       lowerCamelCase		:	Union[str, Any]							=      type_vocab_size
       lowerCamelCase		:	Optional[int]							=      layer_norm_eps
       lowerCamelCase		:	Optional[int]							=      position_embedding_type
       lowerCamelCase		:	List[str]							=      use_cache
       lowerCamelCase		:	List[str]							=      pad_token_id
       lowerCamelCase		:	List[str]							=      bos_token_id
       lowerCamelCase		:	Optional[int]							=      eos_token_id






      @classmethod
      def 						UpperCamelCase__							(      cls    ,  __magic_name__    ,  **__magic_name__      ):
       lowerCamelCase			,    lowerCamelCase		:	int							=      cls.get_config_dict(__magic_name__    ,  **__magic_name__      )

       if config_dict.get("""model_type"""      ) == "bridgetower":
        lowerCamelCase		:	Optional[int]							=      config_dict["""text_config"""]

       if "model_type" in config_dict and hasattr(cls    ,  """model_type"""      ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
            F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''      )

       return cls.from_dict(__magic_name__    ,  **__magic_name__      )





class 							A__		(	__SCREAMING_SNAKE_CASE):
      _UpperCAmelCase				:    Dict           =			"""bridgetower"""
      def __init__(      self    ,  __magic_name__=True    ,  __magic_name__="gelu"    ,  __magic_name__=7_6_8    ,  __magic_name__=1    ,  __magic_name__=1e-05    ,  __magic_name__=False    ,  __magic_name__="add"    ,  __magic_name__=1_2    ,  __magic_name__=6    ,  __magic_name__=False    ,  __magic_name__=False    ,  __magic_name__=None    ,  __magic_name__=None    ,  **__magic_name__    ,  ):
       # TODO: remove this once the Hub files are updated.
       lowerCamelCase		:	int							=      kwargs.pop("""text_config_dict"""    ,  __magic_name__      )
       lowerCamelCase		:	str							=      kwargs.pop("""vision_config_dict"""    ,  __magic_name__      )

       super().__init__(**__magic_name__      )
       lowerCamelCase		:	str							=      share_cross_modal_transformer_layers
       lowerCamelCase		:	Union[str, Any]							=      hidden_act
       lowerCamelCase		:	str							=      hidden_size
       lowerCamelCase		:	Tuple							=      initializer_factor
       lowerCamelCase		:	List[str]							=      layer_norm_eps
       lowerCamelCase		:	int							=      share_link_tower_layers
       lowerCamelCase		:	List[Any]							=      link_tower_type
       lowerCamelCase		:	Tuple							=      num_attention_heads
       lowerCamelCase		:	int							=      num_hidden_layers
       lowerCamelCase		:	Union[str, Any]							=      tie_word_embeddings
       lowerCamelCase		:	Tuple							=      init_layernorm_from_vision_encoder

       if text_config is None:
        lowerCamelCase		:	Any							=      {}
        logger.info("""`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values."""      )

       if vision_config is None:
        lowerCamelCase		:	int							=      {}
        logger.info("""`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values."""      )

       lowerCamelCase		:	Any							=      BridgeTowerTextConfig(**__magic_name__      )
       lowerCamelCase		:	Optional[Any]							=      BridgeTowerVisionConfig(**__magic_name__      )
      @classmethod
      def 						UpperCamelCase__							(      cls    ,  __magic_name__    ,  __magic_name__    ,  **__magic_name__      ):
       return cls(text_config=text_config.to_dict()    ,  vision_config=vision_config.to_dict()    ,  **__magic_name__      )






      def 						UpperCamelCase__							(      self      ):
       lowerCamelCase		:	str							=      copy.deepcopy(self.__dict__      )
       lowerCamelCase		:	int							=      self.text_config.to_dict()
       lowerCamelCase		:	Dict							=      self.vision_config.to_dict()
       lowerCamelCase		:	List[str]							=      self.__class__.model_type
       return output




