Convert formatting to use ruff instead of yapf + isort (#26247)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-05 15:06:22 +01:00
committed by GitHub
parent 17edd8a807
commit d6953beb91
1508 changed files with 115244 additions and 94146 deletions

View File

@@ -28,7 +28,7 @@ def find_loaded_library(lib_name) -> Optional[str]:
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.
""" # noqa
""" # noqa
found_line = None
with open("/proc/self/maps") as f:
for line in f:
@@ -43,17 +43,21 @@ def find_loaded_library(lib_name) -> Optional[str]:
start = found_line.index("/")
path = found_line[start:].strip()
filename = path.split("/")[-1]
assert filename.rpartition(".so")[0].startswith(lib_name), \
assert filename.rpartition(".so")[0].startswith(lib_name), (
f"Unexpected filename: {filename} for library {lib_name}"
)
return path
cumem_available = False
try:
from vllm.cumem_allocator import (init_module, python_create_and_map,
python_unmap_and_release)
from vllm.distributed.device_communicators.cuda_wrapper import (
CudaRTLibrary)
from vllm.cumem_allocator import (
init_module,
python_create_and_map,
python_unmap_and_release,
)
from vllm.distributed.device_communicators.cuda_wrapper import CudaRTLibrary
lib_name = find_loaded_library("cumem_allocator")
libcudart = CudaRTLibrary()
cumem_available = True
@@ -86,20 +90,19 @@ def unmap_and_release(allocation_handle: HandleType) -> None:
def get_pluggable_allocator(
python_malloc_fn: Callable[[int],
int], python_free_func: Callable[[int, int],
None]
python_malloc_fn: Callable[[int], int], python_free_func: Callable[[int, int], None]
) -> torch.cuda.memory.CUDAPluggableAllocator:
init_module(python_malloc_fn, python_free_func)
new_alloc = torch.cuda.memory.CUDAPluggableAllocator(
lib_name, 'my_malloc', 'my_free')
lib_name, "my_malloc", "my_free"
)
return new_alloc
@contextmanager
def use_memory_pool_with_allocator(
python_malloc_fn: Callable[[int], int],
python_free_func: Callable[[int, int], None]) -> None:
python_malloc_fn: Callable[[int], int], python_free_func: Callable[[int, int], None]
) -> None:
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):
@@ -130,6 +133,7 @@ class CuMemAllocator:
the global variable will be overwritten and the free callback will
not work as expected.
"""
instance: "CuMemAllocator" = None
default_tag: str = "default"
@@ -147,10 +151,11 @@ class CuMemAllocator:
def __init__(self):
conf = os.environ.get("PYTORCH_CUDA_ALLOC_CONF", "")
assert "expandable_segments:True" not in conf, \
("Expandable segments are not compatible with memory pool. "
assert "expandable_segments:True" not in conf, (
"Expandable segments are not compatible with memory pool. "
"Please track https://github.com/pytorch/pytorch/issues/147851 "
"for the latest updates.")
"for the latest updates."
)
self.pointer_to_data: dict[int, AllocationData] = {}
self.current_tag: str = CuMemAllocator.default_tag
@@ -167,10 +172,14 @@ class CuMemAllocator:
when memory is allocated in the memory pool."""
py_d_mem = allocation_handle[2]
self.pointer_to_data[py_d_mem] = AllocationData(
allocation_handle, self.current_tag)
allocation_handle, self.current_tag
)
logger.debug(
"Allocated %s bytes for %s with address %s from cumem allocator",
allocation_handle[1], self.current_tag, py_d_mem)
allocation_handle[1],
self.current_tag,
py_d_mem,
)
return
def _python_free_callback(self, ptr: int) -> HandleType:
@@ -182,13 +191,13 @@ class CuMemAllocator:
data.cpu_backup_tensor = None
logger.debug(
"Freed %s bytes for %s with address %s from cumem allocator",
data.handle[1], data.tag, ptr)
data.handle[1],
data.tag,
ptr,
)
return data.handle
def sleep(
self,
offload_tags: Optional[Union[tuple[str, ...],
str]] = None) -> None:
def sleep(self, offload_tags: Optional[Union[tuple[str, ...], str]] = None) -> None:
"""
Put the allocator in sleep mode.
All data in the memory allocation with the specified tag will be
@@ -200,9 +209,9 @@ class CuMemAllocator:
if offload_tags is None:
# by default, allocated tensors are offloaded
# when the allocator sleeps
offload_tags = (CuMemAllocator.default_tag, )
offload_tags = (CuMemAllocator.default_tag,)
elif isinstance(offload_tags, str):
offload_tags = (offload_tags, )
offload_tags = (offload_tags,)
assert isinstance(offload_tags, tuple)
@@ -218,8 +227,9 @@ class CuMemAllocator:
cpu_backup_tensor = torch.empty(
size_in_bytes,
dtype=torch.uint8,
device='cpu',
pin_memory=is_pin_memory_available())
device="cpu",
pin_memory=is_pin_memory_available(),
)
cpu_ptr = cpu_backup_tensor.data_ptr()
libcudart.cudaMemcpy(cpu_ptr, ptr, size_in_bytes)
data.cpu_backup_tensor = cpu_backup_tensor
@@ -228,8 +238,11 @@ class CuMemAllocator:
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 "
"directly.", total_bytes / 1024**3, backup_bytes / 1024**3,
(total_bytes - backup_bytes) / 1024**3)
"directly.",
total_bytes / 1024**3,
backup_bytes / 1024**3,
(total_bytes - backup_bytes) / 1024**3,
)
gc.collect()
torch.cuda.empty_cache()
@@ -251,8 +264,9 @@ class CuMemAllocator:
if data.cpu_backup_tensor is not None:
cpu_backup_tensor = data.cpu_backup_tensor
if cpu_backup_tensor is not None:
size_in_bytes = cpu_backup_tensor.numel(
) * cpu_backup_tensor.element_size()
size_in_bytes = (
cpu_backup_tensor.numel() * cpu_backup_tensor.element_size()
)
cpu_ptr = cpu_backup_tensor.data_ptr()
libcudart.cudaMemcpy(ptr, cpu_ptr, size_in_bytes)
data.cpu_backup_tensor = None
@@ -274,8 +288,9 @@ class CuMemAllocator:
old_tag = self.current_tag
self.current_tag = tag
with use_memory_pool_with_allocator(self.python_malloc_callback,
self.python_free_callback) as data:
with use_memory_pool_with_allocator(
self.python_malloc_callback, self.python_free_callback
) as data:
# 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.