code: def _test_distrib_integration_engine_early_stopping(device):
      from ignite.metrics import Accuracy
  
      if device is None:
          device = idist.device()
      if isinstance(device, str):
          device = torch.device(device)
      metric_device = device
      if device.type == "xla":
          metric_device = "cpu"
  
      rank = idist.get_rank()
      ws = idist.get_world_size()
      torch.manual_seed(12)
  
      n_epochs = 10
      n_iters = 20
  
      y_preds = (
          [torch.randint(0, 2, size=(n_iters, ws)).to(device)]
          + [torch.ones(n_iters, ws).to(device)]
          + [
              torch.randint(0, 2, size=(n_iters, ws)).to(device)
              for _ in range(n_epochs - 2)
          ]
      )
  
      y_true = (
          [torch.randint(0, 2, size=(n_iters, ws)).to(device)]
          + [torch.ones(n_iters, ws).to(device)]
          + [
              torch.randint(0, 2, size=(n_iters, ws)).to(device)
              for _ in range(n_epochs - 2)
          ]
      )
  
      def update(engine, _):
          e = trainer.state.epoch - 1
          i = engine.state.iteration - 1
          return y_preds[e][i, rank], y_true[e][i, rank]
  
      evaluator = Engine(update)
      acc = Accuracy(device=metric_device)
      acc.attach(evaluator, "acc")
  
      def score_function(engine):
          return engine.state.metrics["acc"]
  
      trainer = Engine(lambda e, b: None)
      early_stopping = EarlyStopping(
          patience=3, score_function=score_function, trainer=trainer
      )
  
      @trainer.on(Events.EPOCH_COMPLETED)
      def evaluation(engine):
          data = list(range(n_iters))
          evaluator.run(data=data)
  
      evaluator.add_event_handler(Events.COMPLETED, early_stopping)
      trainer.run([0], max_epochs=10)
      assert trainer.state.epoch == 5
  
imports: import os
  import pytest
  import torch
  import ignite.distributed
  from ignite.engine import Engine, Events
  from ignite.handlers import EarlyStopping
label_desc: Use image node_id 14 for calling a global function with example usage: _test_distrib_integration_engine_early_stopping(device) and returns: y_preds,  y_true, engine