Browse Source

Merge pull request #896 from Vincent-Huang-2000/fix/resume-ckpt-fp32

[fix] keep resume checkpoint weights in fp32
jingyaogong 4 weeks ago
parent
commit
7ea219febb
1 changed files with 3 additions and 3 deletions
  1. 3 3
      trainer/trainer_utils.py

+ 3 - 3
trainer/trainer_utils.py

@@ -71,10 +71,10 @@ def lm_checkpoint(lm_config, weight='full_sft', model=None, optimizer=None, epoc
     if model is not None:
     if model is not None:
         raw_model = model.module if isinstance(model, DistributedDataParallel) else model
         raw_model = model.module if isinstance(model, DistributedDataParallel) else model
         raw_model = getattr(raw_model, '_orig_mod', raw_model)
         raw_model = getattr(raw_model, '_orig_mod', raw_model)
-        state_dict = raw_model.state_dict()
-        state_dict = {k: v.half().cpu() for k, v in state_dict.items()}
+        # 续训权重保持fp32(fp16会抹掉小于半个ulp的累计更新),推理权重另存fp16;先.cpu()避免显存增长(#559)
+        state_dict = {k: v.cpu() for k, v in raw_model.state_dict().items()}
         ckp_tmp = ckp_path + '.tmp'
         ckp_tmp = ckp_path + '.tmp'
-        torch.save(state_dict, ckp_tmp)
+        torch.save({k: v.half() for k, v in state_dict.items()}, ckp_tmp)
         os.replace(ckp_tmp, ckp_path)
         os.replace(ckp_tmp, ckp_path)
         wandb_id = None
         wandb_id = None
         if wandb:
         if wandb: