Skip to content

Fix interaction with save_last and every_n_epochs #12391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
carmocca opened this issue Mar 21, 2022 · 0 comments · Fixed by #12418
Closed

Fix interaction with save_last and every_n_epochs #12391

carmocca opened this issue Mar 21, 2022 · 0 comments · Fixed by #12418
Assignees
Labels
bug Something isn't working callback: model checkpoint priority: 0 High priority task
Milestone

Comments

@carmocca
Copy link
Contributor

carmocca commented Mar 21, 2022

@carmocca for the following snippet, a last.ckpt is generated before this PR, but not anymore after:

import uuid

import torch
from pytorch_lightning import Trainer, LightningModule
from pytorch_lightning.callbacks import ModelCheckpoint
from torch.utils.data import DataLoader, Dataset


class RandomDataset(Dataset):
    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size)

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len


class BoringModel(LightningModule):
    def __init__(self):
        super().__init__()
        self.layer = torch.nn.Linear(32, 2)

    def forward(self, x):
        return self.layer(x)

    def training_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("train_loss", loss)
        return {"loss": loss}

    def validation_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("valid_loss", loss)

    def test_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("test_loss", loss)

    def configure_optimizers(self):
        return torch.optim.SGD(self.layer.parameters(), lr=0.1)


if __name__ == "__main__":
    tmpdir = f"/tmp/{uuid.uuid4()}"
    print(tmpdir)

    trainer = Trainer(
        default_root_dir=tmpdir,
        max_epochs=1,
        callbacks=[ModelCheckpoint(dirpath=tmpdir, every_n_epochs=10, save_last=True)],
        enable_checkpointing=True,
    )
    model = BoringModel()
    trainer.fit(
        model, train_dataloaders=DataLoader(RandomDataset(32, 64), batch_size=2)
    )

The following contract was respected prior to this PR, but not anymore after:

save_last: When True, always saves the model at the end of the epoch to a file last.ckpt

It is a BC-breaking change that changes a behavior some users rely on, regardless of whether it is believed to be a "bug".

Originally posted by @yifuwang in #11805 (comment)

cc @tchaton @rohitgr7 @akihironitta @carmocca @awaelchli @ninginthecloud @jjenniferdai

@carmocca carmocca self-assigned this Mar 21, 2022
@carmocca carmocca added this to the 1.6.x milestone Mar 21, 2022
@carmocca carmocca added callback: model checkpoint bug Something isn't working deprecation Includes a deprecation and removed deprecation Includes a deprecation labels Mar 21, 2022
@carmocca carmocca moved this to Todo in Frameworks Planning Mar 21, 2022
@carmocca carmocca added the priority: 0 High priority task label Mar 21, 2022
@carmocca carmocca modified the milestones: 1.6.x, 1.6 Mar 22, 2022
Repository owner moved this from Todo to Done in Frameworks Planning Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working callback: model checkpoint priority: 0 High priority task
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant