




"""simple docstring"""

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
						from PIL import Image

						from transformers import ConvNextImageProcessor



class  _snake_case				:

				'''simple docstring'''




				def __init__(							self      :  Any					,							snake_case      :  List[str]					,							snake_case      :  Tuple=13					,							snake_case      :  List[Any]=32					,							snake_case      :  str=3					,							snake_case      :  List[Any]=4					,							snake_case      :  Optional[int]=[10, 20, 30, 40]					,							snake_case      :  Tuple=[2, 2, 3, 2]					,							snake_case      :  List[str]=True					,							snake_case      :  Union[str, Any]=True					,							snake_case      :  str=37					,							snake_case      :  List[str]="gelu"					,							snake_case      :  Tuple=10					,							snake_case      :  int=0.02					,							snake_case      :  List[Any]=3					,							snake_case      :  Tuple=None					,							):
								UpperCAmelCase_					:Optional[int]      			=      parent
								UpperCAmelCase_					:int      			=      batch_size
								UpperCAmelCase_					:List[Any]      			=      image_size
								UpperCAmelCase_					:List[str]      			=      num_channels
								UpperCAmelCase_					:Dict      			=      num_stages
								UpperCAmelCase_					:Dict      			=      hidden_sizes
								UpperCAmelCase_					:Any      			=      depths
								UpperCAmelCase_					:Dict      			=      is_training
								UpperCAmelCase_					:Any      			=      use_labels
								UpperCAmelCase_					:Any      			=      intermediate_size
								UpperCAmelCase_					:Any      			=      hidden_act
								UpperCAmelCase_					:Tuple      			=      type_sequence_label_size
								UpperCAmelCase_					:List[Any]      			=      initializer_range
								UpperCAmelCase_					:Dict      			=      scope





				def 				snake_case_ (							self      :  Optional[int]     ):
								UpperCAmelCase_					:Optional[Any]      			=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

								UpperCAmelCase_					:Optional[int]      			=      None
								if self.use_labels:
												UpperCAmelCase_					:Dict      			=      ids_tensor([self.batch_size]					,							self.type_sequence_label_size     )

								UpperCAmelCase_					:Union[str, Any]      			=      self.get_config()

								return config, pixel_values, labels





				def 				snake_case_ (							self      :  Any     ):
								return ConvNextConfig(
								    num_channels=self.num_channels					,							hidden_sizes=self.hidden_sizes					,							depths=self.depths					,							num_stages=self.num_stages					,							hidden_act=self.hidden_act					,							is_decoder=snake_case					,							initializer_range=self.initializer_range					,							)





				def 				snake_case_ (							self      :  str					,							snake_case      :  List[Any]					,							snake_case      :  Tuple					,							snake_case      :  Any     ):
								UpperCAmelCase_					:List[str]      			=      TFConvNextModel(config=snake_case     )
								UpperCAmelCase_					:List[Any]      			=      model(snake_case					,							training=snake_case     )
								# expected last hidden states: B, C, H // 32, W // 32
								self.parent.assertEqual(
								    result.last_hidden_state.shape					,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)					,							)





				def 				snake_case_ (							self      :  Any					,							snake_case      :  Optional[Any]					,							snake_case      :  List[str]					,							snake_case      :  Optional[int]     ):
								UpperCAmelCase_					:Any      			=      self.type_sequence_label_size
								UpperCAmelCase_					:Union[str, Any]      			=      TFConvNextForImageClassification(snake_case     )
								UpperCAmelCase_					:List[str]      			=      model(snake_case					,							labels=snake_case					,							training=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.type_sequence_label_size)     )





				def 				snake_case_ (							self      :  int     ):
								UpperCAmelCase_					:Any      			=      self.prepare_config_and_inputs()
								UpperCAmelCase_			,UpperCAmelCase_			,UpperCAmelCase_					:Optional[int]      			=      config_and_inputs
								UpperCAmelCase_					:Optional[Any]      			=      {'''pixel_values''': pixel_values}
								return config, inputs_dict



