






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			,       UpperCAmelCase			,       ):
							"""simple docstring"""







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

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







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

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

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

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

							_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,
							    input_mask,
							    head_mask,
							    token_type_ids,
							    mc_token_ids,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							)

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       *UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     TFOpenAIGPTModel(config=UpperCAmelCase       )
							_UpperCAmelCase							   =     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
							_UpperCAmelCase							   =     model(UpperCAmelCase       )

							_UpperCAmelCase							   =     [input_ids, input_mask]
							_UpperCAmelCase							   =     model(UpperCAmelCase       )

							_UpperCAmelCase							   =     model(UpperCAmelCase       )

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

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       *UpperCAmelCase       ):
							"""simple docstring"""







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

	def 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       *UpperCAmelCase       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     TFOpenAIGPTDoubleHeadsModel(config=UpperCAmelCase       )

							_UpperCAmelCase							   =     tf.tile(tf.expand_dims(UpperCAmelCase			,       1       )			,       (1, self.num_choices, 1)       )
							_UpperCAmelCase							   =     tf.tile(tf.expand_dims(UpperCAmelCase			,       1       )			,       (1, self.num_choices, 1)       )
							_UpperCAmelCase							   =     tf.tile(tf.expand_dims(UpperCAmelCase			,       1       )			,       (1, self.num_choices, 1)       )

							_UpperCAmelCase							   =     {
							    '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,
							}
							_UpperCAmelCase							   =     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			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       *UpperCAmelCase       ):
							"""simple docstring"""







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

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.prepare_config_and_inputs()

							(
							    (
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					(
							    _UpperCAmelCase       
							)   ,					       
							)							   =     config_and_inputs

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





@require_tf
class __lowerCamelCase					( snake_case__	,			snake_case__	,			unittest.TestCase):





	"""simple docstring"""






	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 			UpperCamelCase       (				self			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase			,       UpperCAmelCase       ):
							"""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       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     TFOpenAIGPTModelTester(self       )
							_UpperCAmelCase							   =     ConfigTester(self			,       config_class=UpperCAmelCase			,       n_embd=37       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							self.config_tester.run_common_tests()

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







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

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_double_head(*UpperCAmelCase       )

	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







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

	@slow
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







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





@require_tf
class __lowerCamelCase					( unittest.TestCase):





	"""simple docstring"""






	@slow
	def 			UpperCamelCase       (				self       ):
							"""simple docstring"""







							_UpperCAmelCase							   =     TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'       )
							_UpperCAmelCase							   =     tf.convert_to_tensor([[481, 4735, 544]]			,       dtype=tf.intaa       )  # the president is
							_UpperCAmelCase							   =     [
							    481,
							    4735,
							    544,
							    246,
							    963,
							    870,
							    762,
							    239,
							    244,
							    4_0477,
							    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

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




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase__        =				logging.get_logger(__name__)

UpperCAmelCase__        =				{
    '''google/rembert''': '''https://huggingface.co/google/rembert/resolve/main/config.json''',
    # See all RemBERT models at https://huggingface.co/models?filter=rembert
}







class  lowerCamelCase__  (			lowerCAmelCase):
     SCREAMING_SNAKE_CASE__      =     '''rembert'''





     def __init__(self						,	UpperCAmelCase=2_5_0_3_0_0						,	UpperCAmelCase=1_1_5_2						,	UpperCAmelCase=3_2						,	UpperCAmelCase=1_8						,	UpperCAmelCase=2_5_6						,	UpperCAmelCase=1_6_6_4						,	UpperCAmelCase=4_6_0_8						,	UpperCAmelCase="gelu"						,	UpperCAmelCase=0.0						,	UpperCAmelCase=0.0						,	UpperCAmelCase=0.1						,	UpperCAmelCase=5_1_2						,	UpperCAmelCase=2						,	UpperCAmelCase=0.02						,	UpperCAmelCase=1e-12						,	UpperCAmelCase=True						,	UpperCAmelCase=0						,	UpperCAmelCase=3_1_2						,	UpperCAmelCase=3_1_3						,	**UpperCAmelCase						,	)	->  Tuple:
      super().__init__(pad_token_id=UpperCAmelCase						,	bos_token_id=UpperCAmelCase						,	eos_token_id=UpperCAmelCase						,	**UpperCAmelCase			)

      _lowercase				 =vocab_size
      _lowercase				 =input_embedding_size
      _lowercase				 =output_embedding_size
      _lowercase				 =max_position_embeddings
      _lowercase				 =hidden_size
      _lowercase				 =num_hidden_layers
      _lowercase				 =num_attention_heads
      _lowercase				 =intermediate_size
      _lowercase				 =hidden_act
      _lowercase				 =hidden_dropout_prob
      _lowercase				 =attention_probs_dropout_prob
      _lowercase				 =classifier_dropout_prob
      _lowercase				 =initializer_range
      _lowercase				 =type_vocab_size
      _lowercase				 =layer_norm_eps
      _lowercase				 =use_cache
      _lowercase				 =False







class  lowerCamelCase__  (			lowerCAmelCase):





     @property
     def 	__A   (self			)	->  Mapping[str, Mapping[int, str]]:
      if self.task == "multiple-choice":
       _lowercase				 ={0: '''batch''', 1: '''choice''', 2: '''sequence'''}
      else:
       _lowercase				 ={0: '''batch''', 1: '''sequence'''}
      return OrderedDict(
          [
              ('''input_ids''', dynamic_axis),
              ('''attention_mask''', dynamic_axis),
              ('''token_type_ids''', dynamic_axis),
          ]			)





     @property
     def 	__A   (self			)	->  float:
      return 1e-4

