






'''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						:				int       ,   __lowercase						:				str       ,   __lowercase						:				List[Any]=13       ,   __lowercase						:				Any=7       ,   __lowercase						:				Union[str, Any]=True       ,   __lowercase						:				int=True       ,   __lowercase						:				int=True       ,   __lowercase						:				int=99       ,   __lowercase						:				int=32       ,   __lowercase						:				int=5       ,   __lowercase						:				Optional[Any]=4       ,   __lowercase						:				str=37       ,   __lowercase						:				int="gelu"       ,   __lowercase						:				Optional[Any]=0.1       ,   __lowercase						:				List[str]=0.1       ,   __lowercase						:				Union[str, Any]=5_12       ,   __lowercase						:				Any=16       ,   __lowercase						:				Tuple=2       ,   __lowercase						:				Any=0.02       ,   __lowercase						:				Dict=3       ,   __lowercase						:				Optional[int]=4       ,   __lowercase						:				Dict=None       ,   )						->    List[str]:
       SCREAMING_SNAKE_CASE__  :					Dict						=parent
       SCREAMING_SNAKE_CASE__  :					List[str]						=batch_size
       SCREAMING_SNAKE_CASE__  :					str						=seq_length
       SCREAMING_SNAKE_CASE__  :					str						=is_training
       SCREAMING_SNAKE_CASE__  :					List[str]						=use_token_type_ids
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=use_labels
       SCREAMING_SNAKE_CASE__  :					List[str]						=vocab_size
       SCREAMING_SNAKE_CASE__  :					List[str]						=hidden_size
       SCREAMING_SNAKE_CASE__  :					int						=num_hidden_layers
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=num_attention_heads
       SCREAMING_SNAKE_CASE__  :					List[Any]						=intermediate_size
       SCREAMING_SNAKE_CASE__  :					List[str]						=hidden_act
       SCREAMING_SNAKE_CASE__  :					Tuple						=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=max_position_embeddings
       SCREAMING_SNAKE_CASE__  :					Tuple						=type_vocab_size
       SCREAMING_SNAKE_CASE__  :					Any						=type_sequence_label_size
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=initializer_range
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=num_labels
       SCREAMING_SNAKE_CASE__  :					Tuple						=num_choices
       SCREAMING_SNAKE_CASE__  :					List[Any]						=scope
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=self.vocab_size - 1




 def   __magic_name__						(   self						:				List[str]   )						->    Optional[int]:
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size   )

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

       SCREAMING_SNAKE_CASE__  :					int						=None
       SCREAMING_SNAKE_CASE__  :					Tuple						=None
       SCREAMING_SNAKE_CASE__  :					str						=None
       if self.use_labels:
             SCREAMING_SNAKE_CASE__  :					Any						=ids_tensor([self.batch_size]       ,   self.type_sequence_label_size   )
             SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=ids_tensor([self.batch_size, self.seq_length]       ,   self.num_labels   )
             SCREAMING_SNAKE_CASE__  :					Tuple						=ids_tensor([self.batch_size]       ,   self.num_choices   )

       SCREAMING_SNAKE_CASE__  :					Tuple						=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__  :					int						=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   __magic_name__						(   self						:				str       ,   __lowercase						:				Optional[Any]       ,   __lowercase						:				Any       ,   __lowercase						:				int       ,   __lowercase						:				Optional[Any]       ,   *__lowercase						:				Dict   )						->    int:
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=OpenAIGPTModel(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()

       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=model(_UpperCAmelCase       ,   token_type_ids=_UpperCAmelCase       ,   head_mask=_UpperCAmelCase   )
       SCREAMING_SNAKE_CASE__  :					str						=model(_UpperCAmelCase       ,   token_type_ids=_UpperCAmelCase   )
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=model(_UpperCAmelCase   )

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




 def   __magic_name__						(   self						:				List[str]       ,   __lowercase						:				List[str]       ,   __lowercase						:				List[str]       ,   __lowercase						:				str       ,   __lowercase						:				Optional[Any]       ,   *__lowercase						:				List[Any]   )						->    List[str]:
       SCREAMING_SNAKE_CASE__  :					List[str]						=OpenAIGPTLMHeadModel(_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()

       SCREAMING_SNAKE_CASE__  :					Optional[int]						=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   __magic_name__						(   self						:				Any       ,   __lowercase						:				int       ,   __lowercase						:				Dict       ,   __lowercase						:				List[Any]       ,   __lowercase						:				Any       ,   *__lowercase						:				Union[str, Any]   )						->    Union[str, Any]:
       SCREAMING_SNAKE_CASE__  :					Any						=OpenAIGPTDoubleHeadsModel(_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()

       SCREAMING_SNAKE_CASE__  :					List[str]						=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   __magic_name__						(   self						:				str       ,   __lowercase						:				Dict       ,   __lowercase						:				List[str]       ,   __lowercase						:				Union[str, Any]       ,   __lowercase						:				Optional[Any]       ,   *__lowercase						:				Union[str, Any]   )						->    List[str]:
       SCREAMING_SNAKE_CASE__  :					Dict						=self.num_labels
       SCREAMING_SNAKE_CASE__  :					Any						=OpenAIGPTForSequenceClassification(_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()

       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=ids_tensor([self.batch_size]       ,   self.type_sequence_label_size   )
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=model(_UpperCAmelCase       ,   token_type_ids=_UpperCAmelCase       ,   labels=_UpperCAmelCase   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)   )




 def   __magic_name__						(   self						:				Dict   )						->    str:
       SCREAMING_SNAKE_CASE__  :					Tuple						=self.prepare_config_and_inputs()
       (
           SCREAMING_SNAKE_CASE__   
       )  :					Dict						=config_and_inputs
       SCREAMING_SNAKE_CASE__  :					Optional[int]						={
           '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   __magic_name__						(   self						:				List[Any]       ,   __lowercase						:				List[str]       ,   __lowercase						:				str       ,   __lowercase						:				Optional[int]       ,   __lowercase						:				int       ,   __lowercase						:				List[str]   )						->    Any:
       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   __magic_name__						(   self						:				int       ,   __lowercase						:				Dict       ,   __lowercase						:				Tuple       ,   __lowercase						:				Union[str, Any]=False   )						->    int:
       SCREAMING_SNAKE_CASE__  :					Dict						=super()._prepare_for_class(_UpperCAmelCase       ,   _UpperCAmelCase       ,   return_labels=_UpperCAmelCase   )

       if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                   SCREAMING_SNAKE_CASE__  :					Optional[Any]						=torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)       ,   dtype=torch.long       ,   device=_UpperCAmelCase       ,   )
                   SCREAMING_SNAKE_CASE__  :					Dict						=inputs_dict['labels']
                   SCREAMING_SNAKE_CASE__  :					List[Any]						=inputs_dict['labels']
                   SCREAMING_SNAKE_CASE__  :					Tuple						=torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.num_choices)       ,   dtype=torch.long       ,   device=_UpperCAmelCase       ,   )
                   SCREAMING_SNAKE_CASE__  :					Tuple						=torch.zeros(
                       self.model_tester.batch_size       ,   dtype=torch.long       ,   device=_UpperCAmelCase   )
       return inputs_dict




 def   __magic_name__						(   self						:				Dict   )						->    List[str]:
       SCREAMING_SNAKE_CASE__  :					Any						=OpenAIGPTModelTester(self   )
       SCREAMING_SNAKE_CASE__  :					Any						=ConfigTester(self       ,   config_class=_UpperCAmelCase       ,   n_embd=37   )




 def   __magic_name__						(   self						:				Optional[int]   )						->    Optional[int]:
       self.config_tester.run_common_tests()




 def   __magic_name__						(   self						:				Optional[int]   )						->    Dict:
       SCREAMING_SNAKE_CASE__  :					List[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				int   )						->    List[Any]:
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				str   )						->    Optional[Any]:
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_double_lm_head_model(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Any   )						->    Tuple:
       SCREAMING_SNAKE_CASE__  :					str						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_UpperCAmelCase   )




 @slow
 def   __magic_name__						(   self						:				int   )						->    Union[str, Any]:
       for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=OpenAIGPTModel.from_pretrained(_UpperCAmelCase   )
             self.assertIsNotNone(_UpperCAmelCase   )






@require_torch
class   __SCREAMING_SNAKE_CASE   (      unittest.TestCase					):




 @slow
 def   __magic_name__						(   self						:				Tuple   )						->    Optional[Any]:
       SCREAMING_SNAKE_CASE__  :					Any						=OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
       model.to(_UpperCAmelCase   )
       SCREAMING_SNAKE_CASE__  :					str						=torch.tensor([[4_81, 47_35, 5_44]]       ,   dtype=torch.long       ,   device=_UpperCAmelCase   )  # the president is
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=[
           4_81,
           47_35,
           5_44,
           2_46,
           9_63,
           8_70,
           7_62,
           2_39,
           2_44,
           4_04_77,
           2_44,
           2_49,
           7_19,
           8_81,
           4_87,
           5_44,
           2_40,
           2_44,
           6_03,
           4_81,
       ]  # the president is a very good man. " \n " i\'m sure he is, " said the

       SCREAMING_SNAKE_CASE__  :					Dict						=model.generate(_UpperCAmelCase       ,   do_sample=_UpperCAmelCase   )
       self.assertListEqual(output_ids[0].tolist()       ,   _UpperCAmelCase   )




'''simple docstring'''

import re

from filelock import FileLock


try:
							import nltk

							__lowerCAmelCase		     =  True
except (ImportError, ModuleNotFoundError):
							__lowerCAmelCase		     =  False

if NLTK_AVAILABLE:
							with FileLock('''.lock''') as lock:
														nltk.download('''punkt''', quiet=True)



def 	__lowerCamelCase	( lowerCAmelCase_    )     -> str:
		re.sub('<n>'		,     ''		,     lowerCAmelCase_    )  # remove pegasus newline char
		assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)"
		return "\n".join(nltk.sent_tokenize(lowerCAmelCase_    )    )




