






"""simple docstring"""




import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase=None ):






  """simple docstring"""





  A_  :	Optional[Any]    						=  None
  # Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
  if str(weight.dtype ) == "torch.bfloat16":
    # Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
    A_  :	List[str]    						=  weight.view(torch.intaa )
    A_  :	List[str]    						=  'bfloat16'
  A_  :	str    						=  weight.cpu().numpy()
  A_  :	Any    						=  os.path.join(lowercase_			, f"""{weight_name}.dat""" )
  if index is not None:
    if dtype is None:
      A_  :	Dict    						=  str(array.dtype )
    A_  :	int    						=  {'dtype': dtype, 'shape': list(array.shape )}
  if array.ndim == 0:
    A_  :	Tuple    						=  array[None]
  A_  :	Any    						=  np.memmap(lowercase_			, dtype=array.dtype			, mode='w+'			, shape=array.shape )
  A_  :	Dict    						=  array[:]
  file_array.flush()
  return index





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	Dict    						=  tuple(weight_info['shape'] )
  if shape == ():
    # NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
    A_  :	Optional[Any]    						=  (1,)

  A_  :	Tuple    						=  weight_info['dtype']
  if dtype == "bfloat16":
    # NumPy does not support bfloat16 so this was saved as a int16
    A_  :	Any    						=  'int16'

  A_  :	Union[str, Any]    						=  np.memmap(lowercase_			, dtype=lowercase_			, shape=lowercase_			, mode='r' )

  if len(weight_info['shape'] ) == 0:
    A_  :	str    						=  weight[0]
  A_  :	Optional[int]    						=  torch.tensor(lowercase_ )
  if weight_info["dtype"] == "bfloat16":
    A_  :	Dict    						=  weight.view(torch.bfloataa )

  return weight





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  if index is None or len(lowercase_ ) == 0:
    # Nothing to save
    return

  A_  :	Any    						=  os.path.join(lowercase_			, 'index.json' )
  if os.path.isfile(lowercase_ ):
    with open(lowercase_			, 'r'			, encoding='utf-8' ) as f:
      A_  :	List[Any]    						=  json.load(lowercase_ )
  else:
    A_  :	str    						=  {}
  current_index.update(lowercase_ )

  with open(lowercase_			, 'w'			, encoding='utf-8' ) as f:
    json.dump(lowercase_			, lowercase_			, indent=2 )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  os.makedirs(lowercase_			, exist_ok=lowercase_ )
  A_  :	Union[str, Any]    						=  {}
  for name, parameter in state_dict.items():
    A_  :	List[Any]    						=  offload_weight(lowercase_			, lowercase_			, lowercase_			, index=lowercase_ )

  # Update index
  save_offload_index(lowercase_			, lowercase_ )

class 			_UpperCAmelCase  (       UpperCAmelCase_		):



      '''simple docstring'''

      def __init__(					self ,				snake_case_ ,				snake_case_  ):

        """simple docstring"""

        A_  :	Union[str, Any]    						=  dataset
        A_  :	Optional[int]    						=  prefix

      def __getitem__(					self ,				snake_case_  ):

        """simple docstring"""

        return self.dataset[F"""{self.prefix}{key}"""]

      def __iter__(					self  ):

        """simple docstring"""

        return iter([key for key in self.dataset if key.startswith(self.prefix  )]  )

      def __len__(					self  ):

        """simple docstring"""

        return len(self.dataset  )


