code: def spatial_gradient(
      input: Tensor,
      mode: str = "sobel",
      order: int = 1,
      normalized: bool = True,
  ) -> Tensor:
      r"""Compute the first order image derivative in both x and y using a Sobel operator.
  
      .. image:: _static/img/spatial_gradient.png
  
      Args:
          input: input image tensor with shape :math:`(B, C, H, W)`.
          mode: derivatives modality, can be: `sobel` or `diff`.
          order: the order of the derivatives.
          normalized: whether the output is normalized.
  
      Return:
          the derivatives of the input feature map. with shape :math:`(B, C, 2, H, W)`.
  
      .. note::
         See a working example `here <https://kornia.github.io/tutorials/nbs/filtering_edges.html>`__.
  
      Examples:
          >>> input = torch.rand(1, 3, 4, 4)
          >>> output = spatial_gradient(input)  # 1x3x2x4x4
          >>> output.shape
          torch.Size([1, 3, 2, 4, 4])
      """
      KORNIA_CHECK_IS_TENSOR(input)
      KORNIA_CHECK_SHAPE(input, ["B", "C", "H", "W"])
  
      # allocate kernel
      kernel = get_spatial_gradient_kernel2d(
          mode, order, device=input.device, dtype=input.dtype
      )
      if normalized:
          kernel = normalize_kernel2d(kernel)
  
      # prepare kernel
      b, c, h, w = input.shape
      tmp_kernel = kernel[:, None, ...]
  
      # Pad with "replicate for spatial dims, but with zeros for channel
      spatial_pad = [
          kernel.size(1) // 2,
          kernel.size(1) // 2,
          kernel.size(2) // 2,
          kernel.size(2) // 2,
      ]
      out_channels: int = 3 if order == 2 else 2
      padded_inp: Tensor = pad(
          input.reshape(b * c, 1, h, w), spatial_pad, "replicate"
      )
      out = F.conv2d(
          padded_inp, tmp_kernel, groups=1, padding=0, stride=1
      )
      return out.reshape(b, c, out_channels, h, w)
  
imports: from __future__ import annotations
  import torch
  import torch.nn.functional
  from kornia.core import Module, Tensor, pad
  from kornia.core.check import KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE
  from .kernels import get_spatial_gradient_kernel2d, get_spatial_gradient_kernel3d, normalize_kernel2d
label_desc: Use image node_id 1 for calling a global function with example usage: spatial_gradient(input, mode, order, normalized) and returns: out