


"""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 		snake_case_:






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






     def   lowerCamelCase__  (						self	:       List[Any]					):
           lowerCAmelCase       :						Union[str, Any]							= ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size					)

           lowerCAmelCase       :						List[str]							= None
           if self.use_input_mask:
                 lowerCAmelCase       :						List[str]							= random_attention_mask([self.batch_size, self.seq_length]					)

           lowerCAmelCase       :						Optional[int]							= None
           if self.use_token_type_ids:
                 lowerCAmelCase       :						str							= ids_tensor([self.batch_size, self.seq_length]       ,					self.type_vocab_size					)

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

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

           lowerCAmelCase       :						Tuple							= 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       :						List[str]							= 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   lowerCamelCase__  (						self	:       Tuple       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       Optional[Any]       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       Optional[int]       ,					*UpperCamelCase_	:       List[Any]					):
           lowerCAmelCase       :						Union[str, Any]							= TFOpenAIGPTModel(config=UpperCamelCase_					)
           lowerCAmelCase       :						int							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowerCAmelCase       :						Dict							= model(UpperCamelCase_					)

           lowerCAmelCase       :						str							= [input_ids, input_mask]
           lowerCAmelCase       :						Optional[Any]							= model(UpperCamelCase_					)

           lowerCAmelCase       :						List[Any]							= model(UpperCamelCase_					)

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






     def   lowerCamelCase__  (						self	:       Optional[Any]       ,					UpperCamelCase_	:       Optional[int]       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       Tuple       ,					*UpperCamelCase_	:       List[str]					):
           lowerCAmelCase       :						List[str]							= TFOpenAIGPTLMHeadModel(config=UpperCamelCase_					)
           lowerCAmelCase       :						List[Any]							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           lowerCAmelCase       :						Tuple							= model(UpperCamelCase_					)
           self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.vocab_size)					)






     def   lowerCamelCase__  (						self	:       List[str]       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       List[Any]       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       Optional[int]       ,					UpperCamelCase_	:       Dict       ,					*UpperCamelCase_	:       int					):
           lowerCAmelCase       :						int							= TFOpenAIGPTDoubleHeadsModel(config=UpperCamelCase_					)

           lowerCAmelCase       :						Tuple							= tf.tile(tf.expand_dims(UpperCamelCase_       ,					1					)       ,					(1, self.num_choices, 1)					)
           lowerCAmelCase       :						Tuple							= tf.tile(tf.expand_dims(UpperCamelCase_       ,					1					)       ,					(1, self.num_choices, 1)					)
           lowerCAmelCase       :						str							= tf.tile(tf.expand_dims(UpperCamelCase_       ,					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       :						Optional[Any]							= 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   lowerCamelCase__  (						self	:       Dict       ,					UpperCamelCase_	:       List[str]       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       str       ,					*UpperCamelCase_	:       int					):
           lowerCAmelCase       :						Optional[Any]							= self.num_labels
           lowerCAmelCase       :						Optional[Any]							= ids_tensor([self.batch_size]       ,					self.type_sequence_label_size					)
           lowerCAmelCase       :						Dict							= {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
               '''labels''': sequence_labels,
           }
           lowerCAmelCase       :						List[Any]							= TFOpenAIGPTForSequenceClassification(UpperCamelCase_					)
           lowerCAmelCase       :						Optional[Any]							= model(UpperCamelCase_					)
           self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)					)






     def   lowerCamelCase__  (						self	:       Union[str, Any]					):
           lowerCAmelCase       :						Tuple							= self.prepare_config_and_inputs()

           (
               (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   
           )       :						Dict							= config_and_inputs

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



@require_tf
class 		snake_case_(							a__				,       a__				,       unittest.TestCase      ):
     __UpperCamelCase	=       (
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     __UpperCamelCase	=       (
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     __UpperCamelCase	=       (
         {
             '''feature-extraction''': TFOpenAIGPTModel,
             '''text-classification''': TFOpenAIGPTForSequenceClassification,
             '''text-generation''': TFOpenAIGPTLMHeadModel,
             '''zero-shot''': TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __UpperCamelCase	=       False
     __UpperCamelCase	=       False






     def   lowerCamelCase__  (						self	:       Union[str, Any]       ,					UpperCamelCase_	:       Any       ,					UpperCamelCase_	:       Optional[Any]       ,					UpperCamelCase_	:       List[Any]       ,					UpperCamelCase_	:       Union[str, Any]       ,					UpperCamelCase_	:       List[str]					):
           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   lowerCamelCase__  (						self	:       int					):
           lowerCAmelCase       :						List[str]							= TFOpenAIGPTModelTester(self					)
           lowerCAmelCase       :						Any							= ConfigTester(self       ,					config_class=UpperCamelCase_       ,					n_embd=3_7					)






     def   lowerCamelCase__  (						self	:       Optional[Any]					):
           self.config_tester.run_common_tests()






     def   lowerCamelCase__  (						self	:       Optional[int]					):
           lowerCAmelCase       :						int							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       str					):
           lowerCAmelCase       :						Optional[int]							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       str					):
           lowerCAmelCase       :						Any							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Tuple					):
           lowerCAmelCase       :						Optional[int]							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase_					)






     @slow
     def   lowerCamelCase__  (						self	:       Union[str, Any]					):
           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase       :						Union[str, Any]							= TFOpenAIGPTModel.from_pretrained(UpperCamelCase_					)
                 self.assertIsNotNone(UpperCamelCase_					)



@require_tf
class 		snake_case_(							unittest.TestCase      ):






     @slow
     def   lowerCamelCase__  (						self	:       int					):
           lowerCAmelCase       :						int							= TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''					)
           lowerCAmelCase       :						Dict							= tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]       ,					dtype=tf.intaa					)  # the president is
           lowerCAmelCase       :						List[str]							= [
               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

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






"""simple docstring"""





from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case__		:  Optional[Any]     					=					logging.get_logger(__name__)

snake_case__		:  str     					=					{
    '''microsoft/prophetnet-large-uncased''': (
        '''https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'''
    ),
}



class 		snake_case_(							a__      ):
     __UpperCamelCase	=       '''prophetnet'''
     __UpperCamelCase	=       ['''past_key_values''']
     __UpperCamelCase	=       {
         '''num_attention_heads''': '''num_encoder_attention_heads''',
     }






     def __init__(						self	:       Union[str, Any]       ,					UpperCamelCase_	:       Optional[float] = 0.1       ,					UpperCamelCase_	:       Optional[Union[str, Callable]] = "gelu"       ,					UpperCamelCase_	:       Optional[int] = 3_0_5_2_2       ,					UpperCamelCase_	:       Optional[int] = 1_0_2_4       ,					UpperCamelCase_	:       Optional[int] = 4_0_9_6       ,					UpperCamelCase_	:       Optional[int] = 1_2       ,					UpperCamelCase_	:       Optional[int] = 1_6       ,					UpperCamelCase_	:       Optional[int] = 4_0_9_6       ,					UpperCamelCase_	:       Optional[int] = 1_2       ,					UpperCamelCase_	:       Optional[int] = 1_6       ,					UpperCamelCase_	:       Optional[float] = 0.1       ,					UpperCamelCase_	:       Optional[float] = 0.1       ,					UpperCamelCase_	:       Optional[int] = 5_1_2       ,					UpperCamelCase_	:       Optional[float] = 0.02       ,					UpperCamelCase_	:       Optional[bool] = True       ,					UpperCamelCase_	:       Optional[bool] = True       ,					UpperCamelCase_	:       Optional[int] = 0       ,					UpperCamelCase_	:       Optional[int] = 2       ,					UpperCamelCase_	:       Optional[int] = 3_2       ,					UpperCamelCase_	:       Optional[int] = 1_2_8       ,					UpperCamelCase_	:       Optional[bool] = False       ,					UpperCamelCase_	:       Optional[float] = 0.0       ,					UpperCamelCase_	:       Optional[bool] = True       ,					UpperCamelCase_	:       Optional[int] = 0       ,					UpperCamelCase_	:       Optional[int] = 1       ,					UpperCamelCase_	:       Optional[int] = 2       ,					**UpperCamelCase_	:       List[Any]       ,					):
           lowerCAmelCase       :						Union[str, Any]							= vocab_size
           lowerCAmelCase       :						Any							= hidden_size
           lowerCAmelCase       :						Tuple							= encoder_ffn_dim
           lowerCAmelCase       :						str							= num_encoder_layers
           lowerCAmelCase       :						int							= num_encoder_attention_heads
           lowerCAmelCase       :						Optional[Any]							= decoder_ffn_dim
           lowerCAmelCase       :						Union[str, Any]							= num_decoder_layers
           lowerCAmelCase       :						Union[str, Any]							= num_decoder_attention_heads
           lowerCAmelCase       :						List[Any]							= max_position_embeddings
           lowerCAmelCase       :						Dict							= init_std  # Normal(0, this parameter)
           lowerCAmelCase       :						Optional[int]							= activation_function

           # parameters for prophetnet
           lowerCAmelCase       :						Tuple							= ngram
           lowerCAmelCase       :						List[Any]							= num_buckets
           lowerCAmelCase       :						Optional[Any]							= relative_max_distance
           lowerCAmelCase       :						Dict							= disable_ngram_loss
           lowerCAmelCase       :						Optional[int]							= eps

           # 3 Types of Dropout
           lowerCAmelCase       :						Any							= attention_dropout
           lowerCAmelCase       :						List[Any]							= activation_dropout
           lowerCAmelCase       :						Tuple							= dropout

           lowerCAmelCase       :						Optional[int]							= use_cache

           super().__init__(
               pad_token_id=UpperCamelCase_       ,					bos_token_id=UpperCamelCase_       ,					eos_token_id=UpperCamelCase_       ,					is_encoder_decoder=UpperCamelCase_       ,					add_cross_attention=UpperCamelCase_       ,					decoder_start_token_id=UpperCamelCase_       ,					**UpperCamelCase_       ,					)






     @property
     def   lowerCamelCase__  (						self	:       str					):
           return self.num_encoder_layers + self.num_decoder_layers






     @num_hidden_layers.setter
     def   lowerCamelCase__  (						self	:       Optional[Any]       ,					UpperCamelCase_	:       int					):
           raise NotImplementedError(
               '''This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'''
               ''' `num_decoder_layers`.'''					)


