

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 SCREAMING_SNAKE_CASE	:
  """simple docstring"""






  def __init__(			self :    Any       ,							lowerCAmelCase :    Optional[Any]       ,							)							->	Tuple:






     """simple docstring"""







     __lowerCAmelCase  :			int					    =						parent
     __lowerCAmelCase  :			str					    =						13
     __lowerCAmelCase  :			Any					    =						7
     __lowerCAmelCase  :			Any					    =						True
     __lowerCAmelCase  :			List[str]					    =						True
     __lowerCAmelCase  :			Union[str, Any]					    =						True
     __lowerCAmelCase  :			Optional[int]					    =						True
     __lowerCAmelCase  :			Any					    =						True
     __lowerCAmelCase  :			str					    =						99
     __lowerCAmelCase  :			int					    =						32
     __lowerCAmelCase  :			List[str]					    =						2
     __lowerCAmelCase  :			Union[str, Any]					    =						4
     __lowerCAmelCase  :			str					    =						37
     __lowerCAmelCase  :			Tuple					    =						"""gelu"""
     __lowerCAmelCase  :			Any					    =						0.1
     __lowerCAmelCase  :			List[str]					    =						0.1
     __lowerCAmelCase  :			str					    =						5_12
     __lowerCAmelCase  :			List[str]					    =						16
     __lowerCAmelCase  :			Tuple					    =						2
     __lowerCAmelCase  :			List[str]					    =						0.02
     __lowerCAmelCase  :			List[str]					    =						3
     __lowerCAmelCase  :			str					    =						4
     __lowerCAmelCase  :			List[Any]					    =						None
     __lowerCAmelCase  :			Dict					    =						self.vocab_size - 1







  def        SCREAMING_SNAKE_CASE    (			self :    Union[str, Any]    )							->	List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :			Any					    =						ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size    )

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

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

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

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

     __lowerCAmelCase  :			Any					    =						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       ,							)

     __lowerCAmelCase  :			int					    =						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 :    Union[str, Any]       ,							lowerCAmelCase :    Any       ,							lowerCAmelCase :    Any       ,							lowerCAmelCase :    Any       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Optional[int]       ,							*lowerCAmelCase :    List[Any]    )							->	Dict:






     """simple docstring"""







     __lowerCAmelCase  :			Dict					    =						TFOpenAIGPTModel(config=snake_case__    )
     __lowerCAmelCase  :			Tuple					    =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
     __lowerCAmelCase  :			Optional[Any]					    =						model(snake_case__    )

     __lowerCAmelCase  :			List[Any]					    =						[input_ids, input_mask]
     __lowerCAmelCase  :			List[Any]					    =						model(snake_case__    )

     __lowerCAmelCase  :			Optional[int]					    =						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 :    Optional[int]       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Tuple       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Optional[int]       ,							*lowerCAmelCase :    Optional[int]    )							->	str:






     """simple docstring"""







     __lowerCAmelCase  :			str					    =						TFOpenAIGPTLMHeadModel(config=snake_case__    )
     __lowerCAmelCase  :			Any					    =						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
     __lowerCAmelCase  :			int					    =						model(snake_case__    )
     self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.vocab_size)    )







  def        SCREAMING_SNAKE_CASE    (			self :    Union[str, Any]       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Dict       ,							lowerCAmelCase :    int       ,							lowerCAmelCase :    Optional[Any]       ,							lowerCAmelCase :    Tuple       ,							*lowerCAmelCase :    List[Any]    )							->	int:






     """simple docstring"""







     __lowerCAmelCase  :			Tuple					    =						TFOpenAIGPTDoubleHeadsModel(config=snake_case__    )

     __lowerCAmelCase  :			Dict					    =						tf.tile(tf.expand_dims(snake_case__       ,							1    )       ,							(1, self.num_choices, 1)    )
     __lowerCAmelCase  :			Tuple					    =						tf.tile(tf.expand_dims(snake_case__       ,							1    )       ,							(1, self.num_choices, 1)    )
     __lowerCAmelCase  :			List[Any]					    =						tf.tile(tf.expand_dims(snake_case__       ,							1    )       ,							(1, self.num_choices, 1)    )

     __lowerCAmelCase  :			Optional[int]					    =						{
         """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,
     }
     __lowerCAmelCase  :			List[Any]					    =						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 :    Dict       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Tuple       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    int       ,							*lowerCAmelCase :    Tuple    )							->	Any:






     """simple docstring"""







     __lowerCAmelCase  :			List[Any]					    =						self.num_labels
     __lowerCAmelCase  :			Tuple					    =						ids_tensor([self.batch_size]       ,							self.type_sequence_label_size    )
     __lowerCAmelCase  :			str					    =						{
         """input_ids""": input_ids,
         """attention_mask""": input_mask,
         """token_type_ids""": token_type_ids,
         """labels""": sequence_labels,
     }
     __lowerCAmelCase  :			List[str]					    =						TFOpenAIGPTForSequenceClassification(snake_case__    )
     __lowerCAmelCase  :			Any					    =						model(snake_case__    )
     self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_labels)    )







  def        SCREAMING_SNAKE_CASE    (			self :    List[str]    )							->	Any:






     """simple docstring"""







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

     (
         (
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,(
         __lowerCAmelCase				
     )    ,				
     )  :			Any					    =						config_and_inputs

     __lowerCAmelCase  :			int					    =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
     return config, inputs_dict






@require_tf
class SCREAMING_SNAKE_CASE	(  a_   ,							a_   ,							unittest.TestCase ):
  """simple docstring"""






  lowerCamelCase    :			List[str]						      =(
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  lowerCamelCase    :			Dict						      =(
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  lowerCamelCase    :			List[str]						      =(
      {
          'feature-extraction': TFOpenAIGPTModel,
          'text-classification': TFOpenAIGPTForSequenceClassification,
          'text-generation': TFOpenAIGPTLMHeadModel,
          'zero-shot': TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  lowerCamelCase    :			Dict						      =False
  lowerCamelCase    :			Tuple						      =False







  def        SCREAMING_SNAKE_CASE    (			self :    str       ,							lowerCAmelCase :    Union[str, Any]       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    List[str]       ,							lowerCAmelCase :    Any       ,							lowerCAmelCase :    Optional[int]    )							->	Dict:






     """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 :    Tuple    )							->	Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :			List[str]					    =						TFOpenAIGPTModelTester(self    )
     __lowerCAmelCase  :			str					    =						ConfigTester(self       ,							config_class=snake_case__       ,							n_embd=37    )







  def        SCREAMING_SNAKE_CASE    (			self :    str    )							->	List[Any]:






     """simple docstring"""







     self.config_tester.run_common_tests()







  def        SCREAMING_SNAKE_CASE    (			self :    Union[str, Any]    )							->	Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :			str					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*snake_case__    )







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






     """simple docstring"""







     __lowerCAmelCase  :			Any					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case__    )







  def        SCREAMING_SNAKE_CASE    (			self :    List[Any]    )							->	Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :			Tuple					    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_double_head(*snake_case__    )







  def        SCREAMING_SNAKE_CASE    (			self :    Dict    )							->	Dict:






     """simple docstring"""







     __lowerCAmelCase  :			Optional[int]					    =						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 :    Union[str, Any]    )							->	Optional[int]:






     """simple docstring"""







     for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase  :			Optional[int]					    =						TFOpenAIGPTModel.from_pretrained(snake_case__    )
        self.assertIsNotNone(snake_case__    )






