Skip to content

glm: --load-limit-gib / TENSORFOLD_LOAD_LIMIT_GB raises the 70%-of-RAM load limit - #521

Closed
heitke wants to merge 1 commit into
ashhart:mainfrom
heitke:load-limit-override
Closed

heitke wants to merge 1 commit into
ashhart:mainfrom
heitke:load-limit-override

Conversation

@heitke

@heitke heitke commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The native GLM-5.3-Flash Metal engine refuses checkpoints heavier than 70% of RAM — a hardcoded gate with no way to raise it, unlike the Python 0.6.x engine's TENSORFOLD_MEMORY_LIMIT_GB. This adds the same escape hatch in the native engine's own idiom:

  • --load-limit-gib GIB — a serve flag that sets the memory admission budget in place of 70% of RAM. It requires a finite, strictly positive GiB value below 2³⁴; zero is refused (nothing would load), as are negative, NaN and out-of-range values. A value past physical RAM is clamped to it.
  • TENSORFOLD_LOAD_LIMIT_GB — the environment-variable fallback, honoured when the flag is absent and listed in the env table (capabilities --json reports it). An absent or empty variable is unset; a set one is always read, and an invalid one is a configuration error even when a valid flag override would win.

The limit is resolved once per engine load and kept on the engine (limit_bytes): the same value gates weight loading, stream admission (fit) and the prompt-cache budget (cacheBudget). No later calculation re-reads the environment, so a higher variable can never supersede a lower explicit cap after load.

The refusal message names the knob:

error: glm: 180.6 GB of weights and 6.3 GB of caches pass this Mac's 179.2 GB load limit (70% of RAM); load a layer subset, the expert-parallel pair, or pass --load-limit-gib (or TENSORFOLD_LOAD_LIMIT_GB) to raise it

Scope. The knob applies to the GLM-5.3-Flash Metal engine's admission budget only. An explicit --load-limit-gib on any other model_type is refused with a message saying so; the variable is read only by the GLM Metal loader, so other engines ignore it (their own memory settings — CUDA's TENSORFOLD_CUDA_MEMORY_LIMIT_GB / TENSORFOLD_MEMORY_RESERVE_GIB, Flash Next's --prompt-cache-gib policy — are unchanged).

About safety. Raising the budget does not make the load safe; it trades the headroom macOS and other processes would have for model room, and the load remains subject to actual memory pressure — the same trade TENSORFOLD_MEMORY_LIMIT_GB offers on the Python side. Clamping to physical RAM only bounds the budget by what exists; it does not guarantee the load fits comfortably. GiB inputs are converted to bytes (2³⁰ per GiB); log output reports GB (10⁹), as the existing lines do.

