def clip_grad_global_norm(parameters, max_norm, check_isfinite=True):
def _norm(array):<EOL><INDENT>if array.stype == '<STR_LIT:default>':<EOL><INDENT>x = array.reshape((-<NUM_LIT:1>))<EOL>return nd.dot(x, x)<EOL><DEDENT>return array.norm().square()<EOL><DEDENT>arrays = []<EOL>i = <NUM_LIT:0><EOL>for p in parameters:<EOL><INDENT>if p.grad_req != '<STR_LIT:null>':<EOL><INDENT>grad_list = p.list_grad()<EOL>arrays.append(grad_list[i % len(grad_list)])<EOL>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>assert len(arrays) > <NUM_LIT:0>, '<STR_LIT>'<EOL>ctx, dtype = arrays[<NUM_LIT:0>].context, arrays[<NUM_LIT:0>].dtype<EOL>total_norm = nd.add_n(*[_norm(arr).as_in_context(ctx) for arr in arrays])<EOL>total_norm = nd.sqrt(total_norm)<EOL>if check_isfinite:<EOL><INDENT>total_norm = total_norm.asscalar()<EOL>if not np.isfinite(total_norm):<EOL><INDENT>warnings.warn(<EOL>UserWarning('<STR_LIT>'<EOL>'<STR_LIT>'), stacklevel=<NUM_LIT:2>)<EOL><DEDENT><DEDENT>scale = max_norm / (total_norm + <NUM_LIT>)<EOL>if check_isfinite:<EOL><INDENT>scale = nd.array([scale], dtype=dtype, ctx=ctx)<EOL><DEDENT>scale = nd.min(nd.concat(scale, nd.ones((<NUM_LIT:1>,), dtype=dtype, ctx=ctx), dim=<NUM_LIT:0>))<EOL>for p in parameters:<EOL><INDENT>if p.grad_req != '<STR_LIT:null>':<EOL><INDENT>for arr in p.list_grad():<EOL><INDENT>arr *= scale.as_in_context(arr.context)<EOL><DEDENT><DEDENT><DEDENT>return total_norm<EOL>
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.

    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 : list of Parameters
    max_norm : float
    check_isfinite : bool, default True
         If True, check that the total_norm is finite (not nan or inf). This
         requires a blocking .asscalar() call.

    Returns
    -------
    NDArray or float
      Total norm. Return type is NDArray of shape (1,) if check_isfinite is
      False. Otherwise a float is returned.