

import copy
import os
from typing import Union

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


lowerCAmelCase__		      =       logging.get_logger(__name__)

lowerCAmelCase__		      =       {
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}

class     a__  (    snake_case	):
			"""simple docstring"""







			__lowerCamelCase       				=				'altclip_text_model'


			def __init__(			self					,					lowercase=250002					,					lowercase=1024					,					lowercase=24					,					lowercase=16					,					lowercase=4096					,					lowercase="gelu"					,					lowercase=0.1					,					lowercase=0.1					,					lowercase=514					,					lowercase=1					,					lowercase=0.02					,					lowercase=0.02					,					lowercase=1e-05					,					lowercase=1					,					lowercase=0					,					lowercase=2					,					lowercase="absolute"					,					lowercase=True					,					lowercase=768					,					**lowercase					,					)     ->  Optional[Any]:
							'''simple docstring'''






							super().__init__(pad_token_id=lowercase					,					bos_token_id=lowercase					,					eos_token_id=lowercase					,					**lowercase				)

							A__						=	vocab_size
							A__						=	hidden_size
							A__						=	num_hidden_layers
							A__						=	num_attention_heads
							A__						=	hidden_act
							A__						=	intermediate_size
							A__						=	hidden_dropout_prob
							A__						=	attention_probs_dropout_prob
							A__						=	max_position_embeddings
							A__						=	type_vocab_size
							A__						=	initializer_range
							A__						=	initializer_factor
							A__						=	layer_norm_eps
							A__						=	position_embedding_type
							A__						=	use_cache
							A__						=	project_dim

