vllm.utils.mem_utils ¶
Classes:
-
MemoryProfilingResult–Memory profiling result. All numbers are in bytes.
-
MemorySnapshot–Memory snapshot.
Functions:
-
get_cpu_memory–Returns the total CPU memory of the node in bytes.
-
get_max_shared_memory_bytes–Returns the maximum shared memory per thread block in bytes.
-
memory_profiling–Memory profiling context manager.
-
release_device_memory_under_pressure–On integrated (UMA) GPUs, release caching-allocator memory back to the
MemoryProfilingResult dataclass ¶
Memory profiling result. All numbers are in bytes.
Source code in vllm/utils/mem_utils.py
MemorySnapshot dataclass ¶
Memory snapshot.
Source code in vllm/utils/mem_utils.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
get_cpu_memory() ¶
get_max_shared_memory_bytes(gpu=0) cached ¶
Returns the maximum shared memory per thread block in bytes.
Source code in vllm/utils/mem_utils.py
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.).
Source code in vllm/utils/mem_utils.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
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.