class_docstr: Implements LAMB algorithm.
  
  Currently GPU-only.  Requires Apex to be installed via
  ``pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./``.
  
  This version of fused LAMB implements 2 fusions.
  
    * Fusion of the LAMB update's elementwise operations
    * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches.
  
  :class:`apex.optimizers.FusedLAMB`'s usage is identical to any ordinary Pytorch optimizer::
  
      opt = apex.optimizers.FusedLAMB(model.parameters(), lr = ....)
      ...
      opt.step()
  
  :class:`apex.optimizers.FusedLAMB` may be used with or without Amp.  If you wish to use :class:`FusedLAMB` with Amp,
  you may choose any ``opt_level``::
  
      opt = apex.optimizers.FusedLAMB(model.parameters(), lr = ....)
      model, opt = amp.initialize(model, opt, opt_level="O0" or "O1 or "O2")
      ...
      opt.step()
  
  In general, ``opt_level="O1"`` is recommended.
  
  LAMB was proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_.
  
  Arguments:
      params (iterable): iterable of parameters to optimize or dicts defining
          parameter groups.
      lr (float, optional): learning rate. (default: 1e-3)
      betas (Tuple[float, float], optional): coefficients used for computing
          running averages of gradient and its norm. (default: (0.9, 0.999))
      eps (float, optional): term added to the denominator to improve
          numerical stability. (default: 1e-8)
      weight_decay (float, optional): weight decay (L2 penalty) (default: 0)
      amsgrad (boolean, optional): whether to use the AMSGrad variant of this
          algorithm from the paper `On the Convergence of Adam and Beyond`_
          NOT SUPPORTED now! (default: False)
      adam_w_mode (boolean, optional): Apply L2 regularization or weight decay
          True for decoupled weight decay(also known as AdamW) (default: True)
      grad_averaging (bool, optional): whether apply (1-beta2) to grad when
          calculating running averages of gradient. (default: True)
      set_grad_none (bool, optional): whether set grad to None when zero_grad()
          method is called. (default: True)
      max_grad_norm (float, optional): value used to clip global grad norm
          (default: 1.0)
      use_nvlamb (boolean, optional): Apply adaptive learning rate to 0.0
          weight decay parameter (default: False)
      step_supports_amp_scaling(boolean, optional): whether to use customized
          gradient unscaling logic (default: True)
  
  .. _Large Batch Optimization for Deep Learning - Training BERT in 76 minutes:
      https://arxiv.org/abs/1904.00962
  .. _On the Convergence of Adam and Beyond:
      https://openreview.net/forum?id=ryQu7f-RZ
class_docstr_tok: ["Implements","LAMB","algorithm",".","Currently","GPU-only",".","Requires","Apex","to","be","installed","via","``","pip","install","-v","--","no-cache-dir","--","global-option=","''","--","cpp_ext","''","--","global-option=","''","--","cuda_ext","''",".\/","``",".","This","version","of","fused","LAMB","implements","2","fusions",".","*","Fusion","of","the","LAMB","update","'s","elementwise","operations","*","A","multi-tensor","apply","launch","that","batches","the","elementwise","updates","applied","to","all","the","model","'s","parameters","into","one","or","a","few","kernel","launches",".",":","class",":","`","apex.optimizers.FusedLAMB","`","'s","usage","is","identical","to","any","ordinary","Pytorch","optimizer",":",":","opt","=","apex.optimizers.FusedLAMB","(","model.parameters","(",")",",","lr","=","....",")","...","opt.step","(",")",":","class",":","`","apex.optimizers.FusedLAMB","`","may","be","used","with","or","without","Amp",".","If","you","wish","to","use",":","class",":","`","FusedLAMB","`","with","Amp",",","you","may","choose","any","``","opt_level","``",":",":","opt","=","apex.optimizers.FusedLAMB","(","model.parameters","(",")",",","lr","=","....",")","model",",","opt","=","amp.initialize","(","model",",","opt",",","opt_level=","''","O0","''","or","``","O1","or","``","O2","''",")","...","opt.step","(",")","In","general",",","``","opt_level=","''","O1","''","``","is","recommended",".","LAMB","was","proposed","in","`","Large","Batch","Optimization","for","Deep","Learning",":","Training","BERT","in","76","minutes","`","_",".","Arguments",":","params","(","iterable",")",":","iterable","of","parameters","to","optimize","or","dicts","defining","parameter","groups",".","lr","(","float",",","optional",")",":","learning","rate",".","(","default",":","1e-3",")","betas","(","Tuple","[","float",",","float","]",",","optional",")",":","coefficients","used","for","computing","running","averages","of","gradient","and","its","norm",".","(","default",":","(","0.9",",","0.999",")",")","eps","(","float",",","optional",")",":","term","added","to","the","denominator","to","improve","numerical","stability",".","(","default",":","1e-8",")","weight_decay","(","float",",","optional",")",":","weight","decay","(","L2","penalty",")","(","default",":","0",")","amsgrad","(","boolean",",","optional",")",":","whether","to","use","the","AMSGrad","variant","of","this","algorithm","from","the","paper","`","On","the","Convergence","of","Adam","and","Beyond","`","_","NOT","SUPPORTED","now","!","(","default",":","False",")","adam_w_mode","(","boolean",",","optional",")",":","Apply","L2","regularization","or","weight","decay","True","for","decoupled","weight","decay","(","also","known","as","AdamW",")","(","default",":","True",")","grad_averaging","(","bool",",","optional",")",":","whether","apply","(","1-beta2",")","to","grad","when","calculating","running","averages","of","gradient",".","(","default",":","True",")","set_grad_none","(","bool",",","optional",")",":","whether","set","grad","to","None","when","zero_grad","(",")","method","is","called",".","(","default",":","True",")","max_grad_norm","(","float",",","optional",")",":","value","used","to","clip","global","grad","norm","(","default",":","1.0",")","use_nvlamb","(","boolean",",","optional",")",":","Apply","adaptive","learning","rate","to","0.0","weight","decay","parameter","(","default",":","False",")","step_supports_amp_scaling","(","boolean",",","optional",")",":","whether","to","use","customized","gradient","unscaling","logic","(","default",":","True",")","..","_Large","Batch","Optimization","for","Deep","Learning","-","Training","BERT","in","76","minutes",":","https",":","\/\/arxiv.org\/abs\/1904.00962","..","_On","the","Convergence","of","Adam","and","Beyond",":","https",":","\/\/openreview.net\/forum","?","id=ryQu7f-RZ"]
code: def _pipeline_block_reductions(self, block_id):
      if self._clip_after_ar:
          self._flatten_grad_mt(1.0 / self._world_size)
  
          if self._full_ar:
              self._full_all_reduce(block_id)
          else:
              self._reduce_scatter_and_all_reduce(block_id)
  
          # Compute L2 grad norm
          if block_id == 0:
              with torch.cuda.stream(self._l2_grad_norm_st):
                  for block_id in range(self._num_blocks):
                      for chunk_id in range(self._num_chunks):
                          self._reductions_works[block_id][
                              chunk_id
                          ].wait()
                  # Since the packed format is contiguous after reductions, only one norm is needed
                  l2_grad_norm_sq = torch.empty([1], device="cuda")
                  if self._full_ar:
                      # this flattening of lists is to keep multi_tensor_apply function happy, it wants depth=1 for l2 norm computation
                      flat_list = [
                          item
                          for sublist in self._fp16_g_chunks
                          for item in sublist
                      ]
                      l2_grad_norm_sq = (
                          multi_tensor_applier(
                              self.multi_tensor_l2norm,
                              self._overflow_buf,
                              [flat_list],
                              False,
                          )[0]
                          ** 2
                      )
                  else:
                      l2_grad_norm_sq = (
                          self._fp16_g.norm(dtype=torch.float32, p=2)
                          ** 2
                      )
                  torch.distributed.all_reduce(
                      l2_grad_norm_sq, group=self._l2_grad_norm_pg
                  )
                  self._L2_grad_norm = l2_grad_norm_sq.sqrt()
      else:
          # Copy model grads to flat grads buffer
          self._flatten_grad_mt(1.0)
  
          # Compute L2 grad norm
          self._l2_grad_norm_st.wait_stream(torch.cuda.current_stream())
          with torch.cuda.stream(self._l2_grad_norm_st):
              if not self._fused_norm:
                  self._L2_grad_norm = self._flat_grads.norm(
                      dtype=torch.float16, p=2
                  ).float()
          torch.cuda.current_stream().wait_stream(self._l2_grad_norm_st)
  
          # Apply clipping & pre-reduction scaling on grads
          loss_scale = self.global_scale
          max_grad_norm = loss_scale * self.defaults["max_grad_norm"]
          coeff = max_grad_norm / (1e-6 + self.L2_grad_norm)
          coeff = (coeff > 1) * self._one + (coeff <= 1) * coeff
          tmp = torch.cat(((self._one), (coeff)))
          index = (coeff + 1 > coeff).int()
          scale = tmp.index_select(0, index).half() / self._world_size
          if not self._fuse_scale:
              self._flat_grads.mul_(scale)
  
          if self._full_ar:
              if self._fuse_scale:
                  self._full_all_reduce_scale(block_id, scale)
              else:
                  self._full_all_reduce(block_id)
          else:
              if self._fuse_scale:
                  self._reduce_scatter_and_all_reduce_scale(
                      block_id, scale
                  )
              else:
                  self._reduce_scatter_and_all_reduce(block_id)
  
          if block_id == 0:
              for block_id in range(self._num_blocks):
                  for chunk_id in range(self._num_chunks):
                      self._reductions_works[block_id][chunk_id].wait()
  
imports: import os
  import math
  import inspect
  import torch
  import importlib
  import amp_C
  from apex.multi_tensor_apply import multi_tensor_applier
  import torch.distributed.distributed_c10d
label_desc: Use image node_id 11 for calling the DistributedFusedLAMB obj's underlying member method code with example usage: obj._pipeline_block_reductions(block_id) without return types