code: def _test_distrib_binary_and_multilabel_inputs(device):
      rank = idist.get_rank()
      torch.manual_seed(12)
  
      def _test(y_pred, y, batch_size, metric_device):
          metric_device = torch.device(metric_device)
          ap = AveragePrecision(device=metric_device)
          torch.manual_seed(10 + rank)
  
          ap.reset()
  
          if batch_size > 1:
              n_iters = y.shape[0] // batch_size + 1
              for i in range(n_iters):
                  idx = i * batch_size
                  ap.update(
                      (
                          y_pred[idx : idx + batch_size],
                          y[idx : idx + batch_size],
                      )
                  )
          else:
              ap.update((y_pred, y))
  
          # gather y_pred, y
          y_pred = idist.all_gather(y_pred)
          y = idist.all_gather(y)
  
          np_y = y.cpu().numpy()
          np_y_pred = y_pred.cpu().numpy()
  
          res = ap.compute()
          assert isinstance(res, float)
          assert average_precision_score(
              np_y, np_y_pred
          ) == pytest.approx(res)
  
      def get_test_cases():
          test_cases = [
              # Binary input data of shape (N,) or (N, 1)
              (
                  torch.randint(0, 2, size=(10,)).long(),
                  torch.randint(0, 2, size=(10,)).long(),
                  1,
              ),
              (
                  torch.randint(0, 2, size=(10, 1)).long(),
                  torch.randint(0, 2, size=(10, 1)).long(),
                  1,
              ),
              # updated batches
              (
                  torch.randint(0, 2, size=(50,)).long(),
                  torch.randint(0, 2, size=(50,)).long(),
                  16,
              ),
              (
                  torch.randint(0, 2, size=(50, 1)).long(),
                  torch.randint(0, 2, size=(50, 1)).long(),
                  16,
              ),
              # Binary input data of shape (N, L)
              (
                  torch.randint(0, 2, size=(10, 4)).long(),
                  torch.randint(0, 2, size=(10, 4)).long(),
                  1,
              ),
              (
                  torch.randint(0, 2, size=(10, 7)).long(),
                  torch.randint(0, 2, size=(10, 7)).long(),
                  1,
              ),
              # updated batches
              (
                  torch.randint(0, 2, size=(50, 4)).long(),
                  torch.randint(0, 2, size=(50, 4)).long(),
                  16,
              ),
              (
                  torch.randint(0, 2, size=(50, 7)).long(),
                  torch.randint(0, 2, size=(50, 7)).long(),
                  16,
              ),
          ]
          return test_cases
  
      for _ in range(3):
          test_cases = get_test_cases()
          for y_pred, y, batch_size in test_cases:
              y_pred = y_pred.to(device)
              y = y.to(device)
              _test(y_pred, y, batch_size, "cpu")
              if device.type != "xla":
                  _test(y_pred, y, batch_size, idist.device())
  
imports: import os
  from unittest.mock import patch
  import pytest
  import sklearn
  import torch
  from sklearn.metrics import average_precision_score
  import ignite.distributed
  from ignite.contrib.metrics import AveragePrecision
  from ignite.engine import Engine
  from ignite.exceptions import NotComputableError
label_desc: Use image node_id 10 for calling a global function with example usage: _test_distrib_binary_and_multilabel_inputs(device) and returns: test_cases