@require_tf
class  _snake_case				(							A__							,						A__							,						unittest.TestCase       ):

				'''simple docstring'''




				UpperCamelCase__		     =(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
				UpperCamelCase__		     =(
				    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
				    if is_tf_available()
				    else {}
				)

				UpperCamelCase__		     =False
				UpperCamelCase__		     =False
				UpperCamelCase__		     =False
				UpperCamelCase__		     =False
				UpperCamelCase__		     =False





				def 				snake_case_ (							self      :  Optional[Any]     ):
								UpperCAmelCase_					:Tuple      			=      TFConvNextModelTester(self     )
								UpperCAmelCase_					:Union[str, Any]      			=      ConfigTester(
								    self					,							config_class=snake_case					,							has_text_modality=snake_case					,							hidden_size=37					,							)





				@unittest.skip(reason='''ConvNext does not use inputs_embeds'''     )
				def 				snake_case_ (							self      :  int     ):
								pass





				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''     )     ) == 0					,							reason='''TF does not support backprop for grouped convolutions on CPU.'''					,							)
				@slow
				def 				snake_case_ (							self      :  Tuple     ):
								super().test_keras_fit()





				@unittest.skip(reason='''ConvNext does not support input and output embeddings'''     )
				def 				snake_case_ (							self      :  Union[str, Any]     ):
								pass





				def 				snake_case_ (							self      :  List[Any]     ):
								UpperCAmelCase_			,UpperCAmelCase_					:Union[str, Any]      			=      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												UpperCAmelCase_					:Dict      			=      model_class(snake_case     )
												UpperCAmelCase_					:Optional[int]      			=      inspect.signature(model.call     )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												UpperCAmelCase_					:Optional[int]      			=      [*signature.parameters.keys()]

												UpperCAmelCase_					:Union[str, Any]      			=      ['''pixel_values''']
												self.assertListEqual(arg_names[:1]					,							snake_case     )





				def 				snake_case_ (							self      :  Tuple     ):
								UpperCAmelCase_					:Union[str, Any]      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case     )





				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''     )     ) == 0					,							reason='''TF does not support backprop for grouped convolutions on CPU.'''					,							)
				def 				snake_case_ (							self      :  str     ):
								super().test_dataset_conversion()





				def 				snake_case_ (							self      :  int     ):
								def check_hidden_states_output(snake_case      :  int					,							snake_case      :  List[str]					,							snake_case      :  str     ):
												UpperCAmelCase_					:Optional[int]      			=      model_class(snake_case     )

												UpperCAmelCase_					:str      			=      model(**self._prepare_for_class(snake_case					,							snake_case     )     )
												UpperCAmelCase_					:Any      			=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												UpperCAmelCase_					:Any      			=      self.model_tester.num_stages
												self.assertEqual(len(snake_case     )					,							expected_num_stages + 1     )

												# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
												self.assertListEqual(
												    list(hidden_states[0].shape[-2:]     )					,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4]					,							)

								UpperCAmelCase_			,UpperCAmelCase_					:Dict      			=      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												UpperCAmelCase_					:List[Any]      			=      True
												check_hidden_states_output(snake_case					,							snake_case					,							snake_case     )

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												UpperCAmelCase_					:Tuple      			=      True

												check_hidden_states_output(snake_case					,							snake_case					,							snake_case     )





				def 				snake_case_ (							self      :  List[Any]     ):
								UpperCAmelCase_			,UpperCAmelCase_					:Union[str, Any]      			=      self.model_tester.prepare_config_and_inputs_for_common()

								def check_equivalence(snake_case      :  Any					,							snake_case      :  List[Any]					,							snake_case      :  List[str]					,							snake_case      :  Tuple={}     ):
												UpperCAmelCase_					:Tuple      			=      model(snake_case					,							return_dict=snake_case					,							**snake_case     )
												UpperCAmelCase_					:Optional[int]      			=      model(snake_case					,							return_dict=snake_case					,							**snake_case     ).to_tuple()

												def recursive_check(snake_case      :  List[Any]					,							snake_case      :  Optional[Any]     ):
																if isinstance(snake_case					,							(List, Tuple)     ):
																				for tuple_iterable_value, dict_iterable_value in zip(snake_case					,							snake_case     ):
																								recursive_check(snake_case					,							snake_case     )
																elif tuple_object is None:
																				return
																else:
																				self.assertTrue(
																				    all(tf.equal(snake_case					,							snake_case     )     )					,							msg=(
																				        '''Tuple and dict output are not equal. Difference:'''
																				        f' {tf.math.reduce_max(tf.abs(tuple_object - dict_object     )     )}'
																				    )					,							)

																recursive_check(snake_case					,							snake_case     )

								for model_class in self.all_model_classes:
												UpperCAmelCase_					:Optional[Any]      			=      model_class(snake_case     )

												UpperCAmelCase_					:int      			=      self._prepare_for_class(snake_case					,							snake_case     )
												UpperCAmelCase_					:Tuple      			=      self._prepare_for_class(snake_case					,							snake_case     )
												check_equivalence(snake_case					,							snake_case					,							snake_case     )

												UpperCAmelCase_					:Dict      			=      self._prepare_for_class(snake_case					,							snake_case					,							return_labels=snake_case     )
												UpperCAmelCase_					:Optional[Any]      			=      self._prepare_for_class(snake_case					,							snake_case					,							return_labels=snake_case     )
												check_equivalence(snake_case					,							snake_case					,							snake_case     )

												UpperCAmelCase_					:Dict      			=      self._prepare_for_class(snake_case					,							snake_case     )
												UpperCAmelCase_					:Any      			=      self._prepare_for_class(snake_case					,							snake_case     )
												check_equivalence(snake_case					,							snake_case					,							snake_case					,							{'''output_hidden_states''': True}     )

												UpperCAmelCase_					:List[Any]      			=      self._prepare_for_class(snake_case					,							snake_case					,							return_labels=snake_case     )
												UpperCAmelCase_					:Optional[int]      			=      self._prepare_for_class(snake_case					,							snake_case					,							return_labels=snake_case     )
												check_equivalence(snake_case					,							snake_case					,							snake_case					,							{'''output_hidden_states''': True}     )





				def 				snake_case_ (							self      :  Any     ):
								UpperCAmelCase_					:List[Any]      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*snake_case     )





				@slow
				def 				snake_case_ (							self      :  Optional[int]     ):
								UpperCAmelCase_					:str      			=      TFConvNextModel.from_pretrained('''facebook/convnext-tiny-224'''     )
								self.assertIsNotNone(snake_case     )



