


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

							def __init__(     self:     str   ,						UpperCamelCase_:     Any   ,						UpperCamelCase_:     Dict=13   ,						UpperCamelCase_:     Union[str, Any]=7   ,						UpperCamelCase_:     Optional[int]=True   ,						UpperCamelCase_:     str=True   ,						UpperCamelCase_:     str=True   ,						UpperCamelCase_:     Dict=99   ,						UpperCamelCase_:     List[str]=32   ,						UpperCamelCase_:     Tuple=5   ,						UpperCamelCase_:     str=4   ,						UpperCamelCase_:     Optional[Any]=37   ,						UpperCamelCase_:     List[Any]="gelu"   ,						UpperCamelCase_:     List[str]=0.1   ,						UpperCamelCase_:     str=0.1   ,						UpperCamelCase_:     Tuple=512   ,						UpperCamelCase_:     List[str]=16   ,						UpperCamelCase_:     Any=2   ,						UpperCamelCase_:     Union[str, Any]=0.02   ,						UpperCamelCase_:     List[str]=3   ,						UpperCamelCase_:     List[str]=4   ,						UpperCamelCase_:     List[Any]=None   ,						)      ->       Optional[Any]:





									"""simple docstring"""







									lowercase__						=				parent
									lowercase__						=				batch_size
									lowercase__						=				seq_length
									lowercase__						=				is_training
									lowercase__						=				use_token_type_ids
									lowercase__						=				use_labels
									lowercase__						=				vocab_size
									lowercase__						=				hidden_size
									lowercase__						=				num_hidden_layers
									lowercase__						=				num_attention_heads
									lowercase__						=				intermediate_size
									lowercase__						=				hidden_act
									lowercase__						=				hidden_dropout_prob
									lowercase__						=				attention_probs_dropout_prob
									lowercase__						=				max_position_embeddings
									lowercase__						=				type_vocab_size
									lowercase__						=				type_sequence_label_size
									lowercase__						=				initializer_range
									lowercase__						=				num_labels
									lowercase__						=				num_choices
									lowercase__						=				scope
									lowercase__						=				self.vocab_size - 1

							def 				lowerCamelCase_			(     self:     Tuple							)      ->       Optional[int]:





									"""simple docstring"""







									lowercase__						=				ids_tensor([self.batch_size, self.seq_length]   ,						self.vocab_size							)

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

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

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

									lowercase__						=				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 				lowerCamelCase_			(     self:     List[Any]   ,						UpperCamelCase_:     Optional[Any]   ,						UpperCamelCase_:     Tuple   ,						UpperCamelCase_:     Dict   ,						UpperCamelCase_:     Tuple   ,						*UpperCamelCase_:     str							)      ->       Union[str, Any]:





									"""simple docstring"""







									lowercase__						=				OpenAIGPTModel(config=_a							)
									model.to(_a							)
									model.eval()

									lowercase__						=				model(_a   ,						token_type_ids=_a   ,						head_mask=_a							)
									lowercase__						=				model(_a   ,						token_type_ids=_a							)
									lowercase__						=				model(_a							)

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

							def 				lowerCamelCase_			(     self:     Optional[int]   ,						UpperCamelCase_:     Any   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     Optional[Any]   ,						UpperCamelCase_:     str   ,						*UpperCamelCase_:     Optional[Any]							)      ->       Optional[int]:





									"""simple docstring"""







									lowercase__						=				OpenAIGPTLMHeadModel(_a							)
									model.to(_a							)
									model.eval()

									lowercase__						=				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 				lowerCamelCase_			(     self:     Optional[int]   ,						UpperCamelCase_:     List[Any]   ,						UpperCamelCase_:     str   ,						UpperCamelCase_:     Union[str, Any]   ,						UpperCamelCase_:     Optional[int]   ,						*UpperCamelCase_:     int							)      ->       int:





									"""simple docstring"""







									lowercase__						=				OpenAIGPTDoubleHeadsModel(_a							)
									model.to(_a							)
									model.eval()

									lowercase__						=				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 				lowerCamelCase_			(     self:     Union[str, Any]   ,						UpperCamelCase_:     str   ,						UpperCamelCase_:     Tuple   ,						UpperCamelCase_:     Union[str, Any]   ,						UpperCamelCase_:     Optional[Any]   ,						*UpperCamelCase_:     List[Any]							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				self.num_labels
									lowercase__						=				OpenAIGPTForSequenceClassification(_a							)
									model.to(_a							)
									model.eval()

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


							def 				lowerCamelCase_			(     self:     Optional[Any]							)      ->       List[Any]:





									"""simple docstring"""







									lowercase__						=				self.prepare_config_and_inputs()
									(
									    lowercase__						
									)						=				config_and_inputs
									lowercase__						=				{
									    """input_ids""": input_ids,
									    """token_type_ids""": token_type_ids,
									    """head_mask""": head_mask,
									}

									return config, inputs_dict


@require_torch
class 	_a							(      UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     unittest.TestCase      ):
							_lowercase     :      List[str]  				=			(
							    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
							    if is_torch_available()
							    else ()
							)
							_lowercase     :      str  				=			(
							    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							_lowercase     :      List[Any]  				=			(
							    {
							        """feature-extraction""": OpenAIGPTModel,
							        """text-classification""": OpenAIGPTForSequenceClassification,
							        """text-generation""": OpenAIGPTLMHeadModel,
							        """zero-shot""": OpenAIGPTForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							def 				lowerCamelCase_			(     self:     int   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     int   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     Optional[Any]   ,						UpperCamelCase_:     Any							)      ->       Union[str, 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 				lowerCamelCase_			(     self:     Union[str, Any]   ,						UpperCamelCase_:     int   ,						UpperCamelCase_:     str   ,						UpperCamelCase_:     List[str]=False							)      ->       str:





									"""simple docstring"""







									lowercase__						=				super()._prepare_for_class(_a   ,						_a   ,						return_labels=_a							)

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

							def 				lowerCamelCase_			(     self:     Any							)      ->       str:





									"""simple docstring"""







									lowercase__						=				OpenAIGPTModelTester(self							)
									lowercase__						=				ConfigTester(self   ,						config_class=_a   ,						n_embd=37							)

							def 				lowerCamelCase_			(     self:     Any							)      ->       Optional[int]:





									"""simple docstring"""







									self.config_tester.run_common_tests()

							def 				lowerCamelCase_			(     self:     Union[str, Any]							)      ->       Dict:





									"""simple docstring"""







									lowercase__						=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*_a							)

							def 				lowerCamelCase_			(     self:     Dict							)      ->       int:





									"""simple docstring"""







									lowercase__						=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*_a							)

							def 				lowerCamelCase_			(     self:     Any							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*_a							)

							def 				lowerCamelCase_			(     self:     int							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_a							)


							@slow
							def 				lowerCamelCase_			(     self:     int							)      ->       Optional[int]:





									"""simple docstring"""







									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__						=				OpenAIGPTModel.from_pretrained(_a							)
											self.assertIsNotNone(_a							)


@require_torch
class 	_a							(      unittest.TestCase      ):

							@slow
							def 				lowerCamelCase_			(     self:     Dict							)      ->       List[Any]:





									"""simple docstring"""







									lowercase__						=				OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''							)
									model.to(_a							)
									lowercase__						=				torch.tensor([[481, 4_735, 544]]   ,						dtype=torch.long   ,						device=_a							)  # the president is
									lowercase__						=				[
									    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

									lowercase__						=				model.generate(_a   ,						do_sample=_a							)
									self.assertListEqual(output_ids[0].tolist()   ,						_a							)






import copy
import os
from typing import Union

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


lowerCAmelCase	=							logging.get_logger(__name__)

lowerCAmelCase	=							{
    'microsoft/git-base': 'https://huggingface.co/microsoft/git-base/resolve/main/config.json',
}


class 	_a							(      UpperCamelCase__      ):
							_lowercase     :      int  				=			'''git_vision_model'''

							def __init__(							self:     Tuple   ,						UpperCamelCase_:     Optional[int]=768   ,						UpperCamelCase_:     Optional[int]=3_072   ,						UpperCamelCase_:     Optional[int]=12   ,						UpperCamelCase_:     Union[str, Any]=12   ,						UpperCamelCase_:     Any=3   ,						UpperCamelCase_:     str=224   ,						UpperCamelCase_:     int=16   ,						UpperCamelCase_:     Any="quick_gelu"   ,						UpperCamelCase_:     Union[str, Any]=1E-5   ,						UpperCamelCase_:     Tuple=0.0   ,						UpperCamelCase_:     Tuple=0.02   ,						**UpperCamelCase_:     str   ,						)      ->       Optional[int]:





									"""simple docstring"""







									super().__init__(**UpperCamelCase_							)

									lowercase__						=				hidden_size
									lowercase__						=				intermediate_size
									lowercase__						=				num_hidden_layers
									lowercase__						=				num_attention_heads
									lowercase__						=				num_channels
									lowercase__						=				patch_size
									lowercase__						=				image_size
									lowercase__						=				initializer_range
									lowercase__						=				attention_dropout
									lowercase__						=				layer_norm_eps
									lowercase__						=				hidden_act


							@classmethod
							def 				lowerCamelCase_			(							cls:     Optional[Any]   ,						UpperCamelCase_:     Union[str, os.PathLike]   ,						**UpperCamelCase_:     List[Any]							)      ->       "PretrainedConfig":





									"""simple docstring"""







									cls._set_token_in_kwargs(UpperCamelCase_							)

									lowercase__					,		lowercase__						=				cls.get_config_dict(UpperCamelCase_   ,						**UpperCamelCase_							)

									# get the vision config dict if we are loading from GITConfig
									if config_dict.get('''model_type'''							) == "git":
											lowercase__						=				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(UpperCamelCase_   ,						**UpperCamelCase_							)


class 	_a							(      UpperCamelCase__      ):
							_lowercase     :      Optional[int]  				=			'''git'''

							def __init__(							self:     Optional[Any]   ,						UpperCamelCase_:     Optional[Any]=None   ,						UpperCamelCase_:     List[str]=30_522   ,						UpperCamelCase_:     List[Any]=768   ,						UpperCamelCase_:     List[Any]=6   ,						UpperCamelCase_:     Union[str, Any]=12   ,						UpperCamelCase_:     Any=3_072   ,						UpperCamelCase_:     Any="gelu"   ,						UpperCamelCase_:     List[Any]=0.1   ,						UpperCamelCase_:     Dict=0.1   ,						UpperCamelCase_:     str=1_024   ,						UpperCamelCase_:     Any=0.02   ,						UpperCamelCase_:     str=1E-1_2   ,						UpperCamelCase_:     Optional[int]=0   ,						UpperCamelCase_:     Optional[Any]="absolute"   ,						UpperCamelCase_:     Optional[int]=True   ,						UpperCamelCase_:     List[Any]=False   ,						UpperCamelCase_:     List[Any]=101   ,						UpperCamelCase_:     int=102   ,						UpperCamelCase_:     List[str]=None   ,						**UpperCamelCase_:     int   ,						)      ->       Union[str, Any]:





									"""simple docstring"""







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

									if vision_config is None:
											lowercase__						=				{}
											logger.info('''vision_config is None. initializing the GitVisionConfig with default values.'''							)

									lowercase__						=				GitVisionConfig(**UpperCamelCase_							)
									lowercase__						=				vocab_size
									lowercase__						=				hidden_size
									lowercase__						=				num_hidden_layers
									lowercase__						=				num_attention_heads
									lowercase__						=				hidden_act
									lowercase__						=				intermediate_size
									lowercase__						=				hidden_dropout_prob
									lowercase__						=				attention_probs_dropout_prob
									lowercase__						=				max_position_embeddings
									lowercase__						=				initializer_range
									lowercase__						=				layer_norm_eps
									lowercase__						=				position_embedding_type
									lowercase__						=				use_cache
									lowercase__						=				tie_word_embeddings
									lowercase__						=				num_image_with_embedding

									lowercase__						=				bos_token_id
									lowercase__						=				eos_token_id


							def 				lowerCamelCase_			(							self:     Dict							)      ->       Tuple:





									"""simple docstring"""







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