class 			_UpperCAmelCase  (       UpperCAmelCase_		):



      '''simple docstring'''

      def __init__(					self ,				snake_case_ = None ,				snake_case_ = None ,				snake_case_ = None ,				snake_case_=None ,				):

        """simple docstring"""

        if state_dict is None and save_folder is None:
          raise ValueError('Need either a `state_dict` or a `save_folder` containing offloaded weights.'  )

        A_  :	Dict    						=  {} if state_dict is None else state_dict
        A_  :	Union[str, Any]    						=  save_folder
        if index is None and save_folder is not None:
          with open(os.path.join(__UpperCamelCase ,				'index.json'  )  ) as f:
            A_  :	Optional[int]    						=  json.load(__UpperCamelCase  )
        A_  :	Optional[int]    						=  {} if index is None else index
        A_  :	int    						=  list(self.state_dict.keys()  )
        self.all_keys.extend([key for key in self.index if key not in self.all_keys]  )
        A_  :	List[str]    						=  device

      def __getitem__(					self ,				snake_case_  ):

        """simple docstring"""

        if key in self.state_dict:
          return self.state_dict[key]
        A_  :	int    						=  self.index[key]
        if weight_info.get('safetensors_file'  ) is not None:
          if not is_safetensors_available():
            raise ImportError('These offloaded weights require the use of safetensors: `pip install safetensors`.'  )

          from safetensors import safe_open

          A_  :	Any    						=  'cpu' if self.device is None else self.device
          with safe_open(weight_info['safetensors_file'] ,				framework='pt' ,				device=__UpperCamelCase  ) as f:
            A_  :	Union[str, Any]    						=  f.get_tensor(weight_info.get('weight_name' ,				__UpperCamelCase  )  )

          if "dtype" in weight_info:
            return tensor.to(getattr(__UpperCamelCase ,				weight_info['dtype']  )  )
          else:
            return tensor

        A_  :	Optional[Any]    						=  os.path.join(self.save_folder ,				F"""{key}.dat"""  )
        return load_offloaded_weight(__UpperCamelCase ,				__UpperCamelCase  )

      def __iter__(					self  ):

        """simple docstring"""

        return iter(self.all_keys  )

      def __len__(					self  ):

        """simple docstring"""

        return len(self.all_keys  )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	str    						=  {}
  for module_name in submodule_names:
    # We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
    # submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
    result.update(
        {
            key: param
            for key, param in state_dict.items()
            if key == module_name or key.startswith(module_name + '.' )
        } )
  return result






"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_					:						List[str]      = logging.get_logger(__name__)

lowerCamelCase_					:						Optional[int]      = {
    'google/electra-small-generator': 'https://huggingface.co/google/electra-small-generator/resolve/main/config.json',
    'google/electra-base-generator': 'https://huggingface.co/google/electra-base-generator/resolve/main/config.json',
    'google/electra-large-generator': 'https://huggingface.co/google/electra-large-generator/resolve/main/config.json',
    'google/electra-small-discriminator': (
        'https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json'
    ),
    'google/electra-base-discriminator': (
        'https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json'
    ),
    'google/electra-large-discriminator': (
        'https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json'
    ),
}

class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_				:			Tuple        =       """electra"""

      def __init__(					self ,				snake_case_=3_0_5_2_2 ,				snake_case_=1_2_8 ,				snake_case_=2_5_6 ,				snake_case_=1_2 ,				snake_case_=4 ,				snake_case_=1_0_2_4 ,				snake_case_="gelu" ,				snake_case_=0.1 ,				snake_case_=0.1 ,				snake_case_=5_1_2 ,				snake_case_=2 ,				snake_case_=0.02 ,				snake_case_=1E-12 ,				snake_case_="first" ,				snake_case_=True ,				snake_case_="gelu" ,				snake_case_=0.1 ,				snake_case_=0 ,				snake_case_="absolute" ,				snake_case_=True ,				snake_case_=None ,				**snake_case_ ,				):

        """simple docstring"""

        super().__init__(pad_token_id=snake_case_ ,				**snake_case_  )

        A_  :	Optional[Any]    						=  vocab_size
        A_  :	Any    						=  embedding_size
        A_  :	Optional[int]    						=  hidden_size
        A_  :	Tuple    						=  num_hidden_layers
        A_  :	Tuple    						=  num_attention_heads
        A_  :	List[Any]    						=  intermediate_size
        A_  :	Any    						=  hidden_act
        A_  :	str    						=  hidden_dropout_prob
        A_  :	Any    						=  attention_probs_dropout_prob
        A_  :	str    						=  max_position_embeddings
        A_  :	Optional[Any]    						=  type_vocab_size
        A_  :	Dict    						=  initializer_range
        A_  :	Optional[int]    						=  layer_norm_eps

        A_  :	int    						=  summary_type
        A_  :	List[str]    						=  summary_use_proj
        A_  :	Optional[Any]    						=  summary_activation
        A_  :	Optional[Any]    						=  summary_last_dropout
        A_  :	List[Any]    						=  position_embedding_type
        A_  :	Union[str, Any]    						=  use_cache
        A_  :	Tuple    						=  classifier_dropout


class 			_UpperCAmelCase  (       UpperCAmelCase__		):



      '''simple docstring'''

      @property
      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        if self.task == "multiple-choice":
          A_  :	int    						=  {0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
          A_  :	Optional[Any]    						=  {0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
                ('token_type_ids', dynamic_axis),
            ]  )