





'''simple docstring'''






from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


A__       :     List[Any]          =logging.get_logger(__name__)

A__       :     str          ={
    '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/config.json''',
    '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/config.json''',
    '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/config.json''',
    '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/config.json''',
    '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/config.json''',
}




class 				UpperCAmelCase  (    snake_case_     ):
      _lowercase:    Optional[Any]		  =     '''gpt2'''
      _lowercase:    List[Any]		  =     ['''past_key_values''']
      _lowercase:    List[Any]		  =     {
          '''hidden_size''': '''n_embd''',
          '''max_position_embeddings''': '''n_positions''',
          '''num_attention_heads''': '''n_head''',
          '''num_hidden_layers''': '''n_layer''',
      }




      def __init__(    self    :				Union[str, Any]    ,    __snake_case    :				List[Any]=5_02_57    ,    __snake_case    :				Optional[Any]=10_24    ,    __snake_case    :				int=7_68    ,    __snake_case    :				List[str]=12    ,    __snake_case    :				Union[str, Any]=12    ,    __snake_case    :				str=None    ,    __snake_case    :				Union[str, Any]="gelu_new"    ,    __snake_case    :				Optional[Any]=0.1    ,    __snake_case    :				Union[str, Any]=0.1    ,    __snake_case    :				List[Any]=0.1    ,    __snake_case    :				List[Any]=1E-5    ,    __snake_case    :				Union[str, Any]=0.02    ,    __snake_case    :				List[Any]="cls_index"    ,    __snake_case    :				Optional[int]=True    ,    __snake_case    :				Tuple=None    ,    __snake_case    :				int=True    ,    __snake_case    :				Dict=0.1    ,    __snake_case    :				List[str]=True    ,    __snake_case    :				List[Any]=True    ,    __snake_case    :				int=5_02_56    ,    __snake_case    :				List[Any]=5_02_56    ,    __snake_case    :				Union[str, Any]=False    ,    __snake_case    :				List[str]=False    ,    **__snake_case    :				str    ,    )       ->   Union[str, Any]:
            _lowerCAmelCase							  =							vocab_size
            _lowerCAmelCase							  =							n_positions
            _lowerCAmelCase							  =							n_embd
            _lowerCAmelCase							  =							n_layer
            _lowerCAmelCase							  =							n_head
            _lowerCAmelCase							  =							n_inner
            _lowerCAmelCase							  =							activation_function
            _lowerCAmelCase							  =							resid_pdrop
            _lowerCAmelCase							  =							embd_pdrop
            _lowerCAmelCase							  =							attn_pdrop
            _lowerCAmelCase							  =							layer_norm_epsilon
            _lowerCAmelCase							  =							initializer_range
            _lowerCAmelCase							  =							summary_type
            _lowerCAmelCase							  =							summary_use_proj
            _lowerCAmelCase							  =							summary_activation
            _lowerCAmelCase							  =							summary_first_dropout
            _lowerCAmelCase							  =							summary_proj_to_labels
            _lowerCAmelCase							  =							scale_attn_weights
            _lowerCAmelCase							  =							use_cache
            _lowerCAmelCase							  =							scale_attn_by_inverse_layer_idx
            _lowerCAmelCase							  =							reorder_and_upcast_attn

            _lowerCAmelCase							  =							bos_token_id
            _lowerCAmelCase							  =							eos_token_id

            super().__init__(bos_token_id=__snake_case    ,    eos_token_id=__snake_case    ,    **__snake_case						)




