


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





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= parent
       UpperCAmelCase_				:   Any							= 13
       UpperCAmelCase_				:   Tuple							= 7
       UpperCAmelCase_				:   Union[str, Any]							= True
       UpperCAmelCase_				:   List[Any]							= True
       UpperCAmelCase_				:   Tuple							= True
       UpperCAmelCase_				:   List[Any]							= True
       UpperCAmelCase_				:   Dict							= True
       UpperCAmelCase_				:   Union[str, Any]							= 99
       UpperCAmelCase_				:   str							= 32
       UpperCAmelCase_				:   str							= 2
       UpperCAmelCase_				:   Union[str, Any]							= 4
       UpperCAmelCase_				:   List[str]							= 37
       UpperCAmelCase_				:   Union[str, Any]							= '''gelu'''
       UpperCAmelCase_				:   Optional[Any]							= 0.1
       UpperCAmelCase_				:   Tuple							= 0.1
       UpperCAmelCase_				:   str							= 512
       UpperCAmelCase_				:   Optional[Any]							= 16
       UpperCAmelCase_				:   Union[str, Any]							= 2
       UpperCAmelCase_				:   List[Any]							= 0.02
       UpperCAmelCase_				:   Optional[Any]							= 3
       UpperCAmelCase_				:   Union[str, Any]							= 4
       UpperCAmelCase_				:   Any							= None
       UpperCAmelCase_				:   str							= self.vocab_size - 1





    def        _lowerCamelCase			(						self					: int						):





       '''simple docstring'''
       UpperCAmelCase_				:   int							= ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size						)

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

       UpperCAmelCase_				:   Optional[Any]							= None
       if self.use_token_type_ids:
          UpperCAmelCase_				:   List[Any]							= ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size						)

       UpperCAmelCase_				:   Tuple							= None
       if self.use_mc_token_ids:
          UpperCAmelCase_				:   Union[str, Any]							= ids_tensor([self.batch_size, self.num_choices]							, self.seq_length						)

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

       UpperCAmelCase_				:   Union[str, 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							, )

       UpperCAmelCase_				:   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        _lowerCamelCase			(						self					: List[Any]							, __snake_case					: Tuple							, __snake_case					: List[Any]							, __snake_case					: Any							, __snake_case					: Optional[Any]							, __snake_case					: str							, *__snake_case					: List[str]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Union[str, Any]							= TFOpenAIGPTModel(config=_lowerCamelCase						)
       UpperCAmelCase_				:   Union[str, Any]							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       UpperCAmelCase_				:   str							= model(_lowerCamelCase						)

       UpperCAmelCase_				:   Optional[int]							= [input_ids, input_mask]
       UpperCAmelCase_				:   Optional[int]							= model(_lowerCamelCase						)

       UpperCAmelCase_				:   Optional[Any]							= model(_lowerCamelCase						)

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





    def        _lowerCamelCase			(						self					: int							, __snake_case					: Optional[Any]							, __snake_case					: Dict							, __snake_case					: List[Any]							, __snake_case					: Any							, __snake_case					: Dict							, *__snake_case					: str						):





       '''simple docstring'''
       UpperCAmelCase_				:   str							= TFOpenAIGPTLMHeadModel(config=_lowerCamelCase						)
       UpperCAmelCase_				:   Union[str, Any]							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
       UpperCAmelCase_				:   Any							= model(_lowerCamelCase						)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)						)





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





       '''simple docstring'''
       UpperCAmelCase_				:   Dict							= TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase						)

       UpperCAmelCase_				:   str							= tf.tile(tf.expand_dims(_lowerCamelCase							, 1						)							, (1, self.num_choices, 1)						)
       UpperCAmelCase_				:   Dict							= tf.tile(tf.expand_dims(_lowerCamelCase							, 1						)							, (1, self.num_choices, 1)						)
       UpperCAmelCase_				:   List[Any]							= tf.tile(tf.expand_dims(_lowerCamelCase							, 1						)							, (1, self.num_choices, 1)						)

       UpperCAmelCase_				:   Union[str, Any]							= {
           '''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_				:   Optional[int]							= model(_lowerCamelCase						)
       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					: Union[str, Any]							, __snake_case					: List[Any]							, __snake_case					: Dict							, __snake_case					: Optional[Any]							, __snake_case					: Optional[Any]							, __snake_case					: Optional[Any]							, *__snake_case					: Any						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= self.num_labels
       UpperCAmelCase_				:   Dict							= ids_tensor([self.batch_size]							, self.type_sequence_label_size						)
       UpperCAmelCase_				:   Any							= {
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
           '''labels''': sequence_labels,
       }
       UpperCAmelCase_				:   Union[str, Any]							= TFOpenAIGPTForSequenceClassification(_lowerCamelCase						)
       UpperCAmelCase_				:   Union[str, Any]							= model(_lowerCamelCase						)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)						)





    def        _lowerCamelCase			(						self					: int						):





       '''simple docstring'''
       UpperCAmelCase_				:   Any							= self.prepare_config_and_inputs()

       (
           (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   (
           UpperCAmelCase_	
       )		,   	
       )				:   Optional[int]							= config_and_inputs

       UpperCAmelCase_				:   Union[str, Any]							= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict





@require_tf
class 			lowerCAmelCase__(     a_     ,     a_     ,     unittest.TestCase    ):






    '''simple docstring'''





    A_	:  Any								=			(
        (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
        if is_tf_available()
        else ()
    )
    A_	:  int								=			(
        (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_	:  List[str]								=			False
    A_	:  Optional[int]								=			False





    def        _lowerCamelCase			(						self					: str							, __snake_case					: List[Any]							, __snake_case					: Union[str, Any]							, __snake_case					: Dict							, __snake_case					: Optional[Any]							, __snake_case					: 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        _lowerCamelCase			(						self					: Any						):





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[int]							= TFOpenAIGPTModelTester(self						)
       UpperCAmelCase_				:   int							= ConfigTester(self							, config_class=_lowerCamelCase							, n_embd=37						)





    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''simple docstring'''
       self.config_tester.run_common_tests()





    def        _lowerCamelCase			(						self					: List[str]						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase						)





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





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase						)





    def        _lowerCamelCase			(						self					: Tuple						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase						)





    def        _lowerCamelCase			(						self					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Tuple							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase						)





    @slow
    def        _lowerCamelCase			(						self					: List[str]						):





       '''simple docstring'''
       for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          UpperCAmelCase_				:   List[Any]							= TFOpenAIGPTModel.from_pretrained(_lowerCamelCase						)
          self.assertIsNotNone(_lowerCamelCase						)





