Skip to content

Fix EMA callback: resume training from the raw weights, not the EMA a…#785

Merged
sundusaijaz merged 1 commit into
masterfrom
sh/checkpoint-fix
Jul 20, 2026
Merged

Fix EMA callback: resume training from the raw weights, not the EMA a…#785
sundusaijaz merged 1 commit into
masterfrom
sh/checkpoint-fix

Conversation

@stefaanhessmann

Copy link
Copy Markdown
Collaborator

Checkpoints are written during validation, when on_validation_epoch_start has copy_to()'d the EMA average into the model; restore() only runs at the next on_train_epoch_start. A run resumed from such a checkpoint therefore loads the EMA average as its model weights, and on_fit_start immediately called store(), overwriting the stored raw weights — so the subsequent restore() was a no-op and training silently continued from the EMA average instead of the raw weights a continuous run would have used.

Two changes:

  • on_fit_start: after loading the EMA state on resume, restore the raw weights (the state's collected_params, captured by the store() at on_validation_epoch_start before the save) into the model.
  • on_validation_epoch_start: skip store()/copy_to() during the sanity check. on_fit_start has already stored the raw weights and copied the EMA average in, so the sanity check still validates the EMA weights; storing again would overwrite the raw weights with the average and reintroduce the bug whenever num_sanity_val_steps > 0 (the default).

Fresh (non-resume) fits are unaffected.

…verage

Checkpoints are written during validation, when on_validation_epoch_start
has copy_to()'d the EMA average into the model; restore() only runs at the
next on_train_epoch_start. A run resumed from such a checkpoint therefore
loads the EMA average as its model weights, and on_fit_start immediately
called store(), overwriting the stored raw weights — so the subsequent
restore() was a no-op and training silently continued from the EMA average
instead of the raw weights a continuous run would have used.

Two changes:
- on_fit_start: after loading the EMA state on resume, restore the raw
  weights (the state's collected_params, captured by the store() at
  on_validation_epoch_start before the save) into the model.
- on_validation_epoch_start: skip store()/copy_to() during the sanity
  check. on_fit_start has already stored the raw weights and copied the
  EMA average in, so the sanity check still validates the EMA weights;
  storing again would overwrite the raw weights with the average and
  reintroduce the bug whenever num_sanity_val_steps > 0 (the default).

Fresh (non-resume) fits are unaffected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@sundusaijaz sundusaijaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified with a before/after TensorBoard comparison, pre-fix resume spiked train_loss whereas post-fix it continues smoothly from the pre-interruption value.

@sundusaijaz
sundusaijaz merged commit 068cea8 into master Jul 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants