Skip to content

Commit

Permalink
bugfix change annotation of ModelCheckpoint's save_last parameter, so…
Browse files Browse the repository at this point in the history
… jsonargparse can correctly parse inputs such as true, false, etc...

before this change jsonargparse gave errors complaining that strings like 'true' or 'false' were given when the annotation expected Optional[Literal[True, False, 'link']]; this is in contrast to most other bool parameters in the ModelCheckpoint callback's constructor. I also added a test in tests/test_pytorch/checkpointing/test_model_checkpoint.py::test_save_last_cli
  • Loading branch information
mariovas3 committed Apr 24, 2024
1 parent b9680a3 commit 4feb7ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lightning/pytorch/callbacks/model_checkpoint.py
Expand Up @@ -27,7 +27,7 @@
from copy import deepcopy
from datetime import timedelta
from pathlib import Path
from typing import Any, Dict, Literal, Optional, Set
from typing import Any, Dict, Literal, Optional, Set, Union
from weakref import proxy

import torch
Expand Down Expand Up @@ -216,7 +216,7 @@ def __init__(
filename: Optional[str] = None,
monitor: Optional[str] = None,
verbose: bool = False,
save_last: Optional[Literal[True, False, "link"]] = None,
save_last: Optional[Union[bool, Literal["link"]]] = None,
save_top_k: int = 1,
save_weights_only: bool = False,
mode: str = "min",
Expand Down
22 changes: 22 additions & 0 deletions tests/tests_pytorch/checkpointing/test_model_checkpoint.py
Expand Up @@ -18,6 +18,7 @@
import time
from argparse import Namespace
from datetime import timedelta
from inspect import signature
from pathlib import Path
from typing import Union
from unittest import mock
Expand All @@ -28,6 +29,7 @@
import pytest
import torch
import yaml
from jsonargparse import ArgumentParser
from lightning.fabric.utilities.cloud_io import _load as pl_load
from lightning.pytorch import Trainer, seed_everything
from lightning.pytorch.callbacks import ModelCheckpoint
Expand All @@ -43,6 +45,26 @@
from omegaconf import Container, OmegaConf


@pytest.mark.parametrize(
("val", "expected"),
[
("yes", True),
("True", True),
("true", True),
("no", False),
("false", False),
("False", False),
("link", "link"),
],
)
def test_save_last_cli(val, expected):
annot = signature(ModelCheckpoint).parameters["save_last"].annotation
parser = ArgumentParser()
parser.add_argument("--a", type=annot)
args = parser.parse_args(["--a", val])
assert args.a == expected


def test_model_checkpoint_state_key():
early_stopping = ModelCheckpoint(monitor="val_loss")
expected_id = (
Expand Down

0 comments on commit 4feb7ff

Please sign in to comment.