fix EMA checkpoint saved with swapped weight state#590
Open
henriasv wants to merge 2 commits intomir-group:developfrom
Open
fix EMA checkpoint saved with swapped weight state#590henriasv wants to merge 2 commits intomir-group:developfrom
henriasv wants to merge 2 commits intomir-group:developfrom
Conversation
Lightning's ModelCheckpoint saves during on_validation_end, which runs before LightningModule.on_validation_end. This means checkpoints can be captured while EMA weights are swapped into the model (during validation), producing a checkpoint where the EMA module does not hold EMA weights. Previously this caused an assertion failure on load. Now: - on_save_checkpoint detects the swapped state and corrects it before save - set_extra_state tolerates old checkpoints saved in swapped state - evaluation_model performs a corrective swap when loading old checkpoints
Collaborator
|
Hi @henriasv , thanks for the contribution. Let me fix the error related to a dependency's breaking change and let the tests run. The fix looks right to me so, happy to merge if all the tests pass (we do check state restoration c.f. EMA weights in the tests). I am curious if there are specific conditions for this failure mode? e.g. was it an abrupt stop? or perhaps it's because you are using custom modules that might change the sequence of saving events somehow? Or could there be some upstream lightning logic that has changed? |
Author
|
Hi, it turned up when working on the compile pipeline for multi-head models. Seemed to be causing an error in loading models, but it might actually be that this thing never triggered a failure at all. |
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.
This error surfaced during development of a multi-head feature for nequip. Solved by Claude Code.
Summary
ModelCheckpointsaves duringon_validation_end, which runs beforeLightningModule.on_validation_end. This means checkpoints can be captured while EMA weights are swapped into the model (during validation), producing a checkpoint where the EMA module does not hold EMA weights.on_save_checkpointto detect and correct the swapped state before saving.