




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					:				Dict		,					A__					:				Optional[Any]		,					A__					:				List[str]=1_3		,					A__					:				List[str]=7		,					A__					:				str=True		,					A__					:				List[Any]=True		,					A__					:				Union[str, Any]=True		,					A__					:				Union[str, Any]=True		,					A__					:				Optional[Any]=9_9		,					A__					:				Union[str, Any]=3_2		,					A__					:				int=5		,					A__					:				List[Any]=4		,					A__					:				List[str]=3_7		,					A__					:				str="gelu"		,					A__					:				Optional[int]=0.1		,					A__					:				List[Any]=0.1		,					A__					:				str=5_1_2		,					A__					:				List[Any]=1_6		,					A__					:				Any=2		,					A__					:				Optional[int]=0.02		,					A__					:				Optional[int]=3		,					A__					:				Dict=4		,					A__					:				Optional[Any]=None		,					)			->    str:
           lowerCamelCase__ 					=			parent
           lowerCamelCase__ 					=			batch_size
           lowerCamelCase__ 					=			seq_length
           lowerCamelCase__ 					=			is_training
           lowerCamelCase__ 					=			use_input_mask
           lowerCamelCase__ 					=			use_token_type_ids
           lowerCamelCase__ 					=			use_labels
           lowerCamelCase__ 					=			vocab_size
           lowerCamelCase__ 					=			hidden_size
           lowerCamelCase__ 					=			num_hidden_layers
           lowerCamelCase__ 					=			num_attention_heads
           lowerCamelCase__ 					=			intermediate_size
           lowerCamelCase__ 					=			hidden_act
           lowerCamelCase__ 					=			hidden_dropout_prob
           lowerCamelCase__ 					=			attention_probs_dropout_prob
           lowerCamelCase__ 					=			max_position_embeddings
           lowerCamelCase__ 					=			type_vocab_size
           lowerCamelCase__ 					=			type_sequence_label_size
           lowerCamelCase__ 					=			initializer_range
           lowerCamelCase__ 					=			num_labels
           lowerCamelCase__ 					=			num_choices
           lowerCamelCase__ 					=			scope




     def 	__snake_case				(      self					:				Dict     )			->    Any:
           lowerCamelCase__ 					=			ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size     )

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

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

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

           lowerCamelCase__ 					=			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					:				Any     )			->    Union[str, 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=A__		,					initializer_range=self.initializer_range		,					)




     def 	__snake_case				(      self					:				List[str]     )			->    int:
           (
               (
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						
           ) 					=			self.prepare_config_and_inputs()

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




     def 	__snake_case				(      self					:				Optional[int]		,					A__					:				Any		,					A__					:				str		,					A__					:				Optional[Any]		,					A__					:				Tuple		,					A__					:				Optional[Any]		,					A__					:				Tuple		,					A__					:				int		,					A__					:				Tuple		,					A__					:				Optional[Any]		,					)			->    Union[str, Any]:
           lowerCamelCase__ 					=			True
           lowerCamelCase__ 					=			ElectraModel(A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(
               A__		,					attention_mask=A__		,					token_type_ids=A__		,					encoder_hidden_states=A__		,					encoder_attention_mask=A__		,					)
           lowerCamelCase__ 					=			model(
               A__		,					attention_mask=A__		,					token_type_ids=A__		,					encoder_hidden_states=A__		,					)
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__     )
           self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size)     )




     def 	__snake_case				(      self					:				Optional[int]		,					A__					:				str		,					A__					:				List[Any]		,					A__					:				List[str]		,					A__					:				Optional[Any]		,					A__					:				Any		,					A__					:				int		,					A__					:				Tuple		,					A__					:				int		,					)			->    Tuple:
           lowerCamelCase__ 					=			ElectraForMaskedLM(config=A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__		,					labels=A__     )
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)     )




     def 	__snake_case				(      self					:				Optional[Any]		,					A__					:				List[Any]		,					A__					:				Dict		,					A__					:				Dict		,					A__					:				Dict		,					A__					:				Optional[Any]		,					A__					:				str		,					A__					:				Optional[Any]		,					A__					:				Any		,					A__					:				Dict		,					)			->    Optional[int]:
           lowerCamelCase__ 					=			ElectraForCausalLM(config=A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__		,					labels=A__     )
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)     )




     def 	__snake_case				(      self					:				Dict		,					A__					:				Any		,					A__					:				str		,					A__					:				str		,					A__					:				int		,					A__					:				str		,					A__					:				Union[str, Any]		,					A__					:				Optional[int]		,					A__					:				str		,					)			->    Dict:
           lowerCamelCase__ 					=			self.num_labels
           lowerCamelCase__ 					=			ElectraForTokenClassification(config=A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__		,					labels=A__     )
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.num_labels)     )




     def 	__snake_case				(      self					:				Union[str, Any]		,					A__					:				int		,					A__					:				List[str]		,					A__					:				List[str]		,					A__					:				Any		,					A__					:				int		,					A__					:				int		,					A__					:				Tuple		,					A__					:				Dict		,					)			->    List[str]:
           lowerCamelCase__ 					=			self.num_labels
           lowerCamelCase__ 					=			ElectraForPreTraining(config=A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__		,					labels=A__     )
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length)     )




     def 	__snake_case				(      self					:				int		,					A__					:				Union[str, Any]		,					A__					:				Any		,					A__					:				List[Any]		,					A__					:				Union[str, Any]		,					A__					:				Union[str, Any]		,					A__					:				int		,					A__					:				List[str]		,					A__					:				Union[str, Any]		,					)			->    List[Any]:
           lowerCamelCase__ 					=			self.num_labels
           lowerCamelCase__ 					=			ElectraForSequenceClassification(A__     )
           model.to(A__     )
           model.eval()
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__		,					token_type_ids=A__		,					labels=A__     )
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)     )




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




     def 	__snake_case				(      self					:				Any     )			->    List[Any]:
           lowerCamelCase__ 					=			self.prepare_config_and_inputs()
           (
               (
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						(
               lowerCamelCase__
           )					,						
           ) 					=			config_and_inputs
           lowerCamelCase__ 					=			{'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"""

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




     def 	__snake_case				(      self					:				Optional[Any]		,					A__					:				List[Any]		,					A__					:				Tuple		,					A__					:				Union[str, Any]=False     )			->    Union[str, Any]:
           lowerCamelCase__ 					=			super()._prepare_for_class(A__		,					A__		,					return_labels=A__     )

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




     def 	__snake_case				(      self					:				List[Any]     )			->    Union[str, Any]:
           lowerCamelCase__ 					=			ElectraModelTester(self     )
           lowerCamelCase__ 					=			ConfigTester(self		,					config_class=A__		,					hidden_size=3_7     )




     def 	__snake_case				(      self					:				Any     )			->    Any:
           self.config_tester.run_common_tests()




     def 	__snake_case				(      self					:				Union[str, Any]     )			->    Optional[Any]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_model(*A__     )




     def 	__snake_case				(      self					:				Optional[Any]     )			->    List[str]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_electra_model_as_decoder(*A__     )




     def 	__snake_case				(      self					:				Optional[Any]     )			->    Optional[int]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowerCamelCase__ 					=			type
                 self.model_tester.create_and_check_electra_model(*A__     )




     def 	__snake_case				(      self					:				Any     )			->    str:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_masked_lm(*A__     )




     def 	__snake_case				(      self					:				Any     )			->    Union[str, Any]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_token_classification(*A__     )




     def 	__snake_case				(      self					:				Tuple     )			->    Tuple:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_pretraining(*A__     )




     def 	__snake_case				(      self					:				int     )			->    Optional[Any]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_sequence_classification(*A__     )




     def 	__snake_case				(      self					:				Dict     )			->    Optional[Any]:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_question_answering(*A__     )




     def 	__snake_case				(      self					:				Any     )			->    Tuple:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_electra_for_multiple_choice(*A__     )




     @slow
     def 	__snake_case				(      self					:				Optional[int]     )			->    List[str]:
           for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase__ 					=			ElectraModel.from_pretrained(A__     )
                 self.assertIsNotNone(A__     )




     def 	__snake_case				(      self					:				Optional[int]     )			->    Any:
           lowerCamelCase__ 					=			self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_electra_for_causal_lm(*A__     )






@require_torch
class      A     (						unittest.TestCase						):
     """simple docstring"""

     @slow
     def 	__snake_case				(      self					:				Tuple     )			->    Any:
           lowerCamelCase__ 					=			ElectraModel.from_pretrained('google/electra-small-discriminator'     )
           lowerCamelCase__ 					=			torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]     )
           lowerCamelCase__ 					=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
           lowerCamelCase__ 					=			model(A__		,					attention_mask=A__     )[0]
           lowerCamelCase__ 					=			torch.Size((1, 1_1, 2_5_6)     )
           self.assertEqual(output.shape		,					A__     )
           lowerCamelCase__ 					=			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]		,					A__		,					atol=1E-4     )     )





# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
      import tensorflow as tf






class      A     (						TensorFormatter[Mapping, "tf.Tensor", Mapping]						):
     """simple docstring"""

     def __init__(      self					:				int		,					A__					:				Tuple=None		,					**A__					:				Tuple     )			->    Dict:
           super().__init__(features=A__     )
           lowerCamelCase__ 					=			tf_tensor_kwargs
           import tensorflow as tf  # noqa: import tf at initialization




     def 	__snake_case				(      self					:				List[Any]		,					A__					:				List[str]     )			->    Dict:
           import tensorflow as tf

           if isinstance(A__		,					A__     ) and column:
                 if all(
                     isinstance(A__		,					tf.Tensor     ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column     ):
                       return tf.stack(A__     )
                 elif all(
                     isinstance(A__		,					(tf.Tensor, tf.RaggedTensor)     ) and x.ndim == 1 and x.dtype == column[0].dtype
                     for x in column     ):
                       # only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
                       return tf.ragged.stack(A__     )

           return column




     def 	__snake_case				(      self					:				Tuple		,					A__					:				Optional[int]     )			->    int:
           import tensorflow as tf

           if value is None:
                 return value

           lowerCamelCase__ 					=			{}

           if isinstance(A__		,					(np.number, np.ndarray)     ) and np.issubdtype(value.dtype		,					np.integer     ):
                 lowerCamelCase__ 					=			{'dtype': tf.intaa}
           elif isinstance(A__		,					(np.number, np.ndarray)     ) and np.issubdtype(value.dtype		,					np.floating     ):
                 lowerCamelCase__ 					=			{'dtype': tf.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                 import PIL.Image

                 if isinstance(A__		,					PIL.Image.Image     ):
                       lowerCamelCase__ 					=			np.asarray(A__     )

           return tf.convert_to_tensor(A__		,					**{**default_dtype, **self.tf_tensor_kwargs}     )




     def 	__snake_case				(      self					:				Optional[int]		,					A__					:				Dict     )			->    List[str]:
           import tensorflow as tf

           # support for torch, tf, jax etc.
           if config.TORCH_AVAILABLE and "torch" in sys.modules:
                 import torch

                 if isinstance(A__		,					torch.Tensor     ):
                       return self._tensorize(data_struct.detach().cpu().numpy()[()]     )
           if hasattr(A__		,					'__array__'     ) and not isinstance(A__		,					tf.Tensor     ):
                 lowerCamelCase__ 					=			data_struct.__array__()
           # support for nested types like struct of list of struct
           if isinstance(A__		,					np.ndarray     ):
                 if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
                       return self._consolidate([self.recursive_tensorize(A__     ) for substruct in data_struct]     )
           elif isinstance(A__		,					(list, tuple)     ):
                 return self._consolidate([self.recursive_tensorize(A__     ) for substruct in data_struct]     )
           return self._tensorize(A__     )




     def 	__snake_case				(      self					:				List[Any]		,					A__					:				dict     )			->    List[str]:
           return map_nested(self._recursive_tensorize		,					A__		,					map_list=A__     )




     def 	__snake_case				(      self					:				Union[str, Any]		,					A__					:				pa.Table     )			->    Mapping:
           lowerCamelCase__ 					=			self.numpy_arrow_extractor().extract_row(A__     )
           lowerCamelCase__ 					=			self.python_features_decoder.decode_row(A__     )
           return self.recursive_tensorize(A__     )




     def 	__snake_case				(      self					:				Optional[Any]		,					A__					:				pa.Table     )			->    "tf.Tensor":
           lowerCamelCase__ 					=			self.numpy_arrow_extractor().extract_column(A__     )
           lowerCamelCase__ 					=			self.python_features_decoder.decode_column(A__		,					pa_table.column_names[0]     )
           lowerCamelCase__ 					=			self.recursive_tensorize(A__     )
           lowerCamelCase__ 					=			self._consolidate(A__     )
           return column




     def 	__snake_case				(      self					:				int		,					A__					:				pa.Table     )			->    Mapping:
           lowerCamelCase__ 					=			self.numpy_arrow_extractor().extract_batch(A__     )
           lowerCamelCase__ 					=			self.python_features_decoder.decode_batch(A__     )
           lowerCamelCase__ 					=			self.recursive_tensorize(A__     )
           for column_name in batch:
                 lowerCamelCase__ 					=			self._consolidate(batch[column_name]     )
           return batch