code: def clip_global_norm(arrays, max_norm, check_isfinite=True):
      """Rescales NDArrays so that the sum of their 2-norm is smaller than `max_norm`.
  
      Parameters
      ----------
      arrays : list of NDArray
      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.
  
      """
  
      # group arrays by ctx
      def group_by_ctx(arr_list):
          groups = collections.defaultdict(list)
          for arr in arr_list:
              ctx = arr.device
              groups[ctx].append(arr)
          return groups
  
      def multi_sum_sq(*args, ctx=None):
          sum = _mx_np.array([0], device=ctx)
          for arg in args:
              sum += _mx_np.square(arg).sum().item()
          return sum
  
      arrays_groups = group_by_ctx(arrays)
      all_ctx_sum = _mx_np.array([0])
      ctx = arrays[0].device
      for group in arrays_groups:
          sum_sq = multi_sum_sq(*arrays_groups[group], ctx=ctx)
          all_ctx_sum += sum_sq
      # global reduce
      total_norm = _mx_np.sqrt(all_ctx_sum)
      if check_isfinite:
          if not np.isfinite(total_norm.item()):
              warnings.warn(
                  UserWarning(
                      "nan or inf is detected. "
                      "Clipping results will be undefined."
                  ),
                  stacklevel=2,
              )
      scale = max_norm / (total_norm + 1e-8)
      scale = _mx_np.min(
          _mx_np.concatenate(
              [scale, _mx_np.ones(1, device=ctx)], axis=0
          )
      )
      for arr in arrays:
          arr *= scale.item()
      if check_isfinite:
          return total_norm.item()
      else:
          return total_norm
  
imports: import os
  import sys
  import hashlib
  import uuid
  import warnings
  import collections
  import weakref
  import requests
  import numpy
  from ..None import ndarray
  from ..util import is_np_shape, is_np_array, TemporaryDirectory
  from ..None import numpy
label_desc: Use image node_id 3 for calling a global function with example usage: clip_global_norm(arrays, max_norm, check_isfinite) and returns: groups, sum, total_norm, total_norm