Skip to content

Conversation

@timesler
Copy link

@timesler timesler commented Feb 20, 2022

What does this PR do?

This is a small change that prevents PL from silently overriding torch.backends.cudnn.benchmark when constructing a Trainer object.

Fixes #12018

With this change, the behaviour can be described by:

For torch.backends.cudnn.benchmark to be changed by Trainer only when the benchmark arg is explicitly set.

Instantiation Behaviour
Trainer() torch.backends.cudnn.benchmark is unchanged from current session value
Trainer(benchmark=None) torch.backends.cudnn.benchmark is unchanged from current session value
Trainer(benchmark=True) torch.backends.cudnn.benchmark set to True
Trainer(benchmark=False) torch.backends.cudnn.benchmark set to False

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 🙃

@timesler
Copy link
Author

This should likely wait until #11944 is merged, then be updated to ensure the same silent updating of cudnn.benchmark doesn't happen with the deterministic arg as well.

@awaelchli awaelchli added this to the 1.6 milestone Feb 26, 2022
@mergify mergify bot removed the has conflicts label Feb 27, 2022
(None, False, True),
(None, False, None),
(None, True, False),
(None, None, None),
Copy link
Contributor

Choose a reason for hiding this comment

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

So now, somebody that creates Trainer() with no arguments, won't get cudnn.benchmark=True as it does in master?

I think having benchmark=True by default is best.

The proposal in this PR only works if we were able to know if the user had set benchmark manually before.

Copy link
Author

@timesler timesler Mar 2, 2022

Choose a reason for hiding this comment

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

The justification for this change is that Pytorch Lightning shouldn't break standard Pytorch functionality, but extend it. In general, PL offers value by establishing sensible default values so users don't need to think about them usually. However, in this case, we are silently setting a global variable, resulting in this behaviour:

import torch
from pytorch_lightning import Trainer

torch.backends.cudnn.benchmark = False
trainer = Trainer(gpus=1)
print(torch.backends.cudnn.benchmark)

Output:

True
# When it should be False

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that, but since there's no way for us to know whether the user previously set the value, we have 2 exclusive options:

  1. Have the better default for most people which may override an existing value (current master)
  2. Always respect the existing value but users have to remember to set this flag (this PR)

I personally prefer 1 as it establishes a "sensible default". We could request the torch folks to add an optional default so the options are not exclusive in the future.

ccing revierwers @awaelchli @ananthsub @krshrimali to see if they think differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that, but since there's no way for us to know whether the user previously set the value,

We can know, by making the default value for the argument None. A default Trainer() would result in taking the value from torch.backends.cudnn.benchmark which by default is True

Copy link
Contributor

Choose a reason for hiding this comment

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

torch.backends.cudnn.benchmark which by default is True

Is it? locally:

$ python -c 'import torch; print(torch.backends.cudnn.benchmark)'
False

@mergify mergify bot added ready PRs ready to be merged and removed has conflicts labels Mar 2, 2022
@mergify mergify bot added has conflicts and removed ready PRs ready to be merged labels Mar 27, 2022
@carmocca carmocca removed this from the 1.6 milestone Mar 28, 2022
@stale
Copy link

stale bot commented Apr 16, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you need further help see our docs: https://pytorch-lightning.readthedocs.io/en/latest/generated/CONTRIBUTING.html#pull-request or ask the assistance of a core contributor here or on Slack. Thank you for your contributions.

@stale stale bot added the won't fix This will not be worked on label Apr 16, 2022
@stale
Copy link

stale bot commented Apr 24, 2022

This pull request is going to be closed. Please feel free to reopen it create a new from the actual master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PL overrides globally set torch.backends.cudnn.benchmark value

5 participants