code: def calc_sample_norms(
      named_params: Iterator[Tuple[str, torch.Tensor]],
      *,
      flat: bool = True,
  ) -> List[torch.Tensor]:
      r"""
      Calculates the norm of the given tensors for each sample.
  
      This function calculates the overall norm of the given tensors for each sample,
      assuming each batch's dim is zero.
  
      Args:
          named_params: An iterator of tuples <name, param> with name being a
              string and param being a tensor of shape ``[B, ...]`` where ``B``
              is the size of the batch and is the 0th dimension.
          flat: A flag, when set to `True` returns a flat norm over all
              layers norms
  
      Returns:
          A list of tensor norms where length of the list is the number of layers
  
      Example:
          >>> t1 = torch.rand((2, 5))
          >>> t2 = torch.rand((2, 5))
          >>> norms = calc_sample_norms([("1", t1), ("2", t2)])
          >>> norms, norms[0].shape
          ([tensor([...])], torch.Size([2]))
      """
      norms = [
          param.view(len(param), -1).norm(2, dim=-1)
          for name, param in named_params
      ]
      # calc norm over all layer norms if flat = True
      if flat:
          norms = [torch.stack(norms, dim=0).norm(2, dim=0)]
      return norms
  
file_path: opacus/opacus/utils/tensor_utils.py
imports: import math
  from typing import Iterator, List, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
label_desc: Use image node_id 1 for calling a global function with example usage: calc_sample_norms(named_params) and returns: norms