mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2025-12-10 06:45:01 +08:00
- **Add SPDX license headers to python source files**
- **Check for SPDX headers using pre-commit**
commit 9d7ef44c3cfb72ca4c32e1c677d99259d10d4745
Author: Russell Bryant <rbryant@redhat.com>
Date: Fri Jan 31 14:18:24 2025 -0500
Add SPDX license headers to python source files
This commit adds SPDX license headers to python source files as
recommended to
the project by the Linux Foundation. These headers provide a concise way
that is
both human and machine readable for communicating license information
for each
source file. It helps avoid any ambiguity about the license of the code
and can
also be easily used by tools to help manage license compliance.
The Linux Foundation runs license scans against the codebase to help
ensure
we are in compliance with the licenses of the code we use, including
dependencies. Having these headers in place helps that tool do its job.
More information can be found on the SPDX site:
- https://spdx.dev/learn/handling-license-info/
Signed-off-by: Russell Bryant <rbryant@redhat.com>
commit 5a1cf1cb3b80759131c73f6a9dddebccac039dea
Author: Russell Bryant <rbryant@redhat.com>
Date: Fri Jan 31 14:36:32 2025 -0500
Check for SPDX headers using pre-commit
Signed-off-by: Russell Bryant <rbryant@redhat.com>
---------
Signed-off-by: Russell Bryant <rbryant@redhat.com>
115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import torch
|
|
|
|
from vllm import LLM, SamplingParams
|
|
from vllm.device_allocator.cumem import CuMemAllocator
|
|
from vllm.utils import GiB_bytes
|
|
|
|
from ..utils import fork_new_process_for_each_test
|
|
|
|
|
|
@fork_new_process_for_each_test
|
|
def test_basic_cumem():
|
|
# some tensors from default memory pool
|
|
shape = (1024, 1024)
|
|
x = torch.empty(shape, device='cuda')
|
|
x.zero_()
|
|
|
|
# some tensors from custom memory pool
|
|
allocator = CuMemAllocator.get_instance()
|
|
with allocator.use_memory_pool():
|
|
# custom memory pool
|
|
y = torch.empty(shape, device='cuda')
|
|
y.zero_()
|
|
y += 1
|
|
z = torch.empty(shape, device='cuda')
|
|
z.zero_()
|
|
z += 2
|
|
|
|
# they can be used together
|
|
output = x + y + z
|
|
assert torch.allclose(output, torch.ones_like(output) * 3)
|
|
|
|
free_bytes = torch.cuda.mem_get_info()[0]
|
|
allocator.sleep()
|
|
free_bytes_after_sleep = torch.cuda.mem_get_info()[0]
|
|
assert free_bytes_after_sleep > free_bytes
|
|
allocator.wake_up()
|
|
|
|
# they can be used together
|
|
output = x + y + z
|
|
assert torch.allclose(output, torch.ones_like(output) * 3)
|
|
|
|
|
|
@fork_new_process_for_each_test
|
|
def test_cumem_with_cudagraph():
|
|
allocator = CuMemAllocator.get_instance()
|
|
with allocator.use_memory_pool():
|
|
weight = torch.eye(1024, device='cuda')
|
|
with allocator.use_memory_pool(tag="discard"):
|
|
cache = torch.empty(1024, 1024, device='cuda')
|
|
|
|
def model(x):
|
|
out = x @ weight
|
|
cache[:out.size(0)].copy_(out)
|
|
return out + 1
|
|
|
|
x = torch.empty(128, 1024, device='cuda')
|
|
|
|
# warmup
|
|
model(x)
|
|
|
|
# capture cudagraph
|
|
model_graph = torch.cuda.CUDAGraph()
|
|
with torch.cuda.graph(model_graph):
|
|
y = model(x)
|
|
|
|
free_bytes = torch.cuda.mem_get_info()[0]
|
|
allocator.sleep()
|
|
free_bytes_after_sleep = torch.cuda.mem_get_info()[0]
|
|
assert free_bytes_after_sleep > free_bytes
|
|
allocator.wake_up()
|
|
|
|
# after waking up, the content in the weight tensor
|
|
# should be restored, but the content in the cache tensor
|
|
# should be discarded
|
|
|
|
# this operation is also compatible with cudagraph
|
|
|
|
x.random_()
|
|
model_graph.replay()
|
|
|
|
# cache content is as expected
|
|
assert torch.allclose(x, cache[:x.size(0)])
|
|
|
|
# output content is as expected
|
|
assert torch.allclose(y, x + 1)
|
|
|
|
|
|
@fork_new_process_for_each_test
|
|
def test_end_to_end():
|
|
free, total = torch.cuda.mem_get_info()
|
|
used_bytes_baseline = total - free # in case other process is running
|
|
llm = LLM("meta-llama/Llama-3.2-1B", enable_sleep_mode=True)
|
|
prompt = "How are you?"
|
|
sampling_params = SamplingParams(temperature=0, max_tokens=10)
|
|
output = llm.generate(prompt, sampling_params)
|
|
|
|
# the benefit of `llm.sleep(level=2)` is mainly CPU memory usage,
|
|
# which is difficult to measure in the test. therefore, we only
|
|
# test sleep level 1 here.
|
|
llm.sleep(level=1)
|
|
|
|
free_gpu_bytes_after_sleep, total = torch.cuda.mem_get_info()
|
|
used_bytes = total - free_gpu_bytes_after_sleep - used_bytes_baseline
|
|
# now the memory usage is mostly cudagraph memory pool,
|
|
# and it should be less than the model weights (1B model, 2GiB weights)
|
|
assert used_bytes < 2 * GiB_bytes
|
|
|
|
llm.wake_up()
|
|
output2 = llm.generate(prompt, sampling_params)
|
|
|
|
# cmp output
|
|
assert output[0].outputs[0].text == output2[0].outputs[0].text
|