




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


 '''simple docstring'''







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



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size		)

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

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

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

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

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase							,							head_mask=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)

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



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						int							,							*_lowerCAmelCase    :						Dict		):
       SCREAMING_SNAKE_CASE_      		=     OpenAIGPTLMHeadModel(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Tuple							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						List[Any]							,							*_lowerCAmelCase    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     OpenAIGPTDoubleHeadsModel(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Any							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Dict							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Optional[int]							,							*_lowerCAmelCase    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     OpenAIGPTForSequenceClassification(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

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




 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					
       )      		=     config_and_inputs
       SCREAMING_SNAKE_CASE_      		=     {
           'input_ids': input_ids,
           'token_type_ids': token_type_ids,
           'head_mask': head_mask,
       }

       return config, inputs_dict





@require_torch
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







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



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

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



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     OpenAIGPTModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     ConfigTester(self							,							config_class=_lowerCAmelCase							,							n_embd=37		)



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



 def  lowerCAmelCase_    (				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_double_lm_head_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase		)




 @slow
 def  lowerCAmelCase_    (				self    :						Tuple		):
       for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_      		=     OpenAIGPTModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)





@require_torch
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 @slow
 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'		)
       model.to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.tensor([[481, 4_735, 544]]							,							dtype=torch.long							,							device=_lowerCAmelCase		)  # the president is
       SCREAMING_SNAKE_CASE_      		=     [
           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

       SCREAMING_SNAKE_CASE_      		=     model.generate(_lowerCAmelCase							,							do_sample=_lowerCAmelCase		)
       self.assertListEqual(output_ids[0].tolist()							,							_lowerCAmelCase		)





def    UpperCAmelCase_				( __UpperCAmelCase		:      int				)   ->					int:
      assert isinstance(__UpperCAmelCase     ,     __UpperCAmelCase				), f"The input value of [n={number}] is not an integer"

      if number == 1:
            return 2
      elif number < 1:
            SCREAMING_SNAKE_CASE_      		=     f"The input value of [n={number}] has to be > 0"
            raise ValueError(__UpperCAmelCase				)
      else:
            SCREAMING_SNAKE_CASE_      		=     sylvester(number - 1				)
            SCREAMING_SNAKE_CASE_      		=     num - 1
            SCREAMING_SNAKE_CASE_      		=     num
            return lower * upper + 1


if __name__ == "__main__":
   print(f'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')