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       :

	'''simple docstring'''



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

	def 					__UpperCAmelCase(       self   ):
			__A							:   Dict	  =				ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size   )

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

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

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

			__A							:   Optional[int]	  =				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=__UpperCamelCase							,			initializer_range=self.initializer_range							,			)

	def 					__UpperCAmelCase(       self   ):
			(
			    (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,          
			)							:   Any	  =				self.prepare_config_and_inputs()

			__A							:   Optional[int]	  =				True
			__A							:   Optional[int]	  =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
			__A							:   List[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 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   int	  =				ElectraModel(config=__UpperCamelCase   )
			model.to(__UpperCamelCase   )
			model.eval()
			__A							:   Optional[Any]	  =				model(__UpperCamelCase							,			attention_mask=__UpperCamelCase							,			token_type_ids=__UpperCamelCase   )
			__A							:   int	  =				model(__UpperCamelCase							,			token_type_ids=__UpperCamelCase   )
			__A							:   int	  =				model(__UpperCamelCase   )
			self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)   )

	def 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   Union[str, Any]	  =				True
			__A							:   Optional[Any]	  =				ElectraModel(__UpperCamelCase   )
			model.to(__UpperCamelCase   )
			model.eval()
			__A							:   Dict	  =				model(
			    __UpperCamelCase							,			attention_mask=__UpperCamelCase							,			token_type_ids=__UpperCamelCase							,			encoder_hidden_states=__UpperCamelCase							,			encoder_attention_mask=__UpperCamelCase							,			)
			__A							:   int	  =				model(
			    __UpperCamelCase							,			attention_mask=__UpperCamelCase							,			token_type_ids=__UpperCamelCase							,			encoder_hidden_states=__UpperCamelCase							,			)
			__A							:   str	  =				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 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   Optional[int]	  =				ElectraForMaskedLM(config=__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.seq_length, self.vocab_size)   )

	def 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   int	  =				ElectraForCausalLM(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 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   Optional[int]	  =				self.num_labels
			__A							:   Tuple	  =				ElectraForTokenClassification(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, self.num_labels)   )

	def 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   str	  =				self.num_labels
			__A							:   int	  =				ElectraForPreTraining(config=__UpperCamelCase   )
			model.to(__UpperCamelCase   )
			model.eval()
			__A							:   int	  =				model(__UpperCamelCase							,			attention_mask=__UpperCamelCase							,			token_type_ids=__UpperCamelCase							,			labels=__UpperCamelCase   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length)   )

	def 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase							,			):
			__A							:   int	  =				self.num_labels
			__A							:   int	  =				ElectraForSequenceClassification(__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.num_labels)   )

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







	def 					__UpperCAmelCase(       self   ):
			__A							:   str	  =				self.prepare_config_and_inputs()
			(
			    (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,       (
			    __A   
			)  ,          
			)							:   Optional[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 _a       (	UpperCAmelCase_   ,							UpperCAmelCase_   ,							unittest.TestCase ):

	'''simple docstring'''



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

	def 					__UpperCAmelCase(       self							,			__UpperCAmelCase							,			__UpperCAmelCase							,			__UpperCAmelCase=False   ):
			__A							:   str	  =				super()._prepare_for_class(__UpperCamelCase							,			__UpperCamelCase							,			return_labels=__UpperCamelCase   )

			if return_labels:
					if model_class in get_values(__UpperCamelCase   ):
							__A							:   Optional[Any]	  =				torch.zeros(
							    (self.model_tester.batch_size, self.model_tester.seq_length)							,			dtype=torch.long							,			device=__UpperCamelCase   )
			return inputs_dict

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				ElectraModelTester(self   )
			__A							:   Tuple	  =				ConfigTester(self							,			config_class=__UpperCamelCase							,			hidden_size=37   )

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

	def 					__UpperCAmelCase(       self   ):
			__A							:   List[Any]	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_model(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.model_tester.prepare_config_and_inputs_for_decoder()
			self.model_tester.create_and_check_electra_model_as_decoder(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Optional[int]	  =				self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					__A							:   List[Any]	  =				type
					self.model_tester.create_and_check_electra_model(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Any	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_masked_lm(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_token_classification(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Optional[Any]	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_pretraining(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   str	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_sequence_classification(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_question_answering(*__UpperCamelCase   )

	def 					__UpperCAmelCase(       self   ):
			__A							:   Union[str, Any]	  =				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_multiple_choice(*__UpperCamelCase   )

	@slow
	def 					__UpperCAmelCase(       self   ):
			for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__A							:   List[Any]	  =				ElectraModel.from_pretrained(__UpperCamelCase   )
					self.assertIsNotNone(__UpperCamelCase   )







	def 					__UpperCAmelCase(       self   ):
			__A							:   List[Any]	  =				self.model_tester.prepare_config_and_inputs_for_decoder()
			self.model_tester.create_and_check_electra_for_causal_lm(*__UpperCamelCase   )


@require_torch
class _a       (	unittest.TestCase ):

	'''simple docstring'''



	@slow
	def 					__UpperCAmelCase(       self   ):
			__A							:   Tuple	  =				ElectraModel.from_pretrained("google/electra-small-discriminator"   )
			__A							:   Union[str, Any]	  =				torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]   )
			__A							:   Optional[Any]	  =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
			__A							:   Optional[int]	  =				model(__UpperCamelCase							,			attention_mask=__UpperCamelCase   )[0]
			__A							:   Optional[Any]	  =				torch.Size((1, 11, 256)   )
			self.assertEqual(output.shape							,			__UpperCamelCase   )
			__A							:   str	  =				torch.tensor(
			    [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]   )

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






from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import is_torch_available, is_transformers_available


if is_transformers_available() and is_torch_available():
					from .image_encoder import PaintByExampleImageEncoder
					from .pipeline_paint_by_example import PaintByExamplePipeline





