






"""simple docstring"""







from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available






def 					_lowerCAmelCase				(					lowerCAmelCase						):







       '''simple docstring'''
       if is_torch_available():
              import torch

              if isinstance(lowerCAmelCase		,					torch.Tensor						):
                     return True

       return isinstance(lowerCAmelCase		,					np.ndarray						)





class      UpperCamelCase_       (				a_     ):



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	fields(self )

         # Safety and consistency checks
         if not len(snake_case__ ):
                raise ValueError(f'''{self.__class__.__name__} has no fields.''' )

         UpperCAmelCase					=	getattr(self       ,						class_fields[0].name )
         UpperCAmelCase					=	all(getattr(self       ,						field.name ) is None for field in class_fields[1:] )

         if other_fields_are_none and isinstance(snake_case__       ,						snake_case__ ):
                for key, value in first_field.items():
                       UpperCAmelCase					=	value
         else:
                for field in class_fields:
                       UpperCAmelCase					=	getattr(self       ,						field.name )
                       if v is not None:
                              UpperCAmelCase					=	v



  def __delitem__(					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	List[str]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``pop`` on a {self.__class__.__name__} instance.''' )



  def 						UpperCamelCase_ (					self       ,						*snake_case__       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         raise Exception(f'''You cannot use ``update`` on a {self.__class__.__name__} instance.''' )



  def __getitem__(					self       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





         if isinstance(snake_case__       ,						snake_case__ ):
                UpperCAmelCase					=	dict(self.items() )
                return inner_dict[k]
         else:
                return self.to_tuple()[k]



  def __setattr__(					self       ,						snake_case__       ,						snake_case__ )	->	Union[str, Any]:



         """simple docstring"""





         if name in self.keys() and value is not None:
                # Don't call self.__setitem__ to avoid recursion errors
                super().__setitem__(snake_case__       ,						snake_case__ )
         super().__setattr__(snake_case__       ,						snake_case__ )



  def __setitem__(					self       ,						snake_case__       ,						snake_case__ )	->	Tuple:



         """simple docstring"""





         super().__setitem__(snake_case__       ,						snake_case__ )
         # Don't call self.__setattr__ to avoid recursion errors
         super().__setattr__(snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Tuple[Any]:



         """simple docstring"""





         return tuple(self[k] for k in self.keys() )








"""simple docstring"""







import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.electra.modeling_flax_electra import (
     FlaxElectraForCausalLM,
     FlaxElectraForMaskedLM,
     FlaxElectraForMultipleChoice,
     FlaxElectraForPreTraining,
     FlaxElectraForQuestionAnswering,
     FlaxElectraForSequenceClassification,
     FlaxElectraForTokenClassification,
     FlaxElectraModel,
 )





class      UpperCamelCase_       (				unittest.TestCase     ):



  def __init__(					self       ,						snake_case__       ,						snake_case__=13       ,						snake_case__=7       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=99       ,						snake_case__=24       ,						snake_case__=32       ,						snake_case__=5       ,						snake_case__=4       ,						snake_case__=37       ,						snake_case__="gelu"       ,						snake_case__=0.1       ,						snake_case__=0.1       ,						snake_case__=5_12       ,						snake_case__=16       ,						snake_case__=2       ,						snake_case__=0.02       ,						snake_case__=4       ,						)	->	int:



         """simple docstring"""





         UpperCAmelCase					=	parent
         UpperCAmelCase					=	batch_size
         UpperCAmelCase					=	seq_length
         UpperCAmelCase					=	is_training
         UpperCAmelCase					=	use_attention_mask
         UpperCAmelCase					=	use_token_type_ids
         UpperCAmelCase					=	use_labels
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	embedding_size
         UpperCAmelCase					=	num_hidden_layers
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	intermediate_size
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	type_vocab_size
         UpperCAmelCase					=	type_sequence_label_size
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	num_choices



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

         UpperCAmelCase					=	None
         if self.use_attention_mask:
                UpperCAmelCase					=	random_attention_mask([self.batch_size, self.seq_length] )

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

         UpperCAmelCase					=	ElectraConfig(
             vocab_size=self.vocab_size       ,						hidden_size=self.hidden_size       ,						embedding_size=self.embedding_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       ,						initializer_range=self.initializer_range       ,						)

         return config, input_ids, token_type_ids, attention_mask



  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	self.prepare_config_and_inputs()
         UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase					=	config_and_inputs
         UpperCAmelCase					=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
         return config, inputs_dict





@require_flax
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    str =		True

  _A				:    List[Any] =		(
      (
          FlaxElectraModel,
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForPreTraining,
          FlaxElectraForTokenClassification,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForMultipleChoice,
          FlaxElectraForSequenceClassification,
      )
      if is_flax_available()
      else ()
  )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	FlaxElectraModelTester(self )



  @slow
  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         for model_class_name in self.all_model_classes:
                if model_class_name == FlaxElectraForMaskedLM:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-generator""" )
                else:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-discriminator""" )
                UpperCAmelCase					=	model(np.ones((1, 1) ) )
                self.assertIsNotNone(snake_case__ )
