






"""simple docstring"""



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 :  str     ,      snake_case :  Dict     ,      snake_case :  Union[str, Any]=13     ,      snake_case :  Union[str, Any]=7     ,      snake_case :  Optional[Any]=True     ,      snake_case :  Union[str, Any]=True     ,      snake_case :  int=True     ,      snake_case :  List[str]=99     ,      snake_case :  str=32     ,      snake_case :  Dict=5     ,      snake_case :  List[str]=4     ,      snake_case :  Dict=37     ,      snake_case :  Dict="gelu"     ,      snake_case :  Any=0.1     ,      snake_case :  Union[str, Any]=0.1     ,      snake_case :  List[str]=512     ,      snake_case :  Optional[int]=16     ,      snake_case :  Dict=2     ,      snake_case :  int=0.02     ,      snake_case :  Any=3     ,      snake_case :  Tuple=4     ,      snake_case :  int=None     ,      ):
													'''simple docstring'''





													A__  :					Tuple											=					parent
													A__  :					List[Any]											=					batch_size
													A__  :					Optional[int]											=					seq_length
													A__  :					Optional[Any]											=					is_training
													A__  :					List[Any]											=					use_token_type_ids
													A__  :					List[Any]											=					use_labels
													A__  :					List[Any]											=					vocab_size
													A__  :					Dict											=					hidden_size
													A__  :					int											=					num_hidden_layers
													A__  :					Optional[int]											=					num_attention_heads
													A__  :					int											=					intermediate_size
													A__  :					str											=					hidden_act
													A__  :					Dict											=					hidden_dropout_prob
													A__  :					Optional[int]											=					attention_probs_dropout_prob
													A__  :					Tuple											=					max_position_embeddings
													A__  :					int											=					type_vocab_size
													A__  :					Any											=					type_sequence_label_size
													A__  :					Tuple											=					initializer_range
													A__  :					Optional[int]											=					num_labels
													A__  :					Any											=					num_choices
													A__  :					Optional[Any]											=					scope
													A__  :					List[str]											=					self.vocab_size - 1


							def   _UpperCamelCase    (     self :  Optional[int]							):
													'''simple docstring'''





													A__  :					Optional[int]											=					ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size							)

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

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

													A__  :					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     ,      )

													A__  :					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   _UpperCamelCase    (     self :  Optional[Any]     ,      snake_case :  Tuple     ,      snake_case :  Tuple     ,      snake_case :  List[Any]     ,      snake_case :  List[Any]     ,      *snake_case :  str							):
													'''simple docstring'''





													A__  :					Optional[Any]											=					OpenAIGPTModel(config=snake_case							)
													model.to(snake_case							)
													model.eval()

													A__  :					Optional[Any]											=					model(snake_case     ,      token_type_ids=snake_case     ,      head_mask=snake_case							)
													A__  :					Tuple											=					model(snake_case     ,      token_type_ids=snake_case							)
													A__  :					List[str]											=					model(snake_case							)

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


							def   _UpperCamelCase    (     self :  Optional[Any]     ,      snake_case :  int     ,      snake_case :  str     ,      snake_case :  List[Any]     ,      snake_case :  Tuple     ,      *snake_case :  Any							):
													'''simple docstring'''





													A__  :					str											=					OpenAIGPTLMHeadModel(snake_case							)
													model.to(snake_case							)
													model.eval()

													A__  :					List[str]											=					model(snake_case     ,      token_type_ids=snake_case     ,      labels=snake_case							)
													self.parent.assertEqual(result.loss.shape     ,      ()							)
													self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)							)


							def   _UpperCamelCase    (     self :  Tuple     ,      snake_case :  List[Any]     ,      snake_case :  Dict     ,      snake_case :  Dict     ,      snake_case :  Any     ,      *snake_case :  Optional[int]							):
													'''simple docstring'''





													A__  :					List[str]											=					OpenAIGPTDoubleHeadsModel(snake_case							)
													model.to(snake_case							)
													model.eval()

													A__  :					Optional[Any]											=					model(snake_case     ,      token_type_ids=snake_case     ,      labels=snake_case							)
													self.parent.assertEqual(result.loss.shape     ,      ()							)
													self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)							)


							def   _UpperCamelCase    (     self :  Any     ,      snake_case :  Optional[Any]     ,      snake_case :  List[Any]     ,      snake_case :  List[str]     ,      snake_case :  Optional[int]     ,      *snake_case :  Dict							):
													'''simple docstring'''





													A__  :					List[str]											=					self.num_labels
													A__  :					str											=					OpenAIGPTForSequenceClassification(snake_case							)
													model.to(snake_case							)
													model.eval()

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


							def   _UpperCamelCase    (     self :  int							):
													'''simple docstring'''





													A__  :					int											=					self.prepare_config_and_inputs()
													(
													    (
													    A__
													)					,					(
													    A__
													)					,					(
													    A__
													)					,					(
													    A__
													)					,					(
													    A__
													)					,					(
													    A__
													)					,					(
													    A__
													)					,					
													)  :					Any											=					config_and_inputs
													A__  :					str											=					{
													    """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						):
							snake_case_       =							(
							    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
							    if is_torch_available()
							    else ()
							)
							snake_case_       =							(
							    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							snake_case_       =							(
							    {
							        'feature-extraction': OpenAIGPTModel,
							        'text-classification': OpenAIGPTForSequenceClassification,
							        'text-generation': OpenAIGPTLMHeadModel,
							        'zero-shot': OpenAIGPTForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)


							def   _UpperCamelCase    (     self :  int     ,      snake_case :  Optional[int]     ,      snake_case :  Union[str, Any]     ,      snake_case :  Optional[Any]     ,      snake_case :  Tuple     ,      snake_case :  Optional[int]							):
													'''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   _UpperCamelCase    (     self :  Tuple     ,      snake_case :  List[Any]     ,      snake_case :  List[Any]     ,      snake_case :  Dict=False							):
													'''simple docstring'''





													A__  :					str											=					super()._prepare_for_class(snake_case     ,      snake_case     ,      return_labels=snake_case							)

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


							def   _UpperCamelCase    (     self :  Optional[Any]							):
													'''simple docstring'''





													A__  :					Tuple											=					OpenAIGPTModelTester(self							)
													A__  :					Optional[int]											=					ConfigTester(self     ,      config_class=snake_case     ,      n_embd=37							)


							def   _UpperCamelCase    (     self :  str							):
													'''simple docstring'''





													self.config_tester.run_common_tests()


							def   _UpperCamelCase    (     self :  Tuple							):
													'''simple docstring'''





													A__  :					Any											=					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_openai_gpt_model(*snake_case							)


							def   _UpperCamelCase    (     self :  Union[str, Any]							):
													'''simple docstring'''





													A__  :					List[str]											=					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_lm_head_model(*snake_case							)


							def   _UpperCamelCase    (     self :  int							):
													'''simple docstring'''





													A__  :					Optional[Any]											=					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_double_lm_head_model(*snake_case							)


							def   _UpperCamelCase    (     self :  Any							):
													'''simple docstring'''





													A__  :					Dict											=					self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case							)


							@slow
							def   _UpperCamelCase    (     self :  int							):
													'''simple docstring'''





													for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A__  :					List[Any]											=					OpenAIGPTModel.from_pretrained(snake_case							)
																			self.assertIsNotNone(snake_case							)






@require_torch
class 							__SCREAMING_SNAKE_CASE  (			unittest.TestCase						):


							@slow
							def   _UpperCamelCase    (     self :  Optional[Any]							):
													'''simple docstring'''





													A__  :					List[str]											=					OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""							)
													model.to(snake_case							)
													A__  :					List[Any]											=					torch.tensor([[481, 4735, 544]]     ,      dtype=torch.long     ,      device=snake_case							)  # the president is
													A__  :					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

													A__  :					List[str]											=					model.generate(snake_case     ,      do_sample=snake_case							)
													self.assertListEqual(output_ids[0].tolist()     ,      snake_case							)














"""simple docstring"""



import warnings

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


A_      =							logging.get_logger(__name__)






class 							__SCREAMING_SNAKE_CASE  (			UpperCamelCase						):


							def __init__(     self :  Optional[int]     ,      snake_case :  List[str]=None     ,      **snake_case :  Any							):
													'''simple docstring'''





													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							)






