Skip to content

vllm.device_allocator.cumem

Classes:

  • CuMemAllocator –

    A singleton class that manages a memory pool for CUDA tensors.

CuMemAllocator

A singleton class that manages a memory pool for CUDA tensors. The memory in this pool can be offloaded or discarded when the allocator sleeps.

Inside the use_memory_pool(tag) context, all tensors created will be allocated in the memory pool, and has the same tag as the tag passed to the context.

When we call sleep, all tensors with the specified tag will be offloaded to CPU memory, and the rest of the tensors will be discarded. When we call wake_up, all tensors that are previously offloaded will be loaded back to GPU memory, and the rest of the tensors will have empty memory (zeroed on ROCm).

The _create_and_map, _unmap_and_release, _create_cpu_backup, _release_cpu_backup, _copy, _get_pluggable_allocator, and _use_memory_pool_with_allocator methods are subclass hooks for out-of-tree device allocators. Their names and signatures form the subclassing contract; renaming them or changing their signatures is a breaking change.

Why it needs to be a singleton? When allocated tensors are garbage collected, PyTorch will call the free callback, which will call the python_free_callback method. The C-extension uses a global variable to store the function of an instance of this class. If we create multiple instances of this class, the global variable will be overwritten and the free callback will not work as expected.

Methods:

  • close –

    Compatibility alias for deterministic pool release.

  • cudagraph_pool –

    Tag CUDA graph capture allocations and yield the graph pool id.

  • discard –

    Discard mapped allocations with the given tags without CPU backup.

  • get_current_usage –

    Get the total number of bytes allocated in the memory pool.

  • get_instance –

    CuMemAllocator is a singleton class.

  • release_pools –

    Drop Python references to MemPool/pluggable allocators eagerly.

  • sleep –

    Put the allocator in sleep mode.

  • use_memory_pool –

    A context manager to use the memory pool.

  • wake_up –

    Wake up the allocator from sleep mode.

_copy(src_ptr, dst_ptr, nbytes) staticmethod

Copy nbytes bytes from src_ptr to dst_ptr.

_python_free_callback(ptr)

Internal method to look up the allocation data when memory is freed in the memory pool.

_python_malloc_callback(allocation_handle)

Internal method to store the allocation data when memory is allocated in the memory pool.

_trim(pool)

Release free segments of a pool that is never allocated from again.

close()

Compatibility alias for deterministic pool release.

cudagraph_pool()

Tag CUDA graph capture allocations and yield the graph pool id.

discard(tags)

Discard mapped allocations with the given tags without CPU backup.

get_current_usage()

Get the total number of bytes allocated in the memory pool.

get_instance() staticmethod

CuMemAllocator is a singleton class. We cannot call the constructor directly. Call this method to get the instance.

release_pools()

Drop Python references to MemPool/pluggable allocators eagerly.

A cumem MemPool outlives the use_memory_pool context (a strong reference is kept in allocator_and_pools to work around pytorch/pytorch#146431), and a captured CUDA graph can keep it alive longer still. MemPool only holds a non-owning pointer to the allocator, whose owning reference lives in the Python CUDAPluggableAllocator. If both are instead dropped during interpreter shutdown, GC may finalize the allocator first; the eventual ~MemPool -> emptyCache -> release_block then makes a virtual call into the freed allocator -- aborting the process with "pure virtual method called" (pytorch/pytorch#145168).

Release the kept-alive pools before interpreter finalization, and keep the pluggable allocator wrappers alive while MemPool destructors run. This is safe to call more than once.

sleep(offload_tags=None, discardable=())

Put the allocator in sleep mode. All data in the memory allocation with the specified tag will be offloaded to CPU memory, and others will be discarded.

Parameters:

  • offload_tags

    (tuple[str, ...] | str | None, default: None ) –

    The tags of the memory allocation that will be offloaded. The rest of the memory allocation will be discarded.

  • discardable

    (Collection[int], default: () ) –

    Addresses of tensors the caller rewrites after waking. Weights allocations holding nothing else are discarded too.

use_memory_pool(tag=None)

A context manager to use the memory pool. All memory allocation created inside the context will be allocated in the memory pool, and has the specified tag.

Parameters:

  • tag

    (str | None, default: None ) –

    The tag of the memory allocation. If None, the default tag will be used.

wake_up(tags=None)

Wake up the allocator from sleep mode. All data that is previously offloaded will be loaded back to GPU memory, and the rest will have empty memory (zeroed on ROCm).

Parameters:

  • tags

    (list[str] | None, default: None ) –

    The deferrable tags (weights, kv_cache) to load back to GPU memory; every other tag is always loaded back. If None, all memory allocation will be loaded back to GPU memory.