code: def _bilateral_blur(
      input: Tensor,
      guidance: Optional[Tensor],
      kernel_size: tuple[int, int] | int,
      sigma_color: float | Tensor,
      sigma_space: tuple[float, float] | Tensor,
      border_type: str = "reflect",
      color_distance_type: str = "l1",
  ) -> Tensor:
      "Single implementation for both Bilateral Filter and Joint Bilateral Filter"
  
      KORNIA_CHECK_IS_TENSOR(input)
      KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])
      if guidance is not None:
          # NOTE: allow guidance and input having different number of channels
          KORNIA_CHECK_IS_TENSOR(guidance)
          KORNIA_CHECK_SHAPE(guidance, ["B", "C", "H", "W"])
          KORNIA_CHECK(
              (guidance.shape[0] == input.shape[0])
              and (guidance.shape[-2:] == input.shape[-2:]),
              "guidance and input should have the same batch size and spatial dimensions",
          )
  
      if isinstance(sigma_color, Tensor):
          KORNIA_CHECK_SHAPE(sigma_color, ["B"])
          sigma_color = sigma_color.to(
              device=input.device, dtype=input.dtype
          ).view(-1, 1, 1, 1, 1)
  
      ky, kx = _unpack_2d_ks(kernel_size)
      pad_y, pad_x = _compute_zero_padding(kernel_size)
  
      padded_input = pad(
          input, (pad_x, pad_x, pad_y, pad_y), mode=border_type
      )
      unfolded_input = (
          padded_input.unfold(2, ky, 1).unfold(3, kx, 1).flatten(-2)
      )  # (B, C, H, W, Ky x Kx)
  
      if guidance is None:
          guidance = input
          unfolded_guidance = unfolded_input
      else:
          padded_guidance = pad(
              guidance, (pad_x, pad_x, pad_y, pad_y), mode=border_type
          )
          unfolded_guidance = (
              padded_guidance.unfold(2, ky, 1)
              .unfold(3, kx, 1)
              .flatten(-2)
          )  # (B, C, H, W, Ky x Kx)
  
      diff = unfolded_guidance - guidance.unsqueeze(-1)
      if color_distance_type == "l1":
          color_distance_sq = diff.abs().sum(1, keepdim=True).square()
      elif color_distance_type == "l2":
          color_distance_sq = diff.square().sum(1, keepdim=True)
      else:
          raise ValueError("color_distance_type only acceps l1 or l2")
      color_kernel = (
          -0.5 / sigma_color**2 * color_distance_sq
      ).exp()  # (B, 1, H, W, Ky x Kx)
  
      space_kernel = get_gaussian_kernel2d(
          kernel_size,
          sigma_space,
          device=input.device,
          dtype=input.dtype,
      )
      space_kernel = space_kernel.view(-1, 1, 1, 1, kx * ky)
  
      kernel = space_kernel * color_kernel
      out = (unfolded_input * kernel).sum(-1) / kernel.sum(-1)
      return out
  
file_path: kornia/kornia/filters/bilateral.py
imports: from __future__ import annotations
  from typing import Optional
  from kornia.core import Module, Tensor, pad
  from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE
  from .kernels import _unpack_2d_ks, get_gaussian_kernel2d
  from .median import _compute_zero_padding
label_desc: Use image node_id 1 for calling a global function with example usage: _bilateral_blur(input, guidance, kernel_size, sigma_color, sigma_space, border_type, color_distance_type) and returns: out