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





      def __init__(		self						:					str					,       __lowerCamelCase						:					Tuple					,       __lowerCamelCase						:					Any=1_3					,       __lowerCamelCase						:					Any=7					,       __lowerCamelCase						:					Dict=True					,       __lowerCamelCase						:					Any=True					,       __lowerCamelCase						:					Any=True					,       __lowerCamelCase						:					int=9_9					,       __lowerCamelCase						:					Tuple=3_2					,       __lowerCamelCase						:					List[str]=5					,       __lowerCamelCase						:					int=4					,       __lowerCamelCase						:					Optional[Any]=3_7					,       __lowerCamelCase						:					List[str]="gelu"					,       __lowerCamelCase						:					str=0.1					,       __lowerCamelCase						:					Any=0.1					,       __lowerCamelCase						:					Optional[Any]=5_1_2					,       __lowerCamelCase						:					List[str]=1_6					,       __lowerCamelCase						:					Union[str, Any]=2					,       __lowerCamelCase						:					List[str]=0.02					,       __lowerCamelCase						:					Union[str, Any]=3					,       __lowerCamelCase						:					List[Any]=4					,       __lowerCamelCase						:					Optional[Any]=None					,       )    ->   Dict:






         """simple docstring"""







         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        _lowercase				(		self						:					List[str]       )    ->   Tuple:






         """simple docstring"""







         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        _lowercase				(		self						:					int					,       __lowerCamelCase						:					Union[str, Any]					,       __lowerCamelCase						:					Dict					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					Tuple					,       *__lowerCamelCase						:					int       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     OpenAIGPTModel(config=__lowerCamelCase       )
         model.to(__lowerCamelCase       )
         model.eval()

         UpperCAmelCase							=     model(__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       head_mask=__lowerCamelCase       )
         UpperCAmelCase							=     model(__lowerCamelCase					,       token_type_ids=__lowerCamelCase       )
         UpperCAmelCase							=     model(__lowerCamelCase       )

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





      def        _lowercase				(		self						:					Optional[int]					,       __lowerCamelCase						:					List[str]					,       __lowerCamelCase						:					Tuple					,       __lowerCamelCase						:					Dict					,       __lowerCamelCase						:					Dict					,       *__lowerCamelCase						:					Any       )    ->   Dict:






         """simple docstring"""







         UpperCAmelCase							=     OpenAIGPTLMHeadModel(__lowerCamelCase       )
         model.to(__lowerCamelCase       )
         model.eval()

         UpperCAmelCase							=     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        _lowercase				(		self						:					int					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					Optional[Any]					,       __lowerCamelCase						:					Tuple					,       *__lowerCamelCase						:					Optional[Any]       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     OpenAIGPTDoubleHeadsModel(__lowerCamelCase       )
         model.to(__lowerCamelCase       )
         model.eval()

         UpperCAmelCase							=     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        _lowercase				(		self						:					List[str]					,       __lowerCamelCase						:					Dict					,       __lowerCamelCase						:					Tuple					,       __lowerCamelCase						:					str					,       __lowerCamelCase						:					int					,       *__lowerCamelCase						:					Any       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     self.num_labels
         UpperCAmelCase							=     OpenAIGPTForSequenceClassification(__lowerCamelCase       )
         model.to(__lowerCamelCase       )
         model.eval()

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





      def        _lowercase				(		self						:					Dict       )    ->   int:






         """simple docstring"""







         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   __lowercase				(  __snake_case ,					__snake_case ,					__snake_case ,					unittest.TestCase  ):
      UpperCamelCase								=       (
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      UpperCamelCase								=       (
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      UpperCamelCase								=       (
          {
              '''feature-extraction''': OpenAIGPTModel,
              '''text-classification''': OpenAIGPTForSequenceClassification,
              '''text-generation''': OpenAIGPTLMHeadModel,
              '''zero-shot''': OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )





      def        _lowercase				(		self						:					List[str]					,       __lowerCamelCase						:					Tuple					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Union[str, Any]					,       __lowerCamelCase						:					Optional[Any]       )    ->   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        _lowercase				(		self						:					Tuple					,       __lowerCamelCase						:					Union[str, Any]					,       __lowerCamelCase						:					str					,       __lowerCamelCase						:					List[str]=False       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     super()._prepare_for_class(__lowerCamelCase					,       __lowerCamelCase					,       return_labels=__lowerCamelCase       )

         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=__lowerCamelCase					,       )
               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=__lowerCamelCase					,       )
               UpperCAmelCase							=     torch.zeros(
                   self.model_tester.batch_size					,       dtype=torch.long					,       device=__lowerCamelCase       )
         return inputs_dict





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






         """simple docstring"""







         UpperCAmelCase							=     OpenAIGPTModelTester(self       )
         UpperCAmelCase							=     ConfigTester(self					,       config_class=__lowerCamelCase					,       n_embd=3_7       )





      def        _lowercase				(		self						:					Dict       )    ->   Optional[Any]:






         """simple docstring"""







         self.config_tester.run_common_tests()





      def        _lowercase				(		self						:					List[Any]       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*__lowerCamelCase       )





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






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*__lowerCamelCase       )





      def        _lowercase				(		self						:					str       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_double_lm_head_model(*__lowerCamelCase       )





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






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCamelCase       )





      @slow
      def        _lowercase				(		self						:					Optional[int]       )    ->   Optional[int]:






         """simple docstring"""







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






@require_torch
class   __lowercase				(  unittest.TestCase  ):





      @slow
      def        _lowercase				(		self						:					Any       )    ->   Optional[int]:






         """simple docstring"""







         UpperCAmelCase							=     OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""       )
         model.to(__lowerCamelCase       )
         UpperCAmelCase							=     torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]					,       dtype=torch.long					,       device=__lowerCamelCase       )  # the president is
         UpperCAmelCase							=     [
             4_8_1,
             4_7_3_5,
             5_4_4,
             2_4_6,
             9_6_3,
             8_7_0,
             7_6_2,
             2_3_9,
             2_4_4,
             4_0_4_7_7,
             2_4_4,
             2_4_9,
             7_1_9,
             8_8_1,
             4_8_7,
             5_4_4,
             2_4_0,
             2_4_4,
             6_0_3,
             4_8_1,
         ]  # the president is a very good man. " \n " i\'m sure he is, " said the

         UpperCAmelCase							=     model.generate(__lowerCamelCase					,       do_sample=__lowerCamelCase       )
         self.assertListEqual(output_ids[0].tolist()					,       __lowerCamelCase       )







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


__a     =			{"""tokenization_herbert""": ["""HerbertTokenizer"""]}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __a     =			["""HerbertTokenizerFast"""]


if TYPE_CHECKING:
 from .tokenization_herbert import HerbertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_herbert_fast import HerbertTokenizerFast

else:
 import sys

 __a     =			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



