code: def sparse_clip_norm(parameters, max_norm, norm_type=2):
      """Clips gradient norm of an iterable of parameters.
  
      The norm is computed over all gradients together, as if they were
      concatenated into a single vector. Gradients are modified in-place.
      Supports sparse gradients.
  
      Parameters
      ----------
      parameters : ``(Iterable[torch.Tensor])``
          An iterable of Tensors that will have gradients normalized.
      max_norm : ``float``
          The max norm of the gradients.
      norm_type : ``float``
          The type of the used p-norm. Can be ``'inf'`` for infinity norm.
  
      Returns
      -------
      Total norm of the parameters (viewed as a single vector).
      """
      # pylint: disable=invalid-name,protected-access
      parameters = list(
          ifilter(lambda p: p.grad is not None, parameters)
      )
      max_norm = float(max_norm)
      norm_type = float(norm_type)
      if norm_type == float("inf"):
          total_norm = max(p.grad.data.abs().max() for p in parameters)
      else:
          total_norm = 0
          for p in parameters:
              if is_sparse(p.grad):
                  # need to coalesce the repeated indices before finding norm
                  grad = p.grad.data.coalesce()
                  param_norm = grad._values().norm(norm_type)
              else:
                  param_norm = p.grad.data.norm(norm_type)
              total_norm += param_norm**norm_type
          total_norm = total_norm ** (1.0 / norm_type)
      clip_coef = max_norm / (total_norm + 1e-6)
      if clip_coef < 1:
          for p in parameters:
              if is_sparse(p.grad):
                  p.grad.data._values().mul_(clip_coef)
              else:
                  p.grad.data.mul_(clip_coef)
      return total_norm
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import logging
  import os
  import shutil
  import time
  import re
  import datetime
  import traceback
  import torch
  import torch.optim.lr_scheduler
  from torch.nn.parallel import replicate, parallel_apply
  from torch.nn.parallel.scatter_gather import scatter_kwargs, gather
  from allennlp.common import Params
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.util import peak_memory_mb, gpu_memory_mb
  from allennlp.common.tqdm import Tqdm
  from allennlp.data.instance import Instance
  from allennlp.data.iterators.data_iterator import DataIterator
  from allennlp.models.model import Model
  from allennlp.nn import util
  from allennlp.training.learning_rate_schedulers import LearningRateScheduler
  from allennlp.training.optimizers import Optimizer
label_desc: Use image node_id 2 for calling a global function with example usage: sparse_clip_norm(parameters, max_norm, norm_type) and returns: total_norm