code: def clip_grad_global_norm(
      parameters: Iterable[Parameter],
      max_norm: float,
      check_isfinite: bool = True,
  ) -> Tuple[float, float, bool]:
      """Rescales gradients of parameters so that the sum of their 2-norm is smaller than `max_norm`.
      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.
          In cases where training happens on multiple contexts, this method should be used in
          conjunction with ``trainer.allreduce_grads()`` and ``trainer.update()``.
          (**not** ``trainer.step()``)
  
      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()
          nlp.utils.clip_grad_global_norm(net.collect_params().values(), max_norm)
          trainer.update(batch_size)
          ...
  
      Parameters
      ----------
      parameters
          The list of parameters to calculate the norm
      max_norm
          If the gradient norm is larger than max_norm, it will be clipped to have max_norm
      check_isfinite
           If True, check whether the total_norm is finite (not nan or inf).
      Returns
      -------
      total_norm
          The total norm
      ratio
          The expected clipping ratio: grad = grad / ratio
          It will be calculated as max(total_norm / max_norm, 1)
      is_finite
          Whether the total norm is finite
      """
      total_norm = grad_global_norm(parameters)
      is_finite = bool(np.isfinite(total_norm))
      ratio = np.maximum(1, total_norm / max_norm)
      if check_isfinite and not is_finite:
          warnings.warn(
              UserWarning(
                  "nan or inf is detected. Clipping results will be undefined."
                  " Thus, skip clipping"
              ),
              stacklevel=2,
          )
          return total_norm, ratio, is_finite
      scale = 1 / ratio
      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":
              for arr in p.list_grad():
                  arr *= scale
      return total_norm, ratio, is_finite
  
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 2 for calling a global function with example usage: clip_grad_global_norm(parameters, max_norm, check_isfinite) and returns: total_norm,  ratio,  is_finite, total_norm,  ratio,  is_finite