





"""simple docstring"""




import random
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   import PIL

   from transformers import FlavaImageProcessor
   from transformers.image_utils import PILImageResampling
   from transformers.models.flava.image_processing_flava import (
       FLAVA_CODEBOOK_MEAN,
       FLAVA_CODEBOOK_STD,
       FLAVA_IMAGE_MEAN,
       FLAVA_IMAGE_STD,
   )
else:
   lowerCamelCase_	:		Optional[Any]         =   None

class 					_UpperCAmelCase    (  unittest.TestCase						):



      '''simple docstring'''

      def __init__(	self   , snake_case_   , snake_case_=7   , snake_case_=3   , snake_case_=3_0   , snake_case_=4_0_0   , snake_case_=True   , snake_case_=None   , snake_case_=True   , snake_case_=None   , snake_case_=None   , snake_case_=True   , snake_case_=1 / 2_5_5   , snake_case_=True   , snake_case_=FLAVA_IMAGE_MEAN   , snake_case_=FLAVA_IMAGE_STD   , snake_case_=1_4   , snake_case_=7_5   , snake_case_=None   , snake_case_=1_6   , snake_case_=0.3   , snake_case_=None   , snake_case_=True   , snake_case_=None   , snake_case_=None   , snake_case_=True   , snake_case_=None   , snake_case_=True   , snake_case_=True   , snake_case_=FLAVA_CODEBOOK_MEAN   , snake_case_=FLAVA_CODEBOOK_STD   , ):

        """simple docstring"""

        A_    :			Union[str, Any]									=    size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
        A_    :			Union[str, Any]									=    crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
        A_    :			Optional[Any]									=    codebook_size if codebook_size is not None else {'height': 1_1_2, 'width': 1_1_2}
        A_    :			List[str]									=    codebook_crop_size if codebook_crop_size is not None else {'height': 1_1_2, 'width': 1_1_2}

        A_    :			Optional[Any]									=    parent
        A_    :			Union[str, Any]									=    batch_size
        A_    :			List[str]									=    num_channels
        A_    :			Union[str, Any]									=    do_resize
        A_    :			Union[str, Any]									=    do_rescale
        A_    :			List[Any]									=    rescale_factor
        A_    :			List[str]									=    min_resolution
        A_    :			Optional[Any]									=    max_resolution
        A_    :			List[Any]									=    size
        A_    :			Optional[Any]									=    resample if resample is not None else PILImageResampling.BICUBIC
        A_    :			Dict									=    do_normalize
        A_    :			List[Any]									=    image_mean
        A_    :			Union[str, Any]									=    image_std
        A_    :			Union[str, Any]									=    do_center_crop
        A_    :			Union[str, Any]									=    crop_size

        A_    :			Optional[Any]									=    input_size_patches
        A_    :			List[Any]									=    total_mask_patches
        A_    :			Dict									=    mask_group_max_patches
        A_    :			List[str]									=    mask_group_min_patches
        A_    :			int									=    mask_group_min_aspect_ratio
        A_    :			Union[str, Any]									=    mask_group_max_aspect_ratio

        A_    :			Optional[int]									=    codebook_do_resize
        A_    :			Union[str, Any]									=    codebook_size
        A_    :			Union[str, Any]									=    codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
        A_    :			Dict									=    codebook_do_center_crop
        A_    :			List[Any]									=    codebook_crop_size
        A_    :			Tuple									=    codebook_do_map_pixels
        A_    :			Dict									=    codebook_do_normalize
        A_    :			Tuple									=    codebook_image_mean
        A_    :			Optional[Any]									=    codebook_image_std

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "resample": self.resample,
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
            "input_size_patches": self.input_size_patches,
            "total_mask_patches": self.total_mask_patches,
            "mask_group_max_patches": self.mask_group_max_patches,
            "mask_group_min_patches": self.mask_group_min_patches,
            "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
            "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
            "codebook_do_resize": self.codebook_do_resize,
            "codebook_size": self.codebook_size,
            "codebook_resample": self.codebook_resample,
            "codebook_do_center_crop": self.codebook_do_center_crop,
            "codebook_crop_size": self.codebook_crop_size,
            "codebook_do_map_pixels": self.codebook_do_map_pixels,
            "codebook_do_normalize": self.codebook_do_normalize,
            "codebook_image_mean": self.codebook_image_mean,
            "codebook_image_std": self.codebook_image_std,
        }

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (self.size["height"], self.size["width"])

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (
            (self.input_size_patches, self.input_size_patches)
            if not isinstance(self.input_size_patches   , snake_case_    )
            else self.input_size_patches
        )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (self.codebook_size["height"], self.codebook_size["width"])


