code: def _test_distrib_integration_binary_input(device):
      rank = idist.get_rank()
      n_iters = 80
      batch_size = 16
      n_classes = 2
  
      def _test(y_preds, y_true, n_epochs, metric_device, update_fn):
          metric_device = torch.device(metric_device)
  
          engine = Engine(update_fn)
  
          ap = AveragePrecision(device=metric_device)
          ap.attach(engine, "ap")
  
          data = list(range(n_iters))
          engine.run(data=data, max_epochs=n_epochs)
  
          y_true = idist.all_gather(y_true)
          y_preds = idist.all_gather(y_preds)
  
          assert "ap" in engine.state.metrics
  
          res = engine.state.metrics["ap"]
  
          true_res = average_precision_score(
              y_true.cpu().numpy(), y_preds.cpu().numpy()
          )
          assert pytest.approx(res) == true_res
  
      def get_tests(is_N):
          torch.manual_seed(12 + rank)
          if is_N:
              y_true = torch.randint(
                  0, n_classes, size=(n_iters * batch_size,)
              ).to(device)
              y_preds = torch.rand(n_iters * batch_size).to(device)
  
              def update_fn(engine, i):
                  return (
                      y_preds[i * batch_size : (i + 1) * batch_size],
                      y_true[i * batch_size : (i + 1) * batch_size],
                  )
  
          else:
              y_true = torch.randint(
                  0, n_classes, size=(n_iters * batch_size, 10)
              ).to(device)
              y_preds = torch.randint(
                  0, n_classes, size=(n_iters * batch_size, 10)
              ).to(device)
  
              def update_fn(engine, i):
                  return (
                      y_preds[i * batch_size : (i + 1) * batch_size, :],
                      y_true[i * batch_size : (i + 1) * batch_size, :],
                  )
  
          return y_preds, y_true, update_fn
  
      metric_devices = ["cpu"]
      if device.type != "xla":
          metric_devices.append(idist.device())
      for metric_device in metric_devices:
          for _ in range(2):
              # Binary input data of shape (N,)
              y_preds, y_true, update_fn = get_tests(is_N=True)
              _test(
                  y_preds,
                  y_true,
                  n_epochs=1,
                  metric_device=metric_device,
                  update_fn=update_fn,
              )
              _test(
                  y_preds,
                  y_true,
                  n_epochs=2,
                  metric_device=metric_device,
                  update_fn=update_fn,
              )
              # Binary input data of shape (N, L)
              y_preds, y_true, update_fn = get_tests(is_N=False)
              _test(
                  y_preds,
                  y_true,
                  n_epochs=1,
                  metric_device=metric_device,
                  update_fn=update_fn,
              )
              _test(
                  y_preds,
                  y_true,
                  n_epochs=2,
                  metric_device=metric_device,
                  update_fn=update_fn,
              )
  
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 11 for calling a global function with example usage: _test_distrib_integration_binary_input(device) and returns: y_preds,  y_true,  update_fn, y_preds,  y_true, y_preds,  y_true