def 				a (						):



				'''simple docstring'''





				UpperCAmelCase_					:Tuple      			=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
				return image





@require_tf
@require_vision
class  _snake_case				(							unittest.TestCase       ):

				'''simple docstring'''




				@cached_property
				def 				snake_case_ (							self      :  Union[str, Any]     ):
								return ConvNextImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''     ) if is_vision_available() else None





				@slow
				def 				snake_case_ (							self      :  Any     ):
								UpperCAmelCase_					:Dict      			=      TFConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''     )

								UpperCAmelCase_					:Optional[int]      			=      self.default_image_processor
								UpperCAmelCase_					:List[str]      			=      prepare_img()
								UpperCAmelCase_					:Any      			=      image_processor(images=snake_case					,							return_tensors='''tf'''     )

								# forward pass
								UpperCAmelCase_					:Dict      			=      model(**snake_case     )

								# verify the logits
								UpperCAmelCase_					:Tuple      			=      tf.TensorShape((1, 1_000)     )
								self.assertEqual(outputs.logits.shape					,							snake_case     )

								UpperCAmelCase_					:Tuple      			=      tf.constant([-0.0_260, -0.4_739, 0.1_911]     )

								tf.debugging.assert_near(outputs.logits[0, :3]					,							snake_case					,							atol=1e-4     )








