Skip to content

vllm.utils.mem_utils

Classes:

Functions:

MemoryProfilingResult dataclass

Memory profiling result. All numbers are in bytes.

MemorySnapshot dataclass

Memory snapshot.

device_memory_info(device=None)

Free and total device memory in bytes, like torch.accelerator.get_memory_info, with the free memory corrected on integrated GPUs.

On UMA (Unified Memory Architecture) platforms where CPU and GPU share physical memory (e.g. GH200, DGX Spark, Jetson Orin), cudaMemGetInfo underreports free memory because it does not account for reclaimable OS memory (page cache, buffers), which a device allocation does reclaim. psutil reports the true available memory there. https://docs.nvidia.com/cuda/cuda-for-tegra-appnote/#estimating-total-allocatable-device-memory-on-an-integrated-gpu-device

get_max_shared_memory_bytes(gpu=0) cached

Returns the maximum shared memory per thread block in bytes.

memory_profiling(baseline_snapshot, weights_memory=0)

Memory profiling context manager.

baseline_snapshot: the memory snapshot before the current vLLM instance. weights_memory: memory used by PyTorch when loading the model weights. Note that, before loading the model weights, we also initialize the device and distributed environment, which may consume some memory. This part is not included in the weights_memory because PyTorch does not control it.

The memory in one GPU can be classified into 3 categories: 1. memory used by anything other than the current vLLM instance. 2. memory used by torch in the current vLLM instance. 3. memory used in the current vLLM instance, but not by torch.

A quantitive example:

Before creating the current vLLM instance

category 1: 1 GiB category 2: 0 GiB category 3: 0 GiB

After creating the current vLLM instance and loading the model, (i.e. before profiling): category 1: 1 GiB category 2: 2 GiB (model weights take 2 GiB) category 3: 0.5 GiB (memory used by NCCL)

During profiling (peak): category 1: 1 GiB category 2: 4 GiB (peak activation tensors take 2 GiB) category 3: 1 GiB (memory used by NCCL + buffers for some attention backends)

After profiling

category 1: 1 GiB category 2: 3 GiB (after garbage-collecting activation tensors) category 3: 1 GiB (memory used by NCCL + buffers for some attention backends)

In this case, non-kv cache takes 5 GiB in total, including: a. 2 GiB used by the model weights (category 2) b. 2 GiB reserved for the peak activation tensors (category 2) c. 1 GiB used by non-torch components (category 3)

The memory used for loading weights (a.) is directly given from the argument weights_memory.

The increase of torch.accelerator.memory_stats()["allocated_bytes.all.peak"] during profiling gives (b.).

The increase of non_torch_memory from creating the current vLLM instance until after profiling to get (c.).

release_device_memory_under_pressure(device)

On integrated (UMA) GPUs, release caching-allocator memory back to the OS when system memory pressure is high. The OS may start thrashing before an allocation failure would trigger PyTorch's own cache release.

Returns:

  • bool –

    True if memory was released.