-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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] Allow for running accumulation computations #5193
[Metrics] Allow for running accumulation computations #5193
Conversation
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-06 20:31:19 UTC |
Codecov Report
@@ Coverage Diff @@
## release/1.2-dev #5193 +/- ##
=================================================
- Coverage 93% 84% -9%
=================================================
Files 146 146
Lines 10211 11865 +1654
=================================================
+ Hits 9486 9980 +494
- Misses 725 1885 +1160 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a comment unrelated to this PR - I would prefer to have the Args section in __init__
to be closer to where it is actually used...
if self.auto_reset_on_compute or not self._internal_reset: | ||
reset_fn = self._reset_to_default | ||
else: | ||
reset_fn = all_except_rank_zero(self._reset_to_default) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this make it not reset on rank 0? Why would we not want to reset on rank 0?
Another possible solution here is to simply never PyTorch has a similar pattern with When logging a metric we could still call both in the logger. @SkafteNicki, @Borda, @justusschock what do you think? The api is still relatively new, and I think this would make more sense for users that want to use metrics with plain PyTorch. |
We might want to do something like this for def _wrap_compute(self, compute):
@functools.wraps(compute)
def wrapped_func(*args, **kwargs):
# return cached value
if self._computed is not None:
return self._computed
dist_sync_fn = self.dist_sync_fn
if (dist_sync_fn is None
and torch.distributed.is_available()
and torch.distributed.is_initialized()):
# User provided a bool, so we assume DDP if available
dist_sync_fn = gather_all_tensors
synced = False
if self._to_sync and dist_sync_fn is not None:
# ADDED: cache prior to syncing
self._cache = {attr: getattr(self, attr) for attr in self._defaults.keys()}
# sync (this was already there)
self._sync_dist(dist_sync_fn)
synced = True
self._computed = compute(*args, **kwargs)
if synced:
# ADDED: if we synced, restore to cache
for attr, val in self._cache.items():
setattr(self, attr, val)
# NO LONGER RESET
# self.reset()
return self._computed
return wrapped_func Note how we cache the state if syncing, and restore after syncing, so that the accumulation will not be messed up. |
@teddykoker personally I would prefer that solution. I only proposed this way, such that it did not break the current setup. |
What does this PR do?
Fixes #5166
Fixes #4641
Multiple users have requested that it should be able to call
compute()
method of a metric withoutreset()
being called. The argument is that this allows for computing accumulated metrics in a running fashion. This PR adds a new flag to all metrics calledauto_reset_on_compute
that as default isTrue
(current behavior) but when set toFalse
disables the automaticreset()
call after compute. It is then up to the user to manually callreset()
when they want the metric states reset.When this new flag is
False
and the metric is called in ddp mode, we will still reset the metric states on all processes != 0. This is to make sure that we do not over-accumulate the states.Before submitting
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:
Did you have fun?
Make sure you had fun coding 🙃