






import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


__lowerCamelCase						= logging.get_logger(__name__)

class      UpperCamelCase_							(				nn.Module       ):




						def __init__(    self       )			->						str:
													super().__init__()
													if version.parse(torch.__version__       ) < version.parse('''1.12.0'''       ):
																				raise ImportError(
																				    F'You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '
																				    '''PytorchGELUTanh. Please upgrade torch.'''       )







						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return nn.functional.gelu(lowercase		,	approximate='''tanh'''       )

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi       ) * (input + 0.044715 * torch.pow(lowercase		,	3.0       ))       ))

class      UpperCamelCase_							(				nn.Module       ):




						def __init__(    self		,	lowercase = False       )			->						str:
													super().__init__()
													if use_gelu_python:
																				_a  :    Optional[Any]			=						self._gelu_python
													else:
																				_a  :    Dict			=						nn.functional.gelu




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0       )       ))







						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return self.act(lowercase       )

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return 0.5 * input * (1.0 + torch.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)       ))

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return input * torch.sigmoid(1.702 * input       )

class      UpperCamelCase_							(				nn.Module       ):




						def __init__(    self		,	lowercase		,	lowercase       )			->						Optional[Any]:
													if min > max:
																				raise ValueError(F'min should be < max (got min: {min}, max: {max})'       )

													super().__init__()
													_a  :    Tuple			=						min
													_a  :    str			=						max







						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return torch.clip(gelu(lowercase       )		,	self.min		,	self.max       )

class      UpperCamelCase_							(				nn.Module       ):




						def __init__(    self       )			->						Tuple:
													super().__init__()
													_a  :    List[str]			=						math.sqrt(2 / math.pi       )







						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044715 * torch.pow(lowercase		,	3       ))       ))

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return nn.functional.silu(lowercase       )

class      UpperCamelCase_							(				nn.Module       ):




						def __init__(    self       )			->						str:
													super().__init__()
													if version.parse(torch.__version__       ) < version.parse('''1.9.0'''       ):
																				_a  :    Optional[Any]			=						self._mish_python
													else:
																				_a  :    List[Any]			=						nn.functional.mish




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return input * torch.tanh(nn.functional.softplus(lowercase       )       )







						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return self.act(lowercase       )

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						Tensor:
													return input

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase		,	lowercase=0.707107		,	lowercase=0.282095       )			->						Tuple:
													_a  :    Any			=						(input - mu).div(sigma * math.sqrt(2.0       )       )
													return 0.5 * (1.0 + torch.erf(lowercase       ))

class      UpperCamelCase_							(				nn.Module       ):




						def      snake_case__(    self		,	lowercase       )			->						List[str]:
													_a  :    Tuple			=						nn.functional.relu(lowercase       )
													_a  :    str			=						torch.square(lowercase       )
													return squared

class      UpperCamelCase_							(				UpperCamelCase       ):




						def __getitem__(    self		,	lowercase       )			->						Optional[int]:
													_a  :    List[Any]			=						super().__getitem__(lowercase       )
													_a		,					_a  :    List[str]			=						content if isinstance(lowercase		,	lowercase       ) else (content, {})
													return cls(**lowercase       )

__lowerCamelCase						= {
    'gelu': GELUActivation,
    'gelu_10': (ClippedGELUActivation, {'min': -10, 'max': 10}),
    'gelu_fast': FastGELUActivation,
    'gelu_new': NewGELUActivation,
    'gelu_python': (GELUActivation, {'use_gelu_python': True}),
    'gelu_pytorch_tanh': PytorchGELUTanh,
    'gelu_accurate': AccurateGELUActivation,
    'laplace': LaplaceActivation,
    'linear': LinearActivation,
    'mish': MishActivation,
    'quick_gelu': QuickGELUActivation,
    'relu': nn.ReLU,
    'relu2': ReLUSquaredActivation,
    'relu6': nn.ReLUa,
    'sigmoid': nn.Sigmoid,
    'silu': SiLUActivation,
    'swish': SiLUActivation,
    'tanh': nn.Tanh,
}
__lowerCamelCase						= ClassInstantier(ACTaCLS)






def    UpperCamelCase__  (   UpperCAmelCase     )     ->		Optional[Any]:







							"""simple docstring"""





							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()     )}'     )


# For backwards compatibility with: from activations import gelu_python
__lowerCamelCase						= get_activation('gelu_python')
__lowerCamelCase						= get_activation('gelu_new')
__lowerCamelCase						= get_activation('gelu')
__lowerCamelCase						= get_activation('gelu_fast')
__lowerCamelCase						= get_activation('quick_gelu')
__lowerCamelCase						= get_activation('silu')
__lowerCamelCase						= get_activation('mish')







__lowerCamelCase						= get_activation('linear')







import argparse

import torch

from transformers import ElectraConfig, ElectraForMaskedLM, ElectraForPreTraining, load_tf_weights_in_electra
from transformers.utils import logging


logging.set_verbosity_info()






def    UpperCamelCase__  (   UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase     )     ->		str:







							"""simple docstring"""





							_a  :    Union[str, Any]			=						ElectraConfig.from_json_file(UpperCAmelCase     )
							print(F'Building PyTorch model from configuration: {config}'     )

							if discriminator_or_generator == "discriminator":
														_a  :    int			=						ElectraForPreTraining(UpperCAmelCase     )
							elif discriminator_or_generator == "generator":
														_a  :    Any			=						ElectraForMaskedLM(UpperCAmelCase     )
							else:
														raise ValueError('''The discriminator_or_generator argument should be either \'discriminator\' or \'generator\''''     )

							# Load weights from tf checkpoint
							load_tf_weights_in_electra(
							    UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase    ,	discriminator_or_generator=UpperCAmelCase     )

							# Save pytorch-model
							print(F'Save PyTorch model to {pytorch_dump_path}'     )
							torch.save(model.state_dict()    ,	UpperCAmelCase     )


if __name__ == "__main__":
						__lowerCamelCase						= argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
						)
						parser.add_argument(
						    '--config_file',
						    default=None,
						    type=str,
						    required=True,
						    help='The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.',
						)
						parser.add_argument(
						    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
						)
						parser.add_argument(
						    '--discriminator_or_generator',
						    default=None,
						    type=str,
						    required=True,
						    help=(
						        'Whether to export the generator or the discriminator. Should be a string, either \'discriminator\' or '
						        '\'generator\'.'
						    ),
						)
						__lowerCamelCase						= parser.parse_args()
						convert_tf_checkpoint_to_pytorch(
						    args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.discriminator_or_generator
						)