code: def compute_mmd(
      y: torch.Tensor, y_pred: torch.Tensor, y_mapping: Callable | None
  ) -> torch.Tensor:
      """
      Args:
          y: first sample (e.g., the reference image). Its shape is (B,C,W,H) for 2D data and (B,C,W,H,D) for 3D.
          y_pred: second sample (e.g., the reconstructed image). It has similar shape as y.
          y_mapping: Callable to transform the y tensors before computing the metric.
      """
      if y_pred.shape[0] == 1 or y.shape[0] == 1:
          raise ValueError(
              "MMD metric requires at least two samples in y and y_pred."
          )
  
      if y_mapping is not None:
          y = y_mapping(y)
          y_pred = y_mapping(y_pred)
  
      if y_pred.shape != y.shape:
          raise ValueError(
              "y_pred and y shapes dont match after being processed "
              f"by their transforms, received y_pred: {y_pred.shape} and y: {y.shape}"
          )
  
      for d in range(len(y.shape) - 1, 1, -1):
          y = y.squeeze(dim=d)
          y_pred = y_pred.squeeze(dim=d)
  
      y = y.view(y.shape[0], -1)
      y_pred = y_pred.view(y_pred.shape[0], -1)
  
      y_y = torch.mm(y, y.t())
      y_pred_y_pred = torch.mm(y_pred, y_pred.t())
      y_pred_y = torch.mm(y_pred, y.t())
  
      m = y.shape[0]
      n = y_pred.shape[0]
  
      # Ref. 1 Eq. 3 (found under Lemma 6)
      # term 1
      c1 = 1 / (m * (m - 1))
      a = torch.sum(y_y - torch.diag(torch.diagonal(y_y)))
  
      # term 2
      c2 = 1 / (n * (n - 1))
      b = torch.sum(
          y_pred_y_pred - torch.diag(torch.diagonal(y_pred_y_pred))
      )
  
      # term 3
      c3 = 2 / (m * n)
      c = torch.sum(y_pred_y)
  
      mmd = c1 * a + c2 * b - c3 * c
      return mmd
  
file_path: MONAI/monai/metrics/mmd.py
imports: from __future__ import annotations
  from collections.abc import Callable
  import torch
  from monai.metrics.metric import Metric
label_desc: Use image node_id 1 for calling a global function with example usage: compute_mmd(y, y_pred, y_mapping) and returns: mmd