


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



   '''simple docstring'''






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




   def   __lowerCAmelCase						(	self	:					Any						)->	List[str]:
          snake_case__					:	Optional[Any]          =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size						)

          snake_case__					:	Tuple          =					None
          if self.use_input_mask:
                 snake_case__					:	str          =					random_attention_mask([self.batch_size, self.seq_length]						)

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

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

          snake_case__					:	List[str]          =					self.get_config()

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




   def   __lowerCAmelCase						(	self	:					List[str]						)->	Optional[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   __lowerCAmelCase						(	self	:					Union[str, Any]						)->	Optional[int]:
          (
              snake_case__  
          )					:	Optional[int]          =					self.prepare_config_and_inputs()

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




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







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



   '''simple docstring'''






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




   def   __lowerCAmelCase						(	self	:					str					, lowerCamelCase	:					str					, lowerCamelCase	:					Optional[Any]					, lowerCamelCase	:					List[Any]=False						)->	str:
          snake_case__					:	Union[str, Any]          =					super()._prepare_for_class(lowerCamelCase					, lowerCamelCase					, return_labels=lowerCamelCase						)

          if return_labels:
                 if model_class in get_values(lowerCamelCase						):
                        snake_case__					:	str          =					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]						)->	Tuple:
          snake_case__					:	int          =					ElectraModelTester(self						)
          snake_case__					:	str          =					ConfigTester(self					, config_class=lowerCamelCase					, hidden_size=37						)




   def   __lowerCAmelCase						(	self	:					Optional[Any]						)->	Optional[Any]:
          self.config_tester.run_common_tests()




   def   __lowerCAmelCase						(	self	:					Tuple						)->	Optional[Any]:
          snake_case__					:	Optional[int]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_model(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					str						)->	Union[str, Any]:
          snake_case__					:	Union[str, Any]          =					self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_electra_model_as_decoder(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					int						)->	Union[str, Any]:
          snake_case__					:	Tuple          =					self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                 snake_case__					:	Any          =					type
                 self.model_tester.create_and_check_electra_model(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					List[str]						)->	Any:
          snake_case__					:	List[Any]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_masked_lm(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Dict						)->	Any:
          snake_case__					:	Any          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_token_classification(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Optional[Any]						)->	Tuple:
          snake_case__					:	List[str]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_pretraining(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Dict						)->	Dict:
          snake_case__					:	List[str]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_sequence_classification(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Union[str, Any]						)->	List[Any]:
          snake_case__					:	Optional[int]          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_question_answering(*lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					str						)->	str:
          snake_case__					:	Any          =					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_electra_for_multiple_choice(*lowerCamelCase						)




   @slow
   def   __lowerCAmelCase						(	self	:					Dict						)->	List[Any]:
          for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case__					:	Tuple          =					ElectraModel.from_pretrained(lowerCamelCase						)
                 self.assertIsNotNone(lowerCamelCase						)




   def   __lowerCAmelCase						(	self	:					Any						)->	Union[str, Any]:
          snake_case__					:	Dict          =					self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_electra_for_causal_lm(*lowerCamelCase						)







@require_torch
class 					_A						(						unittest.TestCase							):



   '''simple docstring'''






   @slow
   def   __lowerCAmelCase						(	self	:					Any						)->	Tuple:
          snake_case__					:	Dict          =					ElectraModel.from_pretrained("""google/electra-small-discriminator"""						)
          snake_case__					:	Tuple          =					torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
          snake_case__					:	str          =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
          snake_case__					:	Optional[Any]          =					model(lowerCamelCase					, attention_mask=lowerCamelCase						)[0]
          snake_case__					:	Dict          =					torch.Size((1, 11, 256)						)
          self.assertEqual(output.shape					, lowerCamelCase						)
          snake_case__					:	Any          =					torch.tensor(
              [[[0.4_471, 0.6_821, -0.3_265], [0.4_627, 0.5_255, -0.3_668], [0.4_532, 0.3_313, -0.4_344]]]						)

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











from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__UpperCAmelCase			:  List[str]										=				{"configuration_levit": ["LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LevitConfig", "LevitOnnxConfig"]}

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase			:  Any										=				["LevitFeatureExtractor"]
      __UpperCAmelCase			:  Tuple										=				["LevitImageProcessor"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase			:  str										=				[
          "LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "LevitForImageClassification",
          "LevitForImageClassificationWithTeacher",
          "LevitModel",
          "LevitPreTrainedModel",
      ]


if TYPE_CHECKING:
      from .configuration_levit import LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, LevitConfig, LevitOnnxConfig

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_levit import LevitFeatureExtractor
            from .image_processing_levit import LevitImageProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_levit import (
                LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
                LevitForImageClassification,
                LevitForImageClassificationWithTeacher,
                LevitModel,
                LevitPreTrainedModel,
            )
else:
      import sys

      __UpperCAmelCase			:  Union[str, Any]										=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






