Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optimizers #266

Merged
merged 5 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions flagai/optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ def get_optimizer(param_groups,
lr=lr,
relative_step=False,
warmup_init=False)
elif optimizer == 'adamw':
from torch.optim import AdamW
optimizer = AdamW(param_groups,
lr=lr,
weight_decay=weight_decay,
betas=(adam_beta1, adam_beta2),
eps=adam_eps)
elif optimizer == 'lion':
from lion_pytorch import Lion
optimizer = Lion(param_groups,
lr=lr,
weight_decay=weight_decay,
betas=(adam_beta1, adam_beta2)
)
elif optimizer == 'adan':
from adan import Adan
optimizer = Adan(param_groups,
lr=lr,
weight_decay=weight_decay,
betas=(adam_beta1, adam_beta2, 0.99),
eps=adam_eps)
elif optimizer == 'lamb':
from torch_optimizer import Lamb
optimizer = Lamb(param_groups,
lr=lr,
weight_decay=weight_decay,
betas=(adam_beta1, adam_beta2),
eps=adam_eps)
else:
raise NotImplementedError

Expand Down
5 changes: 4 additions & 1 deletion flagai/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def __init__(
deepspeed_config=None,
model_parallel_size=1,
training_script="train.py",
optimizer_type='adam',
):

if timers is not None:
Expand All @@ -185,6 +186,8 @@ def __init__(
self.eval_interval = eval_interval
self.tokenizer = tokenizer

self.optimizer_type = optimizer_type

# model checkpointing
self.save_dir = save_dir
self.save_interval = save_interval
Expand Down Expand Up @@ -471,7 +474,7 @@ def train(self,
cpu_optimizer=False,
cpu_torch_adam=False,
fp16=self.fp16,
optimizer='adam') # if not self.fp16 else 'adafactor')
optimizer=self.optimizer_type) # if not self.fp16 else 'adafactor')

if lr_scheduler == None and optimizer != None and self.warm_up > 0 and 'deepspeed' not in self.env_type and self.epochs > 0:
if self.env_type == 'bmtrain':
Expand Down