"""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  _snake_case				:

				'''simple docstring'''




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





				def 				snake_case_ (							self      :  Tuple     ):
								UpperCAmelCase_					:str      			=      ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size     )

								UpperCAmelCase_					:Optional[int]      			=      None
								if self.use_input_mask:
												UpperCAmelCase_					:Any      			=      random_attention_mask([self.batch_size, self.seq_length]     )

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

								UpperCAmelCase_					:str      			=      None
								UpperCAmelCase_					:Dict      			=      None
								UpperCAmelCase_					:str      			=      None
								if self.use_labels:
												UpperCAmelCase_					:str      			=      ids_tensor([self.batch_size]					,							self.type_sequence_label_size     )
												UpperCAmelCase_					:List[Any]      			=      ids_tensor([self.batch_size, self.seq_length]					,							self.num_labels     )
												UpperCAmelCase_					:Tuple      			=      ids_tensor([self.batch_size]					,							self.num_choices     )
												UpperCAmelCase_					:Dict      			=      ids_tensor([self.batch_size, self.seq_length]					,							1     )

								UpperCAmelCase_					:Dict      			=      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      :  str     ):
								return ElectraConfig(
								    vocab_size=self.vocab_size					,							hidden_size=self.hidden_size					,							num_hidden_layers=self.num_hidden_layers					,							num_attention_heads=self.num_attention_heads					,							intermediate_size=self.intermediate_size					,							hidden_act=self.hidden_act					,							hidden_dropout_prob=self.hidden_dropout_prob					,							attention_probs_dropout_prob=self.attention_probs_dropout_prob					,							max_position_embeddings=self.max_position_embeddings					,							type_vocab_size=self.type_vocab_size					,							is_decoder=snake_case					,							initializer_range=self.initializer_range					,							)





				def 				snake_case_ (							self      :  List[Any]     ):
								(
								    (
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,(
								    UpperCAmelCase_
								)			,
								)					:str      			=      self.prepare_config_and_inputs()

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





				def 				snake_case_ (							self      :  int					,							snake_case      :  Any					,							snake_case      :  Optional[Any]					,							snake_case      :  List[Any]					,							snake_case      :  Union[str, Any]					,							snake_case      :  Optional[Any]					,							snake_case      :  Optional[Any]					,							snake_case      :  List[Any]					,							snake_case      :  str					,							snake_case      :  Any					,							):
								UpperCAmelCase_					:Union[str, Any]      			=      True
								UpperCAmelCase_					:Optional[int]      			=      ElectraModel(snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:int      			=      model(
								    snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							encoder_hidden_states=snake_case					,							encoder_attention_mask=snake_case					,							)
								UpperCAmelCase_					:int      			=      model(
								    snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							encoder_hidden_states=snake_case					,							)
								UpperCAmelCase_					:List[str]      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case     )
								self.parent.assertEqual(result.last_hidden_state.shape					,							(self.batch_size, self.seq_length, self.hidden_size)     )





				def 				snake_case_ (							self      :  str					,							snake_case      :  Any					,							snake_case      :  Dict					,							snake_case      :  Optional[int]					,							snake_case      :  Optional[int]					,							snake_case      :  int					,							snake_case      :  List[str]					,							snake_case      :  List[Any]					,							snake_case      :  List[str]					,							):
								UpperCAmelCase_					:Tuple      			=      ElectraForMaskedLM(config=snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:str      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							labels=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)     )





				def 				snake_case_ (							self      :  List[str]					,							snake_case      :  Dict					,							snake_case      :  Union[str, Any]					,							snake_case      :  Union[str, Any]					,							snake_case      :  Optional[Any]					,							snake_case      :  List[str]					,							snake_case      :  Optional[int]					,							snake_case      :  Union[str, Any]					,							snake_case      :  int					,							snake_case      :  Tuple					,							):
								UpperCAmelCase_					:str      			=      ElectraForCausalLM(config=snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:Optional[Any]      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							labels=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)     )





				def 				snake_case_ (							self      :  int					,							snake_case      :  List[Any]					,							snake_case      :  List[str]					,							snake_case      :  Dict					,							snake_case      :  Optional[Any]					,							snake_case      :  Union[str, Any]					,							snake_case      :  Optional[Any]					,							snake_case      :  List[Any]					,							snake_case      :  List[str]					,							):
								UpperCAmelCase_					:Union[str, Any]      			=      self.num_labels
								UpperCAmelCase_					:Optional[int]      			=      ElectraForTokenClassification(config=snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:List[str]      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							labels=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.num_labels)     )





				def 				snake_case_ (							self      :  Tuple					,							snake_case      :  Tuple					,							snake_case      :  int					,							snake_case      :  int					,							snake_case      :  Optional[Any]					,							snake_case      :  Optional[int]					,							snake_case      :  Union[str, Any]					,							snake_case      :  List[str]					,							snake_case      :  List[Any]					,							):
								UpperCAmelCase_					:List[str]      			=      self.num_labels
								UpperCAmelCase_					:Union[str, Any]      			=      ElectraForPreTraining(config=snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:int      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							labels=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length)     )





				def 				snake_case_ (							self      :  Dict					,							snake_case      :  Any					,							snake_case      :  List[Any]					,							snake_case      :  Optional[int]					,							snake_case      :  Optional[Any]					,							snake_case      :  Optional[int]					,							snake_case      :  Dict					,							snake_case      :  int					,							snake_case      :  int					,							):
								UpperCAmelCase_					:Optional[Any]      			=      self.num_labels
								UpperCAmelCase_					:Optional[int]      			=      ElectraForSequenceClassification(snake_case     )
								model.to(snake_case     )
								model.eval()
								UpperCAmelCase_					:Any      			=      model(snake_case					,							attention_mask=snake_case					,							token_type_ids=snake_case					,							labels=snake_case     )
								self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)     )





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





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



@require_torch
class  _snake_case				(							A__							,						A__							,						unittest.TestCase       ):

				'''simple docstring'''




				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 				snake_case_ (							self      :  List[str]					,							snake_case      :  List[str]					,							snake_case      :  Tuple					,							snake_case      :  List[Any]=False     ):
								UpperCAmelCase_					:Optional[int]      			=      super()._prepare_for_class(snake_case					,							snake_case					,							return_labels=snake_case     )

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





				def 				snake_case_ (							self      :  Optional[int]     ):
								UpperCAmelCase_					:int      			=      ElectraModelTester(self     )
								UpperCAmelCase_					:List[str]      			=      ConfigTester(self					,							config_class=snake_case					,							hidden_size=37     )





				def 				snake_case_ (							self      :  List[Any]     ):
								self.config_tester.run_common_tests()





				def 				snake_case_ (							self      :  List[Any]     ):
								UpperCAmelCase_					:Union[str, Any]      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_model(*snake_case     )





				def 				snake_case_ (							self      :  List[str]     ):
								UpperCAmelCase_					:str      			=      self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_electra_model_as_decoder(*snake_case     )





				def 				snake_case_ (							self      :  Union[str, Any]     ):
								UpperCAmelCase_					:Dict      			=      self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
												UpperCAmelCase_					:Dict      			=      type
												self.model_tester.create_and_check_electra_model(*snake_case     )





				def 				snake_case_ (							self      :  str     ):
								UpperCAmelCase_					:Dict      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_masked_lm(*snake_case     )





				def 				snake_case_ (							self      :  Tuple     ):
								UpperCAmelCase_					:Dict      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_token_classification(*snake_case     )





				def 				snake_case_ (							self      :  Optional[int]     ):
								UpperCAmelCase_					:Optional[int]      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_pretraining(*snake_case     )





				def 				snake_case_ (							self      :  Dict     ):
								UpperCAmelCase_					:Any      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_sequence_classification(*snake_case     )





				def 				snake_case_ (							self      :  List[Any]     ):
								UpperCAmelCase_					:Union[str, Any]      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_question_answering(*snake_case     )





				def 				snake_case_ (							self      :  Any     ):
								UpperCAmelCase_					:int      			=      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_electra_for_multiple_choice(*snake_case     )





				@slow
				def 				snake_case_ (							self      :  Dict     ):
								for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												UpperCAmelCase_					:Optional[int]      			=      ElectraModel.from_pretrained(snake_case     )
												self.assertIsNotNone(snake_case     )





				def 				snake_case_ (							self      :  Any     ):
								UpperCAmelCase_					:Any      			=      self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_electra_for_causal_lm(*snake_case     )



@require_torch
class  _snake_case				(							unittest.TestCase       ):

				'''simple docstring'''




				@slow
				def 				snake_case_ (							self      :  List[Any]     ):
								UpperCAmelCase_					:Optional[int]      			=      ElectraModel.from_pretrained('''google/electra-small-discriminator'''     )
								UpperCAmelCase_					:Dict      			=      torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]     )
								UpperCAmelCase_					:int      			=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
								UpperCAmelCase_					:Optional[int]      			=      model(snake_case					,							attention_mask=snake_case     )[0]
								UpperCAmelCase_					:Optional[Any]      			=      torch.Size((1, 11, 256)     )
								self.assertEqual(output.shape					,							snake_case     )
								UpperCAmelCase_					:Optional[int]      			=      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]					,							snake_case					,							atol=1e-4     )     )


