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






			'''simple docstring'''

			def __init__(       self :       Any						,	snake_case_ :       int						,	snake_case_ :       Optional[int]=13						,	snake_case_ :       Union[str, Any]=7						,	snake_case_ :       str=True						,	snake_case_ :       int=True						,	snake_case_ :       Optional[Any]=True						,	snake_case_ :       List[Any]=99						,	snake_case_ :       Union[str, Any]=32						,	snake_case_ :       Union[str, Any]=5						,	snake_case_ :       Union[str, Any]=4						,	snake_case_ :       Tuple=37						,	snake_case_ :       List[Any]="gelu"						,	snake_case_ :       Any=0.1						,	snake_case_ :       Dict=0.1						,	snake_case_ :       Union[str, Any]=512						,	snake_case_ :       List[Any]=16						,	snake_case_ :       List[Any]=2						,	snake_case_ :       str=0.02						,	snake_case_ :       str=3						,	snake_case_ :       Optional[Any]=4						,	snake_case_ :       Optional[int]=None						,	):
							UpperCamelCase_:							List[str]	=						parent
							UpperCamelCase_:							Tuple	=						batch_size
							UpperCamelCase_:							Any	=						seq_length
							UpperCamelCase_:							str	=						is_training
							UpperCamelCase_:							str	=						use_token_type_ids
							UpperCamelCase_:							int	=						use_labels
							UpperCamelCase_:							List[str]	=						vocab_size
							UpperCamelCase_:							List[str]	=						hidden_size
							UpperCamelCase_:							Optional[int]	=						num_hidden_layers
							UpperCamelCase_:							List[str]	=						num_attention_heads
							UpperCamelCase_:							List[Any]	=						intermediate_size
							UpperCamelCase_:							Union[str, Any]	=						hidden_act
							UpperCamelCase_:							Optional[Any]	=						hidden_dropout_prob
							UpperCamelCase_:							Optional[Any]	=						attention_probs_dropout_prob
							UpperCamelCase_:							int	=						max_position_embeddings
							UpperCamelCase_:							Optional[int]	=						type_vocab_size
							UpperCamelCase_:							Any	=						type_sequence_label_size
							UpperCamelCase_:							Tuple	=						initializer_range
							UpperCamelCase_:							Tuple	=						num_labels
							UpperCamelCase_:							Union[str, Any]	=						num_choices
							UpperCamelCase_:							Any	=						scope
							UpperCamelCase_:							Tuple	=						self.vocab_size - 1

			def       lowerCAmelCase__	(       self :       List[str]      ):
							UpperCamelCase_:							Dict	=						ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size      )

							UpperCamelCase_:							Union[str, Any]	=						None
							if self.use_token_type_ids:
											UpperCamelCase_:							Dict	=						ids_tensor([self.batch_size, self.seq_length]						,	self.type_vocab_size      )

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

							UpperCamelCase_:							Dict	=						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						,	)

							UpperCamelCase_:							Optional[int]	=						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 :       Union[str, Any]						,	snake_case_ :       Optional[int]						,	snake_case_ :       Optional[Any]						,	snake_case_ :       List[str]						,	snake_case_ :       Tuple						,	*snake_case_ :       Dict      ):
							UpperCamelCase_:							Optional[Any]	=						OpenAIGPTModel(config=UpperCAmelCase__      )
							model.to(UpperCAmelCase__      )
							model.eval()

							UpperCamelCase_:							List[Any]	=						model(UpperCAmelCase__						,	token_type_ids=UpperCAmelCase__						,	head_mask=UpperCAmelCase__      )
							UpperCamelCase_:							Dict	=						model(UpperCAmelCase__						,	token_type_ids=UpperCAmelCase__      )
							UpperCamelCase_:							Optional[int]	=						model(UpperCAmelCase__      )

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

			def       lowerCAmelCase__	(       self :       Optional[int]						,	snake_case_ :       str						,	snake_case_ :       Any						,	snake_case_ :       Optional[int]						,	snake_case_ :       Any						,	*snake_case_ :       List[Any]      ):
							UpperCamelCase_:							Tuple	=						OpenAIGPTLMHeadModel(UpperCAmelCase__      )
							model.to(UpperCAmelCase__      )
							model.eval()

							UpperCamelCase_:							Tuple	=						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       lowerCAmelCase__	(       self :       int						,	snake_case_ :       Tuple						,	snake_case_ :       List[str]						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       int						,	*snake_case_ :       Union[str, Any]      ):
							UpperCamelCase_:							str	=						OpenAIGPTDoubleHeadsModel(UpperCAmelCase__      )
							model.to(UpperCAmelCase__      )
							model.eval()

							UpperCamelCase_:							Any	=						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       lowerCAmelCase__	(       self :       int						,	snake_case_ :       Optional[Any]						,	snake_case_ :       List[str]						,	snake_case_ :       List[str]						,	snake_case_ :       Dict						,	*snake_case_ :       Any      ):
							UpperCamelCase_:							Dict	=						self.num_labels
							UpperCamelCase_:							Union[str, Any]	=						OpenAIGPTForSequenceClassification(UpperCAmelCase__      )
							model.to(UpperCAmelCase__      )
							model.eval()

							UpperCamelCase_:							Any	=						ids_tensor([self.batch_size]						,	self.type_sequence_label_size      )
							UpperCamelCase_:							Dict	=						model(UpperCAmelCase__						,	token_type_ids=UpperCAmelCase__						,	labels=UpperCAmelCase__      )
							self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)      )




			def       lowerCAmelCase__	(       self :       List[Any]      ):
							UpperCamelCase_:							str	=						self.prepare_config_and_inputs()
							(
							    UpperCamelCase_  
							):							Dict	=						config_and_inputs
							UpperCamelCase_:							Optional[int]	=						{
							    '''input_ids''': input_ids,
							    '''token_type_ids''': token_type_ids,
							    '''head_mask''': head_mask,
							}

							return config, inputs_dict




