code: def sigma_mean(
      x: LazyTensor, y: LazyTensor, dist: LazyTensor, n_min: int = 100
  ) -> torch.Tensor:
      """
      Set bandwidth to the mean distance between instances x and y.
  
      Parameters
      ----------
      x
          LazyTensor of instances with dimension [Nx, 1, features] or [batch_size, Nx, 1, features].
          The singleton dimension is necessary for broadcasting.
      y
          LazyTensor of instances with dimension [1, Ny, features] or [batch_size, 1, Ny, features].
          The singleton dimension is necessary for broadcasting.
      dist
          LazyTensor with dimensions [Nx, Ny] or [batch_size, Nx, Ny] containing the
          pairwise distances between `x` and `y`.
      n_min
          In order to check whether x equals y after squeezing the singleton dimensions, we check if the
          diagonal of the distance matrix (which is a lazy tensor from which the diagonal cannot be directly extracted)
          consists of all zeros. We do this by computing the k-min distances and k-argmin indices over the
          columns of the distance matrix. We then check if the distances on the diagonal of the distance matrix
          are all zero or not. If they are all zero, then we do not use these distances (zeros) when computing
          the mean pairwise distance as bandwidth. If Nx becomes very large, it is advised to set `n_min`
          to a low enough value to avoid OOM issues. By default we set it to 100 instances.
  
      Returns
      -------
      The computed bandwidth, `sigma`.
      """
      batched = len(dist.shape) == 3
      if not batched:
          nx, ny = dist.shape
          axis = 1
      else:
          batch_size, nx, ny = dist.shape
          axis = 2
      n_mean = nx * ny
      if nx == ny:
          n_min = min(n_min, nx) if isinstance(n_min, int) else nx
          d_min, id_min = dist.Kmin_argKmin(n_min, axis=axis)
          if batched:
              d_min, id_min = (
                  d_min[0],
                  id_min[0],
              )  # first instance in permutation test contains the original data
          rows, cols = torch.where(
              id_min.cpu() == torch.arange(nx)[:, None]
          )
          if (d_min[rows, cols] == 0.0).all():
              n_mean = nx * (nx - 1)
      dist_sum = (
          dist.sum(1).sum(1)[0]
          if batched
          else dist.sum(1).sum().unsqueeze(-1)
      )
      sigma = (0.5 * dist_sum / n_mean) ** 0.5
      return sigma
  
imports: from pykeops.torch import LazyTensor
  import torch
  import torch.nn
  from typing import Callable, Optional, Union
  from alibi_detect.utils.frameworks import Framework
  from alibi_detect.utils._types import Literal
  from copy import deepcopy
label_desc: Use image node_id 1 for calling a global function with example usage: sigma_mean(x, y, dist, n_min) and returns: sigma