code: def test_gen_save_best_models_by_val_score():
      acc_scores = [0.1, 0.2, 0.3, 0.4, 0.3, 0.5, 0.6, 0.61, 0.7, 0.5]
      loss_scores = [0.9, 0.8, 0.7, 0.6, 0.7, 0.5, 0.4, 0.39, 0.3, 0.5]
  
      def setup_trainer():
          trainer = Engine(lambda e, b: None)
          evaluator = Engine(lambda e, b: None)
          model = DummyModel()
  
          @trainer.on(Events.EPOCH_COMPLETED)
          def validate(engine):
              evaluator.run([0, 1])
  
          @evaluator.on(Events.EPOCH_COMPLETED)
          def set_eval_metric(engine):
              acc = acc_scores[trainer.state.epoch - 1]
              loss = loss_scores[trainer.state.epoch - 1]
              engine.state.metrics = {"acc": acc, "loss": loss}
  
          return trainer, evaluator, model
  
      trainer, evaluator, model = setup_trainer()
  
      save_handler = MagicMock()
  
      gen_save_best_models_by_val_score(
          save_handler,
          evaluator,
          {"a": model, "b": model},
          metric_name="acc",
          n_saved=2,
          trainer=trainer,
      )
  
      trainer.run([0, 1], max_epochs=len(acc_scores))
  
      assert (
          save_handler.call_count == len(acc_scores) - 2
      )  # 2 score values (0.3 and 0.5) are not the best
      obj_to_save = {"a": model.state_dict(), "b": model.state_dict()}
      save_handler.assert_has_calls(
          [
              call(
                  obj_to_save,
                  f"best_checkpoint_{e}_val_acc={p:.4f}.pt",
                  dict(
                      [
                          ("basename", "best_checkpoint"),
                          ("score_name", "val_acc"),
                          ("priority", p),
                      ]
                  ),
              )
              for e, p in zip(
                  [1, 2, 3, 4, 6, 7, 8, 9],
                  [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.61, 0.7],
              )
          ],
          any_order=True,
      )
  
      trainer, evaluator, model = setup_trainer()
  
      save_handler = MagicMock()
  
      gen_save_best_models_by_val_score(
          save_handler,
          evaluator,
          {"a": model, "b": model},
          metric_name="loss",
          n_saved=2,
          trainer=trainer,
          score_sign=-1.0,
      )
  
      trainer.run([0, 1], max_epochs=len(acc_scores))
  
      assert (
          save_handler.call_count == len(acc_scores) - 2
      )  # 2 score values (-0.7 and -0.5) are not the best
      obj_to_save = {"a": model.state_dict(), "b": model.state_dict()}
      save_handler.assert_has_calls(
          [
              call(
                  obj_to_save,
                  f"best_checkpoint_{e}_val_loss={p:.4f}.pt",
                  dict(
                      [
                          ("basename", "best_checkpoint"),
                          ("score_name", "val_loss"),
                          ("priority", p),
                      ]
                  ),
              )
              for e, p in zip(
                  [1, 2, 3, 4, 6, 7, 8, 9],
                  [-0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.39, -0.3],
              )
          ],
          any_order=True,
      )
  
imports: import os
  import sys
  from unittest.mock import call, MagicMock
  import pytest
  import torch
  import torch.nn
  from torch.utils.data.distributed import DistributedSampler
  import ignite.contrib.handlers
  import ignite.distributed
  from ignite.contrib.engines.common import _setup_logging, add_early_stopping_by_val_score, gen_save_best_models_by_val_score, save_best_model_by_val_score, setup_any_logging, setup_clearml_logging, setup_common_training_handlers, setup_mlflow_logging, setup_neptune_logging, setup_plx_logging, setup_tb_logging, setup_trains_logging, setup_visdom_logging, setup_wandb_logging
  from ignite.engine import Engine, Events
  from ignite.handlers import DiskSaver, TerminateOnNan
label_desc: Use image node_id 8 for calling a global function with example usage: test_gen_save_best_models_by_val_score() and returns: trainer,  evaluator,  model