-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path__init__.py
58 lines (52 loc) · 2.13 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import absolute_import
from functools import partial
from .lr_finder import LRFinder
from .adamw import AdamW as AdamW_my
from .radam import RAdam, PlainRAdam
from .sgdw import SGDW
from .schedulers import LinearLR, ExponentialLR
from .rmsprop import RMSprop
from .lookahead import Lookahead
from torch import optim
try:
from apex.optimizers import FusedSGD
from apex.optimizers import FusedAdam
from apex.optimizers import FusedNovoGrad
HAS_APEX = True
except ModuleNotFoundError:
HAS_APEX = False
# 2e-5 is the lowest epsilon than saves from overflow in fp16
def optimizer_from_name(optim_name):
optim_name = optim_name.lower()
if optim_name == "sgd":
return optim.SGD
elif optim_name == "sgdw":
return SGDW
elif optim_name == "adam":
return partial(optim.Adam, eps=2e-5)
elif optim_name == "adamw":
return partial(AdamW_my, eps=2e-5)
elif optim_name == "adamw_gc":
# in this implementation eps in inside sqrt so it can be smaller
return partial(AdamW_my, center=True, eps=1e-7)
# this optimizer uses decoupled wd by default. so add more aliasses just in case
elif optim_name in ("adamp", "adam_p", "adamwp", "adamw_p"):
from adamp import AdamP
return partial(AdamP, eps=2e-5)
# this optimizer uses decoupled wd by default. so add more aliasses just in case
elif optim_name in ("sgdp", "sgd_p", "sgdwp", "sgdw_p"):
from adamp import SGDP
return partial(SGDP, eps=2e-5)
elif optim_name == "rmsprop":
# in this implementation eps in inside sqrt so it can be smaller
return partial(RMSprop, eps=1e-7)
elif optim_name == "radam":
return partial(RAdam, eps=2e-5)
elif optim_name in ["fused_sgd", "fusedsgd"] and HAS_APEX:
return FusedSGD
elif optim_name in ["fused_adam", "fusedadam"] and HAS_APEX:
return partial(FusedAdam, eps=2e-5)
elif optim_name in ["fused_novograd", "fusednovograd", "novograd"] and HAS_APEX:
return partial(FusedNovoGrad, eps=2e-5)
else:
raise ValueError(f"Optimizer {optim_name} not found or apex is not installed.")