code: def _test_distrib_integration(device):
      from ignite.engine import Engine
  
      def _test(n_epochs, metric_device):
          n_iters = 100
          batch_size = 16
          n_classes = 10
  
          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],
              )
  
          engine = Engine(update)
  
          k = 5
          acc = TopKCategoricalAccuracy(k=k, device=metric_device)
          acc.attach(engine, "acc")
  
          data = list(range(n_iters))
          engine.run(data=data, max_epochs=n_epochs)
  
          y_preds = idist.all_gather(y_preds)
          y_true = idist.all_gather(y_true)
  
          assert "acc" in engine.state.metrics
          res = engine.state.metrics["acc"]
          if isinstance(res, torch.Tensor):
              res = res.cpu().numpy()
  
          true_res = top_k_accuracy(
              y_true.cpu().numpy(), y_preds.cpu().numpy(), k=k
          )
  
          assert pytest.approx(res) == true_res
  
      metric_devices = ["cpu"]
      if device.type != "xla":
          metric_devices.append(idist.device())
      rank = idist.get_rank()
      for i in range(3):
          torch.manual_seed(12 + rank + i)
          for metric_device in metric_devices:
              _test(n_epochs=1, metric_device=metric_device)
              _test(n_epochs=2, metric_device=metric_device)
  
file_path: ignite/tests/ignite/metrics/test_top_k_categorical_accuracy.py
imports: import os
  import pytest
  import torch
  import ignite.distributed
  from ignite.exceptions import NotComputableError
  from ignite.metrics import TopKCategoricalAccuracy
label_desc: Use image node_id 4 for calling a global function with example usage: _test_distrib_integration(device) and returns: y_preds,  y_true