


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


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





      '''simple docstring'''






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


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





      '''simple docstring'''






      UpperCamelCase__					:    Tuple          =   ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size			)

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

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

      UpperCamelCase__					:    List[Any]          =   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							,	)

      UpperCamelCase__					:    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      UpperCAmelCase__	(      self							:     Union[str, Any]							,	lowerCamelCase__							:     Union[str, Any]							,	lowerCamelCase__							:     Tuple							,	lowerCamelCase__							:     int							,	lowerCamelCase__							:     Optional[int]							,	*lowerCamelCase__							:     List[Any]			)  ->      Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   OpenAIGPTModel(config=lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      UpperCamelCase__					:    List[str]          =   model(lowerCamelCase__							,	token_type_ids=lowerCamelCase__							,	head_mask=lowerCamelCase__			)
      UpperCamelCase__					:    Optional[int]          =   model(lowerCamelCase__							,	token_type_ids=lowerCamelCase__			)
      UpperCamelCase__					:    Any          =   model(lowerCamelCase__			)

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


     def      UpperCAmelCase__	(      self							:     Optional[int]							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     Union[str, Any]							,	*lowerCamelCase__							:     Any			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    Tuple          =   OpenAIGPTLMHeadModel(lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      UpperCamelCase__					:    Union[str, Any]          =   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      UpperCAmelCase__	(      self							:     Tuple							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     Any							,	lowerCamelCase__							:     Union[str, Any]							,	lowerCamelCase__							:     Tuple							,	*lowerCamelCase__							:     List[str]			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   OpenAIGPTDoubleHeadsModel(lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      UpperCamelCase__					:    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      UpperCAmelCase__	(      self							:     Tuple							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     List[str]							,	lowerCamelCase__							:     List[str]							,	*lowerCamelCase__							:     Optional[Any]			)  ->      Any:





      '''simple docstring'''






      UpperCamelCase__					:    Union[str, Any]          =   self.num_labels
      UpperCamelCase__					:    Optional[Any]          =   OpenAIGPTForSequenceClassification(lowerCamelCase__			)
      model.to(lowerCamelCase__			)
      model.eval()

      UpperCamelCase__					:    Dict          =   ids_tensor([self.batch_size]							,	self.type_sequence_label_size			)
      UpperCamelCase__					:    Tuple          =   model(lowerCamelCase__							,	token_type_ids=lowerCamelCase__							,	labels=lowerCamelCase__			)
      self.parent.assertEqual(result.logits.shape							,	(self.batch_size, self.num_labels)			)


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[Any]          =   self.prepare_config_and_inputs()
      (
          UpperCamelCase__				
      )					:    Tuple          =   config_and_inputs
      UpperCamelCase__					:    List[str]          =   {
          '''input_ids''': input_ids,
          '''token_type_ids''': token_type_ids,
          '''head_mask''': head_mask,
      }

      return config, inputs_dict



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


     def      UpperCAmelCase__	(      self							:     Optional[int]							,	lowerCamelCase__							:     int							,	lowerCamelCase__							:     Dict							,	lowerCamelCase__							:     Dict							,	lowerCamelCase__							:     int							,	lowerCamelCase__							:     str			)  ->      Tuple:





      '''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      UpperCAmelCase__	(      self							:     Dict							,	lowerCamelCase__							:     Optional[int]							,	lowerCamelCase__							:     str							,	lowerCamelCase__							:     Optional[Any]=False			)  ->      str:





      '''simple docstring'''






      UpperCamelCase__					:    Union[str, Any]          =   super()._prepare_for_class(lowerCamelCase__							,	lowerCamelCase__							,	return_labels=lowerCamelCase__			)

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


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      Any:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[int]          =   OpenAIGPTModelTester(self			)
      UpperCamelCase__					:    str          =   ConfigTester(self							,	config_class=lowerCamelCase__							,	n_embd=37			)


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





      '''simple docstring'''






      self.config_tester.run_common_tests()


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





      '''simple docstring'''






      UpperCamelCase__					:    Union[str, Any]          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     Tuple			)  ->      Optional[int]:





      '''simple docstring'''






      UpperCamelCase__					:    List[Any]          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     Union[str, Any]			)  ->      Dict:





      '''simple docstring'''






      UpperCamelCase__					:    Any          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase__			)


     def      UpperCAmelCase__	(      self							:     Optional[int]			)  ->      List[str]:





      '''simple docstring'''






      UpperCamelCase__					:    List[str]          =   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__			)


     @slow
     def      UpperCAmelCase__	(      self							:     int			)  ->      List[str]:





      '''simple docstring'''






      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCamelCase__					:    int          =   OpenAIGPTModel.from_pretrained(lowerCamelCase__			)
       self.assertIsNotNone(lowerCamelCase__			)



@require_torch
class       __magic_name__	(     unittest.TestCase):


     @slow
     def      UpperCAmelCase__	(      self							:     List[Any]			)  ->      Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__					:    Optional[Any]          =   OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''			)
      model.to(lowerCamelCase__			)
      UpperCamelCase__					:    Dict          =   torch.tensor([[481, 4735, 544]]							,	dtype=torch.long							,	device=lowerCamelCase__			)  # the president is
      UpperCamelCase__					:    Union[str, Any]          =   [
          481,
          4735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          40477,
          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

      UpperCamelCase__					:    Any          =   model.generate(lowerCamelCase__							,	do_sample=lowerCamelCase__			)
      self.assertListEqual(output_ids[0].tolist()							,	lowerCamelCase__			)






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__UpperCamelCase       :       Any						       =				logging.get_logger(__name__)

__UpperCamelCase       :       Any						       =				{
    "andreasmadsen/efficient_mlm_m0.40": (
        "https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json"
    ),
}

class       __magic_name__	(     __lowerCAmelCase):
     A:		List[Any]			=      "roberta-prelayernorm"


     def __init__(      self							:     Tuple							,	lowerCamelCase__							:     List[Any]=50265							,	lowerCamelCase__							:     Optional[Any]=768							,	lowerCamelCase__							:     str=12							,	lowerCamelCase__							:     Union[str, Any]=12							,	lowerCamelCase__							:     Dict=3072							,	lowerCamelCase__							:     int="gelu"							,	lowerCamelCase__							:     Tuple=0.1							,	lowerCamelCase__							:     Tuple=0.1							,	lowerCamelCase__							:     List[str]=512							,	lowerCamelCase__							:     int=2							,	lowerCamelCase__							:     Tuple=0.02							,	lowerCamelCase__							:     List[Any]=1E-1_2							,	lowerCamelCase__							:     str=1							,	lowerCamelCase__							:     int=0							,	lowerCamelCase__							:     int=2							,	lowerCamelCase__							:     Union[str, Any]="absolute"							,	lowerCamelCase__							:     Optional[Any]=True							,	lowerCamelCase__							:     Dict=None							,	**lowerCamelCase__							:     Any							,	)  ->      Optional[int]:





      '''simple docstring'''






      super().__init__(pad_token_id=lowerCamelCase__							,	bos_token_id=lowerCamelCase__							,	eos_token_id=lowerCamelCase__							,	**lowerCamelCase__			)

      UpperCamelCase__					:    Optional[int]          =   vocab_size
      UpperCamelCase__					:    Union[str, Any]          =   hidden_size
      UpperCamelCase__					:    List[str]          =   num_hidden_layers
      UpperCamelCase__					:    Optional[int]          =   num_attention_heads
      UpperCamelCase__					:    List[str]          =   hidden_act
      UpperCamelCase__					:    Optional[int]          =   intermediate_size
      UpperCamelCase__					:    Optional[int]          =   hidden_dropout_prob
      UpperCamelCase__					:    List[str]          =   attention_probs_dropout_prob
      UpperCamelCase__					:    Optional[int]          =   max_position_embeddings
      UpperCamelCase__					:    Optional[Any]          =   type_vocab_size
      UpperCamelCase__					:    Union[str, Any]          =   initializer_range
      UpperCamelCase__					:    Dict          =   layer_norm_eps
      UpperCamelCase__					:    Union[str, Any]          =   position_embedding_type
      UpperCamelCase__					:    Optional[int]          =   use_cache
      UpperCamelCase__					:    int          =   classifier_dropout



class       __magic_name__	(     __lowerCAmelCase):


     @property
     def      UpperCAmelCase__	(      self							:     int			)  ->      Mapping[str, Mapping[int, str]]:





      '''simple docstring'''






      if self.task == "multiple-choice":
       UpperCamelCase__					:    Dict          =   {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
      else:
       UpperCamelCase__					:    Any          =   {0: '''batch''', 1: '''sequence'''}
      return OrderedDict(
          [
              ('''input_ids''', dynamic_axis),
              ('''attention_mask''', dynamic_axis),
          ]			)


