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

Enable inference mode for testing and predicting #8813

Merged
merged 3 commits into from
Sep 8, 2021
Merged

Enable inference mode for testing and predicting #8813

merged 3 commits into from
Sep 8, 2021

Conversation

tangbinh
Copy link
Contributor

@tangbinh tangbinh commented Aug 9, 2021

What does this PR do?

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

None

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 🙃

@codecov
Copy link

codecov bot commented Aug 9, 2021

Codecov Report

Merging #8813 (fe99e5f) into master (ca679cd) will decrease coverage by 4%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #8813    +/-   ##
=======================================
- Coverage      92%     88%    -4%     
=======================================
  Files         179     179            
  Lines       14910   14908     -2     
=======================================
- Hits        13760   13145   -615     
- Misses       1150    1763   +613     

@ananthsub
Copy link
Contributor

@tangbinh thanks for working on this! It'd be awesome to include information on performance speedups for some candidate models as well

@ananthsub ananthsub linked an issue Aug 9, 2021 that may be closed by this pull request
@tangbinh
Copy link
Contributor Author

Should we allow the users to opt out of the inference mode if they want to? I imagine some cases where the gradients are needed at test time (e.g. higher-order derivatives), and being able to turn it off by a flag might be important. I'm think of having _TORCH_GREATER_EQUAL_1_9 and os.getenv("NO_INFERENCE_MODE", "False").lower() == "false".

@tangbinh
Copy link
Contributor Author

@tangbinh ... It'd be awesome to include information on performance speedups for some candidate models as well

Do you have suggestions for the candidate models and some datasets to test them on? I was running some simple tasks and didn't notice much of a difference.

@ananthsub
Copy link
Contributor

Do you have suggestions for the candidate models and some datasets to test them on? I was running some simple tasks and didn't notice much of a difference.

@Borda do you recommend the models used for the parity benchmark?

pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
@tangbinh tangbinh changed the title Enable inference mode for evaluation and prediction Enable inference mode for testing and predicting Aug 11, 2021
@tangbinh tangbinh marked this pull request as ready for review August 11, 2021 22:59
@awaelchli awaelchli added bug Something isn't working feature Is an improvement or enhancement and removed bug Something isn't working labels Aug 11, 2021
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.

For the 2 cases where no_grad has been replaced already, we don't need to check the TrainerFn because by design, they will only be called during trainer.validate(), trainer.test(), or trainer.predict(). On these, no grad operations should take place so we are safe.

This is different from the following, which is the validation part of trainer.fit. This is where I'm not sure if no_grad should be kept:

https://github.com/PyTorchLightning/pytorch-lightning/blob/e0605472306d6b95bf2616ab88f8c29f4498402e/pytorch_lightning/loops/epoch/training_epoch_loop.py#L247

pytorch_lightning/trainer/trainer.py Outdated Show resolved Hide resolved
@tangbinh
Copy link
Contributor Author

@ananthsub @tchaton @awaelchli @carmocca Please see this notebook for some performance tests. It looks like for some common models and datasets, enabling inference mode doesn't make much of a difference in terms of inference time. The observation is consistent on both GPU and CPU. That said, I think it doesn't hurt if we decide to move forward with inference mode.

@mergify mergify bot removed the has conflicts label Aug 19, 2021
Copy link
Contributor

@ananthsub ananthsub left a comment

Choose a reason for hiding this comment

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

thanks for working on this @tangbinh !

@tchaton
Copy link
Contributor

tchaton commented Aug 20, 2021

Dear @tangbinh @ananthsub,

Could we get the benchmark for some traditional models to validate this doesn't bring a regression for Lightning performances + updated docs.

Best,
T.C

@tangbinh
Copy link
Contributor Author

Could we get the benchmark for some traditional models to validate this doesn't bring a regression for Lightning performances + updated docs.

Would you mind proposing some traditional models that you have in mind? I've done some benchmark tests, although they don't follow previous examples as I couldn't find any good one.

@mergify mergify bot removed the has conflicts label Sep 3, 2021
@mergify mergify bot added the ready PRs ready to be merged label Sep 4, 2021
@Borda Borda requested review from awaelchli and tchaton September 6, 2021 07:44
@awaelchli awaelchli added this to the v1.5 milestone Sep 6, 2021
@awaelchli awaelchli enabled auto-merge (squash) September 8, 2021 21:11
@awaelchli awaelchli merged commit a079d7f into Lightning-AI:master Sep 8, 2021
ananthsub added a commit to ananthsub/pytorch-lightning that referenced this pull request Sep 10, 2021
ananthsub added a commit that referenced this pull request Sep 10, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for PyTorch's optimize_for_inference mode
5 participants