






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      :


      def __init__(						self       ,    __UpperCamelCase       ,    __UpperCamelCase=1_3       ,    __UpperCamelCase=7       ,    __UpperCamelCase=True       ,    __UpperCamelCase=True       ,    __UpperCamelCase=True       ,    __UpperCamelCase=9_9       ,    __UpperCamelCase=3_2       ,    __UpperCamelCase=5       ,    __UpperCamelCase=4       ,    __UpperCamelCase=3_7       ,    __UpperCamelCase="gelu"       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=5_1_2       ,    __UpperCamelCase=1_6       ,    __UpperCamelCase=2       ,    __UpperCamelCase=0.0_2       ,    __UpperCamelCase=3       ,    __UpperCamelCase=4       ,    __UpperCamelCase=None       ,    )->						Dict:
           __lowerCAmelCase             =       parent
           __lowerCAmelCase             =       batch_size
           __lowerCAmelCase             =       seq_length
           __lowerCAmelCase             =       is_training
           __lowerCAmelCase             =       use_token_type_ids
           __lowerCAmelCase             =       use_labels
           __lowerCAmelCase             =       vocab_size
           __lowerCAmelCase             =       hidden_size
           __lowerCAmelCase             =       num_hidden_layers
           __lowerCAmelCase             =       num_attention_heads
           __lowerCAmelCase             =       intermediate_size
           __lowerCAmelCase             =       hidden_act
           __lowerCAmelCase             =       hidden_dropout_prob
           __lowerCAmelCase             =       attention_probs_dropout_prob
           __lowerCAmelCase             =       max_position_embeddings
           __lowerCAmelCase             =       type_vocab_size
           __lowerCAmelCase             =       type_sequence_label_size
           __lowerCAmelCase             =       initializer_range
           __lowerCAmelCase             =       num_labels
           __lowerCAmelCase             =       num_choices
           __lowerCAmelCase             =       scope
           __lowerCAmelCase             =       self.vocab_size - 1


      def 			__UpperCAmelCase		(						self						)->						str:
           __lowerCAmelCase             =       ids_tensor([self.batch_size, self.seq_length]       ,    self.vocab_size						)

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

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

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

           __lowerCAmelCase             =       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       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    *__UpperCamelCase						)->						Optional[Any]:
           __lowerCAmelCase             =       OpenAIGPTModel(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()

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

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


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    *__UpperCamelCase						)->						List[Any]:
           __lowerCAmelCase             =       OpenAIGPTLMHeadModel(__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()

           __lowerCAmelCase             =       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 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    *__UpperCamelCase						)->						int:
           __lowerCAmelCase             =       OpenAIGPTDoubleHeadsModel(__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()

           __lowerCAmelCase             =       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 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    *__UpperCamelCase						)->						Any:
           __lowerCAmelCase             =       self.num_labels
           __lowerCAmelCase             =       OpenAIGPTForSequenceClassification(__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()

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


      def 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase             =       self.prepare_config_and_inputs()
           (
               (
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			(
               __lowerCAmelCase
           )      ,			
           )             =       config_and_inputs
           __lowerCAmelCase             =       {
               "input_ids": input_ids,
               "token_type_ids": token_type_ids,
               "head_mask": head_mask,
           }

           return config, inputs_dict



@require_torch
class 				_UpperCamelCase      (a_				,		a_				,		a_				,		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       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						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 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase=False						)->						Union[str, Any]:
           __lowerCAmelCase             =       super()._prepare_for_class(__UpperCamelCase       ,    __UpperCamelCase       ,    return_labels=__UpperCamelCase						)

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


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       OpenAIGPTModelTester(self						)
           __lowerCAmelCase             =       ConfigTester(self       ,    config_class=__UpperCamelCase       ,    n_embd=3_7						)


      def 			__UpperCAmelCase		(						self						)->						Tuple:
           self.config_tester.run_common_tests()


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						str:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_double_lm_head_model(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase						)


      @slow
      def 			__UpperCAmelCase		(						self						)->						List[str]:
           for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                __lowerCAmelCase             =       OpenAIGPTModel.from_pretrained(__UpperCamelCase						)
                self.assertIsNotNone(__UpperCamelCase						)



@require_torch
class 				_UpperCamelCase      (unittest.TestCase      ):


      @slow
      def 			__UpperCAmelCase		(						self						)->						int:
           __lowerCAmelCase             =       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"						)
           model.to(__UpperCamelCase						)
           __lowerCAmelCase             =       torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]       ,    dtype=torch.long       ,    device=__UpperCamelCase						)  # the president is
           __lowerCAmelCase             =       [
               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

           __lowerCAmelCase             =       model.generate(__UpperCamelCase       ,    do_sample=__UpperCamelCase						)
           self.assertListEqual(output_ids[0].tolist()       ,    __UpperCamelCase						)














import os

# Precomputes a list of the 100 first triangular numbers
lowerCamelCase      :       str    =						[int(0.5 * n * (n + 1)) for n in range(1, 101)]





def    __lowerCAmelCase   (    ):
     __lowerCAmelCase             =       os.path.dirname(os.path.realpath(__snake_case							)							)
     __lowerCAmelCase             =       os.path.join(__snake_case       ,			"words.txt"							)

     __lowerCAmelCase             =       ""
     with open(__snake_case							) as f:
          __lowerCAmelCase             =       f.readline()

     __lowerCAmelCase             =       [word.strip("\""							) for word in words.strip("\r\n"							).split(","							)]
     __lowerCAmelCase             =       [
         word
         for word in [sum(ord(__snake_case							) - 64 for x in word							) for word in words]
         if word in TRIANGULAR_NUMBERS
     ]
     return len(__snake_case							)


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






