Repository: Lightning-AI/torchmetrics

Issue:
False positive warning about calling `compute()` before `update()`
## 🐛 Bug

I have an example using `RunningMean` that warns the user about calling `compute()` before `update()`, even though it isn't true.

### To Reproduce

```py
from torchmetrics.aggregation import RunningMean
import torch


metric = RunningMean(window=3)
metric.update(torch.tensor(2.))  # also happens if calling forward
print(metric.compute())
```

Output:
```
/home/adrian/.conda/envs/lit-gpt/lib/python3.10/site-packages/torchmetrics/utilities/prints.py:43: UserWarning: The ``compute`` method of metric MeanMetric was called before the ``update`` method which may lead to errors, as metric states have not yet been updated.
  warnings.warn(*args, **kwargs)  # noqa: B028
tensor(2.)
```

### Expected behavior

No warning, because the order of update and compute is correct.

### Environment

- TorchMetrics version (and how you installed TM, e.g. `conda`, `pip`, build from source): 1.2.1 (pip)
- Python & PyTorch Version (e.g., 1.0): 3.10, 2.2.0.dev20231207+cu121
- Any other relevant information such as OS (e.g., Linux): Linux

### Additional context

<!-- Add any other context about the problem here. -->



Discussion:

Resolving patch:
diff --git a/src/torchmetrics/metric.py b/src/torchmetrics/metric.py
--- a/src/torchmetrics/metric.py
+++ b/src/torchmetrics/metric.py
@@ -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,"
diff --git a/src/torchmetrics/wrappers/running.py b/src/torchmetrics/wrappers/running.py
--- a/src/torchmetrics/wrappers/running.py
+++ b/src/torchmetrics/wrappers/running.py
@@ -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


Test patch:
diff --git a/tests/unittests/wrappers/test_running.py b/tests/unittests/wrappers/test_running.py
--- a/tests/unittests/wrappers/test_running.py
+++ b/tests/unittests/wrappers/test_running.py
@@ -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():
@@ -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()