@require_torch
@require_vision
class 					_UpperCAmelCase    (  UpperCAmelCase__     ,  unittest.TestCase						):



      '''simple docstring'''
      lowercase_ :					List[Any]             =  FlavaImageProcessor if is_vision_available() else None
      lowercase_ :					Any             =  None

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Tuple									=    FlavaImageProcessingTester(self    )

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return self.image_processor_tester.prepare_image_processor_dict()

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			List[str]									=    self.image_processing_class(**self.image_processor_dict    )
        self.assertTrue(hasattr(snake_case_   , 'image_mean'    )    )
        self.assertTrue(hasattr(snake_case_   , 'image_std'    )    )
        self.assertTrue(hasattr(snake_case_   , 'do_normalize'    )    )
        self.assertTrue(hasattr(snake_case_   , 'do_resize'    )    )
        self.assertTrue(hasattr(snake_case_   , 'resample'    )    )
        self.assertTrue(hasattr(snake_case_   , 'crop_size'    )    )
        self.assertTrue(hasattr(snake_case_   , 'do_center_crop'    )    )
        self.assertTrue(hasattr(snake_case_   , 'do_rescale'    )    )
        self.assertTrue(hasattr(snake_case_   , 'rescale_factor'    )    )
        self.assertTrue(hasattr(snake_case_   , 'masking_generator'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_do_resize'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_size'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_resample'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_do_center_crop'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_crop_size'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_do_map_pixels'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_do_normalize'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_image_mean'    )    )
        self.assertTrue(hasattr(snake_case_   , 'codebook_image_std'    )    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Any									=    self.image_processing_class.from_dict(self.image_processor_dict    )
        self.assertEqual(image_processor.size   , {'height': 2_2_4, 'width': 2_2_4}    )
        self.assertEqual(image_processor.crop_size   , {'height': 2_2_4, 'width': 2_2_4}    )
        self.assertEqual(image_processor.codebook_size   , {'height': 1_1_2, 'width': 1_1_2}    )
        self.assertEqual(image_processor.codebook_crop_size   , {'height': 1_1_2, 'width': 1_1_2}    )

        A_    :			Optional[int]									=    self.image_processing_class.from_dict(
            self.image_processor_dict   , size=4_2   , crop_size=8_4   , codebook_size=3_3   , codebook_crop_size=6_6    )
        self.assertEqual(image_processor.size   , {'height': 4_2, 'width': 4_2}    )
        self.assertEqual(image_processor.crop_size   , {'height': 8_4, 'width': 8_4}    )
        self.assertEqual(image_processor.codebook_size   , {'height': 3_3, 'width': 3_3}    )
        self.assertEqual(image_processor.codebook_crop_size   , {'height': 6_6, 'width': 6_6}    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        pass

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Union[str, Any]									=    self.image_processing_class(**self.image_processor_dict    )
        # create random PIL images
        A_    :			Dict									=    prepare_image_inputs(self.image_processor_tester   , equal_resolution=snake_case_    )
        for image in image_inputs:
          self.assertIsInstance(snake_case_   , PIL.Image.Image    )

        # Test not batched input
        A_    :			Any									=    image_processing(image_inputs[0]   , return_tensors='pt'    )

        # Test no bool masked pos
        self.assertFalse('bool_masked_pos' in encoded_images    )

        A_		,			A_    :			Optional[Any]									=    self.image_processor_tester.get_expected_image_size()

        self.assertEqual(
            encoded_images.pixel_values.shape   , (1, self.image_processor_tester.num_channels, expected_height, expected_width)   , )

        # Test batched
        A_    :			int									=    image_processing(snake_case_   , return_tensors='pt'    )
        A_		,			A_    :			int									=    self.image_processor_tester.get_expected_image_size()

        # Test no bool masked pos
        self.assertFalse('bool_masked_pos' in encoded_images    )

        self.assertEqual(
            encoded_images.pixel_values.shape   , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )   , )

      def    lowerCamelCase_     (	self   , snake_case_   , snake_case_    ):

        """simple docstring"""

        A_    :			str									=    self.image_processing_class(**self.image_processor_dict    )
        # create random tensors
        A_    :			str									=    prepare_image_inputs(self.image_processor_tester   , equal_resolution=snake_case_   , **snake_case_    )
        for image in image_inputs:
          self.assertIsInstance(snake_case_   , snake_case_    )

        # Test not batched input
        A_    :			Optional[int]									=    image_processing(image_inputs[0]   , return_tensors='pt'    )

        A_		,			A_    :			str									=    self.image_processor_tester.get_expected_image_size()
        self.assertEqual(
            encoded_images.pixel_values.shape   , (1, self.image_processor_tester.num_channels, expected_height, expected_width)   , )

        A_    :			Tuple									=    image_processing(snake_case_   , return_image_mask=snake_case_   , return_tensors='pt'    )

        A_		,			A_    :			Any									=    self.image_processor_tester.get_expected_image_size()
        self.assertEqual(
            encoded_images.pixel_values.shape   , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )   , )

        A_		,			A_    :			Optional[int]									=    self.image_processor_tester.get_expected_mask_size()
        self.assertEqual(
            encoded_images.bool_masked_pos.shape   , (
                self.image_processor_tester.batch_size,
                expected_height,
                expected_width,
            )   , )

        # Test batched
        A_    :			Dict									=    image_processing(snake_case_   , return_tensors='pt'    ).pixel_values

        A_		,			A_    :			int									=    self.image_processor_tester.get_expected_image_size()
        self.assertEqual(
            encoded_images.shape   , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )   , )

        # Test masking
        A_    :			int									=    image_processing(snake_case_   , return_image_mask=snake_case_   , return_tensors='pt'    )

        A_		,			A_    :			Any									=    self.image_processor_tester.get_expected_image_size()
        self.assertEqual(
            encoded_images.pixel_values.shape   , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )   , )

        A_		,			A_    :			Optional[Any]									=    self.image_processor_tester.get_expected_mask_size()
        self.assertEqual(
            encoded_images.bool_masked_pos.shape   , (
                self.image_processor_tester.batch_size,
                expected_height,
                expected_width,
            )   , )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        self._test_call_framework(np.ndarray   , prepare_kwargs={'numpify': True}    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        self._test_call_framework(torch.Tensor   , prepare_kwargs={'torchify': True}    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        random.seed(1_2_3_4    )
        A_    :			List[Any]									=    self.image_processing_class(**self.image_processor_dict    )
        A_    :			Optional[int]									=    prepare_image_inputs(self.image_processor_tester   , equal_resolution=snake_case_   , torchify=snake_case_    )

        # Test not batched input
        A_    :			str									=    image_processing(image_inputs[0]   , return_image_mask=snake_case_   , return_tensors='pt'    )
        self.assertEqual(encoded_images.bool_masked_pos.sum().item()   , 7_5    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			List[Any]									=    self.image_processing_class(**self.image_processor_dict    )
        # create random PIL images
        A_    :			Optional[int]									=    prepare_image_inputs(self.image_processor_tester   , equal_resolution=snake_case_    )
        for image in image_inputs:
          self.assertIsInstance(snake_case_   , PIL.Image.Image    )

        # Test not batched input
        A_    :			Optional[int]									=    image_processing(image_inputs[0]   , return_codebook_pixels=snake_case_   , return_tensors='pt'    )
        A_		,			A_    :			Any									=    self.image_processor_tester.get_expected_codebook_image_size()
        self.assertEqual(
            encoded_images.codebook_pixel_values.shape   , (1, self.image_processor_tester.num_channels, expected_height, expected_width)   , )

        # Test batched
        A_    :			Optional[int]									=    image_processing(snake_case_   , return_codebook_pixels=snake_case_   , return_tensors='pt'    )
        A_		,			A_    :			int									=    self.image_processor_tester.get_expected_codebook_image_size()
        self.assertEqual(
            encoded_images.codebook_pixel_values.shape   , (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )   , )







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







    def __init__(     self				,						_lowerCamelCase				,						_lowerCamelCase=1_3				,						_lowerCamelCase=7				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=True				,						_lowerCamelCase=9_9				,						_lowerCamelCase=3_2				,						_lowerCamelCase=5				,						_lowerCamelCase=4				,						_lowerCamelCase=3_7				,						_lowerCamelCase="gelu"				,						_lowerCamelCase=0.1				,						_lowerCamelCase=0.1				,						_lowerCamelCase=5_1_2				,						_lowerCamelCase=1_6				,						_lowerCamelCase=2				,						_lowerCamelCase=0.0_2				,						_lowerCamelCase=3				,						_lowerCamelCase=4				,						_lowerCamelCase=None				,						):
      lowercase				       =			parent
      lowercase				       =			batch_size
      lowercase				       =			seq_length
      lowercase				       =			is_training
      lowercase				       =			use_input_mask
      lowercase				       =			use_token_type_ids
      lowercase				       =			use_labels
      lowercase				       =			vocab_size
      lowercase				       =			hidden_size
      lowercase				       =			num_hidden_layers
      lowercase				       =			num_attention_heads
      lowercase				       =			intermediate_size
      lowercase				       =			hidden_act
      lowercase				       =			hidden_dropout_prob
      lowercase				       =			attention_probs_dropout_prob
      lowercase				       =			max_position_embeddings
      lowercase				       =			type_vocab_size
      lowercase				       =			type_sequence_label_size
      lowercase				       =			initializer_range
      lowercase				       =			num_labels
      lowercase				       =			num_choices
      lowercase				       =			scope







    def      UpperCamelCase_ (     self			):
      lowercase				       =			ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size			)

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

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

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

      lowercase				       =			self.get_config()

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







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







    def      UpperCamelCase_ (     self			):
      (
          (
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	
      )				       =			self.prepare_config_and_inputs()

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







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			True
      lowercase				       =			ElectraModel(_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						encoder_hidden_states=_lowerCamelCase				,						encoder_attention_mask=_lowerCamelCase				,						)
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						encoder_hidden_states=_lowerCamelCase				,						)
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase			)
      self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForMaskedLM(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForCausalLM(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForTokenClassification(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.num_labels)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForPreTraining(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_labels
      lowercase				       =			ElectraForSequenceClassification(_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase			)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			ElectraForQuestionAnswering(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						start_positions=_lowerCamelCase				,						end_positions=_lowerCamelCase				,						)
      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				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase				,						):
      lowercase				       =			self.num_choices
      lowercase				       =			ElectraForMultipleChoice(config=_lowerCamelCase			)
      model.to(_lowerCamelCase			)
      model.eval()
      lowercase				       =			input_ids.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			token_type_ids.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			input_mask.unsqueeze(1			).expand(-1				,						self.num_choices				,						-1			).contiguous()
      lowercase				       =			model(
          _lowerCamelCase				,						attention_mask=_lowerCamelCase				,						token_type_ids=_lowerCamelCase				,						labels=_lowerCamelCase				,						)
      self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.prepare_config_and_inputs()
      (
          (
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	(
          lowercase
      )       ,	
      )				       =			config_and_inputs
      lowercase				       =			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict


@require_torch
class 			a	(  a_,							a_,							unittest.TestCase      ):
    UpperCAmelCase_					:	List[str]      	=(
        (
            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_					:	Tuple      	=True







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase				,						_lowerCamelCase=False			):
      lowercase				       =			super()._prepare_for_class(_lowerCamelCase				,						_lowerCamelCase				,						return_labels=_lowerCamelCase			)

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







    def      UpperCamelCase_ (     self			):
      lowercase				       =			ElectraModelTester(self			)
      lowercase				       =			ConfigTester(self				,						config_class=_lowerCamelCase				,						hidden_size=3_7			)







    def      UpperCamelCase_ (     self			):
      self.config_tester.run_common_tests()







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_model(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_model_as_decoder(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        lowercase				       =			type
        self.model_tester.create_and_check_electra_model(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_masked_lm(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_token_classification(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_pretraining(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_sequence_classification(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_question_answering(*_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_multiple_choice(*_lowerCamelCase			)







    @slow
    def      UpperCamelCase_ (     self			):
      for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowercase				       =			ElectraModel.from_pretrained(_lowerCamelCase			)
        self.assertIsNotNone(_lowerCamelCase			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_for_causal_lm(*_lowerCamelCase			)


@require_torch
class 			a	(  unittest.TestCase      ):







    @slow
    def      UpperCamelCase_ (     self			):
      lowercase				       =			ElectraModel.from_pretrained('google/electra-small-discriminator'			)
      lowercase				       =			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]]			)
      lowercase				       =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
      lowercase				       =			model(_lowerCamelCase				,						attention_mask=_lowerCamelCase			)[0]
      lowercase				       =			torch.Size((1, 1_1, 2_5_6)			)
      self.assertEqual(output.shape				,						_lowerCamelCase			)
      lowercase				       =			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]				,						_lowerCamelCase				,						atol=1e-4			)			)