class 				UpperCAmelCase  (    snake_case_     ):




      def __init__(    self    :				Optional[int]    ,    __snake_case    :				PretrainedConfig    ,    __snake_case    :				str = "default"    ,    __snake_case    :				List[PatchingSpec] = None    ,    __snake_case    :				bool = False    ,    )       ->   Dict:
            super().__init__(__snake_case    ,    task=__snake_case    ,    patching_specs=__snake_case    ,    use_past=__snake_case						)
            if not getattr(self._config    ,    """pad_token_id"""    ,    __snake_case						):
                  # TODO: how to do that better?
                  _lowerCAmelCase							  =							0




      @property
      def        lowercase__					(    self    :				Optional[int]						)       ->   Mapping[str, Mapping[int, str]]:
            _lowerCAmelCase							  =							OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}						)
            if self.use_past:
                  self.fill_with_past_key_values_(__snake_case    ,    direction="""inputs"""						)
                  _lowerCAmelCase							  =							{0: """batch""", 1: """past_sequence + sequence"""}
            else:
                  _lowerCAmelCase							  =							{0: """batch""", 1: """sequence"""}

            return common_inputs




      @property
      def        lowercase__					(    self    :				int						)       ->   int:
            return self._config.n_layer




      @property
      def        lowercase__					(    self    :				Optional[Any]						)       ->   int:
            return self._config.n_head




      def        lowercase__					(    self    :				Tuple    ,    __snake_case    :				PreTrainedTokenizer    ,    __snake_case    :				int = -1    ,    __snake_case    :				int = -1    ,    __snake_case    :				bool = False    ,    __snake_case    :				Optional[TensorType] = None    ,    )       ->   Mapping[str, Any]:
            _lowerCAmelCase							  =							super(__snake_case    ,    self						).generate_dummy_inputs(
                __snake_case    ,    batch_size=__snake_case    ,    seq_length=__snake_case    ,    is_pair=__snake_case    ,    framework=__snake_case						)

            # We need to order the input in the way they appears in the forward()
            _lowerCAmelCase							  =							OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}						)

            # Need to add the past_keys
            if self.use_past:
                  if not is_torch_available():
                        raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""						)
                  else:
                        import torch

                        _lowerCAmelCase  ,							_lowerCAmelCase							  =							common_inputs["""input_ids"""].shape
                        # Not using the same length for past_key_values
                        _lowerCAmelCase							  =							seqlen + 2
                        _lowerCAmelCase							  =							(
                            batch,
                            self.num_attention_heads,
                            past_key_values_length,
                            self._config.hidden_size // self.num_attention_heads,
                        )
                        _lowerCAmelCase							  =							[
                            (torch.zeros(__snake_case						), torch.zeros(__snake_case						)) for _ in range(self.num_layers						)
                        ]

            _lowerCAmelCase							  =							common_inputs["""attention_mask"""]
            if self.use_past:
                  _lowerCAmelCase							  =							ordered_inputs["""attention_mask"""].dtype
                  _lowerCAmelCase							  =							torch.cat(
                      [ordered_inputs["""attention_mask"""], torch.ones(__snake_case    ,    __snake_case    ,    dtype=__snake_case						)]    ,    dim=1						)

            return ordered_inputs




      @property
      def        lowercase__					(    self    :				List[str]						)       ->   int:
            return 13













'''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    :				str    ,    __snake_case    :				str    ,    __snake_case    :				str=13    ,    __snake_case    :				Optional[Any]=7    ,    __snake_case    :				str=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=99    ,    __snake_case    :				Dict=24    ,    __snake_case    :				Any=32    ,    __snake_case    :				List[str]=5    ,    __snake_case    :				Any=4    ,    __snake_case    :				Optional[Any]=37    ,    __snake_case    :				Union[str, Any]="gelu"    ,    __snake_case    :				Dict=0.1    ,    __snake_case    :				int=0.1    ,    __snake_case    :				Dict=5_12    ,    __snake_case    :				List[Any]=16    ,    __snake_case    :				Dict=2    ,    __snake_case    :				str=0.02    ,    __snake_case    :				Any=4    ,    )       ->   Union[str, Any]:
            _lowerCAmelCase							  =							parent
            _lowerCAmelCase							  =							batch_size
            _lowerCAmelCase							  =							seq_length
            _lowerCAmelCase							  =							is_training
            _lowerCAmelCase							  =							use_attention_mask
            _lowerCAmelCase							  =							use_token_type_ids
            _lowerCAmelCase							  =							use_labels
            _lowerCAmelCase							  =							vocab_size
            _lowerCAmelCase							  =							hidden_size
            _lowerCAmelCase							  =							embedding_size
            _lowerCAmelCase							  =							num_hidden_layers
            _lowerCAmelCase							  =							num_attention_heads
            _lowerCAmelCase							  =							intermediate_size
            _lowerCAmelCase							  =							hidden_act
            _lowerCAmelCase							  =							hidden_dropout_prob
            _lowerCAmelCase							  =							attention_probs_dropout_prob
            _lowerCAmelCase							  =							max_position_embeddings
            _lowerCAmelCase							  =							type_vocab_size
            _lowerCAmelCase							  =							type_sequence_label_size
            _lowerCAmelCase							  =							initializer_range
            _lowerCAmelCase							  =							num_choices




      def        lowercase__					(    self    :				Optional[Any]						)       ->   Optional[Any]:
            _lowerCAmelCase							  =							ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size						)

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

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

            _lowerCAmelCase							  =							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        lowercase__					(    self    :				Any						)       ->   Optional[int]:
            _lowerCAmelCase							  =							self.prepare_config_and_inputs()
            _lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase							  =							config_and_inputs
            _lowerCAmelCase							  =							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
            return config, inputs_dict




@require_flax
class 				UpperCAmelCase  (    snake_case_							,	unittest.TestCase     ):
      _lowercase:    str		  =     True

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




      def        lowercase__					(    self    :				Tuple						)       ->   Optional[int]:
            _lowerCAmelCase							  =							FlaxElectraModelTester(self						)




      @slow
      def        lowercase__					(    self    :				List[Any]						)       ->   str:
            for model_class_name in self.all_model_classes:
                  if model_class_name == FlaxElectraForMaskedLM:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-generator"""						)
                  else:
                        _lowerCAmelCase							  =							model_class_name.from_pretrained("""google/electra-small-discriminator"""						)
                  _lowerCAmelCase							  =							model(np.ones((1, 1)						)						)
                  self.assertIsNotNone(__snake_case						)






