'''simple docstring'''




from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


__SCREAMING_SNAKE_CASE :  Tuple  =						logging.get_logger(__name__)

# General docstring
__SCREAMING_SNAKE_CASE :  List[str]  =						"""ResNetConfig"""

# Base docstring
__SCREAMING_SNAKE_CASE :  List[str]  =						"""microsoft/resnet-50"""
__SCREAMING_SNAKE_CASE :  Tuple  =						[1, 2_048, 7, 7]

# Image classification docstring
__SCREAMING_SNAKE_CASE :  Optional[Any]  =						"""microsoft/resnet-50"""
__SCREAMING_SNAKE_CASE :  Union[str, Any]  =						"""tiger cat"""

__SCREAMING_SNAKE_CASE :  Dict  =						[
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       List[Any]		,      A		:       int		,      A		:       int = 3		,      A		:       int = 1		,      A		:       str = "relu"		,      **A		:       List[Any] ):
													super().__init__(**A )
													_UpperCAmelCase  :						Dict							   =					kernel_size // 2
													_UpperCAmelCase  :						Dict							   =					tf.keras.layers.ConvaD(
													    A		,      kernel_size=A		,      strides=A		,      padding="valid"		,      use_bias=A		,      name="convolution" )
													# Use same default momentum and epsilon as PyTorch equivalent
													_UpperCAmelCase  :						Optional[Any]							   =					tf.keras.layers.BatchNormalization(epsilon=1E-5		,      momentum=0.9		,      name="normalization" )
													_UpperCAmelCase  :						Tuple							   =					ACTaFN[activation] if activation is not None else tf.keras.layers.Activation("linear" )




						def   _A			( self		:       Optional[Any]		,      A		:       tf.Tensor ):
													# Pad to match that done in the PyTorch Conv2D model
													_UpperCAmelCase  :						Optional[Any]							   =					(self.pad_value, self.pad_value)
													_UpperCAmelCase  :						Any							   =					tf.pad(A		,      [(0, 0), height_pad, width_pad, (0, 0)] )
													_UpperCAmelCase  :						Dict							   =					self.conv(A )
													return hidden_state





						def   _A			( self		:       List[str]		,      A		:       tf.Tensor		,      A		:       bool = False ):
													_UpperCAmelCase  :						int							   =					self.convolution(A )
													_UpperCAmelCase  :						Optional[int]							   =					self.normalization(A		,      training=A )
													_UpperCAmelCase  :						List[str]							   =					self.activation(A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       List[str]		,      A		:       ResNetConfig		,      **A		:       Tuple ):
													super().__init__(**A )
													_UpperCAmelCase  :						str							   =					TFResNetConvLayer(
													    config.embedding_size		,      kernel_size=7		,      stride=2		,      activation=config.hidden_act		,      name="embedder"		,      )
													_UpperCAmelCase  :						Optional[int]							   =					tf.keras.layers.MaxPoolaD(pool_size=3		,      strides=2		,      padding="valid"		,      name="pooler" )
													_UpperCAmelCase  :						List[str]							   =					config.num_channels





						def   _A			( self		:       List[str]		,      A		:       tf.Tensor		,      A		:       bool = False ):
													_UpperCAmelCase  :						Any							   =					shape_list(A )
													if tf.executing_eagerly() and num_channels != self.num_channels:
																				raise ValueError(
																				    "Make sure that the channel dimension of the pixel values match with the one set in the configuration." )
													_UpperCAmelCase  :						Any							   =					pixel_values
													_UpperCAmelCase  :						Union[str, Any]							   =					self.embedder(A )
													_UpperCAmelCase  :						int							   =					tf.pad(A		,      [[0, 0], [1, 1], [1, 1], [0, 0]] )
													_UpperCAmelCase  :						Tuple							   =					self.pooler(A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       str		,      A		:       int		,      A		:       int = 2		,      **A		:       Union[str, Any] ):
													super().__init__(**A )
													_UpperCAmelCase  :						Dict							   =					tf.keras.layers.ConvaD(
													    A		,      kernel_size=1		,      strides=A		,      use_bias=A		,      name="convolution" )
													# Use same default momentum and epsilon as PyTorch equivalent
													_UpperCAmelCase  :						Tuple							   =					tf.keras.layers.BatchNormalization(epsilon=1E-5		,      momentum=0.9		,      name="normalization" )





						def   _A			( self		:       List[Any]		,      A		:       tf.Tensor		,      A		:       bool = False ):
													_UpperCAmelCase  :						List[Any]							   =					x
													_UpperCAmelCase  :						Tuple							   =					self.convolution(A )
													_UpperCAmelCase  :						List[str]							   =					self.normalization(A		,      training=A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       Optional[Any]		,      A		:       int		,      A		:       int		,      A		:       int = 1		,      A		:       str = "relu"		,      **A		:       Dict ):
													super().__init__(**A )
													_UpperCAmelCase  :						List[Any]							   =					in_channels != out_channels or stride != 1
													_UpperCAmelCase  :						Dict							   =					TFResNetConvLayer(A		,      stride=A		,      name="layer.0" )
													_UpperCAmelCase  :						int							   =					TFResNetConvLayer(A		,      activation=A		,      name="layer.1" )
													_UpperCAmelCase  :						List[Any]							   =					(
													    TFResNetShortCut(A		,      stride=A		,      name="shortcut" )
													    if should_apply_shortcut
													    else tf.keras.layers.Activation("linear"		,      name="shortcut" )
													)
													_UpperCAmelCase  :						Optional[int]							   =					ACTaFN[activation]





						def   _A			( self		:       Any		,      A		:       tf.Tensor		,      A		:       bool = False ):
													_UpperCAmelCase  :						Dict							   =					hidden_state
													_UpperCAmelCase  :						Optional[Any]							   =					self.conva(A		,      training=A )
													_UpperCAmelCase  :						List[str]							   =					self.conva(A		,      training=A )
													_UpperCAmelCase  :						Dict							   =					self.shortcut(A		,      training=A )
													hidden_state += residual
													_UpperCAmelCase  :						List[str]							   =					self.activation(A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       List[str]		,      A		:       int		,      A		:       int		,      A		:       int = 1		,      A		:       str = "relu"		,      A		:       int = 4		,      **A		:       str		,      ):
													super().__init__(**A )
													_UpperCAmelCase  :						List[Any]							   =					in_channels != out_channels or stride != 1
													_UpperCAmelCase  :						List[str]							   =					out_channels // reduction
													_UpperCAmelCase  :						List[Any]							   =					TFResNetConvLayer(A		,      kernel_size=1		,      name="layer.0" )
													_UpperCAmelCase  :						Dict							   =					TFResNetConvLayer(A		,      stride=A		,      name="layer.1" )
													_UpperCAmelCase  :						str							   =					TFResNetConvLayer(A		,      kernel_size=1		,      activation=A		,      name="layer.2" )
													_UpperCAmelCase  :						Optional[Any]							   =					(
													    TFResNetShortCut(A		,      stride=A		,      name="shortcut" )
													    if should_apply_shortcut
													    else tf.keras.layers.Activation("linear"		,      name="shortcut" )
													)
													_UpperCAmelCase  :						Union[str, Any]							   =					ACTaFN[activation]





						def   _A			( self		:       Optional[int]		,      A		:       tf.Tensor		,      A		:       bool = False ):
													_UpperCAmelCase  :						Optional[Any]							   =					hidden_state
													_UpperCAmelCase  :						int							   =					self.conva(A		,      training=A )
													_UpperCAmelCase  :						Any							   =					self.conva(A		,      training=A )
													_UpperCAmelCase  :						List[Any]							   =					self.conva(A		,      training=A )
													_UpperCAmelCase  :						List[str]							   =					self.shortcut(A		,      training=A )
													hidden_state += residual
													_UpperCAmelCase  :						Tuple							   =					self.activation(A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       Dict		,      A		:       ResNetConfig		,      A		:       int		,      A		:       int		,      A		:       int = 2		,      A		:       int = 2		,      **A		:       Dict ):
													super().__init__(**A )

													_UpperCAmelCase  :						Optional[int]							   =					TFResNetBottleNeckLayer if config.layer_type == "bottleneck" else TFResNetBasicLayer

													_UpperCAmelCase  :						Dict							   =					[layer(A		,      A		,      stride=A		,      activation=config.hidden_act		,      name="layers.0" )]
													layers += [
													    layer(A		,      A		,      activation=config.hidden_act		,      name=F"""layers.{i + 1}""" )
													    for i in range(depth - 1 )
													]
													_UpperCAmelCase  :						int							   =					layers





						def   _A			( self		:       List[Any]		,      A		:       tf.Tensor		,      A		:       bool = False ):
													for layer in self.stage_layers:
																				_UpperCAmelCase  :						int							   =					layer(A		,      training=A )
													return hidden_state

