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

Save test predictions on multiple GPUs #2926

Merged
merged 26 commits into from
Aug 14, 2020

Conversation

nateraw
Copy link
Contributor

@nateraw nateraw commented Aug 12, 2020

What does this PR do?

This PR lets you save test_step predictions across multiple GPUs via a new function EvalResult.write(name, values, filename='predictions.txt').

Here's what it looks like:

class MyModel(pl.LightningModule):
    ...
    def test_step(self, batch, batch_idx):
        x, y = batch
        logits, loss = self(x)
        preds = torch.argmax(logits, dim=1)
        result = pl.EvalResult()
        result.log('test_loss', loss)
        result.write('preds', preds, filename='./predictions.txt')
        return result
  • If you don't call .write() on your EvalResult objects, nothing should happen.
  • If you do, it'll save at the files you specified.
  • If on multiple GPUs, it should prepend your filename with the rank, leaving you with n_gpus number of prediction files.
    • For example, if running on two GPUs, test dataset size of 10,000, and your prediction file is the default of predictions.txt, you'll end up with:
      • predictions_rank_0.txt --> 5000 predictions
      • predictions_rank_1.txt --> 5000 predictions

Its basically going to take whatever you send to .write() and torch.cat/list.extend it with existing data with matching filename/name keys. In the background, the dictionary we're creating looks like this right now:

_predictions = {
    # The default file that predictions are saved to when you call .write
    'predictions.txt': {
        'preds': ['cat', 'dog', 'horse'],
        'idxs': [0, 1, 2]
    },

    # Optionally, if the predictions you want to save have different lengths, you can add more files
    'others.txt': {
        'something': ['wow', 'so', 'cool', 'dont', 'you', 'think?'],
        'idxs': [0, 1, 2, 3, 4, 5]
    }
}

Fixes # (issue)

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • 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? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Aug 12, 2020

Hello @nateraw! Thanks for updating this PR.

Line 99:5: E303 too many blank lines (2)
Line 139:1: W293 blank line contains whitespace
Line 145:1: W293 blank line contains whitespace

Line 74:120: E501 line too long (149 > 119 characters)

Comment last updated at 2020-08-14 20:42:10 UTC

@codecov
Copy link

codecov bot commented Aug 13, 2020

Codecov Report

Merging #2926 into master will increase coverage by 2%.
The diff coverage is 86%.

@@           Coverage Diff           @@
##           master   #2926    +/-   ##
=======================================
+ Coverage      86%     88%    +2%     
=======================================
  Files          81      81            
  Lines        7554    7836   +282     
=======================================
+ Hits         6476    6907   +431     
+ Misses       1078     929   -149     

@nateraw nateraw marked this pull request as ready for review August 14, 2020 08:02
@mergify mergify bot requested a review from a team August 14, 2020 08:02

# Add step predictions to prediction collection to write later
do_write_predictions = is_result_obj and test_mode
if do_write_predictions:
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. this should just stay inside the prediction object
  2. we already track prediction objects. No need to do anything explicit.

@mergify mergify bot requested a review from a team August 14, 2020 08:13
@@ -388,6 +395,9 @@ def _evaluate(
# log callback metrics
self.__update_callback_metrics(eval_results, using_eval_result)

# Write predictions to disk if they're available.
predictions.to_disk()
Copy link
Contributor

Choose a reason for hiding this comment

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

this should just be an internal function of the prediction object.

Copy link
Contributor

Choose a reason for hiding this comment

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

we can’t wait till the end of epoch to write predictions bc we will accumulate too much memory.

the write needs to happen at every batch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how do we want to deal with writing to the cache file (w/ torch.save) if we want to append to it?

@mergify mergify bot requested a review from a team August 14, 2020 08:14
@Borda Borda added the feature Is an improvement or enhancement label Aug 14, 2020
pytorch_lightning/core/step_result.py Outdated Show resolved Hide resolved
pytorch_lightning/core/step_result.py Outdated Show resolved Hide resolved
pytorch_lightning/core/step_result.py Outdated Show resolved Hide resolved
pytorch_lightning/trainer/evaluation_loop.py Show resolved Hide resolved
pytorch_lightning/trainer/supporters.py Show resolved Hide resolved
pytorch_lightning/trainer/supporters.py Show resolved Hide resolved
pytorch_lightning/trainer/supporters.py Show resolved Hide resolved
@mergify mergify bot requested a review from a team August 14, 2020 09:36
williamFalcon and others added 5 commits August 14, 2020 13:03
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
@williamFalcon williamFalcon merged commit b969523 into Lightning-AI:master Aug 14, 2020
ameliatqy pushed a commit to ameliatqy/pytorch-lightning that referenced this pull request Aug 17, 2020
* Save test predictions on multiple GPUs
@Borda Borda added this to the 0.9.0 milestone Aug 20, 2020
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants