Raise better error when calling `Trainer.save_checkpoint` without a model attached (#12772)
* add error message * add test * changelog Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
This commit is contained in:
parent
bb81802bff
commit
ab60cdbdcb
|
@ -9,6 +9,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
|
|||
|
||||
### Added
|
||||
|
||||
|
||||
- Added a friendly error message when attempting to call `Trainer.save_checkpoint()` without a model attached ([#12772](https://github.com/PyTorchLightning/pytorch-lightning/pull/12772))
|
||||
|
||||
|
||||
- Enabled `torch.inference_mode` for evaluation and prediction ([#12715](https://github.com/PyTorchLightning/pytorch-lightning/pull/12715))
|
||||
|
||||
|
||||
|
|
|
@ -2364,6 +2364,11 @@ class Trainer(
|
|||
storage_options: parameter for how to save to storage, passed to ``CheckpointIO`` plugin
|
||||
|
||||
"""
|
||||
if self.model is None:
|
||||
raise AttributeError(
|
||||
"Saving a checkpoint is only possible if a model is attached to the Trainer. Did you call"
|
||||
" `Trainer.save_checkpoint()` before calling `Trainer.{fit,validate,test,predict}`?"
|
||||
)
|
||||
self._checkpoint_connector.save_checkpoint(filepath, weights_only=weights_only, storage_options=storage_options)
|
||||
|
||||
"""
|
||||
|
|
|
@ -2133,3 +2133,10 @@ def test_trainer_config_device_ids(monkeypatch, trainer_kwargs, expected_device_
|
|||
trainer = Trainer(**trainer_kwargs)
|
||||
assert trainer.device_ids == expected_device_ids
|
||||
assert trainer.num_devices == len(expected_device_ids)
|
||||
|
||||
|
||||
def test_trainer_save_checkpoint_no_model_attached():
|
||||
trainer = Trainer()
|
||||
assert trainer.model is None
|
||||
with pytest.raises(AttributeError, match="Saving a checkpoint is only possible if a model is attached"):
|
||||
trainer.save_checkpoint("checkpoint.ckpt")
|
||||
|
|
Loading…
Reference in New Issue