Commit 910a712
committed
Add adapt_checkpoint_hparams hook for customizing checkpoint hyperparameter loading
Fixes #21255
This commit adds the adapt_checkpoint_hparams() public method to LightningCLI,
allowing users to customize hyperparameters loaded from checkpoints before they
are used to instantiate model classes. This is particularly useful when using
checkpoints from a TrainingModule with a different InferenceModule class that
has different __init__ parameters.
Problem:
When loading a checkpoint trained with TrainingModule(lr=1e-3) into an
InferenceModule() that doesn't accept 'lr' as a parameter, the CLI would fail
during instantiation because it tries to pass all checkpoint hyperparameters
to the new module class.
Solution:
Added adapt_checkpoint_hparams() hook that is called in _parse_ckpt_path()
after loading checkpoint hyperparameters but before applying them. Users can
override this method to:
- Remove training-specific hyperparameters (e.g., lr, weight_decay)
- Modify _class_path for subclass mode
- Transform hyperparameter names/values
- Completely disable checkpoint hyperparameters by returning {}
Example usage:
class MyCLI(LightningCLI):
def adapt_checkpoint_hparams(self, checkpoint_hparams):
checkpoint_hparams.pop('lr', None)
checkpoint_hparams.pop('weight_decay', None)
return checkpoint_hparams
This approach is preferable to:
- Disabling checkpoint loading entirely (loses valuable hyperparameter info)
- Adding CLI arguments (deviates from Trainer parameter pattern)
- Modifying private methods (breaks encapsulation)
The hook provides maximum flexibility while maintaining backward compatibility
(default implementation returns hyperparameters unchanged).1 parent 79ffe50 commit 910a712
1 file changed
+36
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
560 | 560 | | |
561 | 561 | | |
562 | 562 | | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
563 | 593 | | |
564 | 594 | | |
565 | 595 | | |
| |||
571 | 601 | | |
572 | 602 | | |
573 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
574 | 610 | | |
575 | 611 | | |
576 | 612 | | |
| |||
0 commit comments