oneflow
oneflow copied to clipboard
Add lr_scheduler: Cyclilc LR and OneCycle LR
背景:espnet2需求 概述:增加了缺失的两个 Learning Rate Scheduler:Cyclic LR 和 OneCycle LR 注意事项: 在 torch 中,这两个 scheduler 有如下特点:
- 输入有
lr和momentum相关参数,会在构造函数中覆盖掉optimizer.param_group的状态,比如momentum、initial_lr等 - 输入
lr时,支持输入 list,该 list 和 optimizer 的param_groups长度相同,相当于为每一个optimizer.param_groups“定制”一个lr,然后在get_lr()时会把输入的 lr list 和optimizer.param_groups用zip()合在一起,遍历所有optimizer.param_groups,返回一个等长的 lr list - 支持在 step 时更新 optimizer 的
momentum
这些特点导致的后果是:
- 上面第一点的问题是,模型 resume 可能会受到影响
- 上面第二点的问题是,oneflow 的设计是
get_lr()只能计算一个 lr 值,在父类的step函数中遍历optimizer.param_groups来更新 lr。这里为了支持输入 lr list,只能在子类中重写step函数来覆盖父类的step。 - 上面第三点的问题是,请教文骁之后,得知 oneflow 的 scheduler 在 graph 中,是不能拿到和修改 optimizer 的状态的。所以这两个 scheduler 在 graph 模式下,不支持动态修改 optimizer 的
momentum,这里在_generate_conf_for_graph中加了对应判断和抛出异常。
torch 实现: https://github.com/pytorch/pytorch/blob/0ec19db7ac88e307135100ddcfc418ae3925844f/torch/optim/lr_scheduler.py#L1080 https://github.com/pytorch/pytorch/blob/0ec19db7ac88e307135100ddcfc418ae3925844f/torch/optim/lr_scheduler.py#L1430
Code got formatted by CI. Please request CI again if you still want to have this PR merged. If the PR is from a forked repo, please download the patch files from the GitHub Actions web page and apply them locally.
Code got formatted by CI. Please request CI again if you still want to have this PR merged. If the PR is from a forked repo, please download the patch files from the GitHub Actions web page and apply them locally.
Speed stats:
Code got formatted by CI. Please request CI again if you still want to have this PR merged. If the PR is from a forked repo, please download the patch files from the GitHub Actions web page and apply them locally.