class_docstr: Custom Parallel Updater for chainer.
  
  Defines the main update routine.
  
  Args:
      train_iter (iterator | dict[str, iterator]): Dataset iterator for the
          training dataset. It can also be a dictionary that maps strings to
          iterators. If this is just an iterator, then the iterator is
          registered by the name ``'main'``.
      optimizer (optimizer | dict[str, optimizer]): Optimizer to update
          parameters. It can also be a dictionary that maps strings to
          optimizers. If this is just an optimizer, then the optimizer is
          registered by the name ``'main'``.
      converter (espnet.asr.chainer_backend.asr.CustomConverter): Converter
          function to build input arrays. Each batch extracted by the main
          iterator and the ``device`` option are passed to this function.
          :func:`chainer.dataset.concat_examples` is used by default.
      device (torch.device): Device to which the training data is sent.
          Negative value
          indicates the host memory (CPU).
      accum_grad (int):The number of gradient accumulation. if set to 2,
          the network parameters will be updated once in twice,
          i.e. actual batchsize will be doubled.
class_docstr_tok: ["Custom","Parallel","Updater","for","chainer",".","Defines","the","main","update","routine",".","Args",":","train_iter","(","iterator","|","dict","[","str",",","iterator","]",")",":","Dataset","iterator","for","the","training","dataset",".","It","can","also","be","a","dictionary","that","maps","strings","to","iterators",".","If","this","is","just","an","iterator",",","then","the","iterator","is","registered","by","the","name","``","'main","'","``",".","optimizer","(","optimizer","|","dict","[","str",",","optimizer","]",")",":","Optimizer","to","update","parameters",".","It","can","also","be","a","dictionary","that","maps","strings","to","optimizers",".","If","this","is","just","an","optimizer",",","then","the","optimizer","is","registered","by","the","name","``","'main","'","``",".","converter","(","espnet.asr.chainer_backend.asr.CustomConverter",")",":","Converter","function","to","build","input","arrays",".","Each","batch","extracted","by","the","main","iterator","and","the","``","device","``","option","are","passed","to","this","function",".",":","func",":","`","chainer.dataset.concat_examples","`","is","used","by","default",".","device","(","torch.device",")",":","Device","to","which","the","training","data","is","sent",".","Negative","value","indicates","the","host","memory","(","CPU",")",".","accum_grad","(","int",")",":","The","number","of","gradient","accumulation",".","if","set","to","2",",","the","network","parameters","will","be","updated","once","in","twice",",","i.e",".","actual","batchsize","will","be","doubled","."]
docstr: Main Update routine of the custom parallel updater.
docstr_tok: ["Main","Update","routine","of","the","custom","parallel","updater","."]
code: def update_core(self):
      """Main Update routine of the custom parallel updater."""
      self.setup_workers()
  
      self._send_message(("update", None))
      with cuda.Device(self._devices[0]):
          # For reducing memory
  
          optimizer = self.get_optimizer("main")
          batch = self.get_iterator("main").next()
          x = self.converter(batch, self._devices[0])
  
          loss = self._master(*x) / self.accum_grad
          loss.backward()
          loss.unchain_backward()
  
          # NCCL: reduce grads
          null_stream = cuda.Stream.null
          if self.comm is not None:
              gg = gather_grads(self._master)
              self.comm.reduce(
                  gg.data.ptr,
                  gg.data.ptr,
                  gg.size,
                  self.nccl.NCCL_FLOAT,
                  self.nccl.NCCL_SUM,
                  0,
                  null_stream.ptr,
              )
              scatter_grads(self._master, gg)
              del gg
  
          # update parameters
          self.forward_count += 1
          if self.forward_count != self.accum_grad:
              return
          self.forward_count = 0
          # check gradient value
          grad_norm = np.sqrt(
              sum_sqnorm(
                  [p.grad for p in optimizer.target.params(False)]
              )
          )
          logging.info("grad norm={}".format(grad_norm))
  
          # update
          if math.isnan(grad_norm):
              logging.warning("grad norm is nan. Do not update model.")
          else:
              optimizer.update()
          self._master.cleargrads()
  
          if self.comm is not None:
              gp = gather_params(self._master)
              self.comm.bcast(
                  gp.data.ptr,
                  gp.size,
                  self.nccl.NCCL_FLOAT,
                  0,
                  null_stream.ptr,
              )
  
imports: import collections
  import logging
  import math
  import numpy
  from chainer import Variable, cuda, training
  from chainer.training.updaters.multiprocess_parallel_updater import gather_grads, gather_params, scatter_grads
label_desc: Use image node_id 2 for calling the CustomParallelUpdater obj's underlying member method code with example usage: obj.update_core() without return types