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



      def __init__(		self       ,     lowercase       ,     lowercase=13       ,     lowercase=7       ,     lowercase=True       ,     lowercase=True       ,     lowercase=True       ,     lowercase=True       ,     lowercase=99       ,     lowercase=32       ,     lowercase=5       ,     lowercase=4       ,     lowercase=37       ,     lowercase="gelu"       ,     lowercase=0.1       ,     lowercase=0.1       ,     lowercase=5_12       ,     lowercase=16       ,     lowercase=2       ,     lowercase=0.02       ,     lowercase=3       ,     lowercase=4       ,     lowercase=None       ,     )      ->     Dict:





        '''simple docstring'''





        a__:	int     				=      parent
        a__:	List[Any]     				=      batch_size
        a__:	List[str]     				=      seq_length
        a__:	Any     				=      is_training
        a__:	int     				=      use_input_mask
        a__:	Tuple     				=      use_token_type_ids
        a__:	Dict     				=      use_labels
        a__:	List[Any]     				=      vocab_size
        a__:	List[Any]     				=      hidden_size
        a__:	Union[str, Any]     				=      num_hidden_layers
        a__:	Optional[int]     				=      num_attention_heads
        a__:	int     				=      intermediate_size
        a__:	str     				=      hidden_act
        a__:	Optional[int]     				=      hidden_dropout_prob
        a__:	str     				=      attention_probs_dropout_prob
        a__:	List[str]     				=      max_position_embeddings
        a__:	List[str]     				=      type_vocab_size
        a__:	List[Any]     				=      type_sequence_label_size
        a__:	Tuple     				=      initializer_range
        a__:	str     				=      num_labels
        a__:	str     				=      num_choices
        a__:	Any     				=      scope



      def     lowerCamelCase_  (		self)      ->     Dict:





        '''simple docstring'''





        a__:	str     				=      ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size)

        a__:	int     				=      None
        if self.use_input_mask:
          a__:	Any     				=      random_attention_mask([self.batch_size, self.seq_length])

        a__:	Union[str, Any]     				=      None
        if self.use_token_type_ids:
          a__:	Any     				=      ids_tensor([self.batch_size, self.seq_length]       ,     self.type_vocab_size)

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

        a__:	Tuple     				=      self.get_config()

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



      def     lowerCamelCase_  (		self)      ->     str:





        '''simple docstring'''





        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=_SCREAMING_SNAKE_CASE       ,     initializer_range=self.initializer_range       ,     )



      def     lowerCamelCase_  (		self)      ->     Any:





        '''simple docstring'''





        (
            (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       		
        ):	str     				=      self.prepare_config_and_inputs()

        a__:	Tuple     				=      True
        a__:	Union[str, Any]     				=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
        a__:	List[Any]     				=      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     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     str:





        '''simple docstring'''





        a__:	int     				=      ElectraModel(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Union[str, Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE)
        a__:	List[Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE)
        a__:	int     				=      model(_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Tuple:





        '''simple docstring'''





        a__:	Optional[int]     				=      True
        a__:	Optional[Any]     				=      ElectraModel(_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	int     				=      model(
            _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     encoder_hidden_states=_SCREAMING_SNAKE_CASE       ,     encoder_attention_mask=_SCREAMING_SNAKE_CASE       ,     )
        a__:	int     				=      model(
            _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     encoder_hidden_states=_SCREAMING_SNAKE_CASE       ,     )
        a__:	Optional[Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     List[str]:





        '''simple docstring'''





        a__:	Dict     				=      ElectraForMaskedLM(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Optional[int]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     int:





        '''simple docstring'''





        a__:	Optional[int]     				=      ElectraForCausalLM(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Union[str, Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Optional[Any]:





        '''simple docstring'''





        a__:	Tuple     				=      self.num_labels
        a__:	Any     				=      ElectraForTokenClassification(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	List[str]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Any:





        '''simple docstring'''





        a__:	Optional[Any]     				=      self.num_labels
        a__:	str     				=      ElectraForPreTraining(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Union[str, Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Dict:





        '''simple docstring'''





        a__:	str     				=      self.num_labels
        a__:	Tuple     				=      ElectraForSequenceClassification(_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Optional[Any]     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE)
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_labels))



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Union[str, Any]:





        '''simple docstring'''





        a__:	Union[str, Any]     				=      ElectraForQuestionAnswering(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	Union[str, Any]     				=      model(
            _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     start_positions=_SCREAMING_SNAKE_CASE       ,     end_positions=_SCREAMING_SNAKE_CASE       ,     )
        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     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     lowercase       ,     )      ->     Optional[int]:





        '''simple docstring'''





        a__:	str     				=      self.num_choices
        a__:	List[Any]     				=      ElectraForMultipleChoice(config=_SCREAMING_SNAKE_CASE)
        model.to(_SCREAMING_SNAKE_CASE)
        model.eval()
        a__:	List[str]     				=      input_ids.unsqueeze(1).expand(-1       ,     self.num_choices       ,     -1).contiguous()
        a__:	Union[str, Any]     				=      token_type_ids.unsqueeze(1).expand(-1       ,     self.num_choices       ,     -1).contiguous()
        a__:	List[str]     				=      input_mask.unsqueeze(1).expand(-1       ,     self.num_choices       ,     -1).contiguous()
        a__:	Dict     				=      model(
            _SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE       ,     token_type_ids=_SCREAMING_SNAKE_CASE       ,     labels=_SCREAMING_SNAKE_CASE       ,     )
        self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_choices))



      def     lowerCamelCase_  (		self)      ->     Optional[Any]:





        '''simple docstring'''





        a__:	List[Any]     				=      self.prepare_config_and_inputs()
        (
            (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       (
            a__		
        )				,       		
        ):	str     				=      config_and_inputs
        a__:	Dict     				=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict







@require_torch
class   __snake_case			(	snake_case__			, snake_case__			, unittest.TestCase							):
      a__								=						(
          (
              ElectraModel,
              ElectraForPreTraining,
              ElectraForMaskedLM,
              ElectraForCausalLM,
              ElectraForMultipleChoice,
              ElectraForTokenClassification,
              ElectraForSequenceClassification,
              ElectraForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      a__								=						(
          {
              """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 {}
      )
      a__								=						True



      def     lowerCamelCase_  (		self       ,     lowercase       ,     lowercase       ,     lowercase=False)      ->     List[str]:





        '''simple docstring'''





        a__:	List[Any]     				=      super()._prepare_for_class(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE       ,     return_labels=_SCREAMING_SNAKE_CASE)

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



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





        '''simple docstring'''





        a__:	Optional[int]     				=      ElectraModelTester(self)
        a__:	List[Any]     				=      ConfigTester(self       ,     config_class=_SCREAMING_SNAKE_CASE       ,     hidden_size=37)



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





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def     lowerCamelCase_  (		self)      ->     str:





        '''simple docstring'''





        a__:	List[str]     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_model(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     List[str]:





        '''simple docstring'''





        a__:	int     				=      self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_model_as_decoder(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     str:





        '''simple docstring'''





        a__:	Optional[int]     				=      self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          a__:	int     				=      type
          self.model_tester.create_and_check_electra_model(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     str:





        '''simple docstring'''





        a__:	str     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_masked_lm(*_SCREAMING_SNAKE_CASE)



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





        '''simple docstring'''





        a__:	Optional[Any]     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_token_classification(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     Tuple:





        '''simple docstring'''





        a__:	List[Any]     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_pretraining(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     Tuple:





        '''simple docstring'''





        a__:	str     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_sequence_classification(*_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     Any:





        '''simple docstring'''





        a__:	Tuple     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_question_answering(*_SCREAMING_SNAKE_CASE)



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





        '''simple docstring'''





        a__:	Optional[Any]     				=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_multiple_choice(*_SCREAMING_SNAKE_CASE)



      @slow
      def     lowerCamelCase_  (		self)      ->     str:





        '''simple docstring'''





        for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__:	str     				=      ElectraModel.from_pretrained(_SCREAMING_SNAKE_CASE)
          self.assertIsNotNone(_SCREAMING_SNAKE_CASE)



      def     lowerCamelCase_  (		self)      ->     List[Any]:





        '''simple docstring'''





        a__:	List[str]     				=      self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_for_causal_lm(*_SCREAMING_SNAKE_CASE)







@require_torch
class   __snake_case			(	unittest.TestCase							):



      @slow
      def     lowerCamelCase_  (		self)      ->     Any:





        '''simple docstring'''





        a__:	str     				=      ElectraModel.from_pretrained('google/electra-small-discriminator')
        a__:	List[str]     				=      torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
        a__:	Optional[Any]     				=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
        a__:	Tuple     				=      model(_SCREAMING_SNAKE_CASE       ,     attention_mask=_SCREAMING_SNAKE_CASE)[0]
        a__:	Optional[int]     				=      torch.Size((1, 11, 2_56))
        self.assertEqual(output.shape       ,     _SCREAMING_SNAKE_CASE)
        a__:	Optional[Any]     				=      torch.tensor(
            [[[0.4471, 0.6821, -0.3265], [0.4627, 0.5255, -0.3668], [0.4532, 0.3313, -0.4344]]])

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










def   snake_case		(				UpperCAmelCase						:							Optional[int],    UpperCAmelCase						:							str ):
				# "Simpson Rule"
				# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
				A									=   (boundary[1] - boundary[0]) / steps
				A									=   boundary[0]
				A									=   boundary[1]
				A									=   make_points(UpperCAmelCase,    UpperCAmelCase,    UpperCAmelCase )
				A									=   0.0
				y += (h / 3.0) * f(UpperCAmelCase )
				A									=   2
				for i in x_i:
								y += (h / 3) * (4 - 2 * (cnt % 2)) * f(UpperCAmelCase )
								cnt += 1
				y += (h / 3.0) * f(UpperCAmelCase )
				return y







def   snake_case		(				UpperCAmelCase						:							int,    UpperCAmelCase						:							int,    UpperCAmelCase						:							List[Any] ):
				A									=   a + h
				while x < (b - h):
								yield x
								A									=   x + h







def   snake_case		(				UpperCAmelCase						:							str ):  # enter your function here
				A									=   (x - 0) * (x - 0)
				return y







def   snake_case		(				):
				A									=   0.0  # Lower bound of integration
				A									=   1.0  # Upper bound of integration
				A									=   10.0  # define number of steps or resolution
				A									=   [a, b]  # define boundary of integration
				A									=   method_a(UpperCAmelCase,    UpperCAmelCase )
				print(f'y = {y}' )


if __name__ == "__main__":
					main()