@require_tf
class SCREAMING_SNAKE_CASE	(  unittest.TestCase ):
  """simple docstring"""






  @slow
  def        SCREAMING_SNAKE_CASE    (			self :    Any    )							->	List[str]:






     """simple docstring"""







     __lowerCAmelCase  :			Dict					    =						TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""    )
     __lowerCAmelCase  :			Optional[int]					    =						tf.convert_to_tensor([[4_81, 47_35, 5_44]]       ,							dtype=tf.intaa    )  # the president is
     __lowerCAmelCase  :			Optional[int]					    =						[
         4_81,
         47_35,
         5_44,
         2_46,
         9_63,
         8_70,
         7_62,
         2_39,
         2_44,
         4_04_77,
         2_44,
         2_49,
         7_19,
         8_81,
         4_87,
         5_44,
         2_40,
         2_44,
         6_03,
         4_81,
     ]  # the president is a very good man. " \n " i\'m sure he is, " said the

     __lowerCAmelCase  :			Tuple					    =						model.generate(snake_case__       ,							do_sample=snake_case__    )
     self.assertListEqual(output_ids[0].numpy().tolist()       ,							snake_case__    )










"""simple docstring"""







from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       if is_torch_available():
              import torch

              if isinstance(lowerCAmelCase		,					torch.Tensor						):
                     return True

       return isinstance(lowerCAmelCase		,					np.ndarray						)





class      UpperCamelCase_       (				a_     ):



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	fields(self )

         # Safety and consistency checks
         if not len(snake_case__ ):
                raise ValueError(f'''{self.__class__.__name__} has no fields.''' )

         UpperCAmelCase					=	getattr(self       ,						class_fields[0].name )
         UpperCAmelCase					=	all(getattr(self       ,						field.name ) is None for field in class_fields[1:] )

         if other_fields_are_none and isinstance(snake_case__       ,						snake_case__ ):
                for key, value in first_field.items():
                       UpperCAmelCase					=	value
         else:
                for field in class_fields:
                       UpperCAmelCase					=	getattr(self       ,						field.name )
                       if v is not None:
                              UpperCAmelCase					=	v



  def __delitem__(					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	List[str]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``pop`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``update`` on a {self.__class__.__name__} instance.''' )



  def __getitem__(					self       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





         if isinstance(snake_case__       ,						snake_case__ ):
                UpperCAmelCase					=	dict(self.items() )
                return inner_dict[k]
         else:
                return self.to_tuple()[k]



  def __setattr__(					self       ,						snake_case__       ,						snake_case__ )	->	Union[str, Any]:



         """simple docstring"""





         if name in self.keys() and value is not None:
                # Don't call self.__setitem__ to avoid recursion errors
                super().__setitem__(snake_case__       ,						snake_case__ )
         super().__setattr__(snake_case__       ,						snake_case__ )



  def __setitem__(					self       ,						snake_case__       ,						snake_case__ )	->	Tuple:



         """simple docstring"""





         super().__setitem__(snake_case__       ,						snake_case__ )
         # Don't call self.__setattr__ to avoid recursion errors
         super().__setattr__(snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Tuple[Any]:



         """simple docstring"""





         return tuple(self[k] for k in self.keys() )
