-
Notifications
You must be signed in to change notification settings - Fork 9
fix(llm): size VRAM headroom from reclaimable memory on integrated GPUs #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -436,12 +436,35 @@ class _VRAMAllocation: | |||||||||||||||||||||||||||||||||||||
| memory_bytes: int | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _reclaimable_available_bytes() -> int | None: | ||||||||||||||||||||||||||||||||||||||
| """Return kernel-reclaimable system memory (``MemAvailable``) in bytes. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ``MemAvailable`` in ``/proc/meminfo`` accounts for reclaimable page cache, so | ||||||||||||||||||||||||||||||||||||||
| it reflects what the kernel can actually hand out. Returns ``None`` when it | ||||||||||||||||||||||||||||||||||||||
| cannot be read (for example on non-Linux hosts, where ``/proc/meminfo`` is | ||||||||||||||||||||||||||||||||||||||
| absent). | ||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||
| with open("/proc/meminfo", encoding="utf-8") as meminfo: | ||||||||||||||||||||||||||||||||||||||
| for line in meminfo: | ||||||||||||||||||||||||||||||||||||||
| if line.startswith("MemAvailable:"): | ||||||||||||||||||||||||||||||||||||||
| return int(line.split()[1]) * 1024 # reported in kibibytes | ||||||||||||||||||||||||||||||||||||||
| except (OSError, ValueError, IndexError): | ||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _get_vram_allocations(max_vram_fraction: float | None = None) -> dict[int, _VRAMAllocation]: | ||||||||||||||||||||||||||||||||||||||
| """Calculate maximum memory allocation for each available GPU. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Reserves a 2 GiB safety buffer on each device, then applies | ||||||||||||||||||||||||||||||||||||||
| ``max_vram_fraction`` to the remaining free memory. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| On systems with integrated GPUs (e.g. DGX Spark) GPU memory *is* system memory, | ||||||||||||||||||||||||||||||||||||||
| so ``torch.cuda.mem_get_info`` reports only unallocated pages and ignores the | ||||||||||||||||||||||||||||||||||||||
| reclaimable page cache -- badly under-reporting real headroom. There we use | ||||||||||||||||||||||||||||||||||||||
| the kernel's ``MemAvailable`` (capped at device total) instead. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||
| max_vram_fraction: Fraction of total GPU memory to allocate. | ||||||||||||||||||||||||||||||||||||||
| Defaults to ``0.8`` (80 %). | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -457,8 +480,12 @@ def _get_vram_allocations(max_vram_fraction: float | None = None) -> dict[int, _ | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if torch.cuda.is_available(): | ||||||||||||||||||||||||||||||||||||||
| num_gpus = torch.cuda.device_count() | ||||||||||||||||||||||||||||||||||||||
| # System-wide value; read once rather than per device. | ||||||||||||||||||||||||||||||||||||||
| reclaimable = _reclaimable_available_bytes() | ||||||||||||||||||||||||||||||||||||||
| for i in range(num_gpus): | ||||||||||||||||||||||||||||||||||||||
| free, total = torch.cuda.mem_get_info(device=i) | ||||||||||||||||||||||||||||||||||||||
| if getattr(torch.cuda.get_device_properties(i), "is_integrated", False) and reclaimable is not None: | ||||||||||||||||||||||||||||||||||||||
| free = min(max(free, reclaimable), total) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+483
to
+488
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agent ( Minor cleanup:
Suggested change
Please also extend the discrete-GPU test to assert that
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcusds - sorry this one took so long, kinda fell by the wayside. thank you! |
||||||||||||||||||||||||||||||||||||||
| safe_free = max(free - (2 * 1024**3), 0) | ||||||||||||||||||||||||||||||||||||||
| gpu_memory_utilization = min(max_vram_fraction, safe_free / total) if total > 0 else 0.0 | ||||||||||||||||||||||||||||||||||||||
| memory_bytes = int(gpu_memory_utilization * total) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could do
from contextlib import suppress