
from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers.models.openai.modeling_tf_openai import (
      TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFOpenAIGPTDoubleHeadsModel,
      TFOpenAIGPTForSequenceClassification,
      TFOpenAIGPTLMHeadModel,
      TFOpenAIGPTModel,
  )

class __snake_case	:
    def __init__(						self	,snake_case	,):



           '''simple docstring'''







           lowercase							:  List[str] 			=   parent
           lowercase							:  Any 			=   13
           lowercase							:  int 			=   7
           lowercase							:  Tuple 			=   True
           lowercase							:  Dict 			=   True
           lowercase							:  List[Any] 			=   True
           lowercase							:  List[Any] 			=   True
           lowercase							:  str 			=   True
           lowercase							:  Any 			=   99
           lowercase							:  Tuple 			=   32
           lowercase							:  int 			=   2
           lowercase							:  str 			=   4
           lowercase							:  Union[str, Any] 			=   37
           lowercase							:  int 			=   """gelu"""
           lowercase							:  Optional[Any] 			=   0.1
           lowercase							:  Optional[int] 			=   0.1
           lowercase							:  List[str] 			=   512
           lowercase							:  Optional[Any] 			=   16
           lowercase							:  str 			=   2
           lowercase							:  Optional[Any] 			=   0.02
           lowercase							:  int 			=   3
           lowercase							:  List[Any] 			=   4
           lowercase							:  str 			=   None
           lowercase							:  str 			=   self.vocab_size - 1
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  int 			=   ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size   )

           lowercase							:  Tuple 			=   None
           if self.use_input_mask:
                  lowercase							:  int 			=   random_attention_mask([self.batch_size, self.seq_length]   )

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

           lowercase							:  Any 			=   None
           if self.use_mc_token_ids:
                  lowercase							:  List[str] 			=   ids_tensor([self.batch_size, self.num_choices]	,self.seq_length   )

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

           lowercase							:  str 			=   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	,)

           lowercase							:  Tuple 			=   ids_tensor([self.num_hidden_layers, self.num_attention_heads]	,2   )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  Dict 			=   TFOpenAIGPTModel(config=snake_case   )
           lowercase							:  List[Any] 			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           lowercase							:  Any 			=   model(snake_case   )

           lowercase							:  Optional[Any] 			=   [input_ids, input_mask]
           lowercase							:  Tuple 			=   model(snake_case   )

           lowercase							:  Union[str, Any] 			=   model(snake_case   )

           self.parent.assertEqual(result.last_hidden_state.shape	,(self.batch_size, self.seq_length, self.hidden_size)   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  int 			=   TFOpenAIGPTLMHeadModel(config=snake_case   )
           lowercase							:  Optional[Any] 			=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           lowercase							:  str 			=   model(snake_case   )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  Optional[Any] 			=   TFOpenAIGPTDoubleHeadsModel(config=snake_case   )

           lowercase							:  Any 			=   tf.tile(tf.expand_dims(snake_case	,1   )	,(1, self.num_choices, 1)   )
           lowercase							:  Union[str, Any] 			=   tf.tile(tf.expand_dims(snake_case	,1   )	,(1, self.num_choices, 1)   )
           lowercase							:  Union[str, Any] 			=   tf.tile(tf.expand_dims(snake_case	,1   )	,(1, self.num_choices, 1)   )

           lowercase							:  str 			=   {
               """input_ids""": multiple_choice_inputs_ids,
               """mc_token_ids""": mc_token_ids,
               """attention_mask""": multiple_choice_input_mask,
               """token_type_ids""": multiple_choice_token_type_ids,
           }
           lowercase							:  Dict 			=   model(snake_case   )
           self.parent.assertEqual(
               result.logits.shape	,(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)   )
           self.parent.assertEqual(result.mc_logits.shape	,(self.batch_size, self.num_choices)   )
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case	,*snake_case   ):



           '''simple docstring'''







           lowercase							:  Optional[int] 			=   self.num_labels
           lowercase							:  Tuple 			=   ids_tensor([self.batch_size]	,self.type_sequence_label_size   )
           lowercase							:  Union[str, Any] 			=   {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
               """labels""": sequence_labels,
           }
           lowercase							:  str 			=   TFOpenAIGPTForSequenceClassification(snake_case   )
           lowercase							:  Any 			=   model(snake_case   )
           self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.num_labels)   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[Any] 			=   self.prepare_config_and_inputs()

           (
               (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    (
               lowercase
           )			,    
           )							:  List[str] 			=   config_and_inputs

           lowercase							:  str 			=   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
           return config, inputs_dict






@require_tf
class __snake_case	(   lowerCAmelCase      ,			lowerCAmelCase      ,			unittest.TestCase  ):
    _a	:		Any=       (
        (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
        if is_tf_available()
        else ()
    )
    _a	:		Optional[Any]=       (
        (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
    _a	:		str=       (
        {
            "feature-extraction": TFOpenAIGPTModel,
            "text-classification": TFOpenAIGPTForSequenceClassification,
            "text-generation": TFOpenAIGPTLMHeadModel,
            "zero-shot": TFOpenAIGPTForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    _a	:		int=       False
    _a	:		int=       False
    def    _SCREAMING_SNAKE_CASE (						self	,snake_case	,snake_case	,snake_case	,snake_case	,snake_case   ):



           '''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    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  int 			=   TFOpenAIGPTModelTester(self   )
           lowercase							:  Optional[Any] 			=   ConfigTester(self	,config_class=snake_case	,n_embd=37   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







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



           '''simple docstring'''







           lowercase							:  List[str] 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  List[str] 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Dict 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*snake_case   )
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Dict 			=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case   )
    @slow
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase							:  Union[str, Any] 			=   TFOpenAIGPTModel.from_pretrained(snake_case   )
                  self.assertIsNotNone(snake_case   )






@require_tf
class __snake_case	(   unittest.TestCase  ):
    @slow
    def    _SCREAMING_SNAKE_CASE (						self   ):



           '''simple docstring'''







           lowercase							:  Union[str, Any] 			=   TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""   )
           lowercase							:  Dict 			=   tf.convert_to_tensor([[481, 4735, 544]]	,dtype=tf.intaa   )  # the president is
           lowercase							:  Dict 			=   [
               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

           lowercase							:  Optional[Any] 			=   model.generate(snake_case	,do_sample=snake_case   )
           self.assertListEqual(output_ids[0].numpy().tolist()	,snake_case   )


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_keras_nlp_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase						:      Any							=							{
    """configuration_gpt2""": ["""GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPT2Config""", """GPT2OnnxConfig"""],
    """tokenization_gpt2""": ["""GPT2Tokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase						:      List[Any]							=							["""GPT2TokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase						:      str							=							[
      """GPT2_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """GPT2DoubleHeadsModel""",
      """GPT2ForQuestionAnswering""",
      """GPT2ForSequenceClassification""",
      """GPT2ForTokenClassification""",
      """GPT2LMHeadModel""",
      """GPT2Model""",
      """GPT2PreTrainedModel""",
      """load_tf_weights_in_gpt2""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase						:      Tuple							=							[
      """TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFGPT2DoubleHeadsModel""",
      """TFGPT2ForSequenceClassification""",
      """TFGPT2LMHeadModel""",
      """TFGPT2MainLayer""",
      """TFGPT2Model""",
      """TFGPT2PreTrainedModel""",
  ]

try:
  if not is_keras_nlp_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase						:      List[str]							=							["""TFGPT2Tokenizer"""]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase						:      str							=							["""FlaxGPT2LMHeadModel""", """FlaxGPT2Model""", """FlaxGPT2PreTrainedModel"""]

if TYPE_CHECKING:
  from .configuration_gpta import GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTaConfig, GPTaOnnxConfig
  from .tokenization_gpta import GPTaTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_gpta_fast import GPTaTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_gpta import (
        GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
        GPTaDoubleHeadsModel,
        GPTaForQuestionAnswering,
        GPTaForSequenceClassification,
        GPTaForTokenClassification,
        GPTaLMHeadModel,
        GPTaModel,
        GPTaPreTrainedModel,
        load_tf_weights_in_gpta,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_gpta import (
        TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFGPTaDoubleHeadsModel,
        TFGPTaForSequenceClassification,
        TFGPTaLMHeadModel,
        TFGPTaMainLayer,
        TFGPTaModel,
        TFGPTaPreTrainedModel,
    )

  try:
    if not is_keras_nlp_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_gpta_tf import TFGPTaTokenizer

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel, FlaxGPTaPreTrainedModel

else:
  import sys

  lowercase						:      Union[str, Any]							=							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
