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

Correctly reset metric objects in self.log #7055

Merged
merged 12 commits into from Apr 19, 2021

Conversation

SkafteNicki
Copy link
Member

@SkafteNicki SkafteNicki commented Apr 16, 2021

What does this PR do?

Fixes #7052
Since commit Lightning-AI/torchmetrics@19b77cc in torchmetrics, the integration with lightning has been broken if the metric object have been tried to be logged directly. This is because the lightning logging implicit depends on the self._computed variable not being reset whenever reset is called.

This PR tries to solve this by making sure that reset of metric objects is only called once at the very end. Please note that I am not so familiar with the logger connection of the code base.

As an alternative we revert the changes in torchmetrics. However, it does make some sense that the self._computed variable should be reset when reset is called.

cc: @ethanwharris

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?

Make sure you had fun coding 🙃

@SkafteNicki SkafteNicki added the bug Something isn't working label Apr 16, 2021
@codecov
Copy link

codecov bot commented Apr 16, 2021

Codecov Report

❗ No coverage uploaded for pull request base (master@30b7440). Click here to learn what that means.
The diff coverage is 100%.

@@           Coverage Diff            @@
##             master   #7055   +/-   ##
========================================
  Coverage          ?     87%           
========================================
  Files             ?     196           
  Lines             ?   12577           
  Branches          ?       0           
========================================
  Hits              ?   10949           
  Misses            ?    1628           
  Partials          ?       0           

pytorch_lightning/core/step_result.py Outdated Show resolved Hide resolved
Comment on lines +242 to +244
if self._internal_type == ResultStoreType.INSIDE_BATCH_TRAIN_LOOP:
for opt_idx in list(epoch_metrics):
epoch_metrics[opt_idx].reset()
Copy link
Contributor

@ananthsub ananthsub Apr 16, 2021

Choose a reason for hiding this comment

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

could you explain this check? at the surface, inside the batch train loop reads like we're not at the epoch end?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not completely sure about what is going on myself, but apparently the self._internal_type is equal to ResultStoreType.INSIDE_BATCH_TRAIN_LOOP even when we are at epoch end for all training metrics. self._internal_type is equal to ResultStoreType.OUTSIDE_BATCH_TRAIN_LOOP for validation metrics (atleast when this reset function is called).

Copy link
Member

Choose a reason for hiding this comment

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

Correct. I think BATCH should be removed.

But I'd also like to clear all this entirely at some point.

pytorch_lightning/trainer/trainer.py Show resolved Hide resolved
@ananthsub
Copy link
Contributor

thanks for the fix @SkafteNicki !

Co-authored-by: ananthsub <ananth.subramaniam@gmail.com>
Copy link
Member

@ethanwharris ethanwharris left a comment

Choose a reason for hiding this comment

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

Working for me 😃

@edenlightning
Copy link
Contributor

Nice job @SkafteNicki, thanks for the swift fix.

@ananthsub ananthsub added the priority: 0 High priority task label Apr 16, 2021
@pep8speaks
Copy link

pep8speaks commented Apr 16, 2021

Hello @SkafteNicki! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-04-19 08:46:31 UTC

Comment on lines +242 to +244
if self._internal_type == ResultStoreType.INSIDE_BATCH_TRAIN_LOOP:
for opt_idx in list(epoch_metrics):
epoch_metrics[opt_idx].reset()
Copy link
Member

Choose a reason for hiding this comment

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

Correct. I think BATCH should be removed.

But I'd also like to clear all this entirely at some point.

Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

lgtm

@Borda Borda added the ready PRs ready to be merged label Apr 18, 2021
@mergify mergify bot removed the has conflicts label Apr 19, 2021
@Borda Borda enabled auto-merge (squash) April 19, 2021 08:58
@edenlightning edenlightning added the 3rd party Related to a 3rd-party label Apr 19, 2021
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.

LGTM !

@@ -587,6 +578,14 @@ def get_non_metrics_keys(self):
"""
return [k for k, v in self.items() if not isinstance(v, Metric)]

def reset(self) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def reset(self) -> None:
def reset_metrics(self) -> None:

Call at the end of epoch to reset Result objects
"""
for dl_idx in range(self.num_dataloaders):
epoch_metrics = self._internals[dl_idx] if not self.has_reduced else self._internals_reduced[dl_idx]
Copy link
Contributor

Choose a reason for hiding this comment

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

In current usage, self.has_reduced should always be True right ?
Better to add assert self.has_reduced == True there and use self._internals_reduced directly ?

@Borda Borda merged commit fbee5a8 into Lightning-AI:master Apr 19, 2021
@SkafteNicki SkafteNicki deleted the metric_correct_reset branch April 28, 2023 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Related to a 3rd-party bug Something isn't working logging Related to the `LoggerConnector` and `log()` priority: 0 High priority task ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metrics passed to self.log giving RuntimeError
8 participants