



"""simple docstring"""






import math
from collections import OrderedDict

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

from .utils import logging


__UpperCAmelCase      :      Union[str, Any]   					=					logging.get_logger(__name__)

class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def __init__(      self   ):




          """simple docstring"""





          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 		_a (      self    ,       a   ):




          """simple docstring"""





          return nn.functional.gelu(lowerCamelCase__    ,       approximate="tanh"   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi   ) * (input + 0.044715 * torch.pow(lowerCamelCase__    ,       3.0   ))   ))



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def __init__(      self    ,       a = False   ):




          """simple docstring"""





          super().__init__()
          if use_gelu_python:
               snake_case_						:str								=						self._gelu_python
          else:
               snake_case_						:Dict								=						nn.functional.gelu







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0   )   ))







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return self.act(lowerCamelCase__   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return 0.5 * input * (1.0 + torch.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)   ))



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return input * torch.sigmoid(1.702 * input   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def __init__(      self    ,       a    ,       a   ):




          """simple docstring"""





          if min > max:
               raise ValueError(F'''min should be < max (got min: {min}, max: {max})'''   )

          super().__init__()
          snake_case_						:str								=						min
          snake_case_						:List[Any]								=						max







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return torch.clip(gelu(lowerCamelCase__   )    ,       self.min    ,       self.max   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def __init__(      self   ):




          """simple docstring"""





          super().__init__()
          snake_case_						:Any								=						math.sqrt(2 / math.pi   )







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044715 * torch.pow(lowerCamelCase__    ,       3   ))   ))



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return nn.functional.silu(lowerCamelCase__   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def __init__(      self   ):




          """simple docstring"""





          super().__init__()
          if version.parse(torch.__version__   ) < version.parse("1.9.0"   ):
               snake_case_						:Optional[Any]								=						self._mish_python
          else:
               snake_case_						:Optional[int]								=						nn.functional.mish







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return input * torch.tanh(nn.functional.softplus(lowerCamelCase__   )   )







     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return self.act(lowerCamelCase__   )



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          return input



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a    ,       a=0.707107    ,       a=0.282095   ):




          """simple docstring"""





          snake_case_						:Any								=						(input - mu).div(sigma * math.sqrt(2.0   )   )
          return 0.5 * (1.0 + torch.erf(lowerCamelCase__   ))



class 	__lowerCAmelCase							(nn.Module  ):


     '''simple docstring'''


     def 		_a (      self    ,       a   ):




          """simple docstring"""





          snake_case_						:int								=						nn.functional.relu(lowerCamelCase__   )
          snake_case_						:Optional[Any]								=						torch.square(lowerCamelCase__   )
          return squared



class 	__lowerCAmelCase							(UpperCAmelCase__  ):


     '''simple docstring'''


     def __getitem__(      self    ,       a   ):




          """simple docstring"""





          snake_case_						:int								=						super().__getitem__(lowerCamelCase__   )
          snake_case_						:List[str]								=						content if isinstance(lowerCamelCase__    ,       lowerCamelCase__   ) else (content, {})
          return cls(**lowerCamelCase__   )



__UpperCAmelCase      :      Optional[int]   					=					{
    """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,
}
__UpperCAmelCase      :      List[Any]   					=					ClassInstantier(ACTaCLS)
def      A      (			_A							):




     """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
__UpperCAmelCase      :      List[Any]   					=					get_activation('gelu_python')
__UpperCAmelCase      :      Tuple   					=					get_activation('gelu_new')
__UpperCAmelCase      :      List[Any]   					=					get_activation('gelu')
__UpperCAmelCase      :      Optional[Any]   					=					get_activation('gelu_fast')
__UpperCAmelCase      :      Optional[int]   					=					get_activation('quick_gelu')
__UpperCAmelCase      :      str   					=					get_activation('silu')
__UpperCAmelCase      :      Any   					=					get_activation('mish')
__UpperCAmelCase      :      str   					=					get_activation('linear')






"""simple docstring"""






def      A      (			_A,     _A = 1_000							):




					"""simple docstring"""
					if (not isinstance(_A,     _A							)) or (digit_position <= 0):
										raise ValueError("Digit position must be a positive integer"							)
					elif (not isinstance(_A,     _A							)) or (precision < 0):
										raise ValueError("Precision must be a nonnegative integer"							)

					# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
					# accurate
					snake_case_						:List[Any]								=						(
					    4 * _subsum(_A,     1,     _A							)
					    - 2 * _subsum(_A,     4,     _A							)
					    - _subsum(_A,     5,     _A							)
					    - _subsum(_A,     6,     _A							)
					)

					# return the first hex digit of the fractional part of the result
					return hex(int((sum_result % 1) * 16							)							)[2:]
def      A      (			_A,     _A,     _A							):




					"""simple docstring"""
					snake_case_						:int								=						0.0
					for sum_index in range(digit_pos_to_extract + precision							):
										snake_case_						:Optional[Any]								=						8 * sum_index + denominator_addend
										if sum_index < digit_pos_to_extract:
															# if the exponential term is an integer and we mod it by the denominator
															# before dividing, only the integer part of the sum will change;
															# the fractional part will not
															snake_case_						:List[Any]								=						pow(
															    16,     digit_pos_to_extract - 1 - sum_index,     _A							)
										else:
															snake_case_						:Optional[Any]								=						pow(16,     digit_pos_to_extract - 1 - sum_index							)
										total += exponential_term / denominator
					return total


if __name__ == "__main__":
				import doctest

				doctest.testmod()

