You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.2 KiB
157 lines
5.2 KiB
4 years ago
|
""" Activation Factory
|
||
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
|
"""
|
||
4 years ago
|
from typing import Union, Callable, Type
|
||
|
|
||
5 years ago
|
from .activations import *
|
||
|
from .activations_jit import *
|
||
|
from .activations_me import *
|
||
|
from .config import is_exportable, is_scriptable, is_no_jit
|
||
|
|
||
4 years ago
|
# PyTorch has an optimized, native 'silu' (aka 'swish') operator as of PyTorch 1.7.
|
||
|
# Also hardsigmoid, hardswish, and soon mish. This code will use native version if present.
|
||
|
# Eventually, the custom SiLU, Mish, Hard*, layers will be removed and only native variants will be used.
|
||
4 years ago
|
_has_silu = 'silu' in dir(torch.nn.functional)
|
||
4 years ago
|
_has_hardswish = 'hardswish' in dir(torch.nn.functional)
|
||
|
_has_hardsigmoid = 'hardsigmoid' in dir(torch.nn.functional)
|
||
|
_has_mish = 'mish' in dir(torch.nn.functional)
|
||
|
|
||
5 years ago
|
|
||
|
_ACT_FN_DEFAULT = dict(
|
||
4 years ago
|
silu=F.silu if _has_silu else swish,
|
||
|
swish=F.silu if _has_silu else swish,
|
||
4 years ago
|
mish=F.mish if _has_mish else mish,
|
||
5 years ago
|
relu=F.relu,
|
||
|
relu6=F.relu6,
|
||
5 years ago
|
leaky_relu=F.leaky_relu,
|
||
|
elu=F.elu,
|
||
|
celu=F.celu,
|
||
|
selu=F.selu,
|
||
4 years ago
|
gelu=gelu,
|
||
2 years ago
|
gelu_tanh=gelu_tanh,
|
||
5 years ago
|
sigmoid=sigmoid,
|
||
|
tanh=tanh,
|
||
4 years ago
|
hard_sigmoid=F.hardsigmoid if _has_hardsigmoid else hard_sigmoid,
|
||
|
hard_swish=F.hardswish if _has_hardswish else hard_swish,
|
||
5 years ago
|
hard_mish=hard_mish,
|
||
|
)
|
||
|
|
||
|
_ACT_FN_JIT = dict(
|
||
4 years ago
|
silu=F.silu if _has_silu else swish_jit,
|
||
|
swish=F.silu if _has_silu else swish_jit,
|
||
4 years ago
|
mish=F.mish if _has_mish else mish_jit,
|
||
|
hard_sigmoid=F.hardsigmoid if _has_hardsigmoid else hard_sigmoid_jit,
|
||
|
hard_swish=F.hardswish if _has_hardswish else hard_swish_jit,
|
||
5 years ago
|
hard_mish=hard_mish_jit
|
||
|
)
|
||
|
|
||
|
_ACT_FN_ME = dict(
|
||
4 years ago
|
silu=F.silu if _has_silu else swish_me,
|
||
|
swish=F.silu if _has_silu else swish_me,
|
||
4 years ago
|
mish=F.mish if _has_mish else mish_me,
|
||
|
hard_sigmoid=F.hardsigmoid if _has_hardsigmoid else hard_sigmoid_me,
|
||
|
hard_swish=F.hardswish if _has_hardswish else hard_swish_me,
|
||
5 years ago
|
hard_mish=hard_mish_me,
|
||
|
)
|
||
|
|
||
4 years ago
|
_ACT_FNS = (_ACT_FN_ME, _ACT_FN_JIT, _ACT_FN_DEFAULT)
|
||
|
for a in _ACT_FNS:
|
||
|
a.setdefault('hardsigmoid', a.get('hard_sigmoid'))
|
||
|
a.setdefault('hardswish', a.get('hard_swish'))
|
||
|
|
||
|
|
||
5 years ago
|
_ACT_LAYER_DEFAULT = dict(
|
||
4 years ago
|
silu=nn.SiLU if _has_silu else Swish,
|
||
|
swish=nn.SiLU if _has_silu else Swish,
|
||
4 years ago
|
mish=nn.Mish if _has_mish else Mish,
|
||
5 years ago
|
relu=nn.ReLU,
|
||
|
relu6=nn.ReLU6,
|
||
4 years ago
|
leaky_relu=nn.LeakyReLU,
|
||
5 years ago
|
elu=nn.ELU,
|
||
4 years ago
|
prelu=PReLU,
|
||
5 years ago
|
celu=nn.CELU,
|
||
|
selu=nn.SELU,
|
||
4 years ago
|
gelu=GELU,
|
||
2 years ago
|
gelu_tanh=GELUTanh,
|
||
5 years ago
|
sigmoid=Sigmoid,
|
||
|
tanh=Tanh,
|
||
4 years ago
|
hard_sigmoid=nn.Hardsigmoid if _has_hardsigmoid else HardSigmoid,
|
||
|
hard_swish=nn.Hardswish if _has_hardswish else HardSwish,
|
||
5 years ago
|
hard_mish=HardMish,
|
||
|
)
|
||
|
|
||
|
_ACT_LAYER_JIT = dict(
|
||
4 years ago
|
silu=nn.SiLU if _has_silu else SwishJit,
|
||
|
swish=nn.SiLU if _has_silu else SwishJit,
|
||
4 years ago
|
mish=nn.Mish if _has_mish else MishJit,
|
||
|
hard_sigmoid=nn.Hardsigmoid if _has_hardsigmoid else HardSigmoidJit,
|
||
|
hard_swish=nn.Hardswish if _has_hardswish else HardSwishJit,
|
||
5 years ago
|
hard_mish=HardMishJit
|
||
|
)
|
||
|
|
||
|
_ACT_LAYER_ME = dict(
|
||
4 years ago
|
silu=nn.SiLU if _has_silu else SwishMe,
|
||
|
swish=nn.SiLU if _has_silu else SwishMe,
|
||
4 years ago
|
mish=nn.Mish if _has_mish else MishMe,
|
||
|
hard_sigmoid=nn.Hardsigmoid if _has_hardsigmoid else HardSigmoidMe,
|
||
|
hard_swish=nn.Hardswish if _has_hardswish else HardSwishMe,
|
||
5 years ago
|
hard_mish=HardMishMe,
|
||
|
)
|
||
|
|
||
4 years ago
|
_ACT_LAYERS = (_ACT_LAYER_ME, _ACT_LAYER_JIT, _ACT_LAYER_DEFAULT)
|
||
|
for a in _ACT_LAYERS:
|
||
|
a.setdefault('hardsigmoid', a.get('hard_sigmoid'))
|
||
|
a.setdefault('hardswish', a.get('hard_swish'))
|
||
|
|
||
5 years ago
|
|
||
4 years ago
|
def get_act_fn(name: Union[Callable, str] = 'relu'):
|
||
5 years ago
|
""" Activation Function Factory
|
||
|
Fetching activation fns by name with this function allows export or torch script friendly
|
||
|
functions to be returned dynamically based on current config.
|
||
|
"""
|
||
|
if not name:
|
||
|
return None
|
||
4 years ago
|
if isinstance(name, Callable):
|
||
|
return name
|
||
5 years ago
|
if not (is_no_jit() or is_exportable() or is_scriptable()):
|
||
|
# If not exporting or scripting the model, first look for a memory-efficient version with
|
||
|
# custom autograd, then fallback
|
||
|
if name in _ACT_FN_ME:
|
||
|
return _ACT_FN_ME[name]
|
||
4 years ago
|
if not (is_no_jit() or is_exportable()):
|
||
5 years ago
|
if name in _ACT_FN_JIT:
|
||
|
return _ACT_FN_JIT[name]
|
||
|
return _ACT_FN_DEFAULT[name]
|
||
|
|
||
|
|
||
4 years ago
|
def get_act_layer(name: Union[Type[nn.Module], str] = 'relu'):
|
||
5 years ago
|
""" Activation Layer Factory
|
||
|
Fetching activation layers by name with this function allows export or torch script friendly
|
||
|
functions to be returned dynamically based on current config.
|
||
|
"""
|
||
|
if not name:
|
||
|
return None
|
||
3 years ago
|
if not isinstance(name, str):
|
||
|
# callable, module, etc
|
||
4 years ago
|
return name
|
||
5 years ago
|
if not (is_no_jit() or is_exportable() or is_scriptable()):
|
||
|
if name in _ACT_LAYER_ME:
|
||
|
return _ACT_LAYER_ME[name]
|
||
4 years ago
|
if not (is_no_jit() or is_exportable()):
|
||
5 years ago
|
if name in _ACT_LAYER_JIT:
|
||
|
return _ACT_LAYER_JIT[name]
|
||
|
return _ACT_LAYER_DEFAULT[name]
|
||
|
|
||
|
|
||
4 years ago
|
def create_act_layer(name: Union[nn.Module, str], inplace=None, **kwargs):
|
||
5 years ago
|
act_layer = get_act_layer(name)
|
||
4 years ago
|
if act_layer is None:
|
||
5 years ago
|
return None
|
||
2 years ago
|
if inplace is None:
|
||
|
return act_layer(**kwargs)
|
||
|
try:
|
||
|
return act_layer(inplace=inplace, **kwargs)
|
||
|
except TypeError:
|
||
|
# recover if act layer doesn't have inplace arg
|
||
|
return act_layer(**kwargs)
|