@require_tf
class 			lowerCAmelCase__(     unittest.TestCase    ):






    '''simple docstring'''





    @slow
    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Union[str, Any]							= TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''						)
       UpperCAmelCase_				:   Dict							= tf.convert_to_tensor([[481, 4_735, 544]]							, dtype=tf.intaa						)  # the president is
       UpperCAmelCase_				:   Optional[Any]							= [
           481,
           4_735,
           544,
           246,
           963,
           870,
           762,
           239,
           244,
           40_477,
           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_				:   Optional[int]							= model.generate(_lowerCamelCase							, do_sample=_lowerCamelCase						)
       self.assertListEqual(output_ids[0].numpy().tolist()							, _lowerCamelCase						)







"""simple docstring"""



import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_PRETRAINING_MAPPING,
        ElectraForCausalLM,
        ElectraForMaskedLM,
        ElectraForMultipleChoice,
        ElectraForPreTraining,
        ElectraForQuestionAnswering,
        ElectraForSequenceClassification,
        ElectraForTokenClassification,
        ElectraModel,
    )
    from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST

class 			a	:







    def __init__(     self				,						_lowerCamelCase				,						_lowerCamelCase=1_3				,						_lowerCamelCase=7				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=9_9				,						_lowerCamelCase=3_2				,						_lowerCamelCase=5				,						_lowerCamelCase=4				,						_lowerCamelCase=3_7				,						_lowerCamelCase="gelu"				,						_lowerCamelCase=0.1				,						_lowerCamelCase=0.1				,						_lowerCamelCase=5_1_2				,						_lowerCamelCase=1_6				,						_lowerCamelCase=2				,						_lowerCamelCase=0.0_2				,						_lowerCamelCase=3				,						_lowerCamelCase=4				,						_lowerCamelCase=None				,						):
      lowercase				       =			parent
      lowercase				       =			batch_size
      lowercase				       =			seq_length
      lowercase				       =			is_training
      lowercase				       =			use_input_mask
      lowercase				       =			use_token_type_ids
      lowercase				       =			use_labels
      lowercase				       =			vocab_size
      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				       =			max_position_embeddings
      lowercase				       =			type_vocab_size
      lowercase				       =			type_sequence_label_size
      lowercase				       =			initializer_range
      lowercase				       =			num_labels
      lowercase				       =			num_choices
      lowercase				       =			scope







    def      UpperCamelCase_ (     self			):
      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
      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				       =			ids_tensor([self.batch_size, self.seq_length]				,						1			)

      lowercase				       =			self.get_config()

      return (
          config,
          input_ids,
          token_type_ids,
          input_mask,
          sequence_labels,
          token_labels,
          choice_labels,
          fake_token_labels,
      )







    def      UpperCamelCase_ (     self			):
      return ElectraConfig(
          vocab_size=self.vocab_size				,						hidden_size=self.hidden_size				,						num_hidden_layers=self.num_hidden_layers				,						num_attention_heads=self.num_attention_heads				,						intermediate_size=self.intermediate_size				,						hidden_act=self.hidden_act				,						hidden_dropout_prob=self.hidden_dropout_prob				,						attention_probs_dropout_prob=self.attention_probs_dropout_prob				,						max_position_embeddings=self.max_position_embeddings				,						type_vocab_size=self.type_vocab_size				,						is_decoder=_lowerCamelCase				,						initializer_range=self.initializer_range				,						)







    def      UpperCamelCase_ (     self			):
      (
          (
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	
      )				       =			self.prepare_config_and_inputs()

      lowercase				       =			True
      lowercase				       =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
      lowercase				       =			ids_tensor([self.batch_size, self.seq_length]				,						vocab_size=2			)

      return (
          config,
          input_ids,
          token_type_ids,
          input_mask,
          sequence_labels,
          token_labels,
          choice_labels,
          encoder_hidden_states,
          encoder_attention_mask,
      )







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraModel(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase			)
      lowercase				       =			model(_lowerCamelCase				,						token_type_ids=_lowerCamelCase			)
      lowercase				       =			model(_lowerCamelCase			)
      self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			True
      lowercase				       =			ElectraModel(_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						encoder_hidden_states=_lowerCamelCase				,						encoder_attention_mask=_lowerCamelCase				,						)
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						encoder_hidden_states=_lowerCamelCase				,						)
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase			)
      self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForMaskedLM(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForCausalLM(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForTokenClassification(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.num_labels)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForPreTraining(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForSequenceClassification(_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForQuestionAnswering(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						start_positions=_lowerCamelCase				,						end_positions=_lowerCamelCase				,						)
      self.parent.assertEqual(result.start_logits.shape				,						(self.batch_size, self.seq_length)			)
      self.parent.assertEqual(result.end_logits.shape				,						(self.batch_size, self.seq_length)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_choices
      lowercase				       =			ElectraForMultipleChoice(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			input_ids.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			token_type_ids.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			input_mask.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase				,						)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.prepare_config_and_inputs()
      (
          (
          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_torch
class 			a	(  a_,							a_,							unittest.TestCase      ):
    UpperCAmelCase_					:	List[str]      	=(
        (
            ElectraModel,
            ElectraForPreTraining,
            ElectraForMaskedLM,
            ElectraForCausalLM,
            ElectraForMultipleChoice,
            ElectraForTokenClassification,
            ElectraForSequenceClassification,
            ElectraForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    UpperCAmelCase_					:	Optional[Any]      	=(
        {
            "feature-extraction": ElectraModel,
            "fill-mask": ElectraForMaskedLM,
            "question-answering": ElectraForQuestionAnswering,
            "text-classification": ElectraForSequenceClassification,
            "text-generation": ElectraForCausalLM,
            "token-classification": ElectraForTokenClassification,
            "zero-shot": ElectraForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    UpperCAmelCase_					:	Tuple      	=True







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase=False			):
      lowercase				       =			super()._prepare_for_class(_lowerCamelCase				,						_lowerCamelCase				,						return_labels=_lowerCamelCase			)

      if return_labels:
        if model_class in get_values(_lowerCamelCase			):
          lowercase				       =			torch.zeros(
              (self.model_tester.batch_size, self.model_tester.seq_length)				,						dtype=torch.long				,						device=_lowerCamelCase			)
      return inputs_dict







    def      UpperCamelCase_ (     self			):
      lowercase				       =			ElectraModelTester(self			)
      lowercase				       =			ConfigTester(self				,						config_class=_lowerCamelCase				,						hidden_size=3_7			)







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







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_model(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_model_as_decoder(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        lowercase				       =			type
        self.model_tester.create_and_check_electra_model(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_masked_lm(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_token_classification(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_pretraining(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_sequence_classification(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_question_answering(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_multiple_choice(*_lowerCamelCase			)







    @slow
    def      UpperCamelCase_ (     self			):
      for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase				       =			ElectraModel.from_pretrained(_lowerCamelCase			)
        self.assertIsNotNone(_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_for_causal_lm(*_lowerCamelCase			)


@require_torch
class 			a	(  unittest.TestCase      ):







    @slow
    def      UpperCamelCase_ (     self			):
      lowercase				       =			ElectraModel.from_pretrained('google/electra-small-discriminator'			)
      lowercase				       =			torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]			)
      lowercase				       =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase			)[0]
      lowercase				       =			torch.Size((1, 1_1, 2_5_6)			)
      self.assertEqual(output.shape				,						_lowerCamelCase			)
      lowercase				       =			torch.tensor(
          [[[0.4_4_7_1, 0.6_8_2_1, -0.3_2_6_5], [0.4_6_2_7, 0.5_2_5_5, -0.3_6_6_8], [0.4_5_3_2, 0.3_3_1_3, -0.4_3_4_4]]]			)

      self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,						_lowerCamelCase				,						atol=1e-4			)			)


