





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	:







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__=1_3		,				lowerCAmelCase__=7		,				lowerCAmelCase__=True		,				lowerCAmelCase__=True		,				lowerCAmelCase__=True		,				lowerCAmelCase__=9_9		,				lowerCAmelCase__=3_2		,				lowerCAmelCase__=5		,				lowerCAmelCase__=4		,				lowerCAmelCase__=3_7		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=5_1_2		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=2		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=3		,				lowerCAmelCase__=4		,				lowerCAmelCase__=None		,				)       ->						Optional[Any]:



        '''simple docstring'''




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







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




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

        a__     : Any  =None
        if self.use_token_type_ids:
          a__     : Optional[int]  =ids_tensor([self.batch_size, self.seq_length]		,				self.type_vocab_size							)

        a__     : str  =None
        a__     : List[Any]  =None
        a__     : str  =None
        if self.use_labels:
          a__     : str  =ids_tensor([self.batch_size]		,				self.type_sequence_label_size							)
          a__     : Tuple  =ids_tensor([self.batch_size, self.seq_length]		,				self.num_labels							)
          a__     : Union[str, Any]  =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__     : Optional[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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				*lowerCAmelCase__							)       ->						Tuple:



        '''simple docstring'''




        a__     : List[Any]  =OpenAIGPTModel(config=lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

        a__     : Dict  =model(lowerCAmelCase__		,				token_type_ids=lowerCAmelCase__		,				head_mask=lowerCAmelCase__							)
        a__     : int  =model(lowerCAmelCase__		,				token_type_ids=lowerCAmelCase__							)
        a__     : Optional[int]  =model(lowerCAmelCase__							)

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







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				*lowerCAmelCase__							)       ->						int:



        '''simple docstring'''




        a__     : Any  =OpenAIGPTLMHeadModel(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

        a__     : str  =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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				*lowerCAmelCase__							)       ->						Tuple:



        '''simple docstring'''




        a__     : List[Any]  =OpenAIGPTDoubleHeadsModel(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

        a__     : List[str]  =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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				*lowerCAmelCase__							)       ->						int:



        '''simple docstring'''




        a__     : Tuple  =self.num_labels
        a__     : Optional[int]  =OpenAIGPTForSequenceClassification(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

        a__     : str  =ids_tensor([self.batch_size]		,				self.type_sequence_label_size							)
        a__     : Optional[Any]  =model(lowerCAmelCase__		,				token_type_ids=lowerCAmelCase__		,				labels=lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels)							)







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



        '''simple docstring'''




        a__     : Optional[Any]  =self.prepare_config_and_inputs()
        (
            (
            a__
        ) ,							(
            a__
        ) ,							(
            a__
        ) ,							(
            a__
        ) ,							(
            a__
        ) ,							(
            a__
        ) ,							(
            a__
        ) ,							
        )     : str  =config_and_inputs
        a__     : Union[str, Any]  ={
            "input_ids": input_ids,
            "token_type_ids": token_type_ids,
            "head_mask": head_mask,
        }

        return config, inputs_dict







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







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__=False							)       ->						Dict:



        '''simple docstring'''




        a__     : Dict  =super()._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__		,				return_labels=lowerCAmelCase__							)

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







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        a__     : Union[str, Any]  =OpenAIGPTModelTester(self							)
        a__     : Tuple  =ConfigTester(self		,				config_class=lowerCAmelCase__		,				n_embd=3_7							)







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        self.config_tester.run_common_tests()







      def        _lowercase  (   self							)       ->						Optional[Any]:



        '''simple docstring'''




        a__     : List[Any]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__							)







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



        '''simple docstring'''




        a__     : Any  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        a__     : Any  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        a__     : List[str]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__							)







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



        '''simple docstring'''




        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__     : Any  =OpenAIGPTModel.from_pretrained(lowerCAmelCase__							)
          self.assertIsNotNone(lowerCAmelCase__							)







@require_torch
class     __lowerCAmelCase	(   unittest.TestCase):







      @slow
      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        a__     : List[Any]  =OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"							)
        model.to(lowerCAmelCase__							)
        a__     : Tuple  =torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]		,				dtype=torch.long		,				device=lowerCAmelCase__							)  # the president is
        a__     : int  =[
            4_8_1,
            4_7_3_5,
            5_4_4,
            2_4_6,
            9_6_3,
            8_7_0,
            7_6_2,
            2_3_9,
            2_4_4,
            4_0_4_7_7,
            2_4_4,
            2_4_9,
            7_1_9,
            8_8_1,
            4_8_7,
            5_4_4,
            2_4_0,
            2_4_4,
            6_0_3,
            4_8_1,
        ]  # the president is a very good man. " \n " i\'m sure he is, " said the

        a__     : Optional[Any]  =model.generate(lowerCAmelCase__		,				do_sample=lowerCAmelCase__							)
        self.assertListEqual(output_ids[0].tolist()		,				lowerCAmelCase__							)













import os
def 					_A	(		):





  """simple docstring"""



  a__     : Optional[int]  =os.path.join(os.path.dirname(SCREAMING_SNAKE_CASE						)	,	"num.txt"						)
  with open(SCREAMING_SNAKE_CASE						) as file_hand:
    return str(sum(int(SCREAMING_SNAKE_CASE						) for line in file_hand						)						)[:10]


if __name__ == "__main__":
     print(solution())






