



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

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


    """simple docstring"""






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

   def 		A							(      self			:       Dict							)       ->				str:


    """simple docstring"""






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

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

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

    a						=		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						=		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			:       Optional[Any]							,   __lowerCAmelCase			:       Optional[Any]							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       Optional[int]							,   *__lowerCAmelCase			:       Tuple							)       ->				str:


    """simple docstring"""






    a						=		OpenAIGPTModel(config=__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()

    a						=		model(__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							,   head_mask=__lowerCAmelCase							)
    a						=		model(__lowerCAmelCase							,   token_type_ids=__lowerCAmelCase							)
    a						=		model(__lowerCAmelCase							)

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

   def 		A							(      self			:       Optional[Any]							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       List[Any]							,   *__lowerCAmelCase			:       Optional[int]							)       ->				List[str]:


    """simple docstring"""






    a						=		OpenAIGPTLMHeadModel(__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()

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

   def 		A							(      self			:       Union[str, Any]							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       str							,   __lowerCAmelCase			:       List[Any]							,   __lowerCAmelCase			:       Optional[Any]							,   *__lowerCAmelCase			:       List[str]							)       ->				Any:


    """simple docstring"""






    a						=		OpenAIGPTDoubleHeadsModel(__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()

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

   def 		A							(      self			:       Optional[Any]							,   __lowerCAmelCase			:       Any							,   __lowerCAmelCase			:       Optional[int]							,   __lowerCAmelCase			:       Tuple							,   __lowerCAmelCase			:       Any							,   *__lowerCAmelCase			:       Dict							)       ->				Tuple:


    """simple docstring"""






    a						=		self.num_labels
    a						=		OpenAIGPTForSequenceClassification(__lowerCAmelCase							)
    model.to(__lowerCAmelCase							)
    model.eval()

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




   def 		A							(      self			:       List[str]							)       ->				Union[str, Any]:


    """simple docstring"""






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

    return config, inputs_dict



@require_torch
class 		_lowercase		(       UpperCAmelCase__,   UpperCAmelCase__,   UpperCAmelCase__,   unittest.TestCase	):
   _UpperCAmelCase     	=			(
       (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
       if is_torch_available()
       else ()
   )
   _UpperCAmelCase     	=			(
       (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
   )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
   _UpperCAmelCase     	=			(
       {
           '''feature-extraction''': OpenAIGPTModel,
           '''text-classification''': OpenAIGPTForSequenceClassification,
           '''text-generation''': OpenAIGPTLMHeadModel,
           '''zero-shot''': OpenAIGPTForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

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


    """simple docstring"""






    a						=		super()._prepare_for_class(__lowerCAmelCase							,   __lowerCAmelCase							,   return_labels=__lowerCAmelCase							)

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

   def 		A							(      self			:       List[str]							)       ->				str:


    """simple docstring"""






    a						=		OpenAIGPTModelTester(self							)
    a						=		ConfigTester(self							,   config_class=__lowerCAmelCase							,   n_embd=37							)

   def 		A							(      self			:       Optional[Any]							)       ->				Dict:


    """simple docstring"""






    self.config_tester.run_common_tests()

   def 		A							(      self			:       Optional[int]							)       ->				Union[str, Any]:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*__lowerCAmelCase							)

   def 		A							(      self			:       Optional[int]							)       ->				Union[str, Any]:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(*__lowerCAmelCase							)

   def 		A							(      self			:       Tuple							)       ->				Dict:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_double_lm_head_model(*__lowerCAmelCase							)

   def 		A							(      self			:       int							)       ->				List[Any]:


    """simple docstring"""






    a						=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCAmelCase							)




   @slow
   def 		A							(      self			:       Optional[Any]							)       ->				Tuple:


    """simple docstring"""






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



@require_torch
class 		_lowercase		(       unittest.TestCase	):

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


    """simple docstring"""






    a						=		OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"							)
    model.to(__lowerCAmelCase							)
    a						=		torch.tensor([[481, 4735, 544]]							,   dtype=torch.long							,   device=__lowerCAmelCase							)  # the president is
    a						=		[
        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						=		model.generate(__lowerCAmelCase							,   do_sample=__lowerCAmelCase							)
    self.assertListEqual(output_ids[0].tolist()							,   __lowerCAmelCase							)






A_		:     Any        =     [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A_		:     Tuple        =     [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
A_		:     Optional[int]        =     {
    0: '''Sunday''',
    1: '''Monday''',
    2: '''Tuesday''',
    3: '''Wednesday''',
    4: '''Thursday''',
    5: '''Friday''',
    6: '''Saturday''',
}


def 	UpperCAmelCase__      (      UpperCAmelCase__ :int ,					UpperCAmelCase__ :int ,					UpperCAmelCase__ :int					):






 '''simple docstring'''

 assert len(str(UpperCAmelCase__					)					) > 2, "year should be in YYYY format"
 assert 1 <= month <= 12, "month should be between 1 to 12"
 assert 1 <= day <= 31, "day should be between 1 to 31"

 # Doomsday algorithm:
 a						=		year // 1_00
 a						=		(5 * (century % 4) + 2) % 7
 a						=		year % 1_00
 a						=		centurian % 12
 a						=		(
     (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor
 ) % 7
 a						=		(
     DOOMSDAY_NOT_LEAP[month - 1]
     if (year % 4 != 0) or (centurian == 0 and (year % 4_00) == 0)
     else DOOMSDAY_LEAP[month - 1]
 )
 a						=		(dooms_day + day - day_anchor) % 7
 return WEEK_DAY_NAMES[week_day]


if __name__ == "__main__":
     import doctest

     doctest.testmod()

