




'''simple docstring'''



import math
from collections import OrderedDict

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

from .utils import logging


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





class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def __init__(							self     )    ->					int:
         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 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return nn.functional.gelu(SCREAMING_SNAKE_CASE_      ,					approximate='tanh'     )







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi     ) * (input + 0.0_4_4_7_1_5 * torch.pow(SCREAMING_SNAKE_CASE_      ,					3.0     ))     ))







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def __init__(							self      ,					SCREAMING_SNAKE_CASE_ = False     )    ->					str:
         super().__init__()
         if use_gelu_python:
              __lowerCamelCase       :     Any        =		self._gelu_python
         else:
              __lowerCamelCase       :     str        =		nn.functional.gelu







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







    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return self.act(SCREAMING_SNAKE_CASE_     )







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return 0.5 * input * (1.0 + torch.tanh(input * 0.7_9_7_8_8_4_5_6_0_8 * (1.0 + 0.0_4_4_7_1_5 * input * input)     ))







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return input * torch.sigmoid(1.7_0_2 * input     )







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






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

         super().__init__()
         __lowerCamelCase       :     Union[str, Any]        =		min
         __lowerCamelCase       :     List[Any]        =		max







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







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def __init__(							self     )    ->					Union[str, Any]:
         super().__init__()
         __lowerCamelCase       :     int        =		math.sqrt(2 / math.pi     )







    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.0_4_4_7_1_5 * torch.pow(SCREAMING_SNAKE_CASE_      ,					3     ))     ))







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return nn.functional.silu(SCREAMING_SNAKE_CASE_     )







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






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







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







    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return self.act(SCREAMING_SNAKE_CASE_     )







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Tensor:
         return input







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_      ,					SCREAMING_SNAKE_CASE_=0.7_0_7_1_0_7      ,					SCREAMING_SNAKE_CASE_=0.2_8_2_0_9_5     )    ->					List[str]:
         __lowerCamelCase       :     List[Any]        =		(input - mu).div(sigma * math.sqrt(2.0     )     )
         return 0.5 * (1.0 + torch.erf(SCREAMING_SNAKE_CASE_     ))







class        UpperCAmelCase_  (nn.Module  ):





    """simple docstring"""






    def 				lowercase_    (							self      ,					SCREAMING_SNAKE_CASE_     )    ->					Optional[Any]:
         __lowerCamelCase       :     Optional[int]        =		nn.functional.relu(SCREAMING_SNAKE_CASE_     )
         __lowerCamelCase       :     Optional[int]        =		torch.square(SCREAMING_SNAKE_CASE_     )
         return squared







class        UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






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







A__		: Optional[Any]												=  {
    """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,
}
A__		: List[str]												=  ClassInstantier(ACTaCLS)



def       UpperCAmelCase__ (     UpperCAmelCase_   :    Dict							)					->		List[str]:
     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
A__		: Dict												=  get_activation("""gelu_python""")
A__		: List[Any]												=  get_activation("""gelu_new""")
A__		: str												=  get_activation("""gelu""")
A__		: Union[str, Any]												=  get_activation("""gelu_fast""")
A__		: List[Any]												=  get_activation("""quick_gelu""")
A__		: str												=  get_activation("""silu""")
A__		: List[str]												=  get_activation("""mish""")

A__		: List[str]												=  get_activation("""linear""")










'''simple docstring'''



from typing import Literal

A__		: Tuple												=  """ABCDEFGHIJKLMNOPQRSTUVWXYZ"""



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    Literal["encrypt", "decrypt"]							)					->		str:
     __lowerCamelCase       :     Dict        =		LETTERS if mode == 'decrypt' else key
     __lowerCamelCase       :     Tuple        =		key if mode == 'decrypt' else LETTERS
     __lowerCamelCase       :     Optional[int]        =		''
     # loop through each symbol in the message
     for symbol in message:
          if symbol.upper() in chars_a:
               # encrypt/decrypt the symbol
               __lowerCamelCase       :     Optional[int]        =		chars_a.find(symbol.upper()							)
               if symbol.isupper():
                    translated += chars_b[sym_index].upper()
               else:
                    translated += chars_b[sym_index].lower()
          else:
               # symbol is not in LETTERS, just add it
               translated += symbol
     return translated



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str							)					->		str:
     return translate_message(UpperCAmelCase_				,   UpperCAmelCase_				,   'encrypt'							)



def       UpperCAmelCase__ (     UpperCAmelCase_   :    str				,   UpperCAmelCase_   :    str							)					->		str:
     return translate_message(UpperCAmelCase_				,   UpperCAmelCase_				,   'decrypt'							)



def       UpperCAmelCase__ (     )					->		None:
     __lowerCamelCase       :     Dict        =		'Hello World'
     __lowerCamelCase       :     str        =		'QWERTYUIOPASDFGHJKLZXCVBNM'
     __lowerCamelCase       :     Any        =		'decrypt'  # set to 'encrypt' or 'decrypt'

     if mode == "encrypt":
          __lowerCamelCase       :     Optional[int]        =		encrypt_message(UpperCAmelCase_				,   UpperCAmelCase_							)
     elif mode == "decrypt":
          __lowerCamelCase       :     List[str]        =		decrypt_message(UpperCAmelCase_				,   UpperCAmelCase_							)
     print(F'Using the key {key}, the {mode}ed message is: {translated}'							)


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      main()






