oneflow icon indicating copy to clipboard operation
oneflow copied to clipboard

Add lr_scheduler: Cyclilc LR and OneCycle LR

Open marigoold opened this issue 3 years ago • 4 comments
trafficstars

背景:espnet2需求 概述:增加了缺失的两个 Learning Rate Scheduler:Cyclic LR 和 OneCycle LR 注意事项: 在 torch 中,这两个 scheduler 有如下特点:

  • 输入有 lrmomentum 相关参数,会在构造函数中覆盖掉 optimizer.param_group 的状态,比如 momentuminitial_lr
  • 输入 lr 时,支持输入 list,该 list 和 optimizer 的 param_groups 长度相同,相当于为每一个 optimizer.param_groups “定制”一个 lr,然后在 get_lr() 时会把输入的 lr list 和 optimizer.param_groupszip() 合在一起,遍历所有 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

marigoold avatar Sep 16 '22 00:09 marigoold

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.

github-actions[bot] avatar Sep 16 '22 08:09 github-actions[bot]

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.

github-actions[bot] avatar Sep 16 '22 08:09 github-actions[bot]

Speed stats:

github-actions[bot] avatar Sep 16 '22 10:09 github-actions[bot]

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.

github-actions[bot] avatar Sep 16 '22 12:09 github-actions[bot]