code: def _boxes_to_quadrilaterals(
      boxes: torch.Tensor,
      mode: str = "xyxy",
      validate_boxes: bool = True,
  ) -> torch.Tensor:
      """Convert from boxes to quadrilaterals."""
      mode = mode.lower()
  
      if mode.startswith("vertices"):
          batched = boxes.ndim == 4
          if not (
              3 <= boxes.ndim <= 4
              and boxes.shape[-2:] == torch.Size([4, 2])
          ):
              raise ValueError(
                  f"Boxes shape must be (N, 4, 2) or (B, N, 4, 2) when {mode} mode. Got {boxes.shape}."
              )
      elif mode.startswith("xy"):
          batched = boxes.ndim == 3
          if not (2 <= boxes.ndim <= 3 and boxes.shape[-1] == 4):
              raise ValueError(
                  f"Boxes shape must be (N, 4) or (B, N, 4) when {mode} mode. Got {boxes.shape}."
              )
      else:
          raise ValueError(f"Unknown mode {mode}")
  
      boxes = boxes if boxes.is_floating_point() else boxes.float()
      boxes = boxes if batched else boxes.unsqueeze(0)
  
      if mode.startswith("vertices"):
          if mode == "vertices":
              quadrilaterals = boxes.clone()
              # Here, vertices are quadrilaterals with width and height defined as `width = xmax - xmin`  and
              # `height = ymax - ymin`. We need to convert to `width = xmax - xmin + 1` and `height = ymax - ymin + 1` to
              # match with internal Boxes Kornia representation.
              quadrilaterals[..., 1:3, 0] = (
                  quadrilaterals[..., 1:3, 0] - 1
              )
              quadrilaterals[..., 2:, 1] = (
                  quadrilaterals[..., 2:, 1] - 1
              )
          elif mode == "vertices_plus":
              # Avoid passing reference
              quadrilaterals = boxes.clone()
          else:
              raise ValueError(f"Unknown mode {mode}")
          not validate_boxes or validate_bbox(quadrilaterals)
      elif mode.startswith("xy"):
          if mode == "xyxy":
              height, width = (
                  boxes[..., 3] - boxes[..., 1],
                  boxes[..., 2] - boxes[..., 0],
              )
          elif mode == "xyxy_plus":
              height, width = (
                  boxes[..., 3] - boxes[..., 1] + 1,
                  boxes[..., 2] - boxes[..., 0] + 1,
              )
          elif mode == "xywh":
              height, width = boxes[..., 3], boxes[..., 2]
          else:
              raise ValueError(f"Unknown mode {mode}")
  
          if validate_boxes:
              if (width <= 0).any():
                  raise ValueError(
                      "Some boxes have negative widths or 0."
                  )
              if (height <= 0).any():
                  raise ValueError(
                      "Some boxes have negative heights or 0."
                  )
  
          xmin, ymin = boxes[..., 0], boxes[..., 1]
          quadrilaterals = _boxes_to_polygons(xmin, ymin, width, height)
      else:
          raise ValueError(f"Unknown mode {mode}")
  
      quadrilaterals = (
          quadrilaterals if batched else quadrilaterals.squeeze(0)
      )
  
      return quadrilaterals
  
imports: from __future__ import annotations
  from typing import Optional, cast
  import torch
  from torch import Size
  from kornia.core import Tensor, stack, zeros
  from kornia.geometry.bbox import validate_bbox
  from kornia.geometry.linalg import transform_points
  from kornia.utils import eye_like
label_desc: Use image node_id 5 for calling a global function with example usage: _boxes_to_quadrilaterals(boxes, mode, validate_boxes) and returns: quadrilaterals