code: def _test_distrib_metrics_on_diff_devices(device):
      n_classes = 10
      n_iters = 12
      batch_size = 16
      rank = idist.get_rank()
      torch.manual_seed(12 + rank)
  
      y_true = torch.randint(
          0, n_classes, size=(n_iters * batch_size,)
      ).to(device)
      y_preds = torch.rand(n_iters * batch_size, n_classes).to(device)
  
      def update(engine, i):
          return (
              y_preds[i * batch_size : (i + 1) * batch_size, :],
              y_true[i * batch_size : (i + 1) * batch_size],
          )
  
      evaluator = Engine(update)
  
      precision = Precision(average=False, device="cpu")
      recall = Recall(average=False, device=device)
  
      def Fbeta(r, p, beta):
          return torch.mean(
              (1 + beta**2) * p * r / (beta**2 * p + r)
          ).item()
  
      F1 = MetricsLambda(Fbeta, recall, precision, 1)
      F1.attach(evaluator, "f1")
  
      another_f1 = (
          (1.0 + precision * recall * 2 / (precision + recall + 1e-20))
          .mean()
          .item()
      )
      another_f1.attach(evaluator, "ff1")
  
      data = list(range(n_iters))
      state = evaluator.run(data, max_epochs=1)
  
      y_preds = idist.all_gather(y_preds)
      y_true = idist.all_gather(y_true)
  
      assert "f1" in state.metrics
      assert "ff1" in state.metrics
      f1_true = f1_score(
          y_true.view(-1).cpu(),
          y_preds.view(-1, n_classes).argmax(dim=-1).cpu(),
          average="macro",
      )
      assert f1_true == approx(state.metrics["f1"])
      assert 1.0 + f1_true == approx(state.metrics["ff1"])
  
imports: import os
  import numpy
  import pytest
  import torch
  from pytest import approx
  from sklearn.metrics import f1_score, precision_score, recall_score
  import ignite.distributed
  from ignite.engine import Engine
  from ignite.metrics import Accuracy, Metric, MetricsLambda, Precision, Recall
label_desc: Use image node_id 11 for calling a global function with example usage: _test_distrib_metrics_on_diff_devices(device) and returns: y_preds,  y_true, torch