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

Supress warning for running metric wrapper #2265

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

-
- Fixed warning incorrectly being raised in `Running` metrics ([#2256](https://github.com/Lightning-AI/torchmetrics/pull/2265))


## [1.2.1] - 2023-11-30
Expand Down
2 changes: 1 addition & 1 deletion src/torchmetrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def sync_context(
def _wrap_compute(self, compute: Callable) -> Callable:
@functools.wraps(compute)
def wrapped_func(*args: Any, **kwargs: Any) -> Any:
if self._update_count == 0:
if not self.update_called:
rank_zero_warn(
f"The ``compute`` method of metric {self.__class__.__name__}"
" was called before the ``update`` method which may lead to errors,"
Expand Down
1 change: 1 addition & 0 deletions src/torchmetrics/wrappers/running.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def compute(self) -> Any:
"""Compute the metric over the running window."""
for i in range(self.window):
self.base_metric._reduce_states({key: getattr(self, key + f"_{i}") for key in self.base_metric._defaults})
self.base_metric._update_count = self._num_vals_seen
val = self.base_metric.compute()
self.base_metric.reset()
return val
Expand Down
9 changes: 9 additions & 0 deletions tests/unittests/wrappers/test_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from torchmetrics.wrappers import Running

from unittests import NUM_PROCESSES
from unittests.helpers.utilities import no_warning_call


def test_errors_on_wrong_input():
Expand Down Expand Up @@ -150,3 +151,11 @@ def test_ddp_running(dist_sync_on_step, expected):
pytest.pool.map(
partial(_test_ddp_running, dist_sync_on_step=dist_sync_on_step, expected=expected), range(NUM_PROCESSES)
)


def test_no_warning_due_to_reset():
"""Internally we call .reset() which would normally raise a warning, but it should not happen in Runner."""
metric = Running(SumMetric(), window=3)
metric.update(torch.tensor(2.0))
with no_warning_call(UserWarning):
metric.compute()
Loading