Skip to content

Conversation

@awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Sep 3, 2021

What does this PR do?

Fixes a TODO in #9130

The progress bar callback did not resume correctly, because of two reasons:

  • on_train_start() was resuming the correct progress value but on_train_epoch_start() would overwrite it back to 0
  • the on_*_epoch_start hooks reset the tqdm counter to 0 instead of the value we would resume from

This came up in the context of fault tolerant training.

Repro script
import os
from time import sleep

import torch
from torch.utils.data import DataLoader, Dataset

from pytorch_lightning import LightningModule, Trainer


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):
        sleep(0.5)
        print(batch_idx, self.trainer.fit_loop.batch_idx)
        loss = self(batch).sum()
        self.log("train_loss", loss)
        return {"loss": loss}

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


def run():
    train_data = DataLoader(RandomDataset(32, 64), batch_size=2)

    model = BoringModel()
    trainer = Trainer(
        default_root_dir=os.getcwd(),
        weights_summary=None,
        # resume_from_checkpoint=".pl_auto_save.ckpt",
    )
    trainer.fit(model, train_dataloaders=train_data)


if __name__ == "__main__":
    run()

The added test fails on master.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

I made sure I had fun coding 🙃

@awaelchli awaelchli added the bug Something isn't working label Sep 3, 2021
@awaelchli awaelchli modified the milestones: v1.4.x, v1.5 Sep 3, 2021
@awaelchli awaelchli mentioned this pull request Sep 3, 2021
38 tasks
@codecov
Copy link

codecov bot commented Sep 5, 2021

Codecov Report

Merging #9310 (f97953b) into master (f6d4087) will increase coverage by 0%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #9310    +/-   ##
=======================================
  Coverage      92%     92%            
=======================================
  Files         178     178            
  Lines       14850   15402   +552     
=======================================
+ Hits        13701   14241   +540     
- Misses       1149    1161    +12     

Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

Neat !

@tchaton tchaton added the ready PRs ready to be merged label Sep 6, 2021
@Borda Borda merged commit 50198d7 into master Sep 6, 2021
@Borda Borda deleted the feature/progress-bar-resume branch September 6, 2021 08:44
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 ready PRs ready to be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants