


'''simple docstring'''

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





      '''simple docstring'''






      def __init__(self   :   List[str]       ,						UpperCamelCase   :   Dict       ,						):

             '''simple docstring'''







             lowercase__				      =			parent
             lowercase__				      =			13
             lowercase__				      =			7
             lowercase__				      =			True
             lowercase__				      =			True
             lowercase__				      =			True
             lowercase__				      =			True
             lowercase__				      =			True
             lowercase__				      =			99
             lowercase__				      =			32
             lowercase__				      =			2
             lowercase__				      =			4
             lowercase__				      =			37
             lowercase__				      =			'''gelu'''
             lowercase__				      =			0.1
             lowercase__				      =			0.1
             lowercase__				      =			512
             lowercase__				      =			16
             lowercase__				      =			2
             lowercase__				      =			0.02
             lowercase__				      =			3
             lowercase__				      =			4
             lowercase__				      =			None
             lowercase__				      =			self.vocab_size - 1



      def 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size			)

             lowercase__				      =			None
             if self.use_input_mask:
                    lowercase__				      =			random_attention_mask([self.batch_size, self.seq_length]			)

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

             lowercase__				      =			None
             if self.use_mc_token_ids:
                    lowercase__				      =			ids_tensor([self.batch_size, self.num_choices]       ,						self.seq_length			)

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

             lowercase__				      =			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__				      =			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 				UpperCamelCase__		(self   :   List[Any]       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   Tuple       ,						UpperCamelCase   :   List[str]       ,						UpperCamelCase   :   Optional[int]       ,						UpperCamelCase   :   List[str]       ,						*UpperCamelCase   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			TFOpenAIGPTModel(config=UpperCamelCase			)
             lowercase__				      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase__				      =			model(UpperCamelCase			)

             lowercase__				      =			[input_ids, input_mask]
             lowercase__				      =			model(UpperCamelCase			)

             lowercase__				      =			model(UpperCamelCase			)

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



      def 				UpperCamelCase__		(self   :   int       ,						UpperCamelCase   :   List[Any]       ,						UpperCamelCase   :   Optional[Any]       ,						UpperCamelCase   :   Any       ,						UpperCamelCase   :   Tuple       ,						UpperCamelCase   :   Dict       ,						*UpperCamelCase   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			TFOpenAIGPTLMHeadModel(config=UpperCamelCase			)
             lowercase__				      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase__				      =			model(UpperCamelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size)			)



      def 				UpperCamelCase__		(self   :   str       ,						UpperCamelCase   :   List[str]       ,						UpperCamelCase   :   int       ,						UpperCamelCase   :   Optional[Any]       ,						UpperCamelCase   :   Optional[int]       ,						UpperCamelCase   :   Tuple       ,						UpperCamelCase   :   Optional[Any]       ,						*UpperCamelCase   :   Any			):

             '''simple docstring'''







             lowercase__				      =			TFOpenAIGPTDoubleHeadsModel(config=UpperCamelCase			)

             lowercase__				      =			tf.tile(tf.expand_dims(UpperCamelCase       ,						1			)       ,						(1, self.num_choices, 1)			)
             lowercase__				      =			tf.tile(tf.expand_dims(UpperCamelCase       ,						1			)       ,						(1, self.num_choices, 1)			)
             lowercase__				      =			tf.tile(tf.expand_dims(UpperCamelCase       ,						1			)       ,						(1, self.num_choices, 1)			)

             lowercase__				      =			{
                 '''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__				      =			model(UpperCamelCase			)
             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 				UpperCamelCase__		(self   :   Optional[int]       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   int       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   Union[str, Any]       ,						UpperCamelCase   :   Optional[int]       ,						*UpperCamelCase   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.num_labels
             lowercase__				      =			ids_tensor([self.batch_size]       ,						self.type_sequence_label_size			)
             lowercase__				      =			{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
                 '''token_type_ids''': token_type_ids,
                 '''labels''': sequence_labels,
             }
             lowercase__				      =			TFOpenAIGPTForSequenceClassification(UpperCamelCase			)
             lowercase__				      =			model(UpperCamelCase			)
             self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels)			)






      def 				UpperCamelCase__		(self   :   Optional[int]			):

             '''simple docstring'''







             lowercase__				      =			self.prepare_config_and_inputs()

             (
                 (
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,(
                 lowercase__
             )      ,
             )				      =			config_and_inputs

             lowercase__				      =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_tf
class   __lowerCAmelCase						(lowercase_  ,					lowercase_  ,					unittest.TestCase					):





      '''simple docstring'''






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



      def 				UpperCamelCase__		(self   :   List[Any]       ,						UpperCamelCase   :   Optional[int]       ,						UpperCamelCase   :   int       ,						UpperCamelCase   :   Tuple       ,						UpperCamelCase   :   str       ,						UpperCamelCase   :   Union[str, 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 				UpperCamelCase__		(self   :   Union[str, Any]			):

             '''simple docstring'''







             lowercase__				      =			TFOpenAIGPTModelTester(self			)
             lowercase__				      =			ConfigTester(self       ,						config_class=UpperCamelCase       ,						n_embd=37			)



      def 				UpperCamelCase__		(self   :   Tuple			):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def 				UpperCamelCase__		(self   :   Tuple			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Optional[Any]			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   str			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_double_head(*UpperCamelCase			)



      def 				UpperCamelCase__		(self   :   Dict			):

             '''simple docstring'''







             lowercase__				      =			self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase			)






      @slow
      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    lowercase__				      =			TFOpenAIGPTModel.from_pretrained(UpperCamelCase			)
                    self.assertIsNotNone(UpperCamelCase			)






@require_tf
class   __lowerCAmelCase						(unittest.TestCase					):





      '''simple docstring'''






      @slow
      def 				UpperCamelCase__		(self   :   int			):

             '''simple docstring'''







             lowercase__				      =			TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''			)
             lowercase__				      =			tf.convert_to_tensor([[481, 4735, 544]]       ,						dtype=tf.intaa			)  # the president is
             lowercase__				      =			[
                 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__				      =			model.generate(UpperCamelCase       ,						do_sample=UpperCamelCase			)
             self.assertListEqual(output_ids[0].numpy().tolist()       ,						UpperCamelCase			)








'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


lowerCamelCase							:       Tuple  						=     {'configuration_bert_generation': ['BertGenerationConfig']}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase							:       Union[str, Any]  						=     ['BertGenerationTokenizer']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase							:       Dict  						=     [
     'BertGenerationDecoder',
     'BertGenerationEncoder',
     'BertGenerationPreTrainedModel',
     'load_tf_weights_in_bert_generation',
 ]


if TYPE_CHECKING:
 from .configuration_bert_generation import BertGenerationConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_bert_generation import BertGenerationTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_bert_generation import (
      BertGenerationDecoder,
      BertGenerationEncoder,
      BertGenerationPreTrainedModel,
      load_tf_weights_in_bert_generation,
  )

else:
 import sys

 lowerCamelCase							:       int  						=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




