code: def _test_distrib_with_engine_early_stopping(device):
      if device is None:
          device = idist.device()
      if isinstance(device, str):
          device = torch.device(device)
  
      torch.manual_seed(12)
  
      class Counter(object):
          def __init__(self, count=0):
              self.count = count
  
      n_epochs_counter = Counter()
  
      scores = torch.tensor(
          [1.0, 0.8, 1.2, 1.5, 0.9, 1.0, 0.99, 1.1, 0.9],
          requires_grad=False,
      ).to(device)
  
      def score_function(engine):
          i = trainer.state.epoch - 1
          v = scores[i]
          idist.all_reduce(v)
          v /= idist.get_world_size()
          return v.item()
  
      trainer = Engine(do_nothing_update_fn)
      evaluator = Engine(do_nothing_update_fn)
      early_stopping = EarlyStopping(
          patience=3, score_function=score_function, trainer=trainer
      )
  
      @trainer.on(Events.EPOCH_COMPLETED)
      def evaluation(engine):
          evaluator.run([0])
          n_epochs_counter.count += 1
  
      evaluator.add_event_handler(Events.COMPLETED, early_stopping)
      trainer.run([0], max_epochs=10)
      assert trainer.state.epoch == 7
      assert n_epochs_counter.count == 7
  
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 13 for calling a global function with example usage: _test_distrib_with_engine_early_stopping(device) and returns: v