code: def _test_distrib_compute_on_criterion(
      device, y_test_1, y_test_2, tol=None
  ):
      def _test(metric_device, y_test_1, y_test_2):
          criterion = nn.NLLLoss().to(device)
          loss = Loss(criterion, device=metric_device)
  
          y_pred, y, _ = y_test_1
          loss.update((y_pred, y))
          n = loss._num_examples
          assert n == len(y)
          res = loss.compute()
          assert n == loss._num_examples
  
          y_pred = idist.all_gather(y_pred)
          y = idist.all_gather(y)
          true_loss_value = criterion(y_pred, y)
          assert_almost_equal(res, true_loss_value.item())
  
          loss.reset()
          y_pred, y, _ = y_test_2
          loss.update((y_pred, y))
          n = loss._num_examples
          res = loss.compute()
          assert n == loss._num_examples
  
          y_pred = idist.all_gather(y_pred)
          y = idist.all_gather(y)
          true_loss_value = criterion(y_pred, y)
          if tol is None:
              assert_almost_equal(res, true_loss_value.item())
          else:
              assert (
                  pytest.approx(res, rel=tol) == true_loss_value.item()
              )
  
      _test("cpu", y_test_1, y_test_2)
      if device.type != "xla":
          _test(idist.device(), y_test_1, y_test_2)
  
imports: import os
  from unittest.mock import MagicMock
  import pytest
  import torch
  from numpy.testing import assert_almost_equal
  from torch import nn
  from torch.nn.functional import nll_loss
  import ignite.distributed
  from ignite.engine import State
  from ignite.exceptions import NotComputableError
  from ignite.metrics import Loss, Precision
label_desc: Use image node_id 12 for calling a global function with example usage: _test_distrib_compute_on_criterion(device, y_test_1, y_test_2, tol) without return types