我的实验时基于huggingface/diffusers开发的,使用了pytorch_lightning,使用了AMP以及deepspeed zero stage2,初次实验未使用scale learning rate策略,正常运行,没有报错,但是使用了scale_lr之后,报错
Exception: Current loss scale already at minimum - cannot decrease scale anymore. Exiting run.
在网上搜了很多,有用的信息有:
- 主要思路是skip这个exception,可以追溯的codes: https://gitlab.cl.uni-heidelberg.de/will/fairseq/-/commit/03ef3ab8ce7575ff4501872353dec503dceb199d#313a3e8165b59023666f532b7b5d5844b29e9e90
对应的修改文件
deepspeed.runtime.fp16.loss_scaler - 这个错误可能是某些bad batch导致的,有人提供了这样的思路,很遗憾不支持FP16 :https://github.com/Lightning-AI/lightning/issues/4956
- 还有人提出将fp16改成bf16(没测试)
- 观察梯度变化
from lightning.pytorch.utilities import grad_norm
def on_before_optimizer_step(self, optimizer):
# Compute the 2-norm for each layer
# If using mixed precision, the gradients are already unscaled here
norms = grad_norm(self.layer, norm_type=2)
self.log_dict(norms)
看是否出现梯度爆炸的情况,出现的话使用梯度clip的形式处理