
'''simple docstring'''


import math
from collections import OrderedDict

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

from .utils import logging


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

class 		__lowercase			(					nn.Module      ):

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

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi ) * (input + 0.04_47_15 * torch.pow(A     ,				3.0 )) ))

class 		__lowercase			(					nn.Module      ):

			def __init__(self     ,				A = False ):
								super().__init__()
								if use_gelu_python:
													lowerCamelCase_   : str       =						self._gelu_python
								else:
													lowerCamelCase_   : Optional[Any]       =						nn.functional.gelu

			def     UpperCAmelCase__	(self     ,				A ):
								return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0 ) ))






			def     UpperCAmelCase__	(self     ,				A ):
								return self.act(A )

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input) ))

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								return input * torch.sigmoid(1.7_02 * input )

class 		__lowercase			(					nn.Module      ):

			def __init__(self     ,				A     ,				A ):
								if min > max:
													raise ValueError(F"""min should be < max (got min: {min}, max: {max})""" )

								super().__init__()
								lowerCamelCase_   : Optional[int]       =						min
								lowerCamelCase_   : List[str]       =						max






			def     UpperCAmelCase__	(self     ,				A ):
								return torch.clip(gelu(A )     ,				self.min     ,				self.max )

class 		__lowercase			(					nn.Module      ):

			def __init__(self ):
								super().__init__()
								lowerCamelCase_   : List[str]       =						math.sqrt(2 / math.pi )






			def     UpperCAmelCase__	(self     ,				A ):
								return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(A     ,				3 )) ))

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								return nn.functional.silu(A )

class 		__lowercase			(					nn.Module      ):

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

			def     UpperCAmelCase__	(self     ,				A ):
								return input * torch.tanh(nn.functional.softplus(A ) )






			def     UpperCAmelCase__	(self     ,				A ):
								return self.act(A )

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								return input

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A     ,				A=0.70_71_07     ,				A=0.28_20_95 ):
								lowerCamelCase_   : Tuple       =						(input - mu).div(sigma * math.sqrt(2.0 ) )
								return 0.5 * (1.0 + torch.erf(A ))

class 		__lowercase			(					nn.Module      ):

			def     UpperCAmelCase__	(self     ,				A ):
								lowerCamelCase_   : Tuple       =						nn.functional.relu(A )
								lowerCamelCase_   : Tuple       =						torch.square(A )
								return squared

class 		__lowercase			(					_lowercase      ):

			def __getitem__(self     ,				A ):
								lowerCamelCase_   : Optional[Any]       =						super().__getitem__(A )
								lowerCamelCase_, lowerCamelCase_   : Tuple       =						content if isinstance(A     ,				A ) else (content, {})
								return cls(**A )
__lowercase							:				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,
}
__lowercase							:				Union[str, Any]					=       ClassInstantier(ACTaCLS)




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






__lowercase							:				str					=       get_activation('''linear''')




'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


__lowercase							:				List[str]					=       {
    '''configuration_sam''': [
        '''SAM_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''SamConfig''',
        '''SamMaskDecoderConfig''',
        '''SamPromptEncoderConfig''',
        '''SamVisionConfig''',
    ],
    '''processing_sam''': ['''SamProcessor'''],
}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				List[Any]					=       [
					    '''SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''SamModel''',
					    '''SamPreTrainedModel''',
					]
try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				int					=       [
					    '''TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFSamModel''',
					    '''TFSamPreTrainedModel''',
					]
try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				str					=       ['''SamImageProcessor''']


if TYPE_CHECKING:
					from .configuration_sam import (
					    SAM_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    SamConfig,
					    SamMaskDecoderConfig,
					    SamPromptEncoderConfig,
					    SamVisionConfig,
					)
					from .processing_sam import SamProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_sam import SAM_PRETRAINED_MODEL_ARCHIVE_LIST, SamModel, SamPreTrainedModel

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_sam import TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST, TFSamModel, TFSamPreTrainedModel

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_sam import SamImageProcessor

else:
					import sys

					__lowercase							:				List[str]					=       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


