code: def test_integration_batchwise(usage):
      torch.manual_seed(10)
      alpha = 0.98
      n_iters = 10
      batch_size = 10
      n_classes = 10
      max_epochs = 3
      data = list(range(n_iters))
      loss = torch.arange(n_iters, dtype=torch.float)
      y_true = torch.randint(0, n_classes, size=(n_iters, batch_size))
      y_pred = torch.rand(n_iters, batch_size, n_classes)
  
      accuracy_running_averages = torch.tensor(
          list(
              accumulate(
                  map(
                      lambda y_yp: torch.sum(
                          y_yp[1].argmax(dim=-1) == y_yp[0]
                      ).item()
                      / y_yp[0].size(0),
                      zip(
                          y_true
                          if isinstance(
                              usage, SingleEpochRunningBatchWise
                          )
                          else y_true.repeat(max_epochs, 1),
                          y_pred
                          if isinstance(
                              usage, SingleEpochRunningBatchWise
                          )
                          else y_pred.repeat(max_epochs, 1, 1),
                      ),
                  ),
                  lambda ra, acc: ra * alpha + (1 - alpha) * acc,
              )
          )
      )
      if isinstance(usage, SingleEpochRunningBatchWise):
          accuracy_running_averages = accuracy_running_averages.repeat(
              max_epochs
          )
  
      loss_running_averages = torch.tensor(
          list(
              accumulate(
                  loss
                  if isinstance(usage, SingleEpochRunningBatchWise)
                  else loss.repeat(max_epochs),
                  lambda ra, loss_item: ra * alpha
                  + (1 - alpha) * loss_item,
              )
          )
      )
      if isinstance(usage, SingleEpochRunningBatchWise):
          loss_running_averages = loss_running_averages.repeat(
              max_epochs
          )
  
      def update_fn(_, i):
          loss_value = loss[i]
          y_true_batch = y_true[i]
          y_pred_batch = y_pred[i]
          return loss_value, y_pred_batch, y_true_batch
  
      trainer = Engine(update_fn)
  
      acc_metric = RunningAverage(
          Accuracy(output_transform=lambda x: [x[1], x[2]]), alpha=alpha
      )
      acc_metric.attach(trainer, "running_avg_accuracy", usage)
  
      avg_output = RunningAverage(
          output_transform=lambda x: x[0], alpha=alpha
      )
      avg_output.attach(trainer, "running_avg_loss", usage)
  
      metric_acc_running_averages = []
      metric_loss_running_averages = []
  
      @trainer.on(Events.ITERATION_COMPLETED)
      def _(engine):
          metric_acc_running_averages.append(
              engine.state.metrics["running_avg_accuracy"]
          )
          metric_loss_running_averages.append(
              engine.state.metrics["running_avg_loss"]
          )
  
      trainer.run(data, max_epochs=3)
  
      assert (
          torch.tensor(metric_acc_running_averages)
          == accuracy_running_averages
      ).all()
      assert (
          torch.tensor(metric_loss_running_averages)
          == loss_running_averages
      ).all()
  
      metric_state = acc_metric.state_dict()
      saved__value = acc_metric._value
      saved_src__num_correct = acc_metric.src._num_correct
      saved_src__num_examples = acc_metric.src._num_examples
      acc_metric.reset()
      acc_metric.load_state_dict(metric_state)
      assert acc_metric._value == saved__value
      assert acc_metric.src._num_examples == saved_src__num_examples
      assert (
          acc_metric.src._num_correct == saved_src__num_correct
      ).all()
  
      metric_state = avg_output.state_dict()
      saved__value = avg_output._value
      assert avg_output.src is None
      avg_output.reset()
      avg_output.load_state_dict(metric_state)
      assert avg_output._value == saved__value
      assert avg_output.src is None
  
imports: import warnings
  from functools import partial
  from itertools import accumulate
  import numpy
  import pytest
  import torch
  import ignite.distributed
  from ignite.engine import Engine, Events
  from ignite.metrics import Accuracy, RunningAverage
  from ignite.metrics.metric import RunningBatchWise, RunningEpochWise, SingleEpochRunningBatchWise
label_desc: Use image node_id 3 for calling a global function with example usage: test_integration_batchwise(usage) and returns: loss_value,  y_pred_batch,  y_true_batch