code: def safe_cdist(
      a: torch.Tensor,
      b: torch.Tensor,
      add_diag_eps: bool = False,
      diag_inf: bool = False,
  ):
      """L2 pairwise distance of a and b. The diagonal is either
      replaced with a small eps or infinite.
  
      Examples
      --------
      >>> import torch
      >>> a = torch.tensor([[1., 2], [3, 4]])
      >>> b = torch.tensor([[1., 2], [3, 4]])
      >>> safe_cdist(a, b)
      tensor([[0.0000, 2.8284],
              [2.8284, 0.0000]])
      >>> safe_cdist(a, b, add_diag_eps=True)
      tensor([[1.4142e-12, 2.8284e+00],
              [2.8284e+00, 1.4142e-12]])
      >>> safe_cdist(a, b, diag_inf=True)
      tensor([[   inf, 2.8284],
              [2.8284,    inf]])
  
      Parameters
      ----------
      a: torch.Tensor
          First Tensor. Shape: (*BA, na, ndim)
      n: torch.Tensor
          Second Tensor. Shape: (*BB, nb, ndim)
  
      Returns
      -------
      torch.Tensor
          Pairwise distance. Shape: (*BAB, na, nb)
  
      """
      square_mat = a.shape[-2] == b.shape[-2]
  
      dtype = a.dtype
      device = a.device
      ab = a.unsqueeze(-2) - b.unsqueeze(-3)  # (*BAB, na, nb, ndim)
  
      # add the diagonal with a small eps to safeguard from nan
      if add_diag_eps:
          if not square_mat:
              raise ValueError(
                  "Enabling add_diag_eps for non-square result matrix is invalid"
              )
          ab = (
              ab
              + torch.eye(
                  ab.shape[-2], dtype=dtype, device=device
              ).unsqueeze(-1)
              * eps
          )
  
      ab = ab.norm(dim=-1)  # (*BAB, na, nb)
  
      # replace the diagonal with infinite (usually used for coulomb matrix)
      if diag_inf:
          if not square_mat:
              raise ValueError(
                  "Enabling diag_inf for non-square result matrix is invalid"
              )
  
          infdiag = torch.eye(ab.shape[-1], dtype=dtype, device=device)
          idiag = infdiag.diagonal()
          idiag[:] = float("inf")
          ab = ab + infdiag
  
      return ab
  
file_path: deepchem/deepchem/utils/safeops_utils.py
imports: import math
  import torch
  from typing import Union, Optional, Tuple
label_desc: Use image node_id 6 for calling a global function with example usage: safe_cdist(a, b, add_diag_eps, diag_inf) and returns: ab