



import math
from collections import OrderedDict

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

from .utils import logging


A__						:						int          =      logging.get_logger(__name__)



class        __snake_case		(  nn.Module		):




       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      UpperCAmelCase__    ( self  : Dict       ,       A_  : Tensor):
         return nn.functional.gelu(A_       ,       approximate='''tanh''')





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : Optional[Any]       ,       A_  : Tensor):
         return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (input + 0.04_4715 * torch.pow(A_       ,       3.0))))





class        __snake_case		(  nn.Module		):




       def __init__( self  : Union[str, Any]       ,       A_  : bool = False):
         super().__init__()
         if use_gelu_python:
           lowerCAmelCase_    :  Tuple             =				self._gelu_python
         else:
           lowerCAmelCase_    :  Optional[int]             =				nn.functional.gelu




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







       def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : Tensor):
         return self.act(A_)





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : Tensor):
         return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)))





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : int       ,       A_  : Tensor):
         return input * torch.sigmoid(1.702 * input)





class        __snake_case		(  nn.Module		):




       def __init__( self  : List[Any]       ,       A_  : float       ,       A_  : float):
         if min > max:
           raise ValueError(F"""min should be < max (got min: {min}, max: {max})""")

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







       def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tensor):
         return torch.clip(gelu(A_)       ,       self.min       ,       self.max)





class        __snake_case		(  nn.Module		):




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







       def      UpperCAmelCase__    ( self  : str       ,       A_  : Tensor):
         return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(A_       ,       3))))





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tensor):
         return nn.functional.silu(A_)





class        __snake_case		(  nn.Module		):




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




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







       def      UpperCAmelCase__    ( self  : Tuple       ,       A_  : Tensor):
         return self.act(A_)





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : int       ,       A_  : Tensor):
         return input





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tuple       ,       A_  : Optional[Any]=0.70_7107       ,       A_  : Tuple=0.28_2095):
         lowerCAmelCase_    :  int             =				(input - mu).div(sigma * math.sqrt(2.0))
         return 0.5 * (1.0 + torch.erf(A_))





class        __snake_case		(  nn.Module		):




       def      UpperCAmelCase__    ( self  : Tuple       ,       A_  : List[Any]):
         lowerCAmelCase_    :  Optional[Any]             =				nn.functional.relu(A_)
         lowerCAmelCase_    :  Optional[int]             =				torch.square(A_)
         return squared





class        __snake_case		(  UpperCamelCase_		):




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





A__						:						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,
}
A__						:						int          =      ClassInstantier(ACTaCLS)


def     UpperCamelCase(						__UpperCamelCase							:	Union[str, Any]				):
  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__						:						Optional[int]          =      get_activation('''gelu_python''')
A__						:						Tuple          =      get_activation('''gelu_new''')
A__						:						List[str]          =      get_activation('''gelu''')
A__						:						Any          =      get_activation('''gelu_fast''')
A__						:						Dict          =      get_activation('''quick_gelu''')
A__						:						List[str]          =      get_activation('''silu''')
A__						:						Any          =      get_activation('''mish''')





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










from typing import TYPE_CHECKING

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


A__						:						List[str]          =      {
    '''configuration_segformer''': ['''SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SegformerConfig''', '''SegformerOnnxConfig''']
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__						:						int          =      ['''SegformerFeatureExtractor''']
						A__						:						Dict          =      ['''SegformerImageProcessor''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__						:						Any          =      [
						    '''SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''SegformerDecodeHead''',
						    '''SegformerForImageClassification''',
						    '''SegformerForSemanticSegmentation''',
						    '''SegformerLayer''',
						    '''SegformerModel''',
						    '''SegformerPreTrainedModel''',
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__						:						List[Any]          =      [
						    '''TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TFSegformerDecodeHead''',
						    '''TFSegformerForImageClassification''',
						    '''TFSegformerForSemanticSegmentation''',
						    '''TFSegformerModel''',
						    '''TFSegformerPreTrainedModel''',
						]


if TYPE_CHECKING:
						from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_segformer import SegformerFeatureExtractor
												from .image_processing_segformer import SegformerImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_segformer import (
												    SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    SegformerDecodeHead,
												    SegformerForImageClassification,
												    SegformerForSemanticSegmentation,
												    SegformerLayer,
												    SegformerModel,
												    SegformerPreTrainedModel,
												)
						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_segformer import (
												    TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFSegformerDecodeHead,
												    TFSegformerForImageClassification,
												    TFSegformerForSemanticSegmentation,
												    TFSegformerModel,
												    TFSegformerPreTrainedModel,
												)

else:
						import sys

						A__						:						Dict          =      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





