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

[Metrics] Disable default reset after compute #5409

Merged
merged 15 commits into from
Jan 13, 2021

Conversation

SkafteNicki
Copy link
Member

What does this PR do?

Rework of #5193
Fixes #5166
Fixes #4641

This PR removes the automatic reset that happens after compute for metrics (solution proposed by @teddykoker here #5193 (comment)). This give the user more control over when to actually reset the state but also put a bit more responsibility on user as the they would need to reset the metric themself between epochs (except if they use metrics together with self.log, then we still auto reset between epochs for 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 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
  • Check that target branch and milestone match!

Did you have fun?

Make sure you had fun coding 🙃

@pep8speaks
Copy link

pep8speaks commented Jan 7, 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-01-13 08:42:24 UTC

@teddykoker teddykoker self-requested a review January 7, 2021 22:05
@teddykoker
Copy link
Contributor

hmm ddp tests seem to be failing, looking into it now

@teddykoker
Copy link
Contributor

DDP tests broke as we were writing to self._cache multiple times before the cache is restored. Simple solution was store the cache in a local variable instead.

Now just need to fix tests/core/test_metric_result_integration.py

@teddykoker
Copy link
Contributor

Second bug was a little more tricky. When the results object computes metrics, there can be duplicates of the same metric under a different name e.g. accuracy_step and accuracy_epoch. It is important not to reset() accuracy_step, if we need to compute accuracy_epoch afterwards. The simple solution here is to call compute() on epoch end for any step metrics (which we were doing before anyway to avoid any unnecessary accumulation of state.

@codecov
Copy link

codecov bot commented Jan 8, 2021

Codecov Report

Merging #5409 (9508a4b) into release/1.2-dev (29bcf30) will increase coverage by 0%.
The diff coverage is 88%.

@@               Coverage Diff                @@
##           release/1.2-dev   #5409    +/-   ##
================================================
  Coverage               93%     93%            
================================================
  Files                  151     151            
  Lines                10645   10824   +179     
================================================
+ Hits                  9859   10043   +184     
+ Misses                 786     781     -5     

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.

Great job !

self[k].compute()
self[k].reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why reset is being added there ? self[k].compute() and self[k].reset() makes compute() useless no ?

Copy link
Contributor

Choose a reason for hiding this comment

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

compute() will save the computed value to self._computed, so if compute() is called again without any update() call, the cached value will be provided. We still call reset() so that we don't get too much memory usage by needlessly accumulating tensors over epochs.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is important if we have a metric that is logged on both step and epoch. Since it will show up twice in the results meta dict, we want to reset the step metric on epoch end, but still allow the epoch metric to be computed.

docs/source/metrics.rst Outdated Show resolved Hide resolved
@s-rog s-rog enabled auto-merge (squash) January 12, 2021 00:32
@s-rog s-rog merged commit 02acb21 into Lightning-AI:release/1.2-dev Jan 13, 2021
Borda pushed a commit that referenced this pull request Jan 13, 2021
* reset

* self._cache -> cache (make cache local variable so it is not overwritten)

* pep8

* fix metric result integration

* rm print statements

* better comment

* changelog

* Update docs/source/metrics.rst

Co-authored-by: Roger Shieh <[email protected]>

Co-authored-by: Teddy Koker <[email protected]>
Co-authored-by: Roger Shieh <[email protected]>
@SkafteNicki SkafteNicki deleted the metrics/reset branch March 2, 2021 15:55
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
6 participants