




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       _UpperCamelCase   :
      def __init__(      self :List[str]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :List[Any]=13			,   lowerCamelCase :List[Any]=7			,   lowerCamelCase :List[str]=True			,   lowerCamelCase :Optional[int]=True			,   lowerCamelCase :Dict=True			,   lowerCamelCase :Union[str, Any]=True			,   lowerCamelCase :Optional[Any]=99			,   lowerCamelCase :List[str]=32			,   lowerCamelCase :int=5			,   lowerCamelCase :str=4			,   lowerCamelCase :int=37			,   lowerCamelCase :Tuple="gelu"			,   lowerCamelCase :List[str]=0.1			,   lowerCamelCase :List[Any]=0.1			,   lowerCamelCase :List[Any]=512			,   lowerCamelCase :Tuple=16			,   lowerCamelCase :List[str]=2			,   lowerCamelCase :Dict=0.02			,   lowerCamelCase :Optional[Any]=3			,   lowerCamelCase :Tuple=4			,   lowerCamelCase :Tuple=None			,   )  ->      Optional[Any]:
       UpperCAmelCase__							=    parent
       UpperCAmelCase__							=    batch_size
       UpperCAmelCase__							=    seq_length
       UpperCAmelCase__							=    is_training
       UpperCAmelCase__							=    use_input_mask
       UpperCAmelCase__							=    use_token_type_ids
       UpperCAmelCase__							=    use_labels
       UpperCAmelCase__							=    vocab_size
       UpperCAmelCase__							=    hidden_size
       UpperCAmelCase__							=    num_hidden_layers
       UpperCAmelCase__							=    num_attention_heads
       UpperCAmelCase__							=    intermediate_size
       UpperCAmelCase__							=    hidden_act
       UpperCAmelCase__							=    hidden_dropout_prob
       UpperCAmelCase__							=    attention_probs_dropout_prob
       UpperCAmelCase__							=    max_position_embeddings
       UpperCAmelCase__							=    type_vocab_size
       UpperCAmelCase__							=    type_sequence_label_size
       UpperCAmelCase__							=    initializer_range
       UpperCAmelCase__							=    num_labels
       UpperCAmelCase__							=    num_choices
       UpperCAmelCase__							=    scope
      def 						UpperCAmelCase_    (      self :Any   )  ->      Optional[Any]:
       UpperCAmelCase__							=    ids_tensor([self.batch_size, self.seq_length]			,   self.vocab_size   )

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

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

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

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

       UpperCAmelCase__							=    True
       UpperCAmelCase__							=    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
       UpperCAmelCase__							=    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 :Union[str, Any]			,   lowerCamelCase :List[str]			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :List[str]			,   lowerCamelCase :Union[str, Any]			,   lowerCamelCase :Dict			,   lowerCamelCase :List[str]			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :Union[str, Any]			,   )  ->      List[str]:
       UpperCAmelCase__							=    ElectraModel(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    model(lowerCamelCase			,   attention_mask=lowerCamelCase			,   token_type_ids=lowerCamelCase   )
       UpperCAmelCase__							=    model(lowerCamelCase			,   token_type_ids=lowerCamelCase   )
       UpperCAmelCase__							=    model(lowerCamelCase   )
       self.parent.assertEqual(result.last_hidden_state.shape			,   (self.batch_size, self.seq_length, self.hidden_size)   )
      def 						UpperCAmelCase_    (      self :Optional[Any]			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :Any			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :List[str]			,   lowerCamelCase :List[str]			,   lowerCamelCase :Dict			,   lowerCamelCase :Union[str, Any]			,   lowerCamelCase :str			,   )  ->      Optional[int]:
       UpperCAmelCase__							=    True
       UpperCAmelCase__							=    ElectraModel(lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    model(
           lowerCamelCase			,   attention_mask=lowerCamelCase			,   token_type_ids=lowerCamelCase			,   encoder_hidden_states=lowerCamelCase			,   encoder_attention_mask=lowerCamelCase			,   )
       UpperCAmelCase__							=    model(
           lowerCamelCase			,   attention_mask=lowerCamelCase			,   token_type_ids=lowerCamelCase			,   encoder_hidden_states=lowerCamelCase			,   )
       UpperCAmelCase__							=    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 :Any			,   lowerCamelCase :int			,   lowerCamelCase :int			,   lowerCamelCase :Dict			,   lowerCamelCase :List[str]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :Tuple			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Union[str, Any]			,   )  ->      List[str]:
       UpperCAmelCase__							=    ElectraForMaskedLM(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :Optional[int]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Dict			,   lowerCamelCase :Any			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Dict			,   lowerCamelCase :str			,   lowerCamelCase :Union[str, Any]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :Dict			,   )  ->      Union[str, Any]:
       UpperCAmelCase__							=    ElectraForCausalLM(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :Any			,   lowerCamelCase :str			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :int			,   lowerCamelCase :List[str]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :int			,   lowerCamelCase :List[str]			,   )  ->      int:
       UpperCAmelCase__							=    self.num_labels
       UpperCAmelCase__							=    ElectraForTokenClassification(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :int			,   lowerCamelCase :List[Any]			,   lowerCamelCase :int			,   lowerCamelCase :List[str]			,   lowerCamelCase :Dict			,   lowerCamelCase :int			,   lowerCamelCase :int			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Union[str, Any]			,   )  ->      int:
       UpperCAmelCase__							=    self.num_labels
       UpperCAmelCase__							=    ElectraForPreTraining(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :List[Any]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :int			,   lowerCamelCase :Dict			,   lowerCamelCase :str			,   lowerCamelCase :Any			,   lowerCamelCase :Dict			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Any			,   )  ->      Optional[Any]:
       UpperCAmelCase__							=    self.num_labels
       UpperCAmelCase__							=    ElectraForSequenceClassification(lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :Tuple			,   lowerCamelCase :Tuple			,   lowerCamelCase :str			,   lowerCamelCase :int			,   lowerCamelCase :int			,   lowerCamelCase :Any			,   lowerCamelCase :List[str]			,   lowerCamelCase :str			,   lowerCamelCase :Tuple			,   )  ->      int:
       UpperCAmelCase__							=    ElectraForQuestionAnswering(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    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 :Dict			,   lowerCamelCase :Optional[int]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :Union[str, Any]			,   lowerCamelCase :Tuple			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :List[Any]			,   lowerCamelCase :Optional[int]			,   )  ->      Optional[int]:
       UpperCAmelCase__							=    self.num_choices
       UpperCAmelCase__							=    ElectraForMultipleChoice(config=lowerCamelCase   )
       model.to(lowerCamelCase   )
       model.eval()
       UpperCAmelCase__							=    input_ids.unsqueeze(1   ).expand(-1			,   self.num_choices			,   -1   ).contiguous()
       UpperCAmelCase__							=    token_type_ids.unsqueeze(1   ).expand(-1			,   self.num_choices			,   -1   ).contiguous()
       UpperCAmelCase__							=    input_mask.unsqueeze(1   ).expand(-1			,   self.num_choices			,   -1   ).contiguous()
       UpperCAmelCase__							=    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 :Tuple   )  ->      List[Any]:
       UpperCAmelCase__							=    self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			(
           UpperCAmelCase__
       )			,			
       )							=    config_and_inputs
       UpperCAmelCase__							=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_torch
