Repository navigation
mem_admit: an empty or mistyped GGML_HRX_GTT_RESERVE_MB no longer removes the GTT reserve (#2986 follow-up) - #3012
Conversation
…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]>
|
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; The fix is right and the arms are non-vacuous: on the old code 1.
2. Whitespace: decide it, don't inherit it. 3. Landing order, because this is one rule in two repos. Engine Also worth one line in the commit: |
|
VERDICT at 69dfd56: READY. This is the gap I raised on The parsechar* 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 The fourth condition is the one I would not have written: The measurement is worth quoting, because
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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_bytesparses 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:
#455merges before this one (or with it, as#451/#2995did earlier today), and the comment stands; or- the sentence becomes "the fork parses it the same way once
llama.cpp#455lands" — 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
left a comment
There was a problem hiding this comment.
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 samemain()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.
|
VERDICT at
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 ( 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 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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 << 43replaces theUINT64_MAX >> 20bound, 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
left a comment
There was a problem hiding this comment.
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) —0is "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.cppneeds the app target and my lane holds no configure, so this is the parse and the arithmetic, not the compiled row.
|
VERDICT at Before/after, same harness,
|
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".
0still means zero. That preserves the legitimate explicit decision — an operator who types0asked 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 << 43MiB × 2^20 = 2^63 bytes, insideuint64_twith the multiply safe; thestatic_castonisspaceis the UB-avoiding form. - Whitespace on both ends works (
strtollskips leading, the loop skips trailing), and512\nfrom 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.
|
VERDICT: READY "On main, with an 8 GiB pool: Three measured wrong answers from one knob, and the third is the interesting one: Four asks:
On the shape: |
|
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: What is left is the reader. Zero warning lines in the change, so: 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:
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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:
0stays an override. An operator asking for no reserve gets no reserve; that is intent, not a typo. The arm pins it.-1is rejected rather than read as a hugesize_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 << 43keepsmb * kMiBinside 64 bits by construction (2^43 MiB = 2^63 bytes), so no overflow on the way touint64_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.
|
Head moved to |
|
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 ( 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 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 Yours to take or leave; nothing else from #3007 is worth carrying, and the review of your |
|
Failed to review PR |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Refs #2986.
The same defect and fix as the fork's
cl-2986/reserve-env-strict, inapp/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