Skip to content

Let users size the CUDA memory budget with TENSORFOLD_MEMORY_RESERVE_GIB and TENSORFOLD_CUDA_MEMORY_LIMIT_GB - #165

Closed
eleqtrizit wants to merge 4 commits into
ashhart:mainfrom
eleqtrizit:cuda-reserve-env
Closed

eleqtrizit wants to merge 4 commits into
ashhart:mainfrom
eleqtrizit:cuda-reserve-env

Conversation

@eleqtrizit

@eleqtrizit eleqtrizit commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

TensorFold's CUDA server keeps back part of the GPU's memory so requests don't crash
against the card's limits mid-reply. Until now that reserve was fixed, and the amount
of memory you could ask for was likewise fixed. This PR lets you set both.

What it does:

  • One variable controls the reserve: TENSORFOLD_MEMORY_RESERVE_GIB. It sets how much
    memory the server holds back on the GPU and on the host, in GiB. The default stays
    max(4 GiB, a tenth of the memory). A value below 2 is refused at startup.
  • One variable controls the budget: TENSORFOLD_CUDA_MEMORY_LIMIT_GB. It is a hard
    override in GiB: the startup budget is exactly that many GiB, replacing both the
    reserve and the free-memory figure, and it can raise the budget as well as lower it.
    Whoever sets it owns the headroom; a budget past what the card holds ends startup
    or requests with CUDA out-of-memory errors.
  • The limit line prices in the memory serving holds outside the tensor allocator: the
    CUDA context, kernels and streams, and the allocator's reserved-but-unallocated
    slabs, measured at 2 GiB on a 27B Qwen3.8 serve. An admitted checkpoint then fits
    its GiB as nvidia-smi shows it, without serving-time OOM. A tiny limit still serves
    something: the least budget is 2 GiB.
  • With the limit set, startup claims the whole line at once: the request caches reuse
    the already-reserved segments, so nvidia-smi shows the limit's figure while idle and
    no cache growth later asks the driver for memory admission did not count.
  • The live free-memory gate reads the limit too but stays capped by real free memory,
    so requests still cannot take what the card does not have.
  • The memory gate and the Flash Next one-GPU and two-rank copy-peak caps count the
    same tensor budget, so cache growth stays inside the line it was admitted against.

Tests cover both variables, the refusals for invalid values, the 2-GiB floor, and how
the reserve and the limit combine. README, RUNBOOK, docs/api.md, the CUDA recipe, and
the changelog document all of it.

The branch is rebased on upstream main (v0.6.5).

@eleqtrizit eleqtrizit changed the title cuda: size the admission reserve with TENSORFOLD_CUDA_RESERVE_GB Let users set the CUDA memory reserve with TENSORFOLD_MEMORY_RESERVE_GIB Oct 2, 2026
@eleqtrizit

Copy link
Copy Markdown
Contributor Author

@ashhart Please review. This maintains the current default but lets users override with env var. I run a headless linux hosting box (as many due), and don't want to leave free context on the table.

@eleqtrizit eleqtrizit changed the title Let users set the CUDA memory reserve with TENSORFOLD_MEMORY_RESERVE_GIB Let users size the CUDA memory budget with TENSORFOLD_MEMORY_RESERVE_GIB and TENSORFOLD_CUDA_MEMORY_LIMIT_GB Oct 2, 2026
@ashhart

ashhart commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Part of this is in TensorFold 0.6.3, with your commits under your name in the release history: https://github.com/ashhart/TensorFold/releases/tag/v0.6.3

In 0.6.3: TENSORFOLD_MEMORY_RESERVE_GIB moves the memory floor the CUDA startup keeps free, to 2 GiB at the least, and Nemotron's workspace and serial state count inside the budget. On our side the default floor stays on both unified-memory and discrete GPUs (a tenth of the pool, at least 4 GiB). We checked that on a discrete card: with the floor dropped, the grant was all of the card's free memory and the variable had no effect there, so we put the floor back and let your variable move it.

Not in 0.6.3: the two commits you pushed tonight, which make TENSORFOLD_CUDA_MEMORY_LIMIT_GB a hard override of the startup budget and claim it at startup. They arrived after the release was frozen. In 0.6.3 the limit is still a cap from above, as in 0.6.2. PROOFS.md and the uv lock file are not in either.

We are leaving this open and will review the override and the startup claim for the next release. Headless boxes that want the whole card are a real case.

Thank you, @eleqtrizit!

…up budget

The line names the allocator's budget in GiB, above or below free memory; no reserve is
subtracted, and the estimate takes the CUDA context, its kernels and streams (about 2 GiB)
out, floored so a tiny line still serves something. The memory gate and the planned and
lone-stream copy-peak caps count the same tensor budget, and adaptive tests cover the
arithmetic on the host, without GPU imports.
With the line set, a filler of the bytes the tensors have not taken is allocated and handed
back to the allocator without an empty_cache, so the request caches reuse the reserved room:
nvidia-smi shows the line's figure while the GPU idles.
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @eleqtrizit. The Python engine is frozen (#286), and this sizes only its CUDA memory budget, so it won't land there.

On the Zig engine it waits for Zig CUDA serving, which doesn't exist yet. Zig CUDA today is a Nemotron bring-up run from the command line, with no server and no memory gate to size. When serving comes, the split you made is the one to keep: a reserve held back, a hard limit that replaces the free-memory figure, and a limit line that prices in what lives outside the tensor allocator. That carries your credit.

@ashhart ashhart closed this Oct 6, 2026
@ashhart

ashhart commented Oct 14, 2026

Copy link
Copy Markdown
Owner

@eleqtrizit, a correction. When we closed this we wrote that it wouldn't land. It had already shipped: your three commits are in 0.6.3 under your name. And the native CUDA engine now sizes its memory budget with your TENSORFOLD_CUDA_MEMORY_LIMIT_GB, in zig/src/native/cuda_memory.zig, without having credited you.

As of f930fbb you are a co-author on main for it, and the credits page names #165 beside the native file. Sorry for the wrong close, and thank you for the budget split we still use.

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.

2 participants