code: def grad_global_norm(parameters: Iterable[Parameter]) -> float:
      """Calculate the 2-norm of gradients of parameters, and how much they should be scaled down
      such that their 2-norm does not exceed `max_norm`, if `max_norm` if provided.
      If gradients exist for more than one context for a parameter, user needs to explicitly call
      ``trainer.allreduce_grads`` so that the gradients are summed first before calculating
      the 2-norm.
  
      .. note::
          This function is only for use when `update_on_kvstore` is set to False in trainer.
  
      Example::
  
          trainer = Trainer(net.collect_params(), update_on_kvstore=False, ...)
          for x, y in mx.gluon.utils.split_and_load(X, [mx.gpu(0), mx.gpu(1)]):
              with mx.autograd.record():
                  y = net(x)
                  loss = loss_fn(y, label)
              loss.backward()
          trainer.allreduce_grads()
          norm = grad_global_norm(net.collect_params().values())
          ...
  
      Parameters
      ----------
      parameters
          The list of Parameters
  
      Returns
      -------
      total_norm
          Total norm. It's a numpy scalar.
      """
      # Distribute gradients among contexts,
      # For example, assume there are 8 weights and four GPUs, we can ask each GPU to
      # compute the squared sum of two weights and then add the results together
      idx = 0
      arrays = defaultdict(list)
      sum_norms = []
      num_ctx = None
      param_uuid_set = set()
      for p in parameters:
          if p._uuid in param_uuid_set:
              continue
          param_uuid_set.add(p._uuid)
          if p.grad_req != "null":
              p_grads = p.list_grad()
              if num_ctx is None:
                  num_ctx = len(p_grads)
              else:
                  assert num_ctx == len(p_grads)
              arrays[idx % num_ctx].append(p_grads[idx % num_ctx])
              idx += 1
      assert (
          len(arrays) > 0
      ), "No parameter found available for gradient norm."
  
      # TODO(sxjscience)
      #  Investigate the float16 case.
      #  The inner computation accumulative type of norm should be float32.
      ctx = arrays[0][0].context
      for idx, arr_l in enumerate(arrays.values()):
          sum_norm = mx.np.linalg.norm(
              mx.np.concatenate([mx.np.ravel(ele) for ele in arr_l])
          )
          sum_norms.append(sum_norm.as_in_ctx(ctx))
  
      # Reduce over ctx
      if num_ctx == 1:
          total_norm = sum_norms[0]
      else:
          total_norm = mx.np.linalg.norm(
              mx.np.concatenate(sum_norms, axis=None)
          )
      total_norm = float(total_norm)
      return total_norm
  
imports: import warnings
  import numpy
  import mxnet
  from collections import defaultdict
  from mxnet.gluon import Parameter
  from mxnet.util import use_np
  from typing import Iterable, Optional, Tuple
  from collections import OrderedDict
  from mxnet.gluon.utils import shape_is_known
label_desc: Use image node_id 1 for calling a global function with example usage: grad_global_norm(parameters) and returns: total_norm