


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						:

       def __init__(      self						,    a__						,    a__=13						,    a__=7						,    a__=True						,    a__=True						,    a__=True						,    a__=True						,    a__=99						,    a__=32						,    a__=5						,    a__=4						,    a__=37						,    a__="gelu"						,    a__=0.1						,    a__=0.1						,    a__=5_12						,    a__=16						,    a__=2						,    a__=0.02						,    a__=3						,    a__=4						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			List[Any]							=   parent
        snake_case :			List[Any]							=   batch_size
        snake_case :			Optional[int]							=   seq_length
        snake_case :			Optional[Any]							=   is_training
        snake_case :			List[str]							=   use_input_mask
        snake_case :			Union[str, Any]							=   use_token_type_ids
        snake_case :			Any							=   use_labels
        snake_case :			int							=   vocab_size
        snake_case :			int							=   hidden_size
        snake_case :			Union[str, Any]							=   num_hidden_layers
        snake_case :			Optional[int]							=   num_attention_heads
        snake_case :			List[str]							=   intermediate_size
        snake_case :			Dict							=   hidden_act
        snake_case :			Optional[int]							=   hidden_dropout_prob
        snake_case :			Any							=   attention_probs_dropout_prob
        snake_case :			List[str]							=   max_position_embeddings
        snake_case :			str							=   type_vocab_size
        snake_case :			Optional[Any]							=   type_sequence_label_size
        snake_case :			Tuple							=   initializer_range
        snake_case :			int							=   num_labels
        snake_case :			str							=   num_choices
        snake_case :			Optional[Any]							=   scope

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size       )

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

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

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

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

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

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        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 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        (
            snake_case	
        ) :			Union[str, Any]							=   self.prepare_config_and_inputs()

        snake_case :			Optional[Any]							=   True
        snake_case :			Optional[int]							=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
        snake_case :			Dict							=   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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   ElectraModel(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			int							=   model(a__						,    attention_mask=a__						,    token_type_ids=a__       )
        snake_case :			List[str]							=   model(a__						,    token_type_ids=a__       )
        snake_case :			List[Any]							=   model(a__       )
        self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.seq_length, self.hidden_size)       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Any							=   True
        snake_case :			Tuple							=   ElectraModel(a__       )
        model.to(a__       )
        model.eval()
        snake_case :			Any							=   model(
            a__						,    attention_mask=a__						,    token_type_ids=a__						,    encoder_hidden_states=a__						,    encoder_attention_mask=a__						,    )
        snake_case :			Optional[Any]							=   model(
            a__						,    attention_mask=a__						,    token_type_ids=a__						,    encoder_hidden_states=a__						,    )
        snake_case :			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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   ElectraForMaskedLM(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			List[str]							=   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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			int							=   ElectraForCausalLM(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			Optional[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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Tuple							=   self.num_labels
        snake_case :			Optional[Any]							=   ElectraForTokenClassification(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			str							=   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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Any							=   self.num_labels
        snake_case :			int							=   ElectraForPreTraining(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			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)       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






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

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   ElectraForQuestionAnswering(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			Dict							=   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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Dict							=   self.num_choices
        snake_case :			List[Any]							=   ElectraForMultipleChoice(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			str							=   input_ids.unsqueeze(1       ).expand(-1						,    self.num_choices						,    -1       ).contiguous()
        snake_case :			Dict							=   token_type_ids.unsqueeze(1       ).expand(-1						,    self.num_choices						,    -1       ).contiguous()
        snake_case :			Optional[int]							=   input_mask.unsqueeze(1       ).expand(-1						,    self.num_choices						,    -1       ).contiguous()
        snake_case :			Optional[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 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.prepare_config_and_inputs()
        (
            snake_case	
        ) :			List[Any]							=   config_and_inputs
        snake_case :			Dict							=   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class   lowerCamelCase						(	SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__=False       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   super()._prepare_for_class(a__						,    a__						,    return_labels=a__       )

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

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   ElectraModelTester(self       )
        snake_case :			List[str]							=   ConfigTester(self						,    config_class=a__						,    hidden_size=37       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        self.config_tester.run_common_tests()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_model(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_model_as_decoder(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
         snake_case :			Any							=   type
         self.model_tester.create_and_check_electra_model(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			List[Any]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_masked_lm(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			List[str]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_token_classification(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_pretraining(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_sequence_classification(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_question_answering(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Tuple							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_multiple_choice(*a__       )

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case :			Optional[Any]							=   ElectraModel.from_pretrained(a__       )
         self.assertIsNotNone(a__       )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			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  ):

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   ElectraModel.from_pretrained('''google/electra-small-discriminator'''       )
        snake_case :			str							=   torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]       )
        snake_case :			str							=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
        snake_case :			str							=   model(a__						,    attention_mask=a__       )[0]
        snake_case :			str							=   torch.Size((1, 11, 2_56)       )
        self.assertEqual(output.shape						,    a__       )
        snake_case :			Optional[Any]							=   torch.tensor(
            [[[0.4471, 0.6821, -0.3265], [0.4627, 0.5255, -0.3668], [0.4532, 0.3313, -0.4344]]]       )

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














"""simple docstring"""



def   _lowerCAmelCase		(				UpperCAmelCase__							:       float,    UpperCAmelCase__							:       float,    UpperCAmelCase__							:       float   )		->list[int]:
						if hue < 0 or hue > 3_6_0:
												raise Exception("""hue should be between 0 and 360"""   )

						if saturation < 0 or saturation > 1:
												raise Exception("""saturation should be between 0 and 1"""   )

						if value < 0 or value > 1:
												raise Exception("""value should be between 0 and 1"""   )

						A__    :	int			=	value * saturation
						A__    :	Any			=	hue / 6_0
						A__    :	Any			=	chroma * (1 - abs(hue_section % 2 - 1   ))
						A__    :	Optional[Any]			=	value - chroma

						if hue_section >= 0 and hue_section <= 1:
												A__    :	Dict			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Optional[Any]			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Any			=	round(2_5_5 * (match_value)   )
						elif hue_section > 1 and hue_section <= 2:
												A__    :	Optional[int]			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (match_value)   )
						elif hue_section > 2 and hue_section <= 3:
												A__    :	Tuple			=	round(2_5_5 * (match_value)   )
												A__    :	List[str]			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Union[str, Any]			=	round(2_5_5 * (second_largest_component + match_value)   )
						elif hue_section > 3 and hue_section <= 4:
												A__    :	Any			=	round(2_5_5 * (match_value)   )
												A__    :	Dict			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	List[Any]			=	round(2_5_5 * (chroma + match_value)   )
						elif hue_section > 4 and hue_section <= 5:
												A__    :	Any			=	round(2_5_5 * (second_largest_component + match_value)   )
												A__    :	Dict			=	round(2_5_5 * (match_value)   )
												A__    :	Tuple			=	round(2_5_5 * (chroma + match_value)   )
						else:
												A__    :	Tuple			=	round(2_5_5 * (chroma + match_value)   )
												A__    :	Optional[int]			=	round(2_5_5 * (match_value)   )
												A__    :	str			=	round(2_5_5 * (second_largest_component + match_value)   )

						return [red, green, blue]




def   _lowerCAmelCase		(				UpperCAmelCase__							:       int,    UpperCAmelCase__							:       int,    UpperCAmelCase__							:       int   )		->list[float]:
						if red < 0 or red > 2_5_5:
												raise Exception("""red should be between 0 and 255"""   )

						if green < 0 or green > 2_5_5:
												raise Exception("""green should be between 0 and 255"""   )

						if blue < 0 or blue > 2_5_5:
												raise Exception("""blue should be between 0 and 255"""   )

						A__    :	int			=	red / 2_5_5
						A__    :	List[str]			=	green / 2_5_5
						A__    :	Optional[int]			=	blue / 2_5_5
						A__    :	Optional[int]			=	max(UpperCAmelCase__,    UpperCAmelCase__,    UpperCAmelCase__   )
						A__    :	Tuple			=	value - min(UpperCAmelCase__,    UpperCAmelCase__,    UpperCAmelCase__   )
						A__    :	Any			=	0 if value == 0 else chroma / value

						if chroma == 0:
												A__    :	Optional[Any]			=	0.0
						elif value == float_red:
												A__    :	Optional[Any]			=	6_0 * (0 + (float_green - float_blue) / chroma)
						elif value == float_green:
												A__    :	Tuple			=	6_0 * (2 + (float_blue - float_red) / chroma)
						else:
												A__    :	int			=	6_0 * (4 + (float_red - float_green) / chroma)

						A__    :	Optional[int]			=	(hue + 3_6_0) % 3_6_0

						return [hue, saturation, value]




def   _lowerCAmelCase		(				UpperCAmelCase__							:       list[float],    UpperCAmelCase__							:       list[float]   )		->bool:
						A__    :	Optional[Any]			=	abs(hsv_a[0] - hsv_a[0]   ) < 0.2
						A__    :	str			=	abs(hsv_a[1] - hsv_a[1]   ) < 0.002
						A__    :	Optional[int]			=	abs(hsv_a[2] - hsv_a[2]   ) < 0.002

						return check_hue and check_saturation and check_value






