






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






      '''simple docstring'''




      def __init__(   self   :		Optional[Any]				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Union[str, Any]=13				,      __UpperCAmelCase   :		Union[str, Any]=7				,      __UpperCAmelCase   :		str=True				,      __UpperCAmelCase   :		Tuple=True				,      __UpperCAmelCase   :		Tuple=True				,      __UpperCAmelCase   :		Tuple=True				,      __UpperCAmelCase   :		Dict=99				,      __UpperCAmelCase   :		Union[str, Any]=32				,      __UpperCAmelCase   :		int=5				,      __UpperCAmelCase   :		int=4				,      __UpperCAmelCase   :		Dict=37				,      __UpperCAmelCase   :		str="gelu"				,      __UpperCAmelCase   :		Dict=0.1				,      __UpperCAmelCase   :		Dict=0.1				,      __UpperCAmelCase   :		int=512				,      __UpperCAmelCase   :		List[Any]=16				,      __UpperCAmelCase   :		List[Any]=2				,      __UpperCAmelCase   :		Optional[int]=0.02				,      __UpperCAmelCase   :		int=3				,      __UpperCAmelCase   :		Any=4				,      __UpperCAmelCase   :		Optional[int]=None				,      )					->Optional[int]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		List[Any]						)					->Union[str, Any]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		str						)					->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=__lowerCamelCase				,      initializer_range=self.initializer_range				,      )







      def    __lowerCAmelCase      (   self   :		Tuple						)					->List[Any]:




       """simple docstring"""


       (
           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    __lowerCAmelCase      (   self   :		int				,      __UpperCAmelCase   :		List[str]				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		List[Any]				,      )					->Optional[Any]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Union[str, Any]				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		List[Any]				,      )					->str:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Dict				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		List[Any]				,      )					->Union[str, Any]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		str				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		Any				,      )					->Optional[Any]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		Any				,      )					->Optional[Any]:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		str				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		Optional[int]				,      )					->int:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Tuple				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Any				,      )					->str:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Any				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Tuple				,      )					->Dict:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		int				,      __UpperCAmelCase   :		Union[str, Any]				,      __UpperCAmelCase   :		List[Any]				,      __UpperCAmelCase   :		Dict				,      __UpperCAmelCase   :		Optional[Any]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		Optional[int]				,      )					->Dict:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Optional[int]						)					->Optional[Any]:




       """simple docstring"""


       a							=   self.prepare_config_and_inputs()
       (
           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 	lowercase_      (			lowercase				,						lowercase				,						unittest.TestCase     ):






      '''simple docstring'''




      __snake_case  						=						(
          (
              ElectraModel,
              ElectraForPreTraining,
              ElectraForMaskedLM,
              ElectraForCausalLM,
              ElectraForMultipleChoice,
              ElectraForTokenClassification,
              ElectraForSequenceClassification,
              ElectraForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      __snake_case  						=						(
          {
              '''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 {}
      )
      __snake_case  						=						True







      def    __lowerCAmelCase      (   self   :		Optional[int]				,      __UpperCAmelCase   :		Optional[int]				,      __UpperCAmelCase   :		int				,      __UpperCAmelCase   :		Any=False						)					->str:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Optional[Any]						)					->Optional[int]:




       """simple docstring"""


       a							=   ElectraModelTester(self						)
       a							=   ConfigTester(self				,      config_class=__lowerCamelCase				,      hidden_size=37						)







      def    __lowerCAmelCase      (   self   :		Optional[int]						)					->Tuple:




       """simple docstring"""


       self.config_tester.run_common_tests()







      def    __lowerCAmelCase      (   self   :		Optional[Any]						)					->int:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_model(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		Any						)					->int:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_model_as_decoder(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		int						)					->Dict:




       """simple docstring"""


       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    __lowerCAmelCase      (   self   :		Tuple						)					->List[str]:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_masked_lm(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		Optional[Any]						)					->List[Any]:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_token_classification(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		str						)					->Dict:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_pretraining(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		str						)					->List[Any]:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_sequence_classification(*__lowerCamelCase						)







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




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_question_answering(*__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		Dict						)					->str:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_multiple_choice(*__lowerCamelCase						)







      @slow
      def    __lowerCAmelCase      (   self   :		List[str]						)					->List[str]:




       """simple docstring"""


       for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a							=   ElectraModel.from_pretrained(__lowerCamelCase						)
        self.assertIsNotNone(__lowerCamelCase						)







      def    __lowerCAmelCase      (   self   :		Optional[int]						)					->Tuple:




       """simple docstring"""


       a							=   self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_for_causal_lm(*__lowerCamelCase						)






@require_torch
class 	lowercase_      (			unittest.TestCase     ):






      '''simple docstring'''




      @slow
      def    __lowerCAmelCase      (   self   :		Dict						)					->List[Any]:




       """simple docstring"""


       a							=   ElectraModel.from_pretrained('''google/electra-small-discriminator'''						)
       a							=   torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 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, 256)						)
       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						)						)










def 		_a					(   a				:int				)							->    int:
 if not isinstance(a			,							a				):
  raise TypeError('''recursive_lucas_number accepts only integer arguments.'''				)
 if n_th_number == 0:
  return 2
 if n_th_number == 1:
  return 1

 return recursive_lucas_number(n_th_number - 1				) + recursive_lucas_number(
     n_th_number - 2				)
def 		_a					(   a				:int				)							->    int:
 if not isinstance(a			,							a				):
  raise TypeError('''dynamic_lucas_number accepts only integer arguments.'''				)
 a						,  a							=   2, 1
 for _ in range(a				):
  a						,  a							=   b, a + b
 return a


if __name__ == "__main__":
     from doctest import testmod

     testmod()
     UpperCAmelCase__            =	int(input("Enter the number of terms in lucas series:\n").strip())
     print("Using recursive function to calculate lucas series:")
     print(" ".join(str(recursive_lucas_number(i)) for i in range(n)))
     print("\nUsing dynamic function to calculate lucas series:")
     print(" ".join(str(dynamic_lucas_number(i)) for i in range(n)))


