'''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    lowerCamelCase__  :



   def __init__(    self					,      lowerCAmelCase__					,      lowerCAmelCase__=13					,      lowerCAmelCase__=7					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=99					,      lowerCAmelCase__=32					,      lowerCAmelCase__=5					,      lowerCAmelCase__=4					,      lowerCAmelCase__=37					,      lowerCAmelCase__="gelu"					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=512					,      lowerCAmelCase__=16					,      lowerCAmelCase__=2					,      lowerCAmelCase__=0.02					,      lowerCAmelCase__=3					,      lowerCAmelCase__=4					,      lowerCAmelCase__=None					,      )				->     Any:
        """simple docstring"""





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



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :int       =ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     )

        _UpperCamelCase  :Union[str, Any]       =None
        if self.use_input_mask:
             _UpperCamelCase  :str       =random_attention_mask([self.batch_size, self.seq_length]     )

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

        _UpperCamelCase  :Dict       =None
        _UpperCamelCase  :int       =None
        _UpperCamelCase  :int       =None
        if self.use_labels:
             _UpperCamelCase  :Tuple       =ids_tensor([self.batch_size]					,      self.type_sequence_label_size     )
             _UpperCamelCase  :Dict       =ids_tensor([self.batch_size, self.seq_length]					,      self.num_labels     )
             _UpperCamelCase  :Dict       =ids_tensor([self.batch_size]					,      self.num_choices     )
             _UpperCamelCase  :str       =ids_tensor([self.batch_size, self.seq_length]					,      1     )

        _UpperCamelCase  :Optional[int]       =self.get_config()

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



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """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=lowerCamelCase__					,      initializer_range=self.initializer_range					,      )



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        (
            _UpperCamelCase							
        )  :Any       =self.prepare_config_and_inputs()

        _UpperCamelCase  :Optional[Any]       =True
        _UpperCamelCase  :Tuple       =floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
        _UpperCamelCase  :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  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =ElectraModel(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :Union[str, Any]       =model(lowerCamelCase__					,      attention_mask=lowerCamelCase__					,      token_type_ids=lowerCamelCase__     )
        _UpperCamelCase  :Union[str, Any]       =model(lowerCamelCase__					,      token_type_ids=lowerCamelCase__     )
        _UpperCamelCase  :Optional[int]       =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__					,      )				->     int:
        """simple docstring"""





        _UpperCamelCase  :Dict       =True
        _UpperCamelCase  :Optional[Any]       =ElectraModel(lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :List[Any]       =model(
            lowerCamelCase__					,      attention_mask=lowerCamelCase__					,      token_type_ids=lowerCamelCase__					,      encoder_hidden_states=lowerCamelCase__					,      encoder_attention_mask=lowerCamelCase__					,      )
        _UpperCamelCase  :Optional[Any]       =model(
            lowerCamelCase__					,      attention_mask=lowerCamelCase__					,      token_type_ids=lowerCamelCase__					,      encoder_hidden_states=lowerCamelCase__					,      )
        _UpperCamelCase  :Optional[int]       =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__					,      )				->     str:
        """simple docstring"""





        _UpperCamelCase  :str       =ElectraForMaskedLM(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :Dict       =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__					,      )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =ElectraForCausalLM(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :Dict       =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__					,      )				->     Dict:
        """simple docstring"""





        _UpperCamelCase  :Tuple       =self.num_labels
        _UpperCamelCase  :List[Any]       =ElectraForTokenClassification(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :str       =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__					,      )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :Dict       =self.num_labels
        _UpperCamelCase  :Dict       =ElectraForPreTraining(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :List[Any]       =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__					,      )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase  :Any       =self.num_labels
        _UpperCamelCase  :Union[str, Any]       =ElectraForSequenceClassification(lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :Tuple       =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__					,      )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :int       =ElectraForQuestionAnswering(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :int       =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__					,      )				->     Any:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =self.num_choices
        _UpperCamelCase  :Optional[int]       =ElectraForMultipleChoice(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :int       =input_ids.unsqueeze(1     ).expand(-1					,      self.num_choices					,      -1     ).contiguous()
        _UpperCamelCase  :Optional[Any]       =token_type_ids.unsqueeze(1     ).expand(-1					,      self.num_choices					,      -1     ).contiguous()
        _UpperCamelCase  :str       =input_mask.unsqueeze(1     ).expand(-1					,      self.num_choices					,      -1     ).contiguous()
        _UpperCamelCase  :Any       =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     )				->     str:
        """simple docstring"""





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




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



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__=False     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :List[Any]       =super()._prepare_for_class(lowerCamelCase__					,      lowerCamelCase__					,      return_labels=lowerCamelCase__     )

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



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =ElectraModelTester(self     )
        _UpperCamelCase  :Any       =ConfigTester(self					,      config_class=lowerCamelCase__					,      hidden_size=37     )



   def  _UpperCamelCase		(    self     )				->     str:
        """simple docstring"""





        self.config_tester.run_common_tests()



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_model(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Optional[Any]       =self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_model_as_decoder(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase  :List[str]       =self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
             _UpperCamelCase  :Tuple       =type
             self.model_tester.create_and_check_electra_model(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     str:
        """simple docstring"""





        _UpperCamelCase  :Any       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_masked_lm(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Union[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :int       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_token_classification(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :str       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_pretraining(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        _UpperCamelCase  :str       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_sequence_classification(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        _UpperCamelCase  :Optional[Any]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_question_answering(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :List[str]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_multiple_choice(*lowerCamelCase__     )



   @slow
   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCamelCase  :Optional[int]       =ElectraModel.from_pretrained(lowerCamelCase__     )
             self.assertIsNotNone(lowerCamelCase__     )




   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase  :List[str]       =self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_for_causal_lm(*lowerCamelCase__     )




@require_torch
class    lowerCamelCase__  (   unittest.TestCase     ):



   @slow
   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :str       =ElectraModel.from_pretrained("""google/electra-small-discriminator"""     )
        _UpperCamelCase  :Optional[Any]       =torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]     )
        _UpperCamelCase  :List[str]       =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
        _UpperCamelCase  :List[str]       =model(lowerCamelCase__					,      attention_mask=lowerCamelCase__     )[0]
        _UpperCamelCase  :Dict       =torch.Size((1, 11, 256)     )
        self.assertEqual(output.shape					,      lowerCamelCase__     )
        _UpperCamelCase  :List[str]       =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]					,      lowerCamelCase__					,      atol=1e-4     )     )







import random




class 							__A	:





				"""simple docstring"""







				def __init__(					self				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[Any]									=x
									__UpperCamelCase :   int									=y


				def  __lowercase				(					self       ):




									"""simple docstring"""

									return (self.x**2 + self.y**2) <= 1







				@classmethod
				def  __lowercase				(					cls       ):




									"""simple docstring"""

									return cls(x=random.random()				,    y=random.random()       )






def 				A	(			a_   )	->		float:
					if number_of_simulations < 1:
										raise ValueError('At least one simulation is necessary to estimate PI.'   )

					__UpperCamelCase :   List[str]									=0
					for _ in range(a_   ):
										__UpperCamelCase :   Optional[int]									=Point.random_unit_square()

										if random_point.is_in_unit_circle():
															number_in_unit_circle += 1

					return 4 * number_in_unit_circle / number_of_simulations


if __name__ == "__main__":
							# import doctest

							# doctest.testmod()
							from math import pi

							A_			:Optional[int]	    =		'''Please enter the desired number of Monte Carlo simulations: '''
							A_			:int	    =		estimate_pi(int(input(prompt).strip()))
							print(f"An estimate of PI is {my_pi} with an error of {abs(my_pi - pi)}")

