





"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_	:		Dict         =   logging.get_logger(__name__)

lowerCamelCase_	:		Optional[Any]         =   {
    'MBZUAI/swiftformer-xs': 'https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json',
}

class 					_UpperCAmelCase    (  UpperCAmelCase__						):



      '''simple docstring'''
      lowercase_ :					int             =  """swiftformer"""

      def __init__(	self   , snake_case_=3   , snake_case_=[3, 3, 6, 4]   , snake_case_=[4_8, 5_6, 1_1_2, 2_2_0]   , snake_case_=4   , snake_case_=[True, True, True, True]   , snake_case_="gelu"   , snake_case_=3   , snake_case_=2   , snake_case_=1   , snake_case_=0.0   , snake_case_=True   , snake_case_=1E-5   , snake_case_=1E-5   , **snake_case_   , ):

        """simple docstring"""

        super().__init__(**snake_case_    )
        A_    :			Optional[Any]									=    num_channels
        A_    :			Dict									=    depths
        A_    :			str									=    embed_dims
        A_    :			Optional[Any]									=    mlp_ratio
        A_    :			str									=    downsamples
        A_    :			Tuple									=    hidden_act
        A_    :			Tuple									=    down_patch_size
        A_    :			Optional[int]									=    down_stride
        A_    :			List[Any]									=    down_pad
        A_    :			List[str]									=    drop_path_rate
        A_    :			List[Any]									=    use_layer_scale
        A_    :			Union[str, Any]									=    layer_scale_init_value
        A_    :			str									=    batch_norm_eps


