Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -80,8 +80,7 @@ def use_memory_pool_with_allocator(
|
||||
|
||||
|
||||
class CuMemAllocator:
|
||||
"""
|
||||
A singleton class that manages a memory pool for CUDA tensors.
|
||||
"""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.
|
||||
|
||||
@@ -109,8 +108,7 @@ class CuMemAllocator:
|
||||
|
||||
@staticmethod
|
||||
def get_instance() -> "CuMemAllocator":
|
||||
"""
|
||||
CuMemAllocator is a singleton class.
|
||||
"""CuMemAllocator is a singleton class.
|
||||
We cannot call the constructor directly.
|
||||
Call this method to get the instance.
|
||||
"""
|
||||
@@ -182,9 +180,9 @@ class CuMemAllocator:
|
||||
self.release_pools()
|
||||
|
||||
def _python_malloc_callback(self, allocation_handle: HandleType) -> None:
|
||||
"""Internal method to store the allocation data
|
||||
when memory is allocated in the memory pool.
|
||||
"""
|
||||
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(
|
||||
allocation_handle, self.current_tag
|
||||
@@ -198,9 +196,9 @@ class CuMemAllocator:
|
||||
return
|
||||
|
||||
def _python_free_callback(self, ptr: int) -> HandleType:
|
||||
"""Internal method to look up the allocation data
|
||||
when memory is freed in the memory pool.
|
||||
"""
|
||||
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
|
||||
@@ -227,14 +225,14 @@ class CuMemAllocator:
|
||||
return data.handle
|
||||
|
||||
def sleep(self, offload_tags: tuple[str, ...] | str | None = None) -> None:
|
||||
"""
|
||||
Put the allocator in sleep mode.
|
||||
"""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.
|
||||
|
||||
Args:
|
||||
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
|
||||
@@ -325,8 +323,7 @@ class CuMemAllocator:
|
||||
)
|
||||
|
||||
def wake_up(self, tags: list[str] | None = None) -> None:
|
||||
"""
|
||||
Wake up the allocator from sleep mode.
|
||||
"""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.
|
||||
|
||||
@@ -334,6 +331,7 @@ class CuMemAllocator:
|
||||
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.
|
||||
|
||||
"""
|
||||
gc.collect()
|
||||
torch.accelerator.empty_cache()
|
||||
@@ -357,14 +355,14 @@ class CuMemAllocator:
|
||||
|
||||
@contextmanager
|
||||
def use_memory_pool(self, tag: str | None = None):
|
||||
"""
|
||||
A context manager to use the 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.
|
||||
|
||||
Args:
|
||||
tag: The tag of the memory allocation. If None, the default tag
|
||||
will be used.
|
||||
|
||||
"""
|
||||
if tag is None:
|
||||
tag = CuMemAllocator.default_tag
|
||||
@@ -416,9 +414,7 @@ class CuMemAllocator:
|
||||
torch.cuda.memory._set_allocator_settings("expandable_segments:True")
|
||||
|
||||
def get_current_usage(self) -> int:
|
||||
"""
|
||||
Get the total number of bytes allocated in the memory pool.
|
||||
"""
|
||||
"""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
|
||||
|
||||
@@ -70,7 +70,8 @@ class SleepModeBackend(ABC):
|
||||
|
||||
def state(self) -> SleepModeState:
|
||||
"""Current lifecycle state. Lets ``/health`` distinguish a healthy-idle
|
||||
(suspended) engine from a healthy-serving one (see RFC #34303)."""
|
||||
(suspended) engine from a healthy-serving one (see RFC #34303).
|
||||
"""
|
||||
return self._state
|
||||
|
||||
# -- Capability introspection (no instance required) --
|
||||
@@ -83,26 +84,30 @@ class SleepModeBackend(ABC):
|
||||
@classmethod
|
||||
def preserves_communicators(cls) -> bool:
|
||||
"""If False, collective communicators (e.g. NCCL) are destroyed by
|
||||
``suspend`` and the executor must re-initialize them on ``resume``."""
|
||||
``suspend`` and the executor must re-initialize them on ``resume``.
|
||||
"""
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def preserves_compiled_artifacts(cls) -> bool:
|
||||
"""If True, torch.compile / JIT kernels survive suspend/resume and need
|
||||
not be recompiled on resume."""
|
||||
not be recompiled on resume.
|
||||
"""
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def preserves_graphs_with_communicators(cls) -> bool:
|
||||
"""If True, CUDA graphs containing collective communicators (e.g. NCCL)
|
||||
stay valid after resume. False when communicators are rebuilt (embedded
|
||||
comm handles go stale)."""
|
||||
comm handles go stale).
|
||||
"""
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def supports_durable_storage(cls) -> bool:
|
||||
"""If True, suspended state can be persisted beyond the process
|
||||
lifetime (disk or object storage) and restored in a new process."""
|
||||
lifetime (disk or object storage) and restored in a new process.
|
||||
"""
|
||||
return False
|
||||
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ class XpuMemAllocator:
|
||||
Sleep will offload selected payloads to CPU or discard and unmap XPU
|
||||
physical memory. Wake-up remaps physical memory back to the same
|
||||
reserved virtual address and restores payload.
|
||||
|
||||
"""
|
||||
|
||||
instance: "XpuMemAllocator | None" = None
|
||||
|
||||
Reference in New Issue
Block a user