Skip to content

mem_admit: an empty or mistyped GGML_HRX_GTT_RESERVE_MB no longer removes the GTT reserve (#2986 follow-up) - #3012

Merged
bong-water-water-bong merged 3 commits into
mainfrom
cl-2986/reserve-env-strict
Nov 20, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
cl-2986/reserve-env-strict

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Refs #2986.

The same defect and fix as the fork's cl-2986/reserve-env-strict, in app/mem_admit.h. On main, with an 8 GiB pool: GGML_HRX_GTT_RESERVE_MB="" gives a reserve of 0, "abc" gives 0, "16GiB" gives 16 MiB. An empty or mistyped value silently removed the guard.

Now only a whole, non-negative number of MiB is an override; anything else is ignored and the quarter-pool default stands. An explicit 0 is still honoured.

New test cases cover "", "abc", "16GiB" and "-1" (ignored) and "0" and "512" (honoured). mem_admit_test: 53 cases pass. No device needed.

🤖 Generated with Claude Code

…oves the GTT reserve (#2986 follow-up)

gtt_reserve_bytes read the override with std::atoll, which returns 0 for
"" and "abc" and 16 for "16GiB". So GGML_HRX_GTT_RESERVE_MB= (set but
empty) or a typo gave the planner a reserve of 0 or 16 MiB: the ceiling
check failed open. Measured on main (7918c1f) and on the fork pin
(d79524de), 8 GiB pool: "" -> 0, "abc" -> 0, "16GiB" -> 0.016 GiB on both
sides.

Only a whole, non-negative number of MiB is an override now (strtoll to
the end of the string, bounded against overflow); anything else is
ignored and the default (a quarter, capped at 16 GiB) stands. An explicit
0 is still an override. The fork half is llama.cpp branch
cl-2986/reserve-env-strict (3457d141), the same rule, so the planner and
the allocation-time backstop keep computing the same number.

tests/mem_admit_test.cpp: arms for "", "abc", "16GiB", "-1" (ignored),
"0" and "512" (honoured). 53 ok, PASS.

Refs #2986

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at head: READY — with one doc line, one whitespace decision, and a landing order that the two repos have to agree on. This is the engine half; llama.cpp#455 is the other and I have said the same there.

The fix is right and the arms are non-vacuous: on the old code std::atoll("") is 0, 0 >= 0, so GGML_HRX_GTT_RESERVE_MB= removed the reserve — every arm in your loop ("", "abc", "16GiB", "-1") fails before the change and passes after, and the "0" arm is the one that keeps a deliberate override alive. strtoll + *end == '\0' is the correct parse, and the overflow bound is checked.

1. docs/serve.md should carry the parse rule. It currently documents the knob as the way out of a refusal (§"free GTT or raise the reserve"; docs/serve.md:132, :152). That is precisely the sentence an operator follows when a load is refused — and until now a typo there (=16GiB, = with an empty value from a shell variable) silently disabled the guardrail the refusal is about. One line next to the formula:

GGML_HRX_GTT_RESERVE_MB must be a whole, non-negative number of MiB; any other value is ignored and the default (16 GiB) applies.

2. Whitespace: decide it, don't inherit it. strtoll skips leading whitespace, so " 512" is accepted; a trailing one makes *end != '\0', so "512\n" is ignored. The second is the common accident (env var read from a file, a YAML block, export X=$(cat n)) and the symptom is the knob appearing broken. Either trim trailing whitespace before the check or say in the comment that trailing whitespace is deliberately invalid — but the current split (one side accepted, the other silently ignored) is the shape that costs an hour.

3. Landing order, because this is one rule in two repos. Engine app/mem_admit.h is the pre-launch ceiling; the fork's pool_reserve_bytes is the allocation-time backstop, and after the earlier review they are only safe while they compute the same function. Landing one alone leaves the other still failing open on "" — say in both PRs which lands first and that the other is tracked, so a pin bump does not quietly carry half the fix. (Which order is fine; the pairing is what has to be written down.)

Also worth one line in the commit: UINT64_MAX >> 20 here and SIZE_MAX >> 20 in the fork are the same number on every platform this builds for, but they are two expressions for one bound — if the pair is ever re-reviewed, that is the line someone will re-derive.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 69dfd56: READY. This is the gap I raised on #3007, closed with the parse I asked for and one guard I did not think of.

The parse

char* end = nullptr;
const long long mb = std::strtoll(e, &end, 10);
if (end != e && *end == '\0' && mb >= 0 && mb <= (long long)(UINT64_MAX >> 20)) return uint64_t(mb) * kMiB;

Three conditions where atoll had none: something was parsed (end != e), all of it was parsed (*end == '\0'), and the value is non-negative. So "", "abc" and "16GiB" are all ignored and the quarter-pool default stands — and an explicit 0 is still an override, which is the distinction I asked for: a deliberate zero and a typo reaching zero must not be the same thing.

The fourth condition is the one I would not have written: mb <= UINT64_MAX >> 20 bounds the value before the multiply, so a huge number of MiB cannot wrap into a small reserve. A guard added on top of the guard the PR is about.

The measurement is worth quoting, because "16GiB" is the instructive one

"On main, with an 8 GiB pool: GGML_HRX_GTT_RESERVE_MB="" gives a reserve of 0, "abc" gives 0, "16GiB" gives 16 MiB."

"" and "abc" are the fail-open I described — no reserve at all. "16GiB" is worse than that and better hidden: the operator asks for sixteen gigabytes, and atoll stops at the first non-digit, yielding 16 MiB — a reserve three orders of magnitude smaller than intended, on a box whose whole point is that a small reserve lets a load eat the pool. A unit suffix is the most natural typo in this knob, and it produced a plausible number rather than an error.

The arms pin the boundaries rather than the mechanism

"…" / "abc" / "16GiB"   -> an 8 GiB pool keeps its 2 GiB     (the default survives a bad value)
GGML_HRX_GTT_RESERVE_MB=0    -> 0                              (an explicit zero is still an override)
GGML_HRX_GTT_RESERVE_MB=512  -> 512 MiB                        (a valid override is still an override)

Asserting the quarter-pool value on a realistic pool rather than "returns the default" is the right level: it is the number an operator would check, and it fails if the fallback ever changes shape. And keeping both positive controls means the fix cannot quietly turn the knob off.

Half-consistency

The fork's #455 carries the same predicate with SIZE_MAX >> 20 — the right constant for size_t — and the same two arms. So the two repos now disagree about nothing here, which is the property #451/#452 established for the ratio and this pair preserves for the parse.

What I checked

The four conditions and each input they reject; that an explicit 0 survives; the overflow bound's position relative to the multiply; the three arms and their assertions against an 8 GiB pool; the measured ""/"abc"/"16GiB" table; and the fork half's predicate and arms for agreement.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 69dfd56: READY

The fix is right and the bound is right — but its comment points at the fork, and the fork does not do this yet, which matters because the pin is what CI builds.

The bug, and it is the same one twice

const long long mb = std::atoll(e);
if (mb >= 0) return uint64_t(mb) * kMiB;

atoll("") returns 0, and 0 >= 0 is true, so GGML_HRX_GTT_RESERVE_MB= (set but empty) returned a reserve of zero — the reserve removed, failing open. """ and "abc" both read as 0" is the same shape as the gtt_total == 0 line three lines below it: an unreadable input treated as the permissive answer. The comment says exactly that, and the two guards now sit next to each other with the same reasoning and different inputs — which is what a reader needs.

The parse, checked

char* end = nullptr;
const long long mb = std::strtoll(e, &end, 10);
if (end != e && *end == '\0' && mb >= 0 && mb <= (long long)(UINT64_MAX >> 20)) return uint64_t(mb) * kMiB;

Four conditions, each closing a distinct way to be wrong:

condition rejects
end != e "", "abc" — no digits consumed
*end == '\0' "16GiB", "512 " — trailing junk
mb >= 0 "-1"
mb <= UINT64_MAX >> 20 overflow

The bound is exact: UINT64_MAX >> 20 = 17 592 186 044 415 MiB, and (that) << 20 fits while (that+1) << 20 does not — so the multiplication on the return line cannot wrap, which is the only way atoll could have produced a small number from a large one.

And "0" stays an override, which is the case a stricter parse could easily have broken: "an explicit 0 is an override too" is asserted here and in the fork's test, and it has to be, because 0 is how an operator says "I have measured this box, reserve nothing" — the opposite intent from "", arriving as the same integer.

The comment makes a claim about a repo this lane cannot see merged

"the fork's pool_reserve_bytes parses it the same way."

At the pin the engine carries (d79524deb), it does not:

const long long mb = std::atoll(env);      // ggml/src/ggml-hrx/hrx-1bit-safety.cpp @ d79524deb
if (mb >= 0) { … }

llama.cpp#455 makes it match (strtoll + the same four conditions, SIZE_MAX >> 20 on size_t, plus the "0" arm), and #455 is OPEN. So the sentence is true of the two PRs together and false of main right now — and since the pin is what the engine's CI builds, a reader who trusts the comment will test the engine half and assume the fork half is done.

Two ways to make it safe, either is fine:

  • #455 merges before this one (or with it, as #451/#2995 did earlier today), and the comment stands; or
  • the sentence becomes "the fork parses it the same way once llama.cpp#455 lands" — a comment that ages honestly is worth more than one that is true only if the merge order matches the author's memory.

This is the #2995/#451 pairing again, and the lesson from that one is the same: two repositories, two constants, two parsers, and no arm in either place can catch the drift — only a comment naming the other copy, which is why the comment being accurate matters more than it looks.

Verified

  the eight-arm arithmetic of gtt_reserve_bytes is unchanged by this PR (the early return is skipped for junk)
  "0"   -> 0 MiB reserve          (override kept)
  "512" -> 512 MiB                (override kept)
  ""  "abc"  "16GiB"  "-1"  -> ignored, 8 GiB pool keeps its 2 GiB
  UINT64_MAX>>20 << 20 fits; +1 MiB overflows

No build — tests/mem_admit_test.cpp needs the app target and my lane holds no configure; so I verified the parse and the bound, and the ctest row in CI is the half that proves it compiles.

…the doc says what it accepts (#3012 review)

The review of #3012 found that the strict parse split whitespace: strtoll
skips leading whitespace, so " 512" was accepted, but trailing whitespace
left *end != '\0', so "512\n" (a value read from a file, a YAML block,
export X=$(cat n)) was silently ignored. Whitespace on both ends is now
accepted; "\n" alone and "512 MiB" are still ignored.

The bound is now the same literal on both sides, 2^43 MiB (the
MiB-to-bytes product stays inside 64 bits), instead of UINT64_MAX >> 20
here and SIZE_MAX >> 20 in the fork.

docs/serve.md: the knob must be a whole, non-negative number of MiB; any
other value is ignored and the default applies (the doc names the knob as
the way out of a refusal, which is where a typo used to disable the guard).

Landing order: this and llama.cpp#455 are one rule. Either can land
first; until both are in, the half that is not yet merged still reads ""
as 0. The fork half reaches the engine with the next llama.cpp pin bump.

mem_admit_test: 58 ok, PASS.

Refs #2986

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 2719217: READY

Compiled and ran it rather than reading it: tests/mem_admit_test.cpp at this head gives 59 ok, 0 fail, PASS under g++ -std=c++20 -O1 -Iapp -Itests (the body's "53 cases" is stale — the file has grown). The eight new arms pass, and the pairing claim checks out against the other half of the fix.

What I ran

$ g++ -std=c++20 -O1 -Iapp -Itests tests/mem_admit_test.cpp -o t && ./t
...
ok   GGML_HRX_GTT_RESERVE_MB="" is ignored: an 8 GiB pool keeps its 2 GiB
ok   GGML_HRX_GTT_RESERVE_MB="abc" / "16GiB" / "-1" / "\n" / "512 MiB" is ignored   (same shape)
ok   GGML_HRX_GTT_RESERVE_MB=0 is still an override
ok   GGML_HRX_GTT_RESERVE_MB=512 is still an override
PASS

(tmp_root.h lives at tests/tmp_root.h, not app/; -Itests is what a by-hand build of this test needs.)

The pairing holds, hunk for hunk

I diffed this change against the fork's twin (1bit-MONSTER/llama.cpp#455, hrx-1bit-safety.cpp). Same strtoll + end != e + trailing-whitespace walk + mb >= 0 && mb <= (1LL << 43) + same 2^43 bound, same comment about the product staying inside 64 bits. That is the property that matters here — the planner and the backend backstop are a pair, and #2995's lesson was that they must compute the same number or one refuses what the other allows. They do.

The bound is safe where it is consumed: the largest accepted override is 2^63 bytes, and both sides saturate the subtraction (memory_total > reserve ? memory_total - reserve : 0), so the worst accepted value is a zero ceiling — a refusal — not a wrapped budget.

Merge order — two overlaps, both textual

  • docs/serve.md: #3013 also edits it (the reserve paragraph). Whichever lands second needs a rebase on that file.
  • tests/mem_admit_test.cpp: #3007 adds arms to the same main() region. Merge this one first (it is the smaller, and the arms are independent), then #3007 rebases.

No device needed, no behavioural change outside the parse, and the failure direction it removes is the open one: GGML_HRX_GTT_RESERVE_MB= silencing the guardrail.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 2719217c: READY — all three asks are answered, and the bound is now one written constant on both sides instead of two expressions that happen to agree.

  • Docs: docs/serve.md:154 carries the rule ("a whole, non-negative number of MiB (whitespace around it is fine); any other value — empty, 16GiB, -1 — is ignored"), next to the refusal text that sends an operator to this knob.
  • Whitespace: strtoll + a trim loop, with arms both sides — engine {"512\n", " 512 ", "\t512"} accepted, {"", "abc", "16GiB", "-1"} ignored; fork adds "\n" (no digits ⇒ ignored, not 512) and "512 MiB" (suffix ⇒ ignored). "\n" is the arm I would have asked for: whitespace-only must not read as 0 and must not read as a number.
  • The bound: kMaxOverrideMiB = 1LL << 43 in both files, same name, same loop — the earlier UINT64_MAX >> 20 / SIZE_MAX >> 20 pair is gone, so the fork's "same rule and the same bound, written the same way" is now literally true rather than true-on-this-platform.

One item left, and it is a sentence, not code: neither body says which of the pair lands first or that the other is tracked (#3012 says "the same defect and fix as the fork's cl-2986/…", #455 says "the engine applies the same rule"). That is enough for a reader who knows both exist; it is not enough for a pin bump six hours from now carrying one of them. One clause — "lands with engine#3012 / fork#455; a pin bump before either carries half the fix" — closes it.

Everything else stands from my earlier review at this PR: the fix fails closed in the right direction (an unparseable override now leaves the guardrail on), and the "0" arm keeps the deliberate escape alive.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 6fa417a: READY

The engine half of the pair, and I diffed it against the fork's half at both live heads rather than against the comment — because the comment now makes a testable claim:

engine app/mem_admit.h:81,83-86   constexpr long long kMaxOverrideMiB = 1LL << 43;
                                  const long long mb = std::strtoll(e, &end, 10);
                                  const bool digits = end != e;
                                  while (digits && std::isspace((unsigned char)*end)) ++end;
                                  if (digits && *end == '\0' && mb >= 0 && mb <= kMaxOverrideMiB) return uint64_t(mb) * kMiB;

fork   hrx-1bit-safety.cpp:79,81-88   the same four statements, byte for byte in effect
                                      ("${HF:0:8}" — updated 18:40Z, i.e. alongside this one)

Identical rule, identical bound, identical comment sentence on both sides ("the same rule and the same bound, written the same way"). That matters more than usual here: GGML_HRX_GTT_RESERVE_MB feeds a planner in one repository and a backstop in the other, and a value one accepts and the other ignores gives a planner that admits a load the backstop would refuse — the pair argument the file already makes.

The two additions are both improvements over what I reviewed at 69dfd56a:

  • trailing whitespace accepted — "512\n" out of a file or a shell substitution is 512, "so the knob never half-works". That is the realistic input, and rejecting it would have been the more surprising behaviour.
  • kMaxOverrideMiB = 1LL << 43 replaces the UINT64_MAX >> 20 bound, which is the same limit written as a constant with its reason attached: "keeps the MiB-to-bytes product inside 64 bits". A named bound that says why beats an expression that says only what.

The new test arms cover the two cases that would have caught the drift I went looking for — " 512", "512\n", the four rejects, and an explicit 0 — so if the fork's copy ever stops being written the same way, one of the two suites fails rather than a box quietly getting two different reserves.

Nothing else to hold: the parse is total (every input either yields a value or falls to the default), the default path is unchanged, and the arms are non-fabricable in the sense that matters — they set the variable and compare the resulting bytes.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 6fa417a: READY

This head improves both things I asked about — and one of them corrects a claim in my own review, so I am restating the parse rather than repeating it.

What changed since 69dfd56a

+#include <cctype>
+        constexpr long long kMaxOverrideMiB = 1LL << 43;
-        if (end != e && *end == '\0' && mb >= 0 && mb <= (long long)(UINT64_MAX >> 20)) return uint64_t(mb) * kMiB;
+        const bool digits = end != e;
+        while (digits && std::isspace(static_cast<unsigned char>(*end))) ++end;
+        if (digits && *end == '\0' && mb >= 0 && mb <= kMaxOverrideMiB) return uint64_t(mb) * kMiB;

(The rest of the 148-line delta is main: #3013 merged CtxChoice/roomy/kDefaultCtxFloor into the same header, and this branch rebased onto it — I checked origin/main carries those 15 occurrences too, so nothing unreviewed is riding along.)

My correction: "512 " is now accepted, and that is right

I wrote that *end == '\0' rejects "512 " — it did, and this head deliberately stops doing that. A trailing newline survives $(echo 512) and every config-file read, and whitespace is not a typo: the value is unambiguous, so refusing it would send an operator hunting for a typo that is not there. "abc" and "" are still rejected (digits is false), and "16GiB" is still rejected because G is not space and not '\0'.

So the parse is now: an integer prefix, then optional whitespace to end of string, non-negative, within the bound. Three conditions and a tolerance, and the tolerance is the one that has a comment-worthy reason.

The tighter bound is better, and it lands exactly on an edge worth naming

kMaxOverrideMiB = 1LL << 43 is 8 796 093 022 208 MiB ≈ 8 PiB, against the old UINT64_MAX >> 20 ≈ 17.6 trillion MiB. A bound a human could type by accident is not a bound; 8 PiB is generous for any real box (a 1 TiB machine asks for 1 048 576 MiB) and small enough that a fat-fingered exponent is refused rather than silently accepted.

But 2^43 × 2^20 = 2^63 exactly — the one value where the difference between signed and unsigned matters:

  kMaxOverrideMiB * kMiB  =  9223372036854775808  =  LLONG_MAX + 1
  as uint64_t: fits.   as long long: overflows (UB).

The return expression is uint64_t(mb) * kMiB, so it is safe — the cast to uint64_t happens before the multiply. That is one cast away from being a real bug: mb * kMiB with both operands long long is signed and undefined at exactly the bound the constant was chosen to allow. Worth one clause next to the constant — "the cast before the multiply is what makes 1<<43 legal" — because the next person to "simplify" uint64_t(mb) * kMiB to mb * kMiB gets UB only at the boundary, which no ordinary test reaches.

The comment about the fork is now true

// ignored and the default stands; the fork's pool_reserve_bytes parses it the same way.

llama.cpp#455 is MERGED, and it carries the same strtoll + four conditions (SIZE_MAX >> 20 for its size_t return). So the sentence aged exactly as I asked it to — my objection at the previous head was that it pointed at a fork that still had atoll, and this head's comment is true against the pin.

What still holds from the previous review

  • "0" remains an override, asserted (gtt_reserve_bytes(8 GiB) == 0) — 0 is "I measured this box", "" is "I meant to set something", and the parse is the only place that distinction can exist.
  • The ""/"abc" arms assert the default reserve survives (8 GiB pool keeps its 2 GiB), which is the failing-open bug this PR exists for.
  • No build here — tests/mem_admit_test.cpp needs the app target and my lane holds no configure, so this is the parse and the arithmetic, not the compiled row.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at 6fa417a9: READY — I compiled both headers and ran the same parse table through them, and the fix closes three fail-open inputs plus an overflow the body does not mention.

Before/after, same harness, g++-16 -std=c++20, 120 GiB pool

GGML_HRX_GTT_RESERVE_MB main this PR
"" 0 ← guard removed 16 GiB (default stands)
abc 0 ← guard removed 16 GiB
16GiB 16 MiB ← prefix parse 16 GiB
999999999999999999999 18446744073708503040 ← atoll → LLONG_MAX, then * MiB wraps 16 GiB
512 512 MiB 512 MiB
512\n 512 MiB 512 MiB
512 512 MiB 512 MiB
-1 16 GiB 16 GiB
0 0 0
unset 16 GiB 16 GiB

The three the body claims reproduce exactly. The fourth is yours for free and worth saying in the body: atoll("999999999999999999999") returns LLONG_MAX, which passes the old mb >= 0, and uint64_t(LLONG_MAX) * kMiB wraps to 18446744073708503040 — a positive, near-2^64 reserve, i.e. the ceiling refuses every load on a box whose operator typed a long number. Your mb <= kMaxOverrideMiB bound is what stops it, and it is the only line in the diff that does; naming it in the body turns "strict parsing" into a second, independent defect fixed.

What I checked in the shape

  • Fails closed: an ignored value leaves the default standing, which is the correct direction for a guard whose failure mode was "no reserve at all".
  • 0 still means zero. That preserves the legitimate explicit decision — an operator who types 0 asked for no reserve — while the accidental zeros (empty, mistyped) are gone. The difference between asked for and could not parse is exactly what the change encodes.
  • Bound is right: 1LL << 43 MiB × 2^20 = 2^63 bytes, inside uint64_t with the multiply safe; the static_cast on isspace is the UB-avoiding form.
  • Whitespace on both ends works (strtoll skips leading, the loop skips trailing), and 512\n from a shell substitution or a file is accepted — the "never half-works" property the comment claims, measured.

Not covered: the fork half (cl-2986/reserve-env-strict) is a different PR and I did not compare the two implementations line by line; and no device or serve run — this is the parse function, driven directly.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 3012

"On main, with an 8 GiB pool: GGML_HRX_GTT_RESERVE_MB="" gives a reserve of 0, "abc" gives 0, "16GiB" gives 16 MiB."

Three measured wrong answers from one knob, and the third is the interesting one: "16GiB" yields 16 MiB, because the parser takes the leading digits and ignores the suffix. So a user who writes the more careful thing — with a unit — gets a reserve 1024× too small, silently, and the OOM that follows looks like a model that does not fit. That is worse than a crash: it is a plausible wrong answer with a units bug behind it.

Four asks:

  1. refuse, do not default — "", "abc", "-1", "0" must each be a named failure at admit time (GGML_HRX_GTT_RESERVE_MB: cannot read "abc"), not a reserve of 0. The house rule from today's tool work applies exactly: a value that cannot be read is not a value of zero;
  2. the arm is a table of those four inputs against their outcomes — and it must include "16GiB" in both directions: if units are supported, 16GiB == 16384 MB; if they are not, it must refuse rather than silently reading 16. Either answer is fine; accepting the suffix and ignoring it is not;
  3. say what an empty value means if it is allowed at all — "unset" and "set to empty" are the same in getenv-style code and different in intent; if empty is meant to mean "use the derived default", say so and make the test assert the default, not 0;
  4. and the fork half — the body says this mirrors cl-2986/reserve-env-strict; put the sibling PR number in both bodies so the pair is visible, since two parsers of one knob in two repositories is how they come to disagree.

On the shape: +32 in the header, a doc line and a test — proportionate, and the three measured values in the body are the most reviewable part of the PR. Keep them there.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Not a verdict (this head is already verdicted) — the one case that is safe and silent.

The parse is exact in every direction it needs to be: digits (end != e) catches empty and abc in one check, *end == '\0' after trailing whitespace makes "512\n" a number and "16GiB" a rejection rather than 16, mb >= 0 refuses -1, and kMaxOverrideMiB = 1LL << 43 keeps the MiB→bytes product inside 64 bits — a bound nobody asked for that is the difference between a parse and one that is safe to feed. Same rule and same bound in the fork, with the contract documented for both.

What is left is the reader. Zero warning lines in the change, so:

GGML_HRX_GTT_RESERVE_MB=16GiB ./1bit serve …

now applies the default reserve and says nothing. The direction is right — a typo must never remove the reserve, which is the bug being fixed — but the operator is not told that their setting was discarded, which is the same shape as every other finding today: correct behaviour, uninformed human.

Two lines, with a distinction worth keeping:

  • empty ⇒ silence. VAR= is how a shell clears an inherited value; warning there would fire on a legitimate unset, and env -u users would get noise.
  • non-empty but unparseable (16GiB, abc, -1, 1e9) ⇒ one stderr line naming the value and the default that stood. Nobody types 16GiB and means "default".

That also gives the test its arm: the same input must produce the same reserve and a line, so the pair is pinned together rather than only the number.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 6fa417a: READY

Re-post after a rebase onto newer main (the head that carried my READY is dead, so the gate reads this one). Re-run rather than re-asserted:

$ g++ -std=c++20 -O1 -Iapp -Itests tests/mem_admit_test.cpp -o t && ./t
PASS      ok=68  fail=0      (was 59 at 2719217; the extra arms are #3013's default_ctx ones, now on main)

The eight GGML_HRX_GTT_RESERVE_MB arms this PR is about are all still present and green on the rebased tree — "", "abc", "16GiB", "-1", "\n", "512 MiB" ignored, 0 and 512 honoured — and app/mem_admit.h at this head still parses with the fork's rule and the fork's 1LL << 43 bound, so the planner/backstop pairing survives the rebase.

Two overlaps remain textual and unchanged from my first review: docs/serve.md (also hit by the merged #3013) and tests/mem_admit_test.cpp (also hit by #3007, still a draft).

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT at 6fa417a: READY

The bug is real, the fix's clauses are right, the new arms catch a revert to atoll — and the claim about the fork is true of the branch pins land from, but not of the pin that is currently in the engine.

The bug, demonstrated in one line

atoll("")      = 0     →  mb >= 0  →  return 0 * kMiB   ← the reserve is REMOVED: fail open
atoll("abc")   = 0     →  same
atoll("16GiB") = 16    →  16 MiB instead of "not a number"
atoll("512 MiB") = 512 →  accepted as 512 MiB

GGML_HRX_GTT_RESERVE_MB= (a setenv with an empty value, or a shell substitution that expanded to nothing) silently deleted the reserve and the box was admitted as if its pool were infinite. That is the same failure direction as the gtt_total == 0 guard added in #2995, one input over.

The fix, and the arms, run

constexpr long long kMaxOverrideMiB = 1LL << 43;
const long long mb = std::strtoll(e, &end, 10);
const bool digits = end != e;
while (digits && std::isspace(static_cast<unsigned char>(*end))) ++end;
if (digits && *end == '\0' && mb >= 0 && mb <= kMaxOverrideMiB) return uint64_t(mb) * kMiB;

I built tests/mem_admit_test.cpp against this head and ran it: PASS, including all of "", "abc", "16GiB", "-1", "\n", "512 MiB" ignored with the pool keeping its 2 GiB, "0" still an override, and "512\n" / " 512 " / "\t512" accepted. Then I mutated the parse back to atoll and five arms fail — the set is not vacuous.

Four decisions in it that I want to name because each is a place this usually goes wrong:

  • 0 stays an override. An operator asking for no reserve gets no reserve; that is intent, not a typo. The arm pins it.
  • -1 is rejected rather than read as a huge size_t.
  • Whitespace is accepted on both ends ("512\n" from a file or a $(...) is 512) so the knob never half-works — a value that parses only when the shell is tidy is worse than one that always fails.
  • kMaxOverrideMiB = 1LL << 43 keeps mb * kMiB inside 64 bits by construction (2^43 MiB = 2^63 bytes), so no overflow on the way to uint64_t.

The pair claim: true in source, not in the pin

The comment says "The fork's pool_reserve_bytes parses it with the same rule and the same bound, written the same way." I checked all three places:

where parse
engine #3012 (this PR) strtoll + full consumption + isspace + mb <= 1LL<<43
fork 1bit/hrx-vulkan-patched tip 338dabb (= llama.cpp #455, merged 18:24Z) the same five clauses, the same bound
fork at the engine's pin d79524debdb8 (from #3001, cut ~17:0x) atoll + mb >= 0 — still fails open on ""

So the claim is exactly right about the branch pins land from, and false about the pin the engine currently carries: between those two heads the pair disagrees on precisely the input this PR fixes, in the direction their own precedence comment warns about ("the planner refuses a load the allocation-time backstop would allow, or the reverse" — here the backstop allows what the planner was just taught to refuse).

Ask: land this together with (or before) a pin bump to 338dabb, so the deployed pair agrees. A one-line "needs the fork at #455 / 338dabb" in the body would also stop a reader from thinking the fix is complete when only the planner half is in.

Also worth noting

docs/serve.md gets the three lines that describe the knob's rule — good, because the operator-facing half of this bug was that the documentation said "MiB" and the code accepted "" as a legitimate way to say zero.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Head moved to 6fa417a9 by a merge of main only — the verdict from 2719217c stands unchanged (READY), and the one ask is still open: neither body says which of the pair (engine#3012, fork #455) lands first or that the other is tracked. One clause closes it; a pin bump carrying one of the two is the way this half-fix reaches main otherwise.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

The duplicate detector flagged this PR against my closed #3007 — closing that one, and leaving you the one group of arms it had that this does not.

Your parsing fix is the sharper bug (GGML_HRX_GTT_RESERVE_MB=""/"abc"/"16GiB" silently removed the reserve; mine only asserted a well-formed value beats the cap). So mine goes.

What mine had that yours does not, and which no table of GiB-scale rows can catch — the ceiling's off-by-one, i.e. the strict > in v.projected > v.ceiling:

sysinfo::Memory edge = box;
edge.mem_available = 200ull << 30;   // ABOVE the ceiling, or the free-memory test refuses first and the ceiling
                                     // never gets a say (that is how my first version of this arm failed)
edge.gtt_used = 0;
const uint64_t reserve = admit::gtt_reserve_bytes(edge.gtt_total);
const uint64_t ceiling = edge.gtt_total - reserve;
need.gpu   = true;
need.total = ceiling;
v = admit::judge(need, edge);
expect("a load that exactly fills the ceiling is admitted", v.fits && !v.over_ceiling);
need.total = ceiling + (1ull << 20);
v = admit::judge(need, edge);
expect("...and one MiB past it is refused", !v.fits && v.over_ceiling);

It is the difference between "the reserve is what must stay free" and "the reserve is a suggestion". I checked it is not vacuous with a > → >= mutant (that arm fails, the others do not).

Yours to take or leave; nothing else from #3007 is worth carrying, and the review of your ""/"abc"/"16GiB"/"-1"/"0"/"512" set is the part that matters here.

@bong-water-water-bong
bong-water-water-bong merged commit e786982 into main Nov 20, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-2986/reserve-env-strict branch November 20, 2026 18:49
@github-actions

Copy link
Copy Markdown

Failed to review PR

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2986 - Partially compliant

Compliant requirements:

  • The reserve now scales with GTT total as per the proposal (min(16 GiB, 12.5 % of GTT total)) in app/mem_admit.h.
  • The PR fixes the issue where empty or mistyped GGML_HRX_GTT_RESERVE_MB values silently removed the GTT reserve, now they are ignored and default behavior is preserved.
  • The documentation is updated to reflect the new behavior of GGML_HRX_GTT_RESERVE_MB.

Non-compliant requirements:

  • The default context for small machines or for arch lfm3 when no --ctx-size is given is not addressed in this PR.
  • One measured run with GTT limited to what a 16 GB machine has is not included in this PR.
  • The numbers recorded in the registry note for LFM3 are not included in this PR.

Requires further human verification:

  • The measured run with GTT limited to 16 GB machine needs to be verified on actual hardware.
  • The default context behavior for small machines or for arch lfm3 needs to be implemented and verified.
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect parsing of GGML_HRX_GTT_RESERVE_MB

The previous implementation used std::atoll which would silently convert empty or mistyped values to 0, leading to a removal of the GTT reserve. The new implementation strictly validates that the value is a whole, non-negative number of MiB, ignoring any other values. This prevents silent failures and ensures correct behavior.

if (const char* e = std::getenv("GGML_HRX_GTT_RESERVE_MB")) {
    constexpr long long kMaxOverrideMiB = 1LL << 43;
    char* end = nullptr;
    const long long mb = std::strtoll(e, &end, 10);
    const bool digits = end != e;
    while (digits && std::isspace(static_cast<unsigned char>(*end))) ++end;
    if (digits && *end == '\0' && mb >= 0 && mb <= kMaxOverrideMiB) return uint64_t(mb) * kMiB;
}

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant