code: def calc_grad_norm(
      parameters: List[torch.nn.Parameter], p: float
  ) -> torch.Tensor:
      r"""Calculate gradient norm of an iterable of parameters.
      Returns:
          Total norm of the parameters (viewed as a single vector).
      """
      if isinstance(parameters, torch.Tensor):
          parameters = [parameters]
      parameters = list(
          filter(lambda par: par.grad is not None, parameters)
      )
  
      if len(parameters) == 0:
          return torch.tensor(0.0)
      p = float(p)
      if p == inf:
          local_norm = max(par.grad.detach().abs().max() for par in parameters)  # type: ignore
      else:
          # Compute the norm in full precision no matter what
          local_norm = torch.norm(torch.stack([torch.norm(par.grad.detach(), p, dtype=torch.float32) for par in parameters]), p).to(dtype=parameters[0].dtype)  # type: ignore
      return local_norm
  
file_path: fairscale/fairscale/internal/params.py
imports: import collections.abc
  from dataclasses import dataclass
  from math import inf
  from typing import Any, Callable, Dict, List, Optional
  import torch
  import torch.distributed
label_desc: Use image node_id 3 for calling a global function with example usage: calc_grad_norm(parameters, p) and returns: local_norm, torch