class       _UpperCamelCase   (					lowerCAmelCase						,							lowerCAmelCase						,							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 :Optional[Any]			,   lowerCamelCase :Any			,   lowerCamelCase :List[Any]			,   lowerCamelCase :List[str]=False   )  ->      int:
       UpperCAmelCase__							=    super()._prepare_for_class(lowerCamelCase			,   lowerCamelCase			,   return_labels=lowerCamelCase   )

       if return_labels:
        if model_class in get_values(lowerCamelCase   ):
         UpperCAmelCase__							=    torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)			,   dtype=torch.long			,   device=lowerCamelCase   )
       return inputs_dict
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      Union[str, Any]:
       UpperCAmelCase__							=    ElectraModelTester(self   )
       UpperCAmelCase__							=    ConfigTester(self			,   config_class=lowerCamelCase			,   hidden_size=37   )
      def 						UpperCAmelCase_    (      self :Optional[int]   )  ->      Any:
       self.config_tester.run_common_tests()
      def 						UpperCAmelCase_    (      self :List[str]   )  ->      int:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_model(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :List[str]   )  ->      Optional[int]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_model_as_decoder(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      Union[str, Any]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
        UpperCAmelCase__							=    type
        self.model_tester.create_and_check_electra_model(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :List[str]   )  ->      Dict:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_masked_lm(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :str   )  ->      Dict:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_token_classification(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      int:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_pretraining(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :List[Any]   )  ->      Tuple:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_sequence_classification(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Tuple   )  ->      Union[str, Any]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_question_answering(*lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :Optional[int]   )  ->      List[Any]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_multiple_choice(*lowerCamelCase   )
      @slow
      def 						UpperCAmelCase_    (      self :Dict   )  ->      Tuple:
       for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase__							=    ElectraModel.from_pretrained(lowerCamelCase   )
        self.assertIsNotNone(lowerCamelCase   )







      def 						UpperCAmelCase_    (      self :Dict   )  ->      Optional[int]:
       UpperCAmelCase__							=    self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_for_causal_lm(*lowerCamelCase   )



@require_torch
class       _UpperCamelCase   (					unittest.TestCase		):
      @slow
      def 						UpperCAmelCase_    (      self :Optional[Any]   )  ->      str:
       UpperCAmelCase__							=    ElectraModel.from_pretrained("google/electra-small-discriminator"   )
       UpperCAmelCase__							=    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
       UpperCAmelCase__							=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
       UpperCAmelCase__							=    model(lowerCamelCase			,   attention_mask=lowerCamelCase   )[0]
       UpperCAmelCase__							=    torch.Size((1, 11, 256)   )
       self.assertEqual(output.shape			,   lowerCamelCase   )
       UpperCAmelCase__							=    torch.tensor(
           [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]   )

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











def     lowerCAmelCase		(		_lowerCAmelCase				: Tuple    ,				_lowerCAmelCase				: Tuple						):




 """simple docstring"""







 sequence.append(_lowerCAmelCase						)

 UpperCAmelCase__							=    0
 while sequence[index] != target:
  index += 1

 sequence.pop()

 if index == len(_lowerCAmelCase						):
  return None

 return index


if __name__ == "__main__":
  _lowerCAmelCase    : List[Any]		= input("Enter numbers separated by comma:\n").strip()
  _lowerCAmelCase    : Dict		= [int(item) for item in user_input.split(",")]

  _lowerCAmelCase    : Optional[Any]		= input("Enter a single number to be found in the list:\n")
  _lowerCAmelCase    : Tuple		= int(target_input)
  _lowerCAmelCase    : int		= sentinel_linear_search(sequence, target)
  if result is not None:
    print(F'''{target} found at positions: {result}''')
  else:
    print("Not found")





