



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__  :



 """simple docstring"""

 def __init__(						self				:				str ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Optional[int]=13 ,	__lowerCAmelCase				:				str=7 ,	__lowerCAmelCase				:				int=True ,	__lowerCAmelCase				:				Union[str, Any]=True ,	__lowerCAmelCase				:				Optional[Any]=True ,	__lowerCAmelCase				:				Dict=True ,	__lowerCAmelCase				:				List[Any]=99 ,	__lowerCAmelCase				:				Union[str, Any]=32 ,	__lowerCAmelCase				:				Optional[Any]=5 ,	__lowerCAmelCase				:				str=4 ,	__lowerCAmelCase				:				Union[str, Any]=37 ,	__lowerCAmelCase				:				Union[str, Any]="gelu" ,	__lowerCAmelCase				:				Optional[Any]=0.1 ,	__lowerCAmelCase				:				int=0.1 ,	__lowerCAmelCase				:				List[Any]=5_12 ,	__lowerCAmelCase				:				Dict=16 ,	__lowerCAmelCase				:				str=2 ,	__lowerCAmelCase				:				List[Any]=0.02 ,	__lowerCAmelCase				:				Any=3 ,	__lowerCAmelCase				:				Optional[int]=4 ,	__lowerCAmelCase				:				List[str]=None ,	)      ->  Optional[Any]:
    _A						     =					parent
    _A						     =					batch_size
    _A						     =					seq_length
    _A						     =					is_training
    _A						     =					use_input_mask
    _A						     =					use_token_type_ids
    _A						     =					use_labels
    _A						     =					vocab_size
    _A						     =					hidden_size
    _A						     =					num_hidden_layers
    _A						     =					num_attention_heads
    _A						     =					intermediate_size
    _A						     =					hidden_act
    _A						     =					hidden_dropout_prob
    _A						     =					attention_probs_dropout_prob
    _A						     =					max_position_embeddings
    _A						     =					type_vocab_size
    _A						     =					type_sequence_label_size
    _A						     =					initializer_range
    _A						     =					num_labels
    _A						     =					num_choices
    _A						     =					scope

 def 						snake_case_			(						self				:				Dict			)      ->  List[Any]:
    _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	self.vocab_size			)

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

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

    _A						     =					None
    _A						     =					None
    _A						     =					None
    if self.use_labels:
       _A						     =					ids_tensor([self.batch_size] ,	self.type_sequence_label_size			)
       _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	self.num_labels			)
       _A						     =					ids_tensor([self.batch_size] ,	self.num_choices			)
       _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	1			)

    _A						     =					self.get_config()

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

 def 						snake_case_			(						self				:				List[str]			)      ->  str:
    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 						snake_case_			(						self				:				Any			)      ->  Union[str, Any]:
    (
        (
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						
    )						     =					self.prepare_config_and_inputs()

    _A						     =					True
    _A						     =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
    _A						     =					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 						snake_case_			(						self				:				Tuple ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Optional[Any] ,	)      ->  Dict:
    _A						     =					ElectraModel(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase			)
    _A						     =					model(__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase			)
    _A						     =					model(__lowerCAmelCase			)
    self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, self.seq_length, self.hidden_size)			)

 def 						snake_case_			(						self				:				Optional[Any] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Any ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Dict ,	)      ->  Tuple:
    _A						     =					True
    _A						     =					ElectraModel(__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	encoder_hidden_states=__lowerCAmelCase ,	encoder_attention_mask=__lowerCAmelCase ,	)
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	encoder_hidden_states=__lowerCAmelCase ,	)
    _A						     =					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 						snake_case_			(						self				:				Optional[int] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				int ,	)      ->  Any:
    _A						     =					ElectraForMaskedLM(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				Optional[int] ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Any ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Union[str, Any] ,	)      ->  int:
    _A						     =					ElectraForCausalLM(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				str ,	__lowerCAmelCase				:				Any ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Any ,	)      ->  int:
    _A						     =					self.num_labels
    _A						     =					ElectraForTokenClassification(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				Union[str, Any] ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	)      ->  Any:
    _A						     =					self.num_labels
    _A						     =					ElectraForPreTraining(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	labels=__lowerCAmelCase			)
    self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.seq_length)			)

 def 						snake_case_			(						self				:				Union[str, Any] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Any ,	__lowerCAmelCase				:				Optional[Any] ,	)      ->  List[str]:
    _A						     =					self.num_labels
    _A						     =					ElectraForSequenceClassification(__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	labels=__lowerCAmelCase			)
    self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.num_labels)			)

 def 						snake_case_			(						self				:				int ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				Optional[Any] ,	)      ->  str:
    _A						     =					ElectraForQuestionAnswering(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				Dict ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				List[Any] ,	)      ->  List[str]:
    _A						     =					self.num_choices
    _A						     =					ElectraForMultipleChoice(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					input_ids.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					token_type_ids.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					input_mask.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	labels=__lowerCAmelCase ,	)
    self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.num_choices)			)

 def 						snake_case_			(						self				:				str			)      ->  List[str]:
    _A						     =					self.prepare_config_and_inputs()
    (
        (
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						
    )						     =					config_and_inputs
    _A						     =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict


@require_torch
class   lowerCamelCase__  (					_A       ,     _A       ,     unittest.TestCase):



 """simple docstring"""

 a__				:   List[Any]   =     (
     (
         ElectraModel,
         ElectraForPreTraining,
         ElectraForMaskedLM,
         ElectraForCausalLM,
         ElectraForMultipleChoice,
         ElectraForTokenClassification,
         ElectraForSequenceClassification,
         ElectraForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 a__				:   List[str]   =     (
     {
         "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__				:   List[Any]   =     True

 def 						snake_case_			(						self				:				Tuple ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				str=False			)      ->  Any:
    _A						     =					super()._prepare_for_class(__lowerCAmelCase ,	__lowerCAmelCase ,	return_labels=__lowerCAmelCase			)

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

 def 						snake_case_			(						self				:				str			)      ->  Union[str, Any]:
    _A						     =					ElectraModelTester(self			)
    _A						     =					ConfigTester(self ,	config_class=__lowerCAmelCase ,	hidden_size=37			)

 def 						snake_case_			(						self				:				Any			)      ->  Tuple:
    self.config_tester.run_common_tests()

 def 						snake_case_			(						self				:				Optional[Any]			)      ->  str:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_model(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Any			)      ->  List[str]:
    _A						     =					self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_electra_model_as_decoder(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Optional[Any]			)      ->  List[Any]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       _A						     =					type
       self.model_tester.create_and_check_electra_model(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Union[str, Any]			)      ->  Optional[int]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_masked_lm(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Union[str, Any]			)      ->  List[str]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_token_classification(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Optional[int]			)      ->  Tuple:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_pretraining(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Optional[Any]			)      ->  Tuple:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_sequence_classification(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				List[str]			)      ->  Union[str, Any]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_question_answering(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Optional[Any]			)      ->  Optional[Any]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_multiple_choice(*__lowerCAmelCase			)

 @slow
 def 						snake_case_			(						self				:				Any			)      ->  Union[str, Any]:
    for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A						     =					ElectraModel.from_pretrained(__lowerCAmelCase			)
       self.assertIsNotNone(__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Dict			)      ->  int:
    _A						     =					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):



 """simple docstring"""

 @slow
 def 						snake_case_			(						self				:				Union[str, Any]			)      ->  Tuple:
    _A						     =					ElectraModel.from_pretrained('''google/electra-small-discriminator'''			)
    _A						     =					torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]			)
    _A						     =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase			)[0]
    _A						     =					torch.Size((1, 11, 2_56)			)
    self.assertEqual(output.shape ,	__lowerCAmelCase			)
    _A						     =					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 math
from decimal import Decimal, getcontext

def   SCREAMING_SNAKE_CASE_       (  _snake_case  :int = 4_000_000							)     ->    int:

   try:
      _A						     =					int(_snake_case							)
   except (TypeError, ValueError):
      raise TypeError('''Parameter n must be int or castable to int.'''							)
   if n <= 0:
      raise ValueError('''Parameter n must be greater than or equal to one.'''							)
   _A						     =					100
   _A						     =					(Decimal(5							) ** Decimal(0.5							) + 1) / Decimal(2							)

   _A						     =					(math.floor(math.log(n * (phi + 2)  ,      _snake_case							) - 1							) // 3) * 3 + 2
   _A						     =					Decimal(round(phi ** Decimal(index + 1							)							)							) / (phi + 2)
   _A						     =					num // 2
   return int(_snake_case							)


if __name__ == "__main__":
   print(f'{solution() = }')



