code: def caching_allocator_alloc(
      size, device: Union[Device, int] = None, stream=None
  ):
      r"""Perform a memory allocation using the CUDA memory allocator.
  
      Memory is allocated for a given device and a stream, this
      function is intended to be used for interoperability with other
      frameworks. Allocated memory is released through
      :func:`~torch.cuda.caching_allocator_delete`.
  
      Args:
          size (int): number of bytes to be allocated.
          device (torch.device or int, optional): selected device. If it is
              ``None`` the default CUDA device is used.
          stream (torch.cuda.Stream or int, optional): selected stream. If is ``None`` then
              the default stream for the selected device is used.
  
      .. note::
          See :ref:`cuda-memory-management` for more details about GPU memory
          management.
      """
      if device is None:
          device = torch.cuda.current_device()
      device = _get_device_index(device)
      if stream is None:
          stream = torch.cuda.current_stream(device)
      if isinstance(stream, torch.cuda.streams.Stream):
          stream = stream.cuda_stream
      if not isinstance(stream, int):
          raise TypeError(
              "Invalid type for stream argument, must be "
              "`torch.cuda.Stream` or `int` representing a pointer "
              "to a existing stream"
          )
      with torch.cuda.device(device):
          return torch._C._cuda_cudaCachingAllocator_raw_alloc(
              size, stream
          )
  
imports: import collections
  import contextlib
  import ctypes
  import pickle
  import sys
  import warnings
  from inspect import signature
  from typing import Any, Dict, Optional, Tuple, Union
  import torch
  from torch import _C
  from torch.types import Device
  from .None import _get_device_index, _get_nvml_device_index, _lazy_init, is_initialized
  from ._memory_viz import memory, segments
  from ._utils import _dummy_type
label_desc: Use image node_id 3 for calling a global function with example usage: caching_allocator_alloc(size, device, stream) and returns: torch