

'''simple docstring'''

import math
from collections import OrderedDict

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

from .utils import logging


UpperCAmelCase							=					logging.get_logger(__name__)


class 					lowerCAmelCase       ( nn.Module				):







       def __init__(							self     :	int ):







            """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      snake_case				(							self     :	Any  , __lowercase     :	Tensor ):







            """simple docstring"""







            return nn.functional.gelu(__lowercase  , approximate='tanh' )






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	Dict  , __lowercase     :	Tensor ):







            """simple docstring"""







            return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.0_4_4_7_1_5 * torch.pow(__lowercase  , 3.0 )) ))






class 					lowerCAmelCase       ( nn.Module				):







       def __init__(							self     :	Optional[int]  , __lowercase     :	bool = False ):







            """simple docstring"""







            super().__init__()
            if use_gelu_python:
                 __lowercase     						=self._gelu_python
            else:
                 __lowercase     						=nn.functional.gelu







       def      snake_case				(							self     :	Optional[int]  , __lowercase     :	Tensor ):







            """simple docstring"""







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







       def      snake_case				(							self     :	List[str]  , __lowercase     :	Tensor ):







            """simple docstring"""







            return self.act(__lowercase )






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	Dict  , __lowercase     :	Tensor ):







            """simple docstring"""







            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 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	Optional[Any]  , __lowercase     :	Tensor ):







            """simple docstring"""







            return input * torch.sigmoid(1.7_0_2 * input )






class 					lowerCAmelCase       ( nn.Module				):







       def __init__(							self     :	Any  , __lowercase     :	float  , __lowercase     :	float ):







            """simple docstring"""







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

            super().__init__()
            __lowercase     						=min
            __lowercase     						=max







       def      snake_case				(							self     :	Optional[int]  , __lowercase     :	Tensor ):







            """simple docstring"""







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






class 					lowerCAmelCase       ( nn.Module				):







       def __init__(							self     :	Union[str, Any] ):







            """simple docstring"""







            super().__init__()
            __lowercase     						=math.sqrt(2 / math.pi )







       def      snake_case				(							self     :	Union[str, Any]  , __lowercase     :	Tensor ):







            """simple docstring"""







            return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.0_4_4_7_1_5 * torch.pow(__lowercase  , 3 )) ))






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	Union[str, Any]  , __lowercase     :	Tensor ):







            """simple docstring"""







            return nn.functional.silu(__lowercase )






class 					lowerCAmelCase       ( nn.Module				):







       def __init__(							self     :	Optional[int] ):







            """simple docstring"""







            super().__init__()
            if version.parse(torch.__version__ ) < version.parse('1.9.0' ):
                 __lowercase     						=self._mish_python
            else:
                 __lowercase     						=nn.functional.mish







       def      snake_case				(							self     :	Dict  , __lowercase     :	Tensor ):







            """simple docstring"""







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







       def      snake_case				(							self     :	Tuple  , __lowercase     :	Tensor ):







            """simple docstring"""







            return self.act(__lowercase )






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	List[str]  , __lowercase     :	Tensor ):







            """simple docstring"""







            return input






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	List[Any]  , __lowercase     :	str  , __lowercase     :	Any=0.7_0_7_1_0_7  , __lowercase     :	Union[str, Any]=0.2_8_2_0_9_5 ):







            """simple docstring"""







            __lowercase     						=(input - mu).div(sigma * math.sqrt(2.0 ) )
            return 0.5 * (1.0 + torch.erf(__lowercase ))






class 					lowerCAmelCase       ( nn.Module				):







       def      snake_case				(							self     :	List[str]  , __lowercase     :	List[str] ):







            """simple docstring"""







            __lowercase     						=nn.functional.relu(__lowercase )
            __lowercase     						=torch.square(__lowercase )
            return squared






class 					lowerCAmelCase       ( A				):







       def __getitem__(							self     :	Any  , __lowercase     :	str ):







            """simple docstring"""







            __lowercase     						=super().__getitem__(__lowercase )
            __lowercase						,       __lowercase     						=content if isinstance(__lowercase  , __lowercase ) else (content, {})
            return cls(**__lowercase )






UpperCAmelCase							=					{
    '''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							=					ClassInstantier(ACTaCLS)

def 			__UpperCamelCase							(		lowercase__  :					Optional[int]	):






     '''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							=					get_activation('''gelu_python''')
UpperCAmelCase							=					get_activation('''gelu_new''')
UpperCAmelCase							=					get_activation('''gelu''')
UpperCAmelCase							=					get_activation('''gelu_fast''')
UpperCAmelCase							=					get_activation('''quick_gelu''')
UpperCAmelCase							=					get_activation('''silu''')
UpperCAmelCase							=					get_activation('''mish''')







UpperCAmelCase							=					get_activation('''linear''')





'''simple docstring'''

from __future__ import annotations

def 			__UpperCamelCase							(		lowercase__  :					list[int]	):






     '''simple docstring'''





     if not list_of_ints:
          return []

     if min(lowercase__	) < 0:
          raise ValueError('All numbers must be positive'	)

     __lowercase     						=max(len(bin(lowercase__	)[2:]	) for x in list_of_ints	)
     return _msd_radix_sort(lowercase__,   lowercase__	)

def 			__UpperCamelCase							(		lowercase__  :					list[int],   lowercase__  :					int	):






     '''simple docstring'''





     if bit_position == 0 or len(lowercase__	) in [0, 1]:
          return list_of_ints

     __lowercase     						=[]
     __lowercase     						=[]
     # Split numbers based on bit at bit_position from the right
     for number in list_of_ints:
          if (number >> (bit_position - 1)) & 1:
               # number has a one at bit bit_position
               ones.append(lowercase__	)
          else:
               # number has a zero at bit bit_position
               zeros.append(lowercase__	)

    # recursively split both lists further
     __lowercase     						=_msd_radix_sort(lowercase__,   bit_position - 1	)
     __lowercase     						=_msd_radix_sort(lowercase__,   bit_position - 1	)

     # recombine lists
     __lowercase     						=zeros
     res.extend(lowercase__	)

     return res

def 			__UpperCamelCase							(		lowercase__  :					list[int]	):






     '''simple docstring'''





     __lowercase     						=len(lowercase__	)
     if not list_of_ints or length == 1:
          return

     if min(lowercase__	) < 0:
          raise ValueError('All numbers must be positive'	)

     __lowercase     						=max(len(bin(lowercase__	)[2:]	) for x in list_of_ints	)
     _msd_radix_sort_inplace(lowercase__,   lowercase__,   0,   lowercase__	)

def 			__UpperCamelCase							(		lowercase__  :					list[int],   lowercase__  :					int,   lowercase__  :					int,   lowercase__  :					int	):






     '''simple docstring'''





     if bit_position == 0 or end_index - begin_index <= 1:
          return

     bit_position -= 1

     __lowercase     						=begin_index
     __lowercase     						=end_index - 1
     while i <= j:
          __lowercase     						=False
          if not (list_of_ints[i] >> bit_position) & 1:
               # found zero at the beginning
               i += 1
               __lowercase     						=True
          if (list_of_ints[j] >> bit_position) & 1:
               # found one at the end
               j -= 1
               __lowercase     						=True

          if changed:
               continue

          __lowercase						,       __lowercase     						=list_of_ints[j], list_of_ints[i]
          j -= 1
          if j != i:
               i += 1

     _msd_radix_sort_inplace(lowercase__,   lowercase__,   lowercase__,   lowercase__	)
     _msd_radix_sort_inplace(lowercase__,   lowercase__,   lowercase__,   lowercase__	)


if __name__ == "__main__":
       import doctest

       doctest.testmod()


