Lightning-AI / Lightning-AI/pytorch-lightning

Make MiscofigurationException optional for ModelCheckpoint

Open
#18,481 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

callback: model checkpoint feature
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Description & Motivation

At the moment, the checkpointing subsystem can be very brittle. Suppose the model logs various metrics at different stages of the fit loop, and there are several ModelCheckpoints that monitor these metrics. When the checkpoints get recorded at the end of the epoch, unless all the metrics have been populated, a MisconfigurationException is thrown. This behaviour has already caused complaints in #17150 .

I have stumbled upon this when restoring my model from a checkpoint, although this could be a bug in Lightning version 2.0.1. Somehow, when restoring, the trainer runs part of validation loop for the last epoch, and my model code is written in such a way that only part of the metrics get computed, and the loop crashes.

It would be great if ModelCheckpoint had a constructor parameter boolean strict, that would turn off the exception and write a warning instead.

Pitch

Introduce a constructor parameter in ModelCheckpoint that would stop MiscofigurationException from being thrown when the metric has not been computed, and raise a warning instead. Something like

class ModelCheckpoint:
    def __init__(
        self,
        ...,
        strict: bool = True
   )
"""
Args:
    strict: throw a ``MiscofigurationException`` if the monitored metric has not been computed. 
        Default: ``True``. Set to ``False`` to raise a warning instead.
"""
Alternatives

One alternative is make warnings the default behaviour, so strict=False by default.

Additional context

No response

cc @borda @carmocca @awaelchli

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the ModelCheckpoint constructor and the checkpointing path that raises MisconfigurationException when a monitored metric is missing. Add the requested strict option while preserving the current default, and verify that missing metrics warn when strict is false but still raise when it is true, including during checkpoint restoration.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.