vllm.device_allocator.cumem
AllocationData
dataclass
¶
Source code in vllm/device_allocator/cumem.py
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.
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.
Source code in vllm/device_allocator/cumem.py
106 107 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 |
|
__init__
¶
Source code in vllm/device_allocator/cumem.py
get_current_usage
¶
get_current_usage() -> int
Get the total number of bytes allocated in the memory pool.
Source code in vllm/device_allocator/cumem.py
get_instance
staticmethod
¶
get_instance() -> CuMemAllocator
CuMemAllocator is a singleton class. We cannot call the constructor directly. Call this method to get the instance.
Source code in vllm/device_allocator/cumem.py
python_free_callback
¶
python_free_callback(ptr: int) -> HandleType
Internal method to look up the allocation data when memory is freed in the memory pool.
Source code in vllm/device_allocator/cumem.py
python_malloc_callback
¶
python_malloc_callback(
allocation_handle: HandleType,
) -> None
Internal method to store the allocation data when memory is allocated in the memory pool.
Source code in vllm/device_allocator/cumem.py
sleep
¶
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.
:param offload_tags: The tags of the memory allocation that will be offloaded. The rest of the memory allocation will be discarded.
Source code in vllm/device_allocator/cumem.py
use_memory_pool
¶
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.
:param tag: The tag of the memory allocation. If None, the default tag will be used.
Source code in vllm/device_allocator/cumem.py
wake_up
¶
Wake up the allocator from sleep mode. All data that is previously offloaded will be loaded back to GPU memory, and the rest of the data will have empty memory.
:param tags: The tags of the memory allocation that will be loaded back to GPU memory. If None, all memory allocation will be loaded back to GPU memory.
Source code in vllm/device_allocator/cumem.py
create_and_map
¶
create_and_map(allocation_handle: HandleType) -> None
find_loaded_library
¶
According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
the file /proc/self/maps
contains the memory maps of the process, which includes the
shared libraries loaded by the process. We can use this file to find the path of the
a loaded library.
Source code in vllm/device_allocator/cumem.py
get_pluggable_allocator
¶
get_pluggable_allocator(
python_malloc_fn: Callable[[int], int],
python_free_func: Callable[[int, int], None],
) -> CUDAPluggableAllocator
Source code in vllm/device_allocator/cumem.py
unmap_and_release
¶
unmap_and_release(allocation_handle: HandleType) -> None
use_memory_pool_with_allocator
¶
use_memory_pool_with_allocator(
python_malloc_fn: Callable[[int], int],
python_free_func: Callable[[int, int], None],
) -> None