@require_torch
class        _UpperCamelCase				(					lowercase__     ,       lowercase__     ,       lowercase__     ,       unittest.TestCase  ):






			'''simple docstring'''

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

			def       lowerCAmelCase__	(       self :       str						,	snake_case_ :       Tuple						,	snake_case_ :       Union[str, Any]						,	snake_case_ :       List[Any]						,	snake_case_ :       Any						,	snake_case_ :       Dict      ):
							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 :       Tuple						,	snake_case_ :       Optional[int]						,	snake_case_ :       Tuple						,	snake_case_ :       Dict=False      ):
							UpperCamelCase_:							str	=						super()._prepare_for_class(UpperCAmelCase__						,	UpperCAmelCase__						,	return_labels=UpperCAmelCase__      )

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

			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							Optional[int]	=						OpenAIGPTModelTester(self      )
							UpperCamelCase_:							str	=						ConfigTester(self						,	config_class=UpperCAmelCase__						,	n_embd=37      )

			def       lowerCAmelCase__	(       self :       Dict      ):
							self.config_tester.run_common_tests()

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							Dict	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__      )

			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							Any	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__      )

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							Union[str, Any]	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__      )

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							Tuple	=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__      )




			@slow
			def       lowerCAmelCase__	(       self :       Any      ):
							for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCamelCase_:							Optional[Any]	=						OpenAIGPTModel.from_pretrained(UpperCAmelCase__      )
											self.assertIsNotNone(UpperCAmelCase__      )




@require_torch
class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			@slow
			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							int	=						OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""      )
							model.to(UpperCAmelCase__      )
							UpperCamelCase_:							Optional[int]	=						torch.tensor([[481, 4735, 544]]						,	dtype=torch.long						,	device=UpperCAmelCase__      )  # the president is
							UpperCamelCase_:							Optional[int]	=						[
							    481,
							    4735,
							    544,
							    246,
							    963,
							    870,
							    762,
							    239,
							    244,
							    4_0477,
							    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

							UpperCamelCase_:							Optional[int]	=						model.generate(UpperCAmelCase__						,	do_sample=UpperCAmelCase__      )
							self.assertListEqual(output_ids[0].tolist()						,	UpperCAmelCase__      )








import warnings

from ..trainer import Trainer
from ..utils import logging


lowerCamelCase_						: Dict         =  logging.get_logger(__name__)

class        _UpperCamelCase				(					_A  ):






			'''simple docstring'''

			def __init__(       self :       List[str]						,	snake_case_ :       Tuple=None						,	**snake_case_ :       List[str]      ):
							warnings.warn(
							    """`SageMakerTrainer` is deprecated and will be removed in v5 of Transformers. You can use `Trainer` """
							    """instead."""						,	snake_case_						,	)
							super().__init__(args=snake_case_						,	**snake_case_      )