class     a__  (    snake_case	):
			"""simple docstring"""







			__lowerCamelCase       				=				'altclip_vision_model'


			def __init__(			self					,					lowercase=768					,					lowercase=3072					,					lowercase=512					,					lowercase=12					,					lowercase=12					,					lowercase=3					,					lowercase=224					,					lowercase=32					,					lowercase="quick_gelu"					,					lowercase=1e-5					,					lowercase=0.0					,					lowercase=0.02					,					lowercase=1.0					,					**lowercase					,					)     ->  Tuple:
							'''simple docstring'''






							super().__init__(**lowercase				)

							A__						=	hidden_size
							A__						=	intermediate_size
							A__						=	projection_dim
							A__						=	num_hidden_layers
							A__						=	num_attention_heads
							A__						=	num_channels
							A__						=	patch_size
							A__						=	image_size
							A__						=	initializer_range
							A__						=	initializer_factor
							A__						=	attention_dropout
							A__						=	layer_norm_eps
							A__						=	hidden_act


			@classmethod
			def    UpperCamelCase						(			cls					,					lowercase					,					**lowercase				)     ->  "PretrainedConfig":
							'''simple docstring'''






							cls._set_token_in_kwargs(lowercase				)

							A__			, A__						=	cls.get_config_dict(lowercase					,					**lowercase				)

							# get the vision config dict if we are loading from AltCLIPConfig
							if config_dict.get("model_type"				) == "altclip":
											A__						=	config_dict["vision_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(lowercase					,					**lowercase				)

class     a__  (    snake_case	):
			"""simple docstring"""







			__lowerCamelCase       				=				'altclip'
			__lowerCamelCase       				=				True


			def __init__(			self					,					lowercase=None					,					lowercase=None					,					lowercase=768					,					lowercase=2.6592					,					**lowercase				)     ->  List[str]:
							'''simple docstring'''






							A__						=	kwargs.pop("text_config_dict"					,					lowercase				)
							A__						=	kwargs.pop("vision_config_dict"					,					lowercase				)

							super().__init__(**lowercase				)

							# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
							# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
							# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
							if text_config_dict is not None:
											if text_config is None:
															A__						=	{}

											# This is the complete result when using `text_config_dict`.
											A__						=	AltCLIPTextConfig(**lowercase				).to_dict()

											# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
											for key, value in _text_config_dict.items():
															if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																			# If specified in `text_config_dict`
																			if key in text_config_dict:
																							A__						=	(
																							    F'`{key}` is found in both `text_config_dict` and `text_config` but with different values. '
																							    F'The value `text_config_dict["{key}"]` will be used instead.'
																							)
																			# If inferred from default argument values (just to be super careful)
																			else:
																							A__						=	(
																							    F'`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '
																							    F'value `text_config["{key}"]` will be overriden.'
																							)
																			logger.warning(lowercase				)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
											text_config.update(_text_config_dict				)

							if vision_config_dict is not None:
											if vision_config is None:
															A__						=	{}

											# This is the complete result when using `vision_config_dict`.
											A__						=	AltCLIPVisionConfig(**lowercase				).to_dict()
											# convert keys to string instead of integer
											if "id2label" in _vision_config_dict:
															A__						=	{
															    str(lowercase				): value for key, value in _vision_config_dict["id2label"].items()
															}

											# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
											for key, value in _vision_config_dict.items():
															if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																			# If specified in `vision_config_dict`
																			if key in vision_config_dict:
																							A__						=	(
																							    F'`{key}` is found in both `vision_config_dict` and `vision_config` but with different '
																							    F'values. The value `vision_config_dict["{key}"]` will be used instead.'
																							)
																			# If inferred from default argument values (just to be super careful)
																			else:
																							A__						=	(
																							    F'`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '
																							    F'The value `vision_config["{key}"]` will be overriden.'
																							)
																			logger.warning(lowercase				)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
											vision_config.update(_vision_config_dict				)

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

							if vision_config is None:
											A__						=	{}
											logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."				)

							A__						=	AltCLIPTextConfig(**lowercase				)
							A__						=	AltCLIPVisionConfig(**lowercase				)

							A__						=	projection_dim
							A__						=	logit_scale_init_value
							A__						=	1.0


			@classmethod
			def    UpperCamelCase						(			cls					,					lowercase					,					lowercase					,					**lowercase				)     ->  Union[str, Any]:
							'''simple docstring'''






							return cls(text_config=text_config.to_dict()					,					vision_config=vision_config.to_dict()					,					**lowercase				)


			def    UpperCamelCase						(			self				)     ->  Tuple:
							'''simple docstring'''






							A__						=	copy.deepcopy(self.__dict__				)
							A__						=	self.text_config.to_dict()
							A__						=	self.vision_config.to_dict()
							A__						=	self.__class__.model_type
							return output



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     :



	def __init__(    self						,  lowercase						,  lowercase=13						,  lowercase=7						,  lowercase=True						,  lowercase=True						,  lowercase=True						,  lowercase=99						,  lowercase=32						,  lowercase=5						,  lowercase=4						,  lowercase=37						,  lowercase="gelu"						,  lowercase=0.1						,  lowercase=0.1						,  lowercase=512						,  lowercase=16						,  lowercase=2						,  lowercase=0.0_2						,  lowercase=3						,  lowercase=4						,  lowercase=None						,  )				->       List[str]:
					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     SCREAMING_SNAKE_CASE_(    self )				->       Any:
					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     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  *lowercase )				->       Dict:
					lowerCamelCase_						=			OpenAIGPTModel(config=lowercase )
					model.to(lowercase )
					model.eval()

					lowerCamelCase_						=			model(lowercase						,  token_type_ids=lowercase						,  head_mask=lowercase )
					lowerCamelCase_						=			model(lowercase						,  token_type_ids=lowercase )
					lowerCamelCase_						=			model(lowercase )

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



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  *lowercase )				->       int:
					lowerCamelCase_						=			OpenAIGPTLMHeadModel(lowercase )
					model.to(lowercase )
					model.eval()

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



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  *lowercase )				->       Dict:
					lowerCamelCase_						=			OpenAIGPTDoubleHeadsModel(lowercase )
					model.to(lowercase )
					model.eval()

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



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  *lowercase )				->       int:
					lowerCamelCase_						=			self.num_labels
					lowerCamelCase_						=			OpenAIGPTForSequenceClassification(lowercase )
					model.to(lowercase )
					model.eval()

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, Any]:
					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     _SCREAMING_SNAKE_CASE     (      snake_case_			,       snake_case_			,       snake_case_			,       unittest.TestCase						):
	lowerCAmelCase__         = (
	    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
	    if is_torch_available()
	    else ()
	)
	lowerCAmelCase__         = (
	    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	lowerCAmelCase__         = (
	    {
	        'feature-extraction': OpenAIGPTModel,
	        'text-classification': OpenAIGPTForSequenceClassification,
	        'text-generation': OpenAIGPTLMHeadModel,
	        'zero-shot': OpenAIGPTForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase )				->       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     SCREAMING_SNAKE_CASE_(    self						,  lowercase						,  lowercase						,  lowercase=False )				->       Any:
					lowerCamelCase_						=			super()._prepare_for_class(lowercase						,  lowercase						,  return_labels=lowercase )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Dict:
					lowerCamelCase_						=			OpenAIGPTModelTester(self )
					lowerCamelCase_						=			ConfigTester(self						,  config_class=lowercase						,  n_embd=37 )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Any:
					self.config_tester.run_common_tests()



	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, Any]:
					lowerCamelCase_						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*lowercase )



	def     SCREAMING_SNAKE_CASE_(    self )				->       str:
					lowerCamelCase_						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_lm_head_model(*lowercase )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Tuple:
					lowerCamelCase_						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_double_lm_head_model(*lowercase )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Optional[Any]:
					lowerCamelCase_						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase )



	@slow
	def     SCREAMING_SNAKE_CASE_(    self )				->       Any:
					for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase_						=			OpenAIGPTModel.from_pretrained(lowercase )
									self.assertIsNotNone(lowercase )




@require_torch
class     _SCREAMING_SNAKE_CASE     (      unittest.TestCase						):



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

					lowerCamelCase_						=			model.generate(lowercase						,  do_sample=lowercase )
					self.assertListEqual(output_ids[0].tolist()						,  lowercase )


