





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=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.02				,							_UpperCamelCase=3				,							_UpperCamelCase=4				,							_UpperCamelCase=None				,							):
        _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					):
        _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					):
        _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					):
        _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					):
        _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					):
        _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					):
        _UpperCAmelCase           =   self.prepare_config_and_inputs()
        (
            (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _UpperCAmelCase
        )       ,      (
            _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 			__UpperCamelCase    (							A__	, A__	, A__	, unittest.TestCase    ):
     __A	:  List[str]							  =   (
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     __A	:  Any							  =   (
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     __A	:  str							  =   (
         {
             """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					):
        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					):
        _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					):
        _UpperCAmelCase           =   OpenAIGPTModelTester(self					)
        _UpperCAmelCase           =   ConfigTester(self				,							config_class=_UpperCamelCase				,							n_embd=37					)





     def 		UpperCamelCase(     self					):
        self.config_tester.run_common_tests()





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*_UpperCamelCase					)





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_UpperCamelCase					)





     @slow
     def 		UpperCamelCase(     self					):
        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _UpperCAmelCase           =   OpenAIGPTModel.from_pretrained(_UpperCamelCase					)
           self.assertIsNotNone(_UpperCamelCase					)




@require_torch
class 			__UpperCamelCase    (							unittest.TestCase    ):





     @slow
     def 		UpperCamelCase(     self					):
        _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 ..utils import DummyObject, requires_backends




class 			__UpperCamelCase    (							metaclass=A__    ):
     __A	:  str							  =   ["""torch""", """scipy"""]





     def __init__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(self				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)