Skip to content

feat(cuda): TENSORFOLD_MEMORY_RESERVE_GIB sets what the startup budget leaves free - #133

Closed
MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/cuda-memory-reserve
Closed

MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:pr/cuda-memory-reserve

Conversation

@MiaAI-Lab

@MiaAI-Lab MiaAI-Lab commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: TENSORFOLD_MEMORY_RESERVE_GIB sets how much memory the CUDA startup budget leaves free. Unset, the reserve is exactly what it is today: max(4 GiB, a tenth of memory). On unified-memory machines such as the DGX Spark, where the default holds back 12.2 GiB, an operator who knows the headroom can give more of it to KV caches (or keep more back for other work).

The problem

cuda/capacity.available_bytes sizes every CUDA engine's startup budget as free memory minus max(4 GiB, total / 10). The reserve covers what the estimate doesn't count (CUDA context, NCCL, workspaces) and the host itself, and it can't be changed. On a unified-memory GPU the host side is the whole budget, so a DGX Spark (128 GB, MemTotal 121.7 GiB) always keeps 12.2 GiB back, however much of that the machine actually needs. With large models, that decides how many streams or how long a window fits. The only way to leave more room for other work is a smaller --context / --parallel.

The change

  • capacity.reserve_bytes(total, host=False): with the variable unset it returns today's reserve exactly, including the rounding (the GPU path's math.ceil(total / 10) and the host path's MemTotal // 10). When TENSORFOLD_MEMORY_RESERVE_GIB is set, it returns that many GiB instead, on both sides.
  • Accepted values: a number of GiB from 2 to the memory's size. Anything else (below 2, above the total, nan, not a number) raises a ValueError naming the variable at startup.
  • available_bytes calls reserve_bytes in place of the two inline expressions. Nothing else changes.
  • docs/api.md: the unified-memory paragraph names the variable, with a warning that the reserve also carries memory the estimate doesn't count and that running a unified GPU out of memory can freeze the host.

Tests

  • tests/test_memory_reserve.py: the default is unchanged, including the rounding on each side and the 4 GiB floor; overrides (integer and fractional, surrounding spaces); out-of-range and non-numeric values refuse; available_bytes uses the reserve on a unified GPU, on a discrete GPU (bounded by both sides), and without /proc/meminfo.
  • In tensorfold-glm53:v0.5.0 (NVIDIA PyTorch container, aarch64): test_memory_reserve.py, test_cuda_capacity.py, test_cuda_unified_memory.py, test_cuda_geometry.py: 81 passed. With pytest tests --ignore=tests/cuda -k 'glm or capacity or geometry or cuda or memory': 1155 passed and 21 failed. The failures are the same 21 on unmodified main (1146 passed): MLX memory tests (test_memory_window.py, test_prompt_memory.py, test_resume_memory.py, ...) that need mlx.
  • Not run: a live server with the variable set. The change is limited to the budget arithmetic, and the tests cover it directly.

…t leaves free

available_bytes left max(4 GiB, a tenth of memory) free on both sides: the GPU's mem_get_info total (a tenth
rounded up) and, where /proc/meminfo exists, MemTotal (a tenth rounded down). reserve_bytes() keeps that default
exactly and takes TENSORFOLD_MEMORY_RESERVE_GIB instead when it is set: a number of GiB from 2 to the memory's
size, anything else refuses at startup. On a unified-memory GPU the host side is the whole budget, so a DGX Spark
(128 GB, MemTotal 121.7 GiB) always kept 12.2 GiB back; an operator who knows the machine's headroom can now
give some of it to KV caches, or keep more for other work.

docs/api.md names the variable next to the reserve it replaces.

Tests: tests/test_memory_reserve.py (the default, rounding included; overrides; out-of-range and non-numeric
values refuse; available_bytes on unified and discrete GPUs with and without /proc/meminfo).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014wCj5zx4NuKdUNWUeJi3X1
@jkuepker

Copy link
Copy Markdown
Contributor

This PR conflicts with #106 (the host-RAM tier for Qwen3.8-27B) in capacity.available_bytes.

#106 moves the host term into a helper, host_room(), which is MemAvailable less max(4 GiB, MemTotal // 10). available_bytes calls it, and so does --ram-tier-gib at startup to refuse a tier larger than the host can spare. A merge that keeps only one side's version of that hunk leaves the two reserves out of step:

  • The tier's check keeps the fixed reserve while the startup budget uses TENSORFOLD_MEMORY_RESERVE_GIB, or
  • The env var stops reaching the host term.

The resolution I'd suggest is to have host_room() use your reserve, and available_bytes keep calling it:

def host_room() -> int | None:
    memory = _meminfo()
    return None if memory is None else max(0, memory["MemAvailable"] - reserve_bytes(memory["MemTotal"], host=True))


def available_bytes(torch) -> int:
    free, total = map(int, torch.cuda.mem_get_info())
    available = max(0, free - reserve_bytes(total))
    host = host_room()
    ...

I tried this merge locally. Your test_memory_reserve.py passes, and so do #106's tier tests and the capacity and unified-memory tests (105 passed). With TENSORFOLD_MEMORY_RESERVE_GIB=6, the tier's check sees the same 6 GiB reserve as the startup budget. #106's refusal message ("available memory less a tenth of it or 4 GiB") would then need to name the startup reserve instead.

If this PR lands first, I'll rebase #106 onto it with the change above.

@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks for the knob. TENSORFOLD_MEMORY_RESERVE_GIB replaces the startup reserve when you set it, from 2 GiB up to the size of memory, and a value that does not parse refuses to start. The default is unchanged, the larger of 4 GiB and a tenth of memory. On a 128 GB unified machine that default was keeping 12.2 GiB back from a 121.7 GiB total. The docs keep the warning that a reserve set too low can freeze the machine. This is in 0.6.0, out tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants