code: def create_trainer(
      model,
      optimizer,
      criterion,
      lr_scheduler,
      train_sampler,
      config,
      logger,
  ):
      device = idist.device()
  
      # Setup Ignite trainer:
      # - let's define training step
      # - add other common handlers:
      #    - TerminateOnNan,
      #    - handler to setup learning rate scheduling,
      #    - ModelCheckpoint
      #    - RunningAverage` on `train_step` output
      #    - Two progress bars on epochs and optionally on iterations
  
      with_amp = config["with_amp"]
      scaler = GradScaler(enabled=with_amp)
  
      def train_step(engine, batch):
          x, y = batch[0], batch[1]
  
          if x.device != device:
              x = x.to(device, non_blocking=True)
              y = y.to(device, non_blocking=True)
  
          model.train()
  
          with autocast(enabled=with_amp):
              y_pred = model(x)
              loss = criterion(y_pred, y)
  
          optimizer.zero_grad()
          scaler.scale(loss).backward()
          scaler.step(optimizer)
          scaler.update()
  
          return {
              "batch loss": loss.item(),
          }
  
      trainer = Engine(train_step)
      trainer.logger = logger
  
      to_save = {
          "trainer": trainer,
          "model": model,
          "optimizer": optimizer,
          "lr_scheduler": lr_scheduler,
      }
      metric_names = [
          "batch loss",
      ]
  
      common.setup_common_training_handlers(
          trainer=trainer,
          train_sampler=train_sampler,
          to_save=to_save,
          save_every_iters=config["checkpoint_every"],
          save_handler=get_save_handler(config),
          lr_scheduler=lr_scheduler,
          output_names=metric_names
          if config["log_every_iters"] > 0
          else None,
          with_pbars=False,
          clear_cuda_cache=False,
      )
  
      resume_from = config["resume_from"]
      if resume_from is not None:
          checkpoint_fp = Path(resume_from)
          assert (
              checkpoint_fp.exists()
          ), f"Checkpoint '{checkpoint_fp.as_posix()}' is not found"
          logger.info(
              f"Resume from a checkpoint: {checkpoint_fp.as_posix()}"
          )
          checkpoint = torch.load(
              checkpoint_fp.as_posix(), map_location="cpu"
          )
          Checkpoint.load_objects(
              to_load=to_save, checkpoint=checkpoint
          )
  
      return trainer
  
imports: from datetime import datetime
  from pathlib import Path
  from typing import Any, Optional
  import fire
  import torch
  import torch.nn
  import torch.optim
  import utils
  from torch.cuda.amp import autocast, GradScaler
  import ignite
  import ignite.distributed
  from ignite.contrib.engines import common
  from ignite.contrib.handlers import PiecewiseLinear
  from ignite.engine import Engine, Events
  from ignite.handlers import Checkpoint, DiskSaver, global_step_from_engine
  from ignite.metrics import Accuracy, Loss
  from ignite.utils import manual_seed, setup_logger
label_desc: Use image node_id 7 for calling a global function with example usage: create_trainer(model, optimizer, criterion, lr_scheduler, train_sampler, config, logger) and returns: trainer, dict