Skip to content
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

Support deterministic="warn" in Trainer for Pytorch 1.11+ #12588

Merged
merged 19 commits into from
Apr 27, 2022
Merged

Support deterministic="warn" in Trainer for Pytorch 1.11+ #12588

merged 19 commits into from
Apr 27, 2022

Conversation

weiji14
Copy link
Contributor

@weiji14 weiji14 commented Apr 3, 2022

What does this PR do?

In Pytorch 1.11, operations that do not have a deterministic implementation can be set to throw a warning instead of an error when ran in deterministic mode. See https://pytorch.org/docs/1.11/generated/torch.use_deterministic_algorithms.html

Fixes #<issue_number>

Does your PR introduce any breaking changes? If yes, please list them.

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 list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
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?

Make sure you had fun coding 🙃

@akihironitta akihironitta added feature Is an improvement or enhancement trainer: argument labels Apr 4, 2022
weiji14 added 2 commits April 5, 2022 14:58
Full error message `pytorch_lightning/trainer/connectors/accelerator_connector.py:216: error: Unexpected keyword argument "warn_only" for "use_deterministic_algorithms"  [call-arg]` is thrown because code checks run using Pytorch 1.10 but this should be checked in Pytorch 1.11+
@weiji14 weiji14 marked this pull request as ready for review April 5, 2022 20:39
@carmocca carmocca added this to the 1.7 milestone Apr 6, 2022
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Cool! Pushed a commit with some minor changes

@carmocca carmocca self-assigned this Apr 6, 2022
Copy link
Contributor

@akihironitta akihironitta left a comment

Choose a reason for hiding this comment

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

Nice addition!


The type check is failing in CI: https://github.com/PyTorchLightning/pytorch-lightning/runs/5853483314?check_suite_focus=true

Once the failure is resolved, LGTM!

@mergify mergify bot added the ready PRs ready to be merged label Apr 7, 2022
@carmocca carmocca enabled auto-merge (squash) April 7, 2022 14:33
@weiji14
Copy link
Contributor Author

weiji14 commented Apr 7, 2022

Cool, thanks everyone for sorting out the type hints! Great to learn about "Literal" types 😆

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

I find this is a confusing feature. Either I want determinism or not. Something in-between like "warn" does not exist for me. Perhaps I'm missing the use case here.

@weiji14
Copy link
Contributor Author

weiji14 commented Apr 7, 2022

I find this is a confusing feature. Either I want determinism or not. Something in-between like "warn" does not exist for me. Perhaps I'm missing the use case here.

This thread might be a useful read: pytorch/pytorch#64883. Ideally yes, full determinism would be desirable, but in practice, not every algorithm has a deterministic implementation and this gets us 90% of the way there (depending on the model being used). I'm coming at it more from a reproducibility standpoint, it's better to be able to reproduce a model's result by 90% than to not be able to reproduce it at all because of all the randomness going on.

@mergify mergify bot removed the ready PRs ready to be merged label Apr 8, 2022
@carmocca carmocca requested a review from Borda April 12, 2022 16:23
@mergify mergify bot removed the has conflicts label Apr 13, 2022
@mergify mergify bot added the ready PRs ready to be merged label Apr 26, 2022
@carmocca carmocca merged commit 6490996 into Lightning-AI:master Apr 27, 2022
@weiji14 weiji14 deleted the deterministic-warn-only branch April 27, 2022 13:03
@mareikethies
Copy link

Hi, nice update which is useful for me. Could it be that the add_argparse_args() method in Trainer needs to be adapted as well? Currently, it accepts only boolean values for the deterministic keyword. At least I don't know how to pass the warn flag using argparse. Thanks!

@weiji14
Copy link
Contributor Author

weiji14 commented Apr 28, 2022

Hmm, looking at add_argparse_args(), I don't quite know where a change needs to happen. That said, what version of pytorch-lightning are you using? Don't think this feature is in the recent PyPI 1.6.2 release, might need to wait for 1.7.0 or install from GitHub.

@mareikethies
Copy link

Yes, I did install from github today. I have not had a look at the source code, but I observe that when I run something like python train.py --deterministic warn using Trainer.add_argparse_args() in train.py, I get this error: error: argument --deterministic: invalid str_to_bool value: 'warn'. But I might miss something here or pass it the wrong way. Just plain python train.py --deterministic works.

@mauvilsa
Copy link
Contributor

@mareikethies any particular reason you want to use add_argparse_args? If you use LightningCLI this will not be a problem.

@weiji14
Copy link
Contributor Author

weiji14 commented Apr 28, 2022

Yes, I did install from github today. I have not had a look at the source code, but I observe that when I run something like python train.py --deterministic warn using Trainer.add_argparse_args() in train.py, I get this error: error: argument --deterministic: invalid str_to_bool value: 'warn'. But I might miss something here or pass it the wrong way. Just plain python train.py --deterministic works.

Thanks @mareikethies, that error message is very helpful. I can confirm the invalid str_to_bool value: 'warn' error. Minimal working example:

  1. Do pip install https://github.com/PyTorchLightning/pytorch-lightning/archive/6490996b3974019a254f2750f5c01e6d38ff6a6e.zip
  2. Create a file called train_determinstic.py like so:
import argparse
import pytorch_lightning as pl
import torch


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

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

    def training_step(self, batch, batch_idx):
        output = self(batch)
        loss = torch.nn.functional.mse_loss(batch, output)
        return {"loss": loss}

    def configure_optimizers(self):
        return torch.optim.Adam(params=self.parameters(), lr=0.1, weight_decay=0.0005)


class RandomDataset(torch.utils.data.Dataset):
    def __init__(self):
        pass

    def __len__(self):
        return 1

    def __getitem__(self, idx: int):
        return torch.randn(2)


def cli_main():
    parser = argparse.ArgumentParser()
    parser = pl.Trainer.add_argparse_args(parent_parser=parser)
    args = parser.parse_args()
    trainer = pl.Trainer.from_argparse_args(args=args, max_epochs=1, accelerator="auto")

    model = BasicNet()
    dataloader = torch.utils.data.DataLoader(dataset=RandomDataset())

    trainer.fit(model=model, train_dataloaders=dataloader)


if __name__ == "__main__":
    cli_main()
  1. Run python deterministic.py --deterministic=warn, which will produce deterministic.py: error: argument --deterministic: invalid str_to_bool value: 'warn'

So my guess is that the code here might need to change:
https://github.com/PyTorchLightning/pytorch-lightning/blob/0fe2ae74e919c8ef220a029cab2203572ca536a5/pytorch_lightning/utilities/argparse.py#L232-L251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement ready PRs ready to be merged trainer: argument
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants