code: def log_model_grads(engine, model=None, fp=None, **kwargs):
      """Helper method to log norms of model gradients: print and dump into a file"""
      assert model and fp
      output = {"grads/total": 0.0}
      max_counter = 5
      for name, p in model.named_parameters():
          if p.grad is None:
              continue
          name = name.replace(".", "/")
          n = torch.norm(p.grad)
          if max_counter > 0:
              output[f"grads/{name}"] = n
          output["grads/total"] += n
          max_counter -= 1
  
      output_items = " - ".join(
          [f"{m}:{v:.4f}" for m, v in output.items()]
      )
      msg = f"{engine.state.epoch} | {engine.state.iteration}: {output_items}"
  
      with open(fp, "a") as h:
          h.write(msg)
          h.write("\n")
  
imports: from argparse import ArgumentParser
  from pathlib import Path
  import torch
  import torch.nn.functional
  from torch import nn
  from torch.optim import SGD
  from torch.optim.lr_scheduler import StepLR
  from torch.utils.data import DataLoader
  from torchvision.datasets import MNIST
  from torchvision.transforms import Compose, Normalize, ToTensor
  from tqdm import tqdm
  from ignite.engine import create_supervised_evaluator, create_supervised_trainer, Events
  from ignite.handlers import Checkpoint, DiskSaver
  from ignite.metrics import Accuracy, Loss, RunningAverage
  from ignite.utils import manual_seed
label_desc: Use image node_id 3 for calling a global function with example usage: log_model_grads(engine, model, fp) without return types