cumem.py 12.2 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7
8
9
10
11
# cumem-based pytorch pluggable allocator to implement sleep mode.
# other approaches tried but failed:
# - cuda-python package binding
# - custom libcuda driver ctypes wrapper
# both of them failed because of cuda context mismatch.
# not sure why, they are created from a different context.
# the only successful approach is to call cuda driver API in C.
import dataclasses
12
import gc
13
import os
14
from contextlib import contextmanager
15
from typing import Any, Callable, Optional, Union
16
17
18

import torch

19
from vllm.logger import init_logger
20
21
from vllm.utils import is_pin_memory_available

22
23
logger = init_logger(__name__)

24
25
26
27
28
29
30

def find_loaded_library(lib_name) -> Optional[str]:
    """
    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.
31
    """  # noqa
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    found_line = None
    with open("/proc/self/maps") as f:
        for line in f:
            if lib_name in line:
                found_line = line
                break
    if found_line is None:
        # the library is not loaded in the current process
        return None
    # if lib_name is libcudart, we need to match a line with:
    # address /path/to/libcudart-hash.so.11.0
    start = found_line.index("/")
    path = found_line[start:].strip()
    filename = path.split("/")[-1]
46
    assert filename.rpartition(".so")[0].startswith(lib_name), (
47
        f"Unexpected filename: {filename} for library {lib_name}"
48
    )
49
50
51
52
53
    return path


cumem_available = False
try:
54
55
56
57
58
59
60
    from vllm.cumem_allocator import (
        init_module,
        python_create_and_map,
        python_unmap_and_release,
    )
    from vllm.distributed.device_communicators.cuda_wrapper import CudaRTLibrary

61
62
63
64
65
66
67
68
69
70
71
72
73
    lib_name = find_loaded_library("cumem_allocator")
    libcudart = CudaRTLibrary()
    cumem_available = True
except ModuleNotFoundError:
    # rocm platform does not support cumem allocator
    init_module = None
    python_create_and_map = None
    python_unmap_and_release = None
    CudaRTLibrary = None
    lib_name = None
    libcudart = None

# py_device, py_alignedSize, py_d_mem, py_p_memHandle
74
HandleType = tuple[int, int, int, int]
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


@dataclasses.dataclass
class AllocationData:
    handle: HandleType
    tag: str
    cpu_backup_tensor: Optional[torch.Tensor] = None


def create_and_map(allocation_handle: HandleType) -> None:
    python_create_and_map(*allocation_handle)


def unmap_and_release(allocation_handle: HandleType) -> None:
    python_unmap_and_release(*allocation_handle)


def get_pluggable_allocator(
93
    python_malloc_fn: Callable[[int], int], python_free_func: Callable[[int, int], None]
94
95
96
) -> torch.cuda.memory.CUDAPluggableAllocator:
    init_module(python_malloc_fn, python_free_func)
    new_alloc = torch.cuda.memory.CUDAPluggableAllocator(
97
98
        lib_name, "my_malloc", "my_free"
    )
99
100
101
102
103
    return new_alloc


@contextmanager
def use_memory_pool_with_allocator(
104
105
    python_malloc_fn: Callable[[int], int], python_free_func: Callable[[int, int], None]
) -> None:
106
107
108
    new_alloc = get_pluggable_allocator(python_malloc_fn, python_free_func)
    mem_pool = torch.cuda.memory.MemPool(new_alloc._allocator)
    with torch.cuda.memory.use_mem_pool(mem_pool):
109
        yield mem_pool, new_alloc
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


class 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.
    """
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    instance: "CuMemAllocator" = None
    default_tag: str = "default"

    @staticmethod
    def get_instance() -> "CuMemAllocator":
        """
        CuMemAllocator is a singleton class.
        We cannot call the constructor directly.
        Call this method to get the instance.
        """
        assert cumem_available, "cumem allocator is not available"
        if CuMemAllocator.instance is None:
            CuMemAllocator.instance = CuMemAllocator()
        return CuMemAllocator.instance

    def __init__(self):
153
        conf = os.environ.get("PYTORCH_CUDA_ALLOC_CONF", "")
154
155
        assert "expandable_segments:True" not in conf, (
            "Expandable segments are not compatible with memory pool. "
156
            "Please track https://github.com/pytorch/pytorch/issues/147851 "
157
158
            "for the latest updates."
        )
159

160
        self.pointer_to_data: dict[int, AllocationData] = {}
161
        self.current_tag: str = CuMemAllocator.default_tag
162
        self.allocator_and_pools: dict[str, Any] = {}
163
164
165
166
167
        # Creating strong references to the two callbacks here to prevent
        # these ephemeral bound-method objects being garbage collected.
        # See discussions in https://github.com/vllm-project/vllm/pull/22724
        self.python_malloc_callback = self._python_malloc_callback
        self.python_free_callback = self._python_free_callback
168

169
    def _python_malloc_callback(self, allocation_handle: HandleType) -> None:
170
171
172
173
174
        """
        Internal method to store the allocation data
        when memory is allocated in the memory pool."""
        py_d_mem = allocation_handle[2]
        self.pointer_to_data[py_d_mem] = AllocationData(
175
176
            allocation_handle, self.current_tag
        )
177
178
        logger.debug(
            "Allocated %s bytes for %s with address %s from cumem allocator",
179
180
181
182
            allocation_handle[1],
            self.current_tag,
            py_d_mem,
        )
183
184
        return

185
    def _python_free_callback(self, ptr: int) -> HandleType:
186
187
188
189
190
191
        """
        Internal method to look up the allocation data
        when memory is freed in the memory pool."""
        data = self.pointer_to_data.pop(ptr)
        if data.cpu_backup_tensor is not None:
            data.cpu_backup_tensor = None
192
193
        logger.debug(
            "Freed %s bytes for %s with address %s from cumem allocator",
194
195
196
197
            data.handle[1],
            data.tag,
            ptr,
        )
198
199
        return data.handle

200
    def sleep(self, offload_tags: Optional[Union[tuple[str, ...], str]] = None) -> None:
201
202
        """
        Put the allocator in sleep mode.
