code: def test_metric_lightning_log(tmpdir):
      """Test logging a metric object and that the metric state gets reset after each epoch."""
  
      class TestModel(BoringModel):
          def __init__(self) -> None:
              super().__init__()
  
              # initialize one metric for every combination of `on_step` and `on_epoch` and `forward` and `update`
              self.metric_update = SumMetric()
              self.metric_update_step = SumMetric()
              self.metric_update_epoch = SumMetric()
  
              self.metric_forward = SumMetric()
              self.metric_forward_step = SumMetric()
              self.metric_forward_epoch = SumMetric()
  
              self.compo_update = SumMetric() + SumMetric()
              self.compo_update_step = SumMetric() + SumMetric()
              self.compo_update_epoch = SumMetric() + SumMetric()
  
              self.compo_forward = SumMetric() + SumMetric()
              self.compo_forward_step = SumMetric() + SumMetric()
              self.compo_forward_epoch = SumMetric() + SumMetric()
  
              self.sum = []
  
          def training_step(self, batch, batch_idx):
              x = batch
              s = x.sum()
  
              for metric in [
                  self.metric_update,
                  self.metric_update_step,
                  self.metric_update_epoch,
              ]:
                  metric.update(s)
              for metric in [
                  self.metric_forward,
                  self.metric_forward_step,
                  self.metric_forward_epoch,
              ]:
                  _ = metric(s)
              for metric in [
                  self.compo_update,
                  self.compo_update_step,
                  self.compo_update_epoch,
              ]:
                  metric.update(s)
              for metric in [
                  self.compo_forward,
                  self.compo_forward_step,
                  self.compo_forward_epoch,
              ]:
                  _ = metric(s)
  
              self.sum.append(s)
  
              self.log("metric_update", self.metric_update)
              self.log(
                  "metric_update_step",
                  self.metric_update_step,
                  on_epoch=False,
                  on_step=True,
              )
              self.log(
                  "metric_update_epoch",
                  self.metric_update_epoch,
                  on_epoch=True,
                  on_step=False,
              )
  
              self.log("metric_forward", self.metric_forward)
              self.log(
                  "metric_forward_step",
                  self.metric_forward_step,
                  on_epoch=False,
                  on_step=True,
              )
              self.log(
                  "metric_forward_epoch",
                  self.metric_forward_epoch,
                  on_epoch=True,
                  on_step=False,
              )
  
              self.log("compo_update", self.compo_update)
              self.log(
                  "compo_update_step",
                  self.compo_update_step,
                  on_epoch=False,
                  on_step=True,
              )
              self.log(
                  "compo_update_epoch",
                  self.compo_update_epoch,
                  on_epoch=True,
                  on_step=False,
              )
  
              self.log("compo_forward", self.compo_forward)
              self.log(
                  "compo_forward_step",
                  self.compo_forward_step,
                  on_epoch=False,
                  on_step=True,
              )
              self.log(
                  "compo_forward_epoch",
                  self.compo_forward_epoch,
                  on_epoch=True,
                  on_step=False,
              )
  
              return self.step(x)
  
      model = TestModel()
  
      logger = CSVLogger("tmpdir/logs")
      trainer = Trainer(
          default_root_dir=tmpdir,
          limit_train_batches=2,
          limit_val_batches=0,
          max_epochs=2,
          log_every_n_steps=1,
          logger=logger,
      )
      with no_warning_call(
          UserWarning,
          match="Torchmetrics v0.9 introduced a new argument class property called.*",
      ):
          trainer.fit(model)
  
      logged_metrics = logger._experiment.metrics
  
      epoch_0_step_0 = logged_metrics[0]
      assert "metric_forward" in epoch_0_step_0
      assert epoch_0_step_0["metric_forward"] == model.sum[0]
      assert "metric_forward_step" in epoch_0_step_0
      assert epoch_0_step_0["metric_forward_step"] == model.sum[0]
      assert "compo_forward" in epoch_0_step_0
      assert epoch_0_step_0["compo_forward"] == 2 * model.sum[0]
      assert "compo_forward_step" in epoch_0_step_0
      assert epoch_0_step_0["compo_forward_step"] == 2 * model.sum[0]
  
      epoch_0_step_1 = logged_metrics[1]
      assert "metric_forward" in epoch_0_step_1
      assert epoch_0_step_1["metric_forward"] == model.sum[1]
      assert "metric_forward_step" in epoch_0_step_1
      assert epoch_0_step_1["metric_forward_step"] == model.sum[1]
      assert "compo_forward" in epoch_0_step_1
      assert epoch_0_step_1["compo_forward"] == 2 * model.sum[1]
      assert "compo_forward_step" in epoch_0_step_1
      assert epoch_0_step_1["compo_forward_step"] == 2 * model.sum[1]
  
      epoch_0 = logged_metrics[2]
      assert "metric_update_epoch" in epoch_0
      assert epoch_0["metric_update_epoch"] == sum(
          [model.sum[0], model.sum[1]]
      )
      assert "metric_forward_epoch" in epoch_0
      assert epoch_0["metric_forward_epoch"] == sum(
          [model.sum[0], model.sum[1]]
      )
      assert "compo_update_epoch" in epoch_0
      assert epoch_0["compo_update_epoch"] == 2 * sum(
          [model.sum[0], model.sum[1]]
      )
      assert "compo_forward_epoch" in epoch_0
      assert epoch_0["compo_forward_epoch"] == 2 * sum(
          [model.sum[0], model.sum[1]]
      )
  
      epoch_1_step_0 = logged_metrics[3]
      assert "metric_forward" in epoch_1_step_0
      assert epoch_1_step_0["metric_forward"] == model.sum[2]
      assert "metric_forward_step" in epoch_1_step_0
      assert epoch_1_step_0["metric_forward_step"] == model.sum[2]
      assert "compo_forward" in epoch_1_step_0
      assert epoch_1_step_0["compo_forward"] == 2 * model.sum[2]
      assert "compo_forward_step" in epoch_1_step_0
      assert epoch_1_step_0["compo_forward_step"] == 2 * model.sum[2]
  
      epoch_1_step_1 = logged_metrics[4]
      assert "metric_forward" in epoch_1_step_1
      assert epoch_1_step_1["metric_forward"] == model.sum[3]
      assert "metric_forward_step" in epoch_1_step_1
      assert epoch_1_step_1["metric_forward_step"] == model.sum[3]
      assert "compo_forward" in epoch_1_step_1
      assert epoch_1_step_1["compo_forward"] == 2 * model.sum[3]
      assert "compo_forward_step" in epoch_1_step_1
      assert epoch_1_step_1["compo_forward_step"] == 2 * model.sum[3]
  
      epoch_1 = logged_metrics[5]
      assert "metric_update_epoch" in epoch_1
      assert epoch_1["metric_update_epoch"] == sum(
          [model.sum[2], model.sum[3]]
      )
      assert "metric_forward_epoch" in epoch_1
      assert epoch_1["metric_forward_epoch"] == sum(
          [model.sum[2], model.sum[3]]
      )
      assert "compo_update_epoch" in epoch_1
      assert epoch_1["compo_update_epoch"] == 2 * sum(
          [model.sum[2], model.sum[3]]
      )
      assert "compo_forward_epoch" in epoch_1
      assert epoch_1["compo_forward_epoch"] == 2 * sum(
          [model.sum[2], model.sum[3]]
      )
  
imports: from unittest import mock
  import torch
  from lightning_utilities import module_available
  from torch import tensor
  from torch.nn import Linear
  from torchmetrics import MetricCollection
  from torchmetrics.aggregation import SumMetric
  from torchmetrics.classification import BinaryAccuracy, BinaryAveragePrecision
  from torchmetrics.regression import MeanSquaredError
  from torchmetrics.wrappers import MultitaskWrapper
  from integrations.helpers import no_warning_call
  from integrations.lightning.boring_model import BoringModel
label_desc: Use image node_id 3 for calling a global function with example usage: test_metric_lightning_log(tmpdir) and returns: self