code: def to_nested_tensor(
      x: Tensor,
      batch: Optional[Tensor] = None,
      ptr: Optional[Tensor] = None,
      batch_size: Optional[int] = None,
  ) -> Tensor:
      r"""Given a contiguous batch of tensors
      :math:`\mathbf{X} \in \mathbb{R}^{(N_1 + \ldots + N_B) \times *}`
      (with :math:`N_i` indicating the number of elements in example :math:`i`),
      creates a `nested PyTorch tensor
      <https://pytorch.org/docs/stable/nested.html>`__.
      Reverse operation of :meth:`from_nested_tensor`.
  
      Args:
          x (torch.Tensor): The input tensor
              :math:`\mathbf{X} \in \mathbb{R}^{(N_1 + \ldots + N_B) \times *}`.
          batch (torch.Tensor, optional): The batch vector
              :math:`\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N`, which assigns each
              element to a specific example. Must be ordered.
              (default: :obj:`None`)
          ptr (torch.Tensor, optional): Alternative representation of
              :obj:`batch` in compressed format. (default: :obj:`None`)
          batch_size (int, optional): The batch size :math:`B`.
              (default: :obj:`None`)
      """
      if ptr is not None:
          offsets = ptr[1:] - ptr[:-1]
          sizes = offsets.tolist()
          xs = list(torch.split(x, sizes, dim=0))
      elif batch is not None:
          offsets = scatter(
              torch.ones_like(batch), batch, dim_size=batch_size
          )
          sizes = offsets.tolist()
          xs = list(torch.split(x, sizes, dim=0))
      else:
          xs = [x]
  
      # This currently copies the data, although `x` is already contiguous.
      # Sadly, there does not exist any (public) API to preven this :(
      return torch.nested.as_nested_tensor(xs)
  
imports: from typing import Optional, Tuple, Union
  import torch
  from torch import Tensor
  from torch_geometric.utils import scatter
label_desc: Use image node_id 1 for calling a global function with example usage: to_nested_tensor(x, batch, ptr, batch_size) and returns: torch