

'''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 		lowerCAmelCase__  :



 """simple docstring"""



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


      """simple docstring"""




      __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  UpperCAmelCase__						(						self   :			Any     )    ->					Tuple:


      """simple docstring"""




      __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  UpperCAmelCase__						(						self   :			Any   ,				__SCREAMING_SNAKE_CASE   :			int   ,				__SCREAMING_SNAKE_CASE   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			str   ,				__SCREAMING_SNAKE_CASE   :			List[str]   ,				*__SCREAMING_SNAKE_CASE   :			List[Any]     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = OpenAIGPTModel(config=__SCREAMING_SNAKE_CASE     )
      model.to(__SCREAMING_SNAKE_CASE     )
      model.eval()

      __SCREAMING_SNAKE_CASE      = model(__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE   ,				head_mask=__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = model(__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = model(__SCREAMING_SNAKE_CASE     )

      self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)     )
 def  UpperCAmelCase__						(						self   :			str   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Any   ,				__SCREAMING_SNAKE_CASE   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]   ,				*__SCREAMING_SNAKE_CASE   :			str     )    ->					Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = OpenAIGPTLMHeadModel(__SCREAMING_SNAKE_CASE     )
      model.to(__SCREAMING_SNAKE_CASE     )
      model.eval()

      __SCREAMING_SNAKE_CASE      = model(__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE   ,				labels=__SCREAMING_SNAKE_CASE     )
      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   ,				__SCREAMING_SNAKE_CASE   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			str   ,				__SCREAMING_SNAKE_CASE   :			Any   ,				*__SCREAMING_SNAKE_CASE   :			Dict     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = OpenAIGPTDoubleHeadsModel(__SCREAMING_SNAKE_CASE     )
      model.to(__SCREAMING_SNAKE_CASE     )
      model.eval()

      __SCREAMING_SNAKE_CASE      = model(__SCREAMING_SNAKE_CASE   ,				token_type_ids=__SCREAMING_SNAKE_CASE   ,				labels=__SCREAMING_SNAKE_CASE     )
      self.parent.assertEqual(result.loss.shape   ,				()     )
      self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)     )
 def  UpperCAmelCase__						(						self   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Any   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]   ,				__SCREAMING_SNAKE_CASE   :			Dict   ,				__SCREAMING_SNAKE_CASE   :			int   ,				*__SCREAMING_SNAKE_CASE   :			str     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.num_labels
      __SCREAMING_SNAKE_CASE      = OpenAIGPTForSequenceClassification(__SCREAMING_SNAKE_CASE     )
      model.to(__SCREAMING_SNAKE_CASE     )
      model.eval()

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


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


      """simple docstring"""




      __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__  ( a					,   a					,   a					,   unittest.TestCase   ):



 """simple docstring"""



 lowerCAmelCase__       = (
     (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
     if is_torch_available()
     else ()
 )
 lowerCAmelCase__       = (
     (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 lowerCAmelCase__       = (
     {
         "feature-extraction": OpenAIGPTModel,
         "text-classification": OpenAIGPTForSequenceClassification,
         "text-generation": OpenAIGPTLMHeadModel,
         "zero-shot": OpenAIGPTForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def  UpperCAmelCase__						(						self   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			int   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]   ,				__SCREAMING_SNAKE_CASE   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			Optional[int]     )    ->					Optional[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  UpperCAmelCase__						(						self   :			List[str]   ,				__SCREAMING_SNAKE_CASE   :			List[str]   ,				__SCREAMING_SNAKE_CASE   :			List[Any]   ,				__SCREAMING_SNAKE_CASE   :			Dict=False     )    ->					str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = super()._prepare_for_class(__SCREAMING_SNAKE_CASE   ,				__SCREAMING_SNAKE_CASE   ,				return_labels=__SCREAMING_SNAKE_CASE     )

      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=__SCREAMING_SNAKE_CASE   ,				)
                __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=__SCREAMING_SNAKE_CASE   ,				)
                __SCREAMING_SNAKE_CASE      = torch.zeros(
                    self.model_tester.batch_size   ,				dtype=torch.long   ,				device=__SCREAMING_SNAKE_CASE     )
      return inputs_dict
 def  UpperCAmelCase__						(						self   :			Optional[Any]     )    ->					Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = OpenAIGPTModelTester(self     )
      __SCREAMING_SNAKE_CASE      = ConfigTester(self   ,				config_class=__SCREAMING_SNAKE_CASE   ,				n_embd=37     )
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					Optional[int]:


      """simple docstring"""




      self.config_tester.run_common_tests()
 def  UpperCAmelCase__						(						self   :			Dict     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*__SCREAMING_SNAKE_CASE     )
 def  UpperCAmelCase__						(						self   :			str     )    ->					int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*__SCREAMING_SNAKE_CASE     )
 def  UpperCAmelCase__						(						self   :			Union[str, Any]     )    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*__SCREAMING_SNAKE_CASE     )
 def  UpperCAmelCase__						(						self   :			int     )    ->					List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__SCREAMING_SNAKE_CASE     )


 @slow
 def  UpperCAmelCase__						(						self   :			int     )    ->					int:


      """simple docstring"""




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







@require_torch
class 		lowerCAmelCase__  ( unittest.TestCase   ):



 """simple docstring"""



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


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""     )
      model.to(__SCREAMING_SNAKE_CASE     )
      __SCREAMING_SNAKE_CASE      = torch.tensor([[481, 4_735, 544]]   ,				dtype=torch.long   ,				device=__SCREAMING_SNAKE_CASE     )  # 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(__SCREAMING_SNAKE_CASE   ,				do_sample=__SCREAMING_SNAKE_CASE     )
      self.assertListEqual(output_ids[0].tolist()   ,				__SCREAMING_SNAKE_CASE     )









'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase					:			Tuple       =	logging.get_logger(__name__)

UpperCAmelCase					:			Any       =	{'ctrl': 'https://huggingface.co/ctrl/resolve/main/config.json'}







class 		lowerCAmelCase__  ( a   ):



 """simple docstring"""



 lowerCAmelCase__       = "ctrl"
 lowerCAmelCase__       = ["past_key_values"]
 lowerCAmelCase__       = {
     "max_position_embeddings": "n_positions",
     "hidden_size": "n_embd",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }
 def __init__(						self   :			Tuple   ,				__SCREAMING_SNAKE_CASE   :			Tuple=246_534   ,				__SCREAMING_SNAKE_CASE   :			str=256   ,				__SCREAMING_SNAKE_CASE   :			Optional[Any]=1_280   ,				__SCREAMING_SNAKE_CASE   :			Dict=8_192   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]=48   ,				__SCREAMING_SNAKE_CASE   :			Dict=16   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]=0.1   ,				__SCREAMING_SNAKE_CASE   :			str=0.1   ,				__SCREAMING_SNAKE_CASE   :			Union[str, Any]=1E-6   ,				__SCREAMING_SNAKE_CASE   :			Tuple=0.02   ,				__SCREAMING_SNAKE_CASE   :			Tuple=True   ,				**__SCREAMING_SNAKE_CASE   :			Union[str, Any]   ,				)    ->					Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE      = vocab_size
      __SCREAMING_SNAKE_CASE      = n_positions
      __SCREAMING_SNAKE_CASE      = n_embd
      __SCREAMING_SNAKE_CASE      = n_layer
      __SCREAMING_SNAKE_CASE      = n_head
      __SCREAMING_SNAKE_CASE      = dff
      __SCREAMING_SNAKE_CASE      = resid_pdrop
      __SCREAMING_SNAKE_CASE      = embd_pdrop
      __SCREAMING_SNAKE_CASE      = layer_norm_epsilon
      __SCREAMING_SNAKE_CASE      = initializer_range

      __SCREAMING_SNAKE_CASE      = use_cache

      super().__init__(**__SCREAMING_SNAKE_CASE     )






