Repository navigation
Preserve BF16 optimizer states when resuming checkpoints - #4615
Conversation
tianyu-l
left a comment
There was a problem hiding this comment.
hmm ... sounds correct & improper at the same time
| if len(state) == 0: | ||
| state["step"] = ( | ||
| torch.zeros((), dtype=torch.float32, device=p.device) | ||
| if group.get("capturable") or group.get("fused") |
There was a problem hiding this comment.
what is "capturable" and what is "fused"?
| ``load_state_dict`` casts floating-point states to the parameter dtype. | ||
| """ | ||
|
|
||
| def _bf16_state_init_hook( |
|
@claude review this PR and give feedback on my comments |
|
Claude finished @tianyu-l's task in 3m 22s —— View job PR Review: Preserve BF16 optimizer states when resuming checkpointsI read Re: line 345 — "why we had this function... does torch not allow specifying state dtype at construction?"Correct, that's exactly it. So the only lever available is to pre-seed Re: line 356 — "what is
|
With FP32 parameters and
fused_opt_states_bf16, resuming a checkpoint silently promotes Adam/AdamW moments to FP32: the native optimizer loader casts states to the parameter dtype, and the step pre-hook skips nonempty state. This doubles the persistent memory for the two moment buffers and changes subsequent updates because the moments are no longer rounded back to BF16.Register a load-state post-hook for the BF16 implementation to restore
exp_avg,exp_avg_sq, and optional AMSGradmax_exp_avg_sqto BF16. Preserve the native policy forstepand other state, and keep the generic checkpoint helpers optimizer-agnostic. Update the checkpointing documentation to describe the behavior.The post-hook fixes persistent state and continuation behavior; the native loader can still allocate temporary FP32 state during loading.
Local validation on PyTorch 2.13.0+cu130, NVIDIA RTX A1000 Laptop GPU:
torch.float32 != torch.bfloat16.