class_docstr: Custom Updater for Pytorch.
  
  Args:
      model (torch.nn.Module): The model to update.
      grad_clip_threshold (float): The gradient clipping value to use.
      train_iter (chainer.dataset.Iterator): The training iterator.
      optimizer (torch.optim.optimizer): The training optimizer.
  
      device (torch.device): The device to use.
      ngpu (int): The number of gpus to use.
      use_apex (bool): The flag to use Apex in backprop.
      use_ddp (bool): The flag to use DDP for multi-GPU training.
class_docstr_tok: ["Custom","Updater","for","Pytorch",".","Args",":","model","(","torch.nn.Module",")",":","The","model","to","update",".","grad_clip_threshold","(","float",")",":","The","gradient","clipping","value","to","use",".","train_iter","(","chainer.dataset.Iterator",")",":","The","training","iterator",".","optimizer","(","torch.optim.optimizer",")",":","The","training","optimizer",".","device","(","torch.device",")",":","The","device","to","use",".","ngpu","(","int",")",":","The","number","of","gpus","to","use",".","use_apex","(","bool",")",":","The","flag","to","use","Apex","in","backprop",".","use_ddp","(","bool",")",":","The","flag","to","use","DDP","for","multi-GPU","training","."]
docstr: Main update routine of the CustomUpdater.
docstr_tok: ["Main","update","routine","of","the","CustomUpdater","."]
code: def update_core(self):
      """Main update routine of the CustomUpdater."""
      # When we pass one iterator and optimizer to StandardUpdater.__init__,
      # they are automatically named 'main'.
      train_iter = self.get_iterator("main")
      optimizer = self.get_optimizer("main")
      epoch = train_iter.epoch
  
      # Get the next batch (a list of json files)
      batch = train_iter.next()
      # self.iteration += 1 # Increase may result in early report,
      # which is done in other place automatically.
      x = _recursive_to(batch, self.device)
      is_new_epoch = train_iter.epoch != epoch
      # When the last minibatch in the current epoch is given,
      # gradient accumulation is turned off in order to evaluate the model
      # on the validation set in every epoch.
      # see details in https://github.com/espnet/espnet/pull/1388
  
      # Compute the loss at this time step and accumulate it
      if self.ngpu == 0 or self.use_ddp:
          loss = self.model(*x).mean() / self.accum_grad
      else:
          # apex does not support torch.nn.DataParallel
          loss = (
              data_parallel(self.model, x, range(self.ngpu)).mean()
              / self.accum_grad
          )
      if self.use_apex:
          from apex import amp
  
          # NOTE: for a compatibility with noam optimizer
          opt = (
              optimizer.optimizer
              if hasattr(optimizer, "optimizer")
              else optimizer
          )
          with amp.scale_loss(loss, opt) as scaled_loss:
              scaled_loss.backward()
      else:
          loss.backward()
      # gradient noise injection
      if self.grad_noise:
          from espnet.asr.asr_utils import add_gradient_noise
  
          add_gradient_noise(
              self.model,
              self.iteration,
              duration=100,
              eta=1.0,
              scale_factor=0.55,
          )
  
      # update parameters
      self.forward_count += 1
      if not is_new_epoch and self.forward_count != self.accum_grad:
          return
      self.forward_count = 0
      # compute the gradient norm to check if it is normal or not
      grad_norm = torch.nn.utils.clip_grad_norm_(
          self.model.parameters(), self.grad_clip_threshold
      )
      if self.use_ddp:
          # NOTE: assuming gradients have not been reduced yet here.
          # Try to gather the norm of gradients from all workers,
          # and calculate average grad norm.
          dist.all_reduce(grad_norm)
      logging.info("grad norm={}".format(grad_norm))
      if math.isnan(grad_norm):
          logging.warning("grad norm is nan. Do not update model.")
      else:
          optimizer.step()
      optimizer.zero_grad()
  
imports: import copy
  import itertools
  import json
  import logging
  import math
  import os
  import numpy
  import torch
  import torch.distributed
  from chainer import reporter
  from chainer import training
  from chainer.training import extensions
  from chainer.training.updater import StandardUpdater
  from packaging.version import parse
  from torch.nn.parallel import DistributedDataParallel
  from torch.nn.parallel import data_parallel
  from torch.utils.data.distributed import DistributedSampler
  import espnet.lm.pytorch_backend.extlm
  import espnet.nets.pytorch_backend.lm.default
  from espnet.asr.asr_utils import CompareValueTrigger, adadelta_eps_decay, add_results_to_json, format_mulenc_args, get_model_conf, plot_spectrogram, restore_snapshot, snapshot_object, torch_load, torch_resume, torch_snapshot
  from espnet.asr.pytorch_backend.asr_init import freeze_modules, load_trained_model, load_trained_modules
  from espnet.nets.asr_interface import ASRInterface
  from espnet.nets.beam_search_transducer import BeamSearchTransducer
  from espnet.nets.pytorch_backend.e2e_asr import pad_list
  from espnet.nets.pytorch_backend.streaming.segment import SegmentStreamingE2E
  from espnet.nets.pytorch_backend.streaming.window import WindowStreamingE2E
  from espnet.transform.spectrogram import IStft
  from espnet.transform.transformation import Transformation
  from espnet.utils.cli_writers import file_writer_helper
  from espnet.utils.dataset import ChainerDataLoader, Transform, TransformDataset
  from espnet.utils.deterministic_utils import set_deterministic_pytorch
  from espnet.utils.dynamic_import import dynamic_import
  from espnet.utils.io_utils import LoadInputsAndTargets
  from espnet.utils.training.batchfy import make_batchset
  from espnet.utils.training.evaluator import BaseEvaluator
  from espnet.utils.training.iterators import ShufflingEnabler
  from espnet.utils.training.tensorboard_logger import TensorboardLogger
  from espnet.utils.training.train_utils import check_early_stop, set_early_stop
label_desc: Use image node_id 2 for calling the CustomUpdater obj's underlying member method code with example usage: obj.update_core() without return types