Skip to content

Resume from ckpt path - #536

Merged
lm2612 merged 21 commits into
mainfrom
resume_from_ckpt_path
Aug 5, 2026
Merged

Resume from ckpt path#536
lm2612 merged 21 commits into
mainfrom
resume_from_ckpt_path

Conversation

@lm2612

@lm2612 lm2612 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Pull Request

Description

We might want to continue training from same checkpoint e.g., if training run fails or crashes. This includes full state resume, including model weights and optimizer state.

Fixes #541

I've added an option for ckpt_path in config. This should be the full path to the checkpoint that includes model weights, optimizer state, etc. This will be passed to the Lightning trainer in pvnet/training/train.py. If it is not specified, it will be ignored:

trainer.fit(model=model, datamodule=datamodule, 
                ckpt_path=config.ckpt_path if 'ckpt_path' in config else None)

How Has This Been Tested?

New test test_checkpoint_load in tests/training/test_train.py. This will use the output from test_train_pvnet after the first epoch, and restart two training runs from there. Then we check that the saved checkpoints are identical.

  • Yes

Checklist:

  • My code follows OCF's coding style guidelines
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked my code and corrected any misspellings

@lm2612
lm2612 marked this pull request as ready for review August 4, 2026 10:32
@lm2612
lm2612 requested a review from felix-e-h-p August 4, 2026 10:32
Comment thread tests/training/test_train.py Outdated
# Compare state_dict
for key, value in ckpt0['state_dict'].items():
assert key in ckpt1['state_dict'], f"model parameter {key} present in {ckpt_epoch1_path[0]} not found in {ckpt_epoch1_path[1]}"
assert ckpt1['state_dict'][key] == pytest.approx(value, abs=1e-9), f"model weights different for {key} by {ckpt1['state_dict'][key]-value}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think can be reduced to:

torch.testing.assert_close(ckpt1['state_dict'][key], value, atol=1e-9, rtol=0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! added

ckpt0 = torch.load(ckpt_epoch1_path[0], map_location="cpu", weights_only=False)
ckpt1 = torch.load(ckpt_epoch1_path[1], map_location="cpu", weights_only=False)

# Compare state_dict

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this loop and the optimizer state one could be collapsed into like a helper and then just a couple assert lines - not important but just to reduce lines a little

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, just added a helper function to clean it up a bit

@felix-e-h-p felix-e-h-p left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, cheers! Just couple changes perhaps

@lm2612
lm2612 merged commit bffe96e into main Aug 5, 2026
5 checks passed
@lm2612
lm2612 deleted the resume_from_ckpt_path branch August 5, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resume training from checkpoint

2 participants