Fix BaseFinetuning re-freezing unfrozen modules on resume - #21902
Open
yentur wants to merge 1 commit into
Open
Conversation
yentur
requested review from
ethanwharris,
justusschock and
tchaton
as code owners
August 14, 2026 13:03
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.
What does this PR do?
Fixes #21901
BaseFinetuningrestores half of its state when a run is resumed.setup()callsfreeze_before_training()on every fit, including a resumed one, because it has to run beforeconfigure_optimizers().on_fit_start()then puts the saved param groups back on the optimizer, but it only restores group membership, neverparam.requires_grad.The result is that modules unfrozen before the interruption come back frozen. They sit in the optimizer with the right learning rate and get no gradients, so they never update again.
BackboneFinetuning.finetune_function()unfreezes only on the exact epochepoch == unfreeze_backbone_at_epochand only rescales the learning rate afterwards, so nothing putsrequires_gradback.This restores
requires_gradfor the param groups that were added after the onesconfigure_optimizers()created. Those are precisely the groupsunfreeze_and_add_param_group()appended in the previous run, so the frozen state is restored as it was rather than blanket-unfrozen. Resuming before the unfreeze epoch adds no extra groups and therefore changes nothing.Same 4 epochs, tracking
requires_gradand whether the backbone weights actually change, interrupted after epoch 1:After the fix the resumed columns match the uninterrupted ones for every epoch.
With a schedule that unfreezes block
iat epochi, resuming at epoch 2:Known limitation, not addressed here:
freeze_module()also setstrack_running_stats = FalseonBatchNormlayers, and that flag is not recoverable from the saved param groups. ABatchNormthat was unfrozen withtrain_bn=Falsewill have its affine parameters trainable again but keeptrack_running_stats=False, and one withaffine=Falsehas no parameters in any group at all. Restoring that would need the callback to persist which modules it unfroze, which is a larger change to the checkpoint contents.The bug only appears when the unfrozen modules were added to the optimizer as a new param group, which is the pattern the
BackboneFinetuningdocstring shows. Ifconfigure_optimizers()already hands every parameter to the optimizer,unfreeze_and_add_param_group()drops them infilter_on_optimizer(), no group is recorded, and there is nothing in the metadata to restore from.test_callbacks_restore_backbonealready walks this resume path but asserts nothing after the secondfit(), which is why the suite stayed green. The new test covers both directions: unfrozen stays unfrozen, and still-frozen stays frozen.With only the change to
finetuning.pyreverted:Before submitting
PR review
Anyone in the community is welcome to review the PR.