class 					_UpperCAmelCase    (  UpperCAmelCase__						):



      '''simple docstring'''
      lowercase_ :					int             =  version.parse("""1.11"""						)

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return OrderedDict(
            [
                ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
            ]    )

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return 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  lowerCamelCase__				:


	"""simple docstring"""





	def __init__(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__=1_3   ,     UpperCAmelCase__=7   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=True   ,     UpperCAmelCase__=9_9   ,     UpperCAmelCase__=3_2   ,     UpperCAmelCase__=5   ,     UpperCAmelCase__=4   ,     UpperCAmelCase__=3_7   ,     UpperCAmelCase__="gelu"   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=0.1   ,     UpperCAmelCase__=5_1_2   ,     UpperCAmelCase__=1_6   ,     UpperCAmelCase__=2   ,     UpperCAmelCase__=0.0_2   ,     UpperCAmelCase__=3   ,     UpperCAmelCase__=4   ,     UpperCAmelCase__=None   ,     )       ->				Optional[Any]:
					_A :				Optional[int]				=  parent
					_A :				List[Any]				=  batch_size
					_A :				Optional[int]				=  seq_length
					_A :				Dict				=  is_training
					_A :				Union[str, Any]				=  use_input_mask
					_A :				Tuple				=  use_token_type_ids
					_A :				str				=  use_labels
					_A :				List[str]				=  vocab_size
					_A :				Tuple				=  hidden_size
					_A :				Tuple				=  num_hidden_layers
					_A :				List[str]				=  num_attention_heads
					_A :				Optional[Any]				=  intermediate_size
					_A :				str				=  hidden_act
					_A :				Optional[int]				=  hidden_dropout_prob
					_A :				int				=  attention_probs_dropout_prob
					_A :				Optional[Any]				=  max_position_embeddings
					_A :				List[Any]				=  type_vocab_size
					_A :				Tuple				=  type_sequence_label_size
					_A :				Any				=  initializer_range
					_A :				Any				=  num_labels
					_A :				str				=  num_choices
					_A :				List[str]				=  scope




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				List[Any]				=  ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size			)

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

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

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

					_A :				List[Any]				=  self.get_config()

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




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					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=UpperCAmelCase__   ,     initializer_range=self.initializer_range   ,     )




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

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




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[str]:
					_A :				Dict				=  True
					_A :				Any				=  ElectraModel(UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				List[str]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     encoder_attention_mask=UpperCAmelCase__   ,     )
					_A :				Optional[int]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     encoder_hidden_states=UpperCAmelCase__   ,     )
					_A :				List[Any]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__			)
					self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.seq_length, self.hidden_size)			)




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




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




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[str]:
					_A :				str				=  self.num_labels
					_A :				str				=  ElectraForTokenClassification(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				str				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[Any]:
					_A :				Optional[int]				=  self.num_labels
					_A :				Optional[int]				=  ElectraForPreTraining(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Dict				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Tuple:
					_A :				Optional[Any]				=  self.num_labels
					_A :				Optional[Any]				=  ElectraForSequenceClassification(UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Any				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__			)
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				List[Any]:
					_A :				Optional[int]				=  ElectraForQuestionAnswering(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Union[str, Any]				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     start_positions=UpperCAmelCase__   ,     end_positions=UpperCAmelCase__   ,     )
					self.parent.assertEqual(result.start_logits.shape   ,     (self.batch_size, self.seq_length)			)
					self.parent.assertEqual(result.end_logits.shape   ,     (self.batch_size, self.seq_length)			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     )       ->				Union[str, Any]:
					_A :				Union[str, Any]				=  self.num_choices
					_A :				Union[str, Any]				=  ElectraForMultipleChoice(config=UpperCAmelCase__			)
					model.to(UpperCAmelCase__			)
					model.eval()
					_A :				Dict				=  input_ids.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				str				=  token_type_ids.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				Dict				=  input_mask.unsqueeze(1			).expand(-1   ,     self.num_choices   ,     -1			).contiguous()
					_A :				str				=  model(
					    UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__   ,     token_type_ids=UpperCAmelCase__   ,     labels=UpperCAmelCase__   ,     )
					self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_choices)			)




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				Optional[Any]				=  self.prepare_config_and_inputs()
					(
					    (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       (
					    _A
					)	,       
					) :				int				=  config_and_inputs
					_A :				Dict				=  {'''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"""





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




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__   ,     UpperCAmelCase__   ,     UpperCAmelCase__=False			)       ->				Optional[Any]:
					_A :				List[str]				=  super()._prepare_for_class(UpperCAmelCase__   ,     UpperCAmelCase__   ,     return_labels=UpperCAmelCase__			)

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




	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Optional[Any]				=  ElectraModelTester(self			)
					_A :				List[str]				=  ConfigTester(self   ,     config_class=UpperCAmelCase__   ,     hidden_size=3_7			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					self.config_tester.run_common_tests()




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_model(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_model_as_decoder(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				int				=  self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									_A :				List[str]				=  type
									self.model_tester.create_and_check_electra_model(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_masked_lm(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Any:
					_A :				Dict				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_token_classification(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Union[str, Any]:
					_A :				List[str]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_pretraining(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Any				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_sequence_classification(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_question_answering(*UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_multiple_choice(*UpperCAmelCase__			)




	@slow
	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_A :				List[str]				=  ElectraModel.from_pretrained(UpperCAmelCase__			)
									self.assertIsNotNone(UpperCAmelCase__			)




	def 				_lowerCamelCase				(					self			)       ->				List[Any]:
					_A :				str				=  self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_for_causal_lm(*UpperCAmelCase__			)



@require_torch
class  lowerCamelCase__				(						unittest.TestCase						):


	"""simple docstring"""





	@slow
	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Any				=  ElectraModel.from_pretrained('''google/electra-small-discriminator'''			)
					_A :				Union[str, Any]				=  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]]			)
					_A :				Union[str, Any]				=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
					_A :				Optional[int]				=  model(UpperCAmelCase__   ,     attention_mask=UpperCAmelCase__			)[0]
					_A :				Tuple				=  torch.Size((1, 1_1, 2_5_6)			)
					self.assertEqual(output.shape   ,     UpperCAmelCase__			)
					_A :				Optional[Any]				=  torch.tensor(
					    [[[0.4_4_7_1, 0.6_8_2_1, -0.3_2_6_5], [0.4_6_2_7, 0.5_2_5_5, -0.3_6_6_8], [0.4_5_3_2, 0.3_3_1_3, -0.4_3_4_4]]]			)

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