code: def _batch_and_ptr(
      slices: Any,
      device: Optional[torch.device] = None,
  ) -> Tuple[Any, Any]:
      if isinstance(slices, Tensor) and slices.dim() == 1:
          # Default case, turn slices tensor into batch.
          repeats = slices[1:] - slices[:-1]
          batch = repeat_interleave(repeats.tolist(), device=device)
          ptr = cumsum(repeats.to(device))
          return batch, ptr
  
      elif isinstance(slices, Mapping):
          # Recursively batch elements of dictionaries.
          batch, ptr = {}, {}
          for k, v in slices.items():
              batch[k], ptr[k] = _batch_and_ptr(v, device)
          return batch, ptr
  
      elif (
          isinstance(slices, Sequence)
          and not isinstance(slices, str)
          and isinstance(slices[0], Tensor)
      ):
          # Recursively batch elements of lists.
          batch, ptr = [], []
          for s in slices:
              sub_batch, sub_ptr = _batch_and_ptr(s, device)
              batch.append(sub_batch)
              ptr.append(sub_ptr)
          return batch, ptr
  
      else:
          # Failure of batching, usually due to slices.dim() != 1
          return None, None
  
file_path: pytorch_geometric/torch_geometric/data/collate.py
imports: from collections import defaultdict
  from collections.abc import Mapping, Sequence
  from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, TypeVar, Union
  import torch
  from torch import Tensor
  import torch_geometric.typing
  from torch_geometric import EdgeIndex
  from torch_geometric.data.data import BaseData
  from torch_geometric.data.storage import BaseStorage, NodeStorage
  from torch_geometric.edge_index import SortOrder
  from torch_geometric.typing import SparseTensor, TensorFrame, torch_frame, torch_sparse
  from torch_geometric.utils import cumsum, is_sparse, is_torch_sparse_tensor
  from torch_geometric.utils.sparse import cat
label_desc: Use image node_id 3 for calling a global function with example usage: _batch_and_ptr(slices, device) and returns: batch,  ptr, batch,  ptr, batch,  ptr, None,  None