Receipt

  • Environment, with the TensorFold commit, MLX or PyTorch version, machine and GPU, checkpoint and revision:
    • TensorFold main at f8fe17d (1.0.2) + this branch; Zig 0.17.0 (the version pinned in .zig-version).
    • Mac Studio M3 Ultra, 256 GiB unified memory, macOS 26.x (Darwin 25.x).
    • Checkpoint: GLM-5.3-Flash MLX 4-bit MTP (model_type glm5_next), 45 of 45 layers + the MTP layer, 180.58 GB in buffers.
  • Tests run, with pass, fail and skip counts:
    • python3 tools/zig/lean_check.py: 783 files, 511 problems against the target branch's 782 files, 517 — zero added problems; six inherited findings fixed as a side effect of edits (per-file diff against main shows no file newly carrying findings).
    • zig fmt --check on every touched file: clean.
    • zig build test: 162/163 host tests pass, 1 skipped, the single failure being families.nemotron.kernels.test.the prebuilt metallib carries every packed kernel function, which reproduces on unmodified main on this machine (it validates the real Metal library; this machine lacks full Xcode, and the Zig compiler otherwise type-checks the whole graph — a full-Xcode build compiles that library for real).
    • zig build test-native: success (includes the new load_limit.zig tests, 9 cases: default across RAM sizes, integer/fractional conversion, flag-over-env precedence, lower-cap authority, clamping, zero/negative/NaN/inf refusal for both settings, empty-vs-set variable, unknown-RAM refusal, exact-fit boundary).
    • zig build test-cli: success (includes the --load-limit-gib parser tests: accepted values, null default, zero/negative/oversized/malformed refusals).
    • zig build native builds zig-out/native/bin/tensorfold-native; the server-side serve parser tests run inside zig build test (the cli.zig test binary under the test step), not only the checkpoint CLI target.
    • Live model checks (single process at a time, the installed checkpoint, no downloads): default configuration reproduced the original refusal at 179.2 GB; TENSORFOLD_LOAD_LIMIT_GB=235 loaded and served; --load-limit-gib 235 loaded and served — info: GLM-5.3-Flash loaded in 76.4 s (180.6 GB of weights), context 131072 tokens, 1 streams, then serving glm-5.3-flash at http://127.0.0.1:8421/v1; a chat completion round-tripped against the running server. Prompt-cache behaviour is identical between the two settings because both resolve to the same retained limit_bytes feeding cacheBudget(). Automatic parallelism admitted what fit under the retained budget; an explicit --parallel above it is refused by fit() as before.
    • Not run, and why: zig build test-golden — the frozen-corpus step requires the real Metal toolchain (full Xcode) to produce the kernels the goldens drive; the host portion of the corpus work that does not need the GPU ran inside zig build test. Exactness receipts (tools/bench_concurrent.py --alone --serial, token_sha against "draft": false) were not taken on this branch because the shim-built binary's Metal kernels are placeholders; these contracts are untouched by the change (the limit gates loading, not arithmetic), and the maintainer's release gate runs them on a full toolchain.
  • Not run, and why (prior observations, not a substitute): the earlier independent Linux review reported the native build passing, 61 server-suite passes, 75 Linux host passes and one direct-I/O failure that also reproduced from unchanged main.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Untouched by this change: the limit gates loading, not arithmetic.)
  • No precision traded for speed.
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not. (Ran Metal on one Apple-Silicon host; CUDA not run — the change does not touch the CUDA backend, which keeps its own settings.)
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The load_limit.zig tests exercise pure resolution helpers — no GPU, no model — and the refusal paths they cover were the pre-fix behaviour: an invalid value silently fell back to the default, and fit()/cacheBudget() re-read the environment instead of the resolved limit.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

@heitke
heitke marked this pull request as draft October 8, 2026 16:46
…M load limit

The native GLM-5.3-Flash engine refuses checkpoints heavier than 70% of RAM
with no way to raise it, where the Python engine honours
TENSORFOLD_MEMORY_LIMIT_GB. A new --load-limit-gib serve flag (and the
TENSORFOLD_LOAD_LIMIT_GB env fallback, now in the env table) sets the load
limit in GiB in its place, clamped to physical RAM; the refusal message names
the knob. fit() and cacheBudget() follow the same limit, so stream admission
and the prompt-cache budget stay consistent with what loaded.
@ashhart

ashhart commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Thank you, @heitke, and sorry for the wait while we decided the 70% question. The answer is yes, by explicit request only, with the risk in the message. Main already has GLM_LOAD_LIMIT_GB (60c4851) for loads up to 70% of the RAM's bytes, so to keep one name, the step past 70% extends that variable instead of adding --load-limit-gib and TENSORFOLD_LOAD_LIMIT_GB. Your design comes with it: the limit resolved once per load and read by the load check, stream admission and the cache budget, and a bad value refused instead of ignored. The commit has you as co-author and the release notes thank you. I'll close this when it lands.

@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @heitke, and sorry for the wait while we decided the 70% question. The answer is yes, on explicit request only, with the risk in the message. Main already had GLM_LOAD_LIMIT_GB (60c4851) for loads up to 70% of RAM, so to keep one name, 601417f extends that variable instead of adding --load-limit-gib and TENSORFOLD_LOAD_LIMIT_GB.

Your design came with it: the limit is resolved once per load and read by the load check, stream admission and the cache budget, a value that doesn't parse is refused instead of ignored, and an unknown RAM size refuses every load. Past 70% the limit is capped at the GPU's recommended working set rather than at RAM, and the server logs the risk at startup.

The commit has you as co-author, and the release notes thank you. I'm closing this in favour of 601417f. Thank you for the tests and the receipt.

@ashhart ashhart closed this Oct 10, 2026
mrpmorris pushed a commit to mrpmorris/ashhart__TensorFold that referenced this pull request Oct 10, 2026
@heitke

heitke commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for adding the feature and sorry for the duplicate, PR! I'll try to check better in the future to see if others are working on the same thing. Odd that you were getting panics at a fairly low usage level. It seems like down the road there is probably a better way to have the system set an intelligent memory cap without a overly cautious hard-coded limit and without the user having to "guess" with a variable. Either way, this unblocks a lot of things for the 256gb machines, so thanks again for the fix!

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