Fix EMA callback: resume training from the raw weights, not the EMA a…#785
Merged
Conversation
…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
approved these changes
Jul 20, 2026
sundusaijaz
left a comment
Collaborator
There was a problem hiding this comment.
Verified with a before/after TensorBoard comparison, pre-fix resume spiked train_loss whereas post-fix it continues smoothly from the pre-interruption value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Fresh (non-resume) fits are unaffected.