203
        All data in the memory allocation with the specified tag will be
204
205
206
207
208
209
210
211
        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.
        """
        if offload_tags is None:
            # by default, allocated tensors are offloaded
            # when the allocator sleeps
212
            offload_tags = (CuMemAllocator.default_tag,)
213
        elif isinstance(offload_tags, str):
214
            offload_tags = (offload_tags,)
215
216
217

        assert isinstance(offload_tags, tuple)

218
219
220
        total_bytes = 0
        backup_bytes = 0

221
222
        for ptr, data in self.pointer_to_data.items():
            handle = data.handle
223
            total_bytes += handle[1]
224
            if data.tag in offload_tags:
225
                backup_bytes += handle[1]
226
227
228
229
                size_in_bytes = handle[1]
                cpu_backup_tensor = torch.empty(
                    size_in_bytes,
                    dtype=torch.uint8,
230
231
232
                    device="cpu",
                    pin_memory=is_pin_memory_available(),
                )
233
234
235
236
237
                cpu_ptr = cpu_backup_tensor.data_ptr()
                libcudart.cudaMemcpy(cpu_ptr, ptr, size_in_bytes)
                data.cpu_backup_tensor = cpu_backup_tensor
            unmap_and_release(handle)

238
239
240
        logger.info(
            "CuMemAllocator: sleep freed %.2f GiB memory in total, of which "
            "%.2f GiB is backed up in CPU and the rest %.2f GiB is discarded "
241
242
243
244
245
            "directly.",
            total_bytes / 1024**3,
            backup_bytes / 1024**3,
            (total_bytes - backup_bytes) / 1024**3,
        )
246

247
248
249
        gc.collect()
        torch.cuda.empty_cache()

250
    def wake_up(self, tags: Optional[list[str]] = None) -> None:
251
252
        """
        Wake up the allocator from sleep mode.
253
        All data that is previously offloaded will be loaded back to GPU
254
        memory, and the rest of the data will have empty memory.
255

256
257
258
259
        :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.
        """
260
        for ptr, data in self.pointer_to_data.items():
261
262
263
264
265
266
            if tags is None or data.tag in tags:
                handle = data.handle
                create_and_map(handle)
                if data.cpu_backup_tensor is not None:
                    cpu_backup_tensor = data.cpu_backup_tensor
                    if cpu_backup_tensor is not None:
267
268
269
                        size_in_bytes = (
                            cpu_backup_tensor.numel() * cpu_backup_tensor.element_size()
                        )
270
271
272
                        cpu_ptr = cpu_backup_tensor.data_ptr()
                        libcudart.cudaMemcpy(ptr, cpu_ptr, size_in_bytes)
                        data.cpu_backup_tensor = None
273
274
275
276
277

    @contextmanager
    def use_memory_pool(self, tag: Optional[str] = None):
        """
        A context manager to use the memory pool.
278
        All memory allocation created inside the context will be allocated
279
280
281
282
283
284
285
286
287
288
289
290
        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.
        """
        if tag is None:
            tag = CuMemAllocator.default_tag

        assert isinstance(tag, str)

        old_tag = self.current_tag
        self.current_tag = tag
291
292
293
        with use_memory_pool_with_allocator(
            self.python_malloc_callback, self.python_free_callback
        ) as data:
294
295
296
297
298
299
            # start to hit another PyTorch bug in PyTorch 2.6,
            # possibly because of gc-related issue w.r.t. the allocator and
            # the memory pool.
            # to avoid the issue, we keep a reference of the data.
            # see https://github.com/pytorch/pytorch/issues/146431 .
            self.allocator_and_pools[tag] = data
300
301
302
303
304
            yield
            # PyTorch's bug, calling torch.cuda.empty_cache() will error
            # when using pluggable allocator, see
            # https://github.com/pytorch/pytorch/issues/145168 .
            # if we have some memory allocated and then freed,
305
306
307
308
309
310
311
312
313
314
315
            # the memory will not be released, e.g. in online quantization,
            # where the model is created in higher precision, and then
            # quantized in lower precision.
            # Find all unused allocations and manually release them.
            # TODO: we should expose `empty_cache` method in the memory pool.
            # TODO: ask for help from PyTorch team to expose this method.
            allocations = data[0].snapshot()
            for allocation in allocations:
                if allocation["allocated_size"] == 0:
                    handle = self._python_free_callback(allocation["address"])
                    unmap_and_release(handle)
316
317
318
319
320
321
322
323
324
325
326
            self.current_tag = old_tag

    def get_current_usage(self) -> int:
        """
        Get the total number of bytes allocated in the memory pool.
        """
        sum_bytes: int = 0
        for ptr, data in self.pointer_to_data.items():
            handle = data.handle
            sum_bytes += handle[1]
        return sum_bytes