class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						def __init__( self		:       Optional[int]		,      A		:       ResNetConfig		,      **A		:       Optional[Any] ):
													super().__init__(**A )
													# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
													_UpperCAmelCase  :						Optional[Any]							   =					[
													    TFResNetStage(
													        A		,      config.embedding_size		,      config.hidden_sizes[0]		,      stride=2 if config.downsample_in_first_stage else 1		,      depth=config.depths[0]		,      name="stages.0"		,      )
													]
													for i, (in_channels, out_channels, depth) in enumerate(
													    zip(config.hidden_sizes		,      config.hidden_sizes[1:]		,      config.depths[1:] ) ):
																				self.stages.append(TFResNetStage(A		,      A		,      A		,      depth=A		,      name=F"""stages.{i + 1}""" ) )





						def   _A			( self		:       Tuple		,      A		:       tf.Tensor		,      A		:       bool = False		,      A		:       bool = True		,      A		:       bool = False		,      ):
													_UpperCAmelCase  :						List[str]							   =					() if output_hidden_states else None

													for stage_module in self.stages:
																				if output_hidden_states:
																											_UpperCAmelCase  :						Dict							   =					hidden_states + (hidden_state,)

																				_UpperCAmelCase  :						str							   =					stage_module(A		,      training=A )

													if output_hidden_states:
																				_UpperCAmelCase  :						Union[str, Any]							   =					hidden_states + (hidden_state,)

													if not return_dict:
																				return tuple(v for v in [hidden_state, hidden_states] if v is not None )

													return TFBaseModelOutputWithNoAttention(last_hidden_state=A		,      hidden_states=A )

class 			lowerCamelCase_			(snake_case__					):
						'''simple docstring'''

						__UpperCamelCase:						str      			=		ResNetConfig
						__UpperCamelCase:						List[str]      			=		"resnet"
						__UpperCamelCase:						int      			=		"pixel_values"




						@property
						def   _A			( self		:       List[Any] ):
													return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)		,      dtype=tf.floataa )}

__SCREAMING_SNAKE_CASE :  Dict  =						R"""
    This model is a TensorFlow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular TensorFlow Module and refer to the TensorFlow documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""


__SCREAMING_SNAKE_CASE :  int  =						R"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""

@keras_serializable
class 			lowerCamelCase_			(tf.keras.layers.Layer					):
						'''simple docstring'''

						__UpperCamelCase:						Any      			=		ResNetConfig




						def __init__( self		:       int		,      A		:       ResNetConfig		,      **A		:       str ):
													super().__init__(**A )
													_UpperCAmelCase  :						int							   =					config
													_UpperCAmelCase  :						str							   =					TFResNetEmbeddings(A		,      name="embedder" )
													_UpperCAmelCase  :						List[str]							   =					TFResNetEncoder(A		,      name="encoder" )
													_UpperCAmelCase  :						List[Any]							   =					tf.keras.layers.GlobalAveragePoolingaD(keepdims=A )





						@unpack_inputs
						def   _A			( self		:       str		,      A		:       tf.Tensor		,      A		:       Optional[bool] = None		,      A		:       Optional[bool] = None		,      A		:       bool = False		,      ):
													_UpperCAmelCase  :						Union[str, Any]							   =					(
													    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
													)
													_UpperCAmelCase  :						int							   =					return_dict if return_dict is not None else self.config.use_return_dict

													# TF 2.0 image layers can't use NCHW format when running on CPU.
													# We transpose to NHWC format and then transpose back after the full forward pass.
													# (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
													_UpperCAmelCase  :						Optional[Any]							   =					tf.transpose(A		,      perm=[0, 2, 3, 1] )
													_UpperCAmelCase  :						int							   =					self.embedder(A		,      training=A )

													_UpperCAmelCase  :						List[Any]							   =					self.encoder(
													    A		,      output_hidden_states=A		,      return_dict=A		,      training=A )

													_UpperCAmelCase  :						Any							   =					encoder_outputs[0]

													_UpperCAmelCase  :						List[Any]							   =					self.pooler(A )

													# Transpose all the outputs to the NCHW format
													# (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
													_UpperCAmelCase  :						str							   =					tf.transpose(A		,      (0, 3, 1, 2) )
													_UpperCAmelCase  :						Optional[int]							   =					tf.transpose(A		,      (0, 3, 1, 2) )
													_UpperCAmelCase  :						Dict							   =					()
													for hidden_state in encoder_outputs[1:]:
																				_UpperCAmelCase  :						Optional[Any]							   =					hidden_states + tuple(tf.transpose(A		,      (0, 3, 1, 2) ) for h in hidden_state )

													if not return_dict:
																				return (last_hidden_state, pooled_output) + hidden_states

													_UpperCAmelCase  :						List[Any]							   =					hidden_states if output_hidden_states else None

													return TFBaseModelOutputWithPoolingAndNoAttention(
													    last_hidden_state=A		,      pooler_output=A		,      hidden_states=A		,      )

@add_start_docstrings(
    "The bare ResNet model outputting raw features without any specific head on top."							,				snake_case__							,				)
class 			lowerCamelCase_			(snake_case__					):
						'''simple docstring'''

						def __init__( self		:       str		,      A		:       ResNetConfig		,      **A		:       Optional[int] ):
													super().__init__(A		,      **A )
													_UpperCAmelCase  :						List[str]							   =					TFResNetMainLayer(config=A		,      name="resnet" )





						@add_start_docstrings_to_model_forward(A )
						@add_code_sample_docstrings(
						    checkpoint=_CHECKPOINT_FOR_DOC		,      output_type=A		,      config_class=_CONFIG_FOR_DOC		,      modality="vision"		,      expected_output=_EXPECTED_OUTPUT_SHAPE		,      )
						@unpack_inputs
						def   _A			( self		:       Optional[int]		,      A		:       tf.Tensor		,      A		:       Optional[bool] = None		,      A		:       Optional[bool] = None		,      A		:       bool = False		,      ):
													_UpperCAmelCase  :						List[str]							   =					(
													    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
													)
													_UpperCAmelCase  :						Tuple							   =					return_dict if return_dict is not None else self.config.use_return_dict

													_UpperCAmelCase  :						str							   =					self.resnet(
													    pixel_values=A		,      output_hidden_states=A		,      return_dict=A		,      training=A		,      )
													return resnet_outputs

@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "							,				snake_case__							,				)
class 			lowerCamelCase_			(snake_case__							,				snake_case__					):
						'''simple docstring'''

						def __init__( self		:       Tuple		,      A		:       ResNetConfig		,      **A		:       Any ):
													super().__init__(A		,      **A )
													_UpperCAmelCase  :						List[Any]							   =					config.num_labels
													_UpperCAmelCase  :						int							   =					TFResNetMainLayer(A		,      name="resnet" )
													# classification head
													_UpperCAmelCase  :						Optional[int]							   =					(
													    tf.keras.layers.Dense(config.num_labels		,      name="classifier.1" )
													    if config.num_labels > 0
													    else tf.keras.layers.Activation("linear"		,      name="classifier.1" )
													)




						def   _A			( self		:       Optional[int]		,      A		:       tf.Tensor ):
													_UpperCAmelCase  :						List[Any]							   =					tf.keras.layers.Flatten()(A )
													_UpperCAmelCase  :						List[str]							   =					self.classifier_layer(A )
													return logits





						@add_start_docstrings_to_model_forward(A )
						@add_code_sample_docstrings(
						    checkpoint=_IMAGE_CLASS_CHECKPOINT		,      output_type=A		,      config_class=_CONFIG_FOR_DOC		,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,      )
						@unpack_inputs
						def   _A			( self		:       List[Any]		,      A		:       tf.Tensor = None		,      A		:       tf.Tensor = None		,      A		:       bool = None		,      A		:       bool = None		,      A		:       bool = False		,      ):
													_UpperCAmelCase  :						Any							   =					return_dict if return_dict is not None else self.config.use_return_dict

													_UpperCAmelCase  :						Optional[Any]							   =					self.resnet(
													    A		,      output_hidden_states=A		,      return_dict=A		,      training=A )

													_UpperCAmelCase  :						List[Any]							   =					outputs.pooler_output if return_dict else outputs[1]

													_UpperCAmelCase  :						Optional[int]							   =					self.classifier(A )

													_UpperCAmelCase  :						Dict							   =					None if labels is None else self.hf_compute_loss(A		,      A )

													if not return_dict:
																				_UpperCAmelCase  :						Optional[int]							   =					(logits,) + outputs[2:]
																				return (loss,) + output if loss is not None else output

													return TFImageClassifierOutputWithNoAttention(loss=A		,      logits=A		,      hidden_states=outputs.hidden_states )


'''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 			lowerCamelCase_			:
      '''simple docstring'''

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




      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Union[str, Any]							   =					ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size )

             _UpperCAmelCase  :						int							   =					None
             if self.use_input_mask:
                    _UpperCAmelCase  :						List[str]							   =					random_attention_mask([self.batch_size, self.seq_length] )

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

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

             _UpperCAmelCase  :						Any							   =					self.get_config()

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




      def   _A			( self		:       List[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=A		,      initializer_range=self.initializer_range		,      )




      def   _A			( self		:       Union[str, Any] ):
             (
                 (
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						
             )  :						Dict							   =					self.prepare_config_and_inputs()

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




      def   _A			( self		:       List[Any]		,      A		:       Any		,      A		:       Optional[int]		,      A		:       List[Any]		,      A		:       Dict		,      A		:       List[Any]		,      A		:       Optional[int]		,      A		:       List[str]		,      A		:       Dict		,      A		:       str		,      ):
             _UpperCAmelCase  :						Optional[Any]							   =					True
             _UpperCAmelCase  :						List[str]							   =					ElectraModel(A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						int							   =					model(
                 A		,      attention_mask=A		,      token_type_ids=A		,      encoder_hidden_states=A		,      encoder_attention_mask=A		,      )
             _UpperCAmelCase  :						Tuple							   =					model(
                 A		,      attention_mask=A		,      token_type_ids=A		,      encoder_hidden_states=A		,      )
             _UpperCAmelCase  :						Union[str, Any]							   =					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   _A			( self		:       Optional[int]		,      A		:       int		,      A		:       Optional[Any]		,      A		:       int		,      A		:       Tuple		,      A		:       List[str]		,      A		:       Optional[Any]		,      A		:       str		,      A		:       Dict		,      ):
             _UpperCAmelCase  :						Optional[Any]							   =					ElectraForMaskedLM(config=A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						int							   =					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   _A			( self		:       Optional[Any]		,      A		:       Optional[int]		,      A		:       Optional[int]		,      A		:       Any		,      A		:       str		,      A		:       Optional[int]		,      A		:       Any		,      A		:       str		,      A		:       int		,      A		:       Dict		,      ):
             _UpperCAmelCase  :						Tuple							   =					ElectraForCausalLM(config=A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						List[Any]							   =					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   _A			( self		:       List[str]		,      A		:       Tuple		,      A		:       Dict		,      A		:       Tuple		,      A		:       List[str]		,      A		:       List[Any]		,      A		:       Optional[Any]		,      A		:       str		,      A		:       Dict		,      ):
             _UpperCAmelCase  :						Any							   =					self.num_labels
             _UpperCAmelCase  :						Tuple							   =					ElectraForTokenClassification(config=A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						Union[str, Any]							   =					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   _A			( self		:       Union[str, Any]		,      A		:       Optional[Any]		,      A		:       Optional[int]		,      A		:       Union[str, Any]		,      A		:       str		,      A		:       Union[str, Any]		,      A		:       Tuple		,      A		:       Optional[int]		,      A		:       Optional[int]		,      ):
             _UpperCAmelCase  :						Union[str, Any]							   =					self.num_labels
             _UpperCAmelCase  :						int							   =					ElectraForPreTraining(config=A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						Dict							   =					model(A		,      attention_mask=A		,      token_type_ids=A		,      labels=A )
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length) )




      def   _A			( self		:       Union[str, Any]		,      A		:       List[Any]		,      A		:       int		,      A		:       int		,      A		:       Optional[int]		,      A		:       Optional[Any]		,      A		:       Dict		,      A		:       str		,      A		:       Any		,      ):
             _UpperCAmelCase  :						Union[str, Any]							   =					self.num_labels
             _UpperCAmelCase  :						List[str]							   =					ElectraForSequenceClassification(A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						str							   =					model(A		,      attention_mask=A		,      token_type_ids=A		,      labels=A )
             self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels) )




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





      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						List[Any]							   =					self.prepare_config_and_inputs()
             (
                 (
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						(
                 _UpperCAmelCase
             )   ,						
             )  :						Tuple							   =					config_and_inputs
             _UpperCAmelCase  :						List[Any]							   =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
             return config, inputs_dict

@require_torch
class 			lowerCamelCase_			(snake_case__							,				snake_case__							,				unittest.TestCase					):
      '''simple docstring'''

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




      def   _A			( self		:       Optional[Any]		,      A		:       Tuple		,      A		:       Optional[Any]		,      A		:       List[Any]=False ):
             _UpperCAmelCase  :						int							   =					super()._prepare_for_class(A		,      A		,      return_labels=A )

             if return_labels:
                    if model_class in get_values(A ):
                           _UpperCAmelCase  :						List[str]							   =					torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.seq_length)		,      dtype=torch.long		,      device=A )
             return inputs_dict




      def   _A			( self		:       Optional[int] ):
             _UpperCAmelCase  :						Any							   =					ElectraModelTester(self )
             _UpperCAmelCase  :						Tuple							   =					ConfigTester(self		,      config_class=A		,      hidden_size=37 )




      def   _A			( self		:       Tuple ):
             self.config_tester.run_common_tests()




      def   _A			( self		:       Dict ):
             _UpperCAmelCase  :						Dict							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_model(*A )




      def   _A			( self		:       int ):
             _UpperCAmelCase  :						List[Any]							   =					self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_electra_model_as_decoder(*A )




      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Optional[Any]							   =					self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                    _UpperCAmelCase  :						Optional[int]							   =					type
                    self.model_tester.create_and_check_electra_model(*A )




      def   _A			( self		:       Optional[Any] ):
             _UpperCAmelCase  :						Optional[Any]							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_masked_lm(*A )




      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Optional[Any]							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_token_classification(*A )




      def   _A			( self		:       str ):
             _UpperCAmelCase  :						int							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_pretraining(*A )




      def   _A			( self		:       Tuple ):
             _UpperCAmelCase  :						List[str]							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_sequence_classification(*A )




      def   _A			( self		:       Dict ):
             _UpperCAmelCase  :						List[Any]							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_question_answering(*A )




      def   _A			( self		:       Dict ):
             _UpperCAmelCase  :						Optional[int]							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_electra_for_multiple_choice(*A )




      @slow
      def   _A			( self		:       Union[str, Any] ):
             for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _UpperCAmelCase  :						List[Any]							   =					ElectraModel.from_pretrained(A )
                    self.assertIsNotNone(A )





      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						Union[str, Any]							   =					self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_electra_for_causal_lm(*A )

@require_torch
class 			lowerCamelCase_			(unittest.TestCase					):
      '''simple docstring'''

      @slow
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						List[Any]							   =					ElectraModel.from_pretrained("google/electra-small-discriminator" )
             _UpperCAmelCase  :						List[Any]							   =					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] )
             _UpperCAmelCase  :						List[str]							   =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
             _UpperCAmelCase  :						str							   =					model(A		,      attention_mask=A )[0]
             _UpperCAmelCase  :						Union[str, Any]							   =					torch.Size((1, 11, 256) )
             self.assertEqual(output.shape		,      A )
             _UpperCAmelCase  :						Optional[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]		,      A		,      atol=1E-4 ) )

