code: def _test_distrib_all_reduce(device):
      res = idist.all_reduce(10)
      assert res == 10 * idist.get_world_size()
  
      t = torch.tensor(10, device=device)
      res = idist.all_reduce(t)
      assert res.item() == 10 * idist.get_world_size()
  
      rank = idist.get_rank()
      t = torch.tensor(rank * 2.0 + 1.0, device=device)
      res = idist.all_reduce(t)
      assert res.item() == sum(
          [i * 2.0 + 1.0 for i in range(idist.get_world_size())]
      )
  
      t = torch.tensor(rank * 2.0 + 1.0, device=device)
      res = idist.all_reduce(t, "MIN").item()
      true_val = min([i * 2 + 1 for i in range(idist.get_world_size())])
      assert res == true_val, f"{res} vs {true_val}"
  
      t = torch.tensor(rank * 2.0 + 1.0, device=device)
      res = idist.all_reduce(t, "MAX").item()
      true_val = max(
          [i * 2.0 + 1.0 for i in range(idist.get_world_size())]
      )
      assert res == true_val, f"{res} vs {true_val}"
  
      t = torch.ones(4, 4, device=device) * (rank * 2.0 + 1.0)
      res = idist.all_reduce(t, "MAX")
      true_val = torch.ones(4, 4, device=device) * (
          (idist.get_world_size() - 1) * 2.0 + 1.0
      )
      assert res.equal(true_val), f"{res} vs {true_val}"
  
      t = torch.tensor(rank * 2.0 + 1.0, device=device)
      res = idist.all_reduce(t, "PRODUCT").item()
      true_val = 1
      for v in [i * 2.0 + 1.0 for i in range(idist.get_world_size())]:
          true_val *= v
      assert res == true_val, f"{res} vs {true_val}"
  
      if idist.get_world_size() > 1:
          with pytest.raises(TypeError, match=r"Unhandled input type"):
              idist.all_reduce("abc")
  
          with pytest.raises(
              ValueError, match=r"Unsupported reduction operation"
          ):
              idist.all_reduce(10, op="ABC")
  
          t = torch.tensor([0, 1, 2])
          res = idist.all_reduce(t)
          assert res.device == t.device, f"{res.device} vs {t.device}"
  
file_path: ignite/tests/ignite/distributed/utils/__init__.py
imports: import pytest
  import torch
  import torch.distributed
  import ignite.distributed
  from ignite.distributed.utils import sync
  from ignite.engine import Engine, Events
label_desc: Use image node_id 5 for calling a global function with example usage: _test_distrib_all_reduce(device) without return types