






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






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase=13		,		_UpperCAmelCase=7		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=99		,		_UpperCAmelCase=32		,		_UpperCAmelCase=5		,		_UpperCAmelCase=4		,		_UpperCAmelCase=37		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=512		,		_UpperCAmelCase=16		,		_UpperCAmelCase=2		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=3		,		_UpperCAmelCase=4		,		_UpperCAmelCase=None		,		)			-> Dict:
     UpperCamelCase_									=     parent
     UpperCamelCase_									=     batch_size
     UpperCamelCase_									=     seq_length
     UpperCamelCase_									=     is_training
     UpperCamelCase_									=     use_token_type_ids
     UpperCamelCase_									=     use_labels
     UpperCamelCase_									=     vocab_size
     UpperCamelCase_									=     hidden_size
     UpperCamelCase_									=     num_hidden_layers
     UpperCamelCase_									=     num_attention_heads
     UpperCamelCase_									=     intermediate_size
     UpperCamelCase_									=     hidden_act
     UpperCamelCase_									=     hidden_dropout_prob
     UpperCamelCase_									=     attention_probs_dropout_prob
     UpperCamelCase_									=     max_position_embeddings
     UpperCamelCase_									=     type_vocab_size
     UpperCamelCase_									=     type_sequence_label_size
     UpperCamelCase_									=     initializer_range
     UpperCamelCase_									=     num_labels
     UpperCamelCase_									=     num_choices
     UpperCamelCase_									=     scope
     UpperCamelCase_									=     self.vocab_size - 1




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size )

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

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

     UpperCamelCase_									=     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_									=     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 )			-> List[Any]:
     UpperCamelCase_									=     OpenAIGPTModel(config=UpperCamelCase_ )
     model.to(UpperCamelCase_ )
     model.eval()

     UpperCamelCase_									=     model(UpperCamelCase_		,		token_type_ids=UpperCamelCase_		,		head_mask=UpperCamelCase_ )
     UpperCamelCase_									=     model(UpperCamelCase_		,		token_type_ids=UpperCamelCase_ )
     UpperCamelCase_									=     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 )			-> int:
     UpperCamelCase_									=     OpenAIGPTLMHeadModel(UpperCamelCase_ )
     model.to(UpperCamelCase_ )
     model.eval()

     UpperCamelCase_									=     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 )			-> Dict:
     UpperCamelCase_									=     OpenAIGPTDoubleHeadsModel(UpperCamelCase_ )
     model.to(UpperCamelCase_ )
     model.eval()

     UpperCamelCase_									=     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:
     UpperCamelCase_									=     self.num_labels
     UpperCamelCase_									=     OpenAIGPTForSequenceClassification(UpperCamelCase_ )
     model.to(UpperCamelCase_ )
     model.eval()

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




  def    _UpperCAmelCase     (     self )			-> Dict:
     UpperCamelCase_									=     self.prepare_config_and_inputs()
     (
         UpperCamelCase_ 
     )									=     config_and_inputs
     UpperCamelCase_									=     {
         "input_ids": input_ids,
         "token_type_ids": token_type_ids,
         "head_mask": head_mask,
     }

     return config, inputs_dict




@require_torch
class 						_a       (  UpperCAmelCase__						,       UpperCAmelCase__						,       UpperCAmelCase__						,       unittest.TestCase    ):






  """simple docstring"""





  A_						=   (
      (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
      if is_torch_available()
      else ()
  )
  A_						=   (
      (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  A_						=   (
      {
          "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 )			-> List[Any]:
     UpperCamelCase_									=     super()._prepare_for_class(UpperCamelCase_		,		UpperCamelCase_		,		return_labels=UpperCamelCase_ )

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




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     OpenAIGPTModelTester(self )
     UpperCamelCase_									=     ConfigTester(self		,		config_class=UpperCamelCase_		,		n_embd=37 )




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




  def    _UpperCAmelCase     (     self )			-> Any:
     UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase_ )




  def    _UpperCAmelCase     (     self )			-> int:
     UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_ )




  def    _UpperCAmelCase     (     self )			-> List[Any]:
     UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase_ )




  def    _UpperCAmelCase     (     self )			-> List[str]:
     UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase_ )




  @slow
  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase_									=     OpenAIGPTModel.from_pretrained(UpperCamelCase_ )
        self.assertIsNotNone(UpperCamelCase_ )




@require_torch
class 						_a       (  unittest.TestCase    ):






  """simple docstring"""





  @slow
  def    _UpperCAmelCase     (     self )			-> int:
     UpperCamelCase_									=     OpenAIGPTLMHeadModel.from_pretrained('openai-gpt' )
     model.to(UpperCamelCase_ )
     UpperCamelCase_									=     torch.tensor([[481, 4735, 544]]		,		dtype=torch.long		,		device=UpperCamelCase_ )  # the president is
     UpperCamelCase_									=     [
         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_									=     model.generate(UpperCamelCase_		,		do_sample=UpperCamelCase_ )
     self.assertListEqual(output_ids[0].tolist()		,		UpperCamelCase_ )













from typing import Any






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      ):
   _validation(
       __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      )
   # Creates data structures and fill initial step
   UpperCamelCase_									=     {}
   UpperCamelCase_									=     {}
   for state in states_space:
      UpperCamelCase_									=     observations_space[0]
      UpperCamelCase_									=     (
          initial_probabilities[state] * emission_probabilities[state][observation]
      )
      UpperCamelCase_									=     None

   # Fills the data structure with the probabilities of
   # different transitions and pointers to previous states
   for o in range(1    ,      len(__lowercase)):
      UpperCamelCase_									=     observations_space[o]
      UpperCamelCase_									=     observations_space[o - 1]
      for state in states_space:
         # Calculates the argmax for probability function
         UpperCamelCase_									=     ''
         UpperCamelCase_									=     -1
         for k_state in states_space:
            UpperCamelCase_									=     (
                probabilities[(k_state, prior_observation)]
                * transition_probabilities[k_state][state]
                * emission_probabilities[state][observation]
            )
            if probability > max_probability:
               UpperCamelCase_									=     probability
               UpperCamelCase_									=     k_state

            # Update probabilities and pointers dicts
         UpperCamelCase_									=     (
             probabilities[(arg_max, prior_observation)]
             * transition_probabilities[arg_max][state]
             * emission_probabilities[state][observation]
         )

         UpperCamelCase_									=     arg_max

    # The final observation
   UpperCamelCase_									=     observations_space[len(__lowercase) - 1]

   # argmax for given final observation
   UpperCamelCase_									=     ''
   UpperCamelCase_									=     -1
   for k_state in states_space:
      UpperCamelCase_									=     probabilities[(k_state, final_observation)]
      if probability > max_probability:
         UpperCamelCase_									=     probability
         UpperCamelCase_									=     k_state
   UpperCamelCase_									=     arg_max

   # Process pointers backwards
   UpperCamelCase_									=     last_state
   UpperCamelCase_									=     []
   for o in range(len(__lowercase) - 1    ,      -1    ,      -1):
      result.append(__lowercase)
      UpperCamelCase_									=     pointers[previous, observations_space[o]]
   result.reverse()

   return result






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      ):
   _validate_not_empty(
       __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      )
   _validate_lists(__lowercase    ,      __lowercase)
   _validate_dicts(
       __lowercase    ,      __lowercase    ,      __lowercase)






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase    ,      ):
   if not all(
       [
           observations_space,
           states_space,
           initial_probabilities,
           transition_probabilities,
           emission_probabilities,
       ]):
      raise ValueError('There\'s an empty parameter')






def     _snake_case    (__lowercase    ,      __lowercase):
   _validate_list(__lowercase    ,      'observations_space')
   _validate_list(__lowercase    ,      'states_space')






def     _snake_case    (__lowercase    ,      __lowercase):
   if not isinstance(_object    ,      __lowercase):
      UpperCamelCase_									=     f"""{var_name} must be a list"""
      raise ValueError(__lowercase)
   else:
      for x in _object:
         if not isinstance(__lowercase    ,      __lowercase):
            UpperCamelCase_									=     f"""{var_name} must be a list of strings"""
            raise ValueError(__lowercase)






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase    ,      ):
   _validate_dict(__lowercase    ,      'initial_probabilities'    ,      __lowercase)
   _validate_nested_dict(__lowercase    ,      'transition_probabilities')
   _validate_nested_dict(__lowercase    ,      'emission_probabilities')






def     _snake_case    (__lowercase    ,      __lowercase):
   _validate_dict(_object    ,      __lowercase    ,      __lowercase)
   for x in _object.values():
      _validate_dict(__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase)






def     _snake_case    (__lowercase    ,      __lowercase    ,      __lowercase    ,      __lowercase = False):
   if not isinstance(_object    ,      __lowercase):
      UpperCamelCase_									=     f"""{var_name} must be a dict"""
      raise ValueError(__lowercase)
   if not all(isinstance(__lowercase    ,      __lowercase) for x in _object):
      UpperCamelCase_									=     f"""{var_name} all keys must be strings"""
      raise ValueError(__lowercase)
   if not all(isinstance(__lowercase    ,      __lowercase) for x in _object.values()):
      UpperCamelCase_									=     'nested dictionary ' if nested else ''
      UpperCamelCase_									=     f"""{var_name} {nested_text}all values must be {value_type.__name__}"""
      raise ValueError(__lowercase)


if __name__ == "__main__":
 from doctest import testmod

 testmod()





