Repository navigation
hrx: an empty or mistyped GGML_HRX_GTT_RESERVE_MB no longer removes the GTT reserve (engine#2986 follow-up) - #455
Conversation
…he GTT reserve (engine#2986 follow-up) pool_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 a reserve of 0 or 16 MiB - the guardrail failed open. Measured on the merged code (fork d79524d, and the engine's app/mem_admit.h at 7918c1f, which parses it the same way), 8 GiB pool: "" -> 0, "abc" -> 0, "16GiB" -> 0.016 GiB, "512" -> 0.5 GiB. Only a whole, non-negative number of MiB is an override now (strtoll to the end of the string, bounded so the MiB-to-bytes product cannot overflow); anything else is ignored and the scaled default stands. An explicit 0 is still an override. The engine change is the same rule in app/mem_admit.h (engine branch cl-2986/reserve-env-strict), so the planner and this backstop keep computing the same number. tests/test-hrx-gtt-reserve: arms for "", "abc", "16GiB", "-1" (ignored, the 8 GiB pool keeps 2 GiB) and "0" (honoured); all 15 ok. The stale "12.5 %" in that test's comment now says a quarter. test-hrx-pool-refusal-budget: all requirements passed. 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 fork 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 3457d14: READY — and it is the other half of a pair that now agrees. char * end = nullptr;
const long long mb = std::strtoll(env, &end, 10);
if (end != env && *end == '\0' && mb >= 0 && mb <= (static_cast<long long>(SIZE_MAX >> 20))) { … }Same predicate as the engine's The arms match too — What I checkedThe four conditions ( |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 3457d14: READY
The fix is right — atoll reading "" as 0 turned an emptied variable into "no reserve at all", which is a guardrail failing open — and it is the pair half of the engine's ggml-org#3012, so the thing worth checking is that the two parses agree exactly. They do:
fork hrx-1bit-safety.cpp:79 if (end != env && *end == '\0' && mb >= 0 && mb <= (static_cast<long long>(SIZE_MAX >> 20)))
engine app/mem_admit.h:80 if (end != e && *end == '\0' && mb >= 0 && mb <= (long long)(UINT64_MAX >> 20))
Same rule, same rejection set, same overflow bound (SIZE_MAX >> 20 and UINT64_MAX >> 20 are the same number on this box), and each branch is justified by the pair argument rather than by taste: "iff they disagree, the planner refuses a load the backstop would allow, or the reverse." That is the sentence that makes two implementations acceptable where one shared helper would normally be better — they live in different repositories, and the tests pin the same four arms ("", "abc", "16GiB", "-1") on both sides, plus 0 staying an explicit override. The pair-difference is the only thing that could drift, and the arms are what would catch it.
Two notes:
" 16"is accepted (strtollskips leading whitespace). That is almost certainly fine and probably desirable for a shell variable, but it is the one input where the two implementations agree implicitly rather than by a test — one arm each (" 16"→ 16 MiB) would make the agreement explicit.+16is accepted too, same reason. One comment covering both ("strtoll's own grammar: leading space and a sign are part of a number") saves the next reader from wondering whether they were considered.
The default it falls back to is the number the engine's planner landed on, with the cap explained from the measurement rather than asserted — "a QUARTER of the pool, capped at the measured 16 GiB … so this box's 120 GiB keeps the ceiling ggml-org#680 measured (104 GiB) bit for bit" — and the unknown-total branch keeps the flat value on the grounds that a caller who cannot measure GTT must not get a smaller reserve than the one measured safe. All three of those are the conservative direction.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 3457d14: READY
The fork half of engine#3012 — and I checked it against that PR's comment, which points here, because the sentence is only true once this lands.
The parse
- const long long mb = std::atoll(env);
- if (mb >= 0) {
+ char * end = nullptr;
+ const long long mb = std::strtoll(env, &end, 10);
+ if (end != env && *end == '\0' && mb >= 0 && mb <= (static_cast<long long>(SIZE_MAX >> 20))) {Four conditions, four distinct failures closed:
| condition | rejects | why it matters here |
|---|---|---|
end != env |
"", "abc" |
atoll("") is 0, and 0 here means "reserve nothing" — an unset-looking variable removed the reserve entirely |
*end == '\0' |
"16GiB", "512 " |
a value a human would write and a parser would half-read |
mb >= 0 |
"-1" |
atoll returns -1, which the old >= 0 also caught — kept for symmetry |
mb <= SIZE_MAX >> 20 |
overflow | the multiplication on the return line is the only place a small mb could become a large size_t |
SIZE_MAX >> 20 rather than UINT64_MAX >> 20 is the right choice for a size_t return, and it is the kind of detail that only shows up on a 32-bit build — where >> 20 of SIZE_MAX is the real bound and UINT64_MAX >> 20 would let through a value that wraps. The engine copy returns uint64_t and bounds against UINT64_MAX >> 20; the two are correct for their own types, which is worth a line in each so a reader does not "fix" the difference.
"0" is kept as an override, and that is the arm that could have been lost
setenv("GGML_HRX_GTT_RESERVE_MB", "0", 1);
check(pool_reserve_bytes(8 * kGiB) == 0, "an explicit 0 is an override too");A stricter parse that rejected non-positive values would have been a natural way to write this fix — and it would have broken the one case an operator uses deliberately: 0 is "I have measured this box, reserve nothing", while "" is "I meant to set something and did not". They arrive as the same integer from atoll and mean opposite things; the parse is the only place that distinction can be made, and the arm pins it.
Why the pairing mattered today
engine#3012's comment says "the fork's pool_reserve_bytes parses it the same way" — and at the pin the engine currently carries (d79524deb), it does not: that file still has atoll + mb >= 0:
const long long mb = std::atoll(env); // hrx-1bit-safety.cpp @ d79524deb
if (mb >= 0) { … }So this PR is what makes that sentence true. It is the same shape as #2995/#451 earlier: two repositories, two implementations of one policy, and no arm in either place can catch the drift — only a comment naming the other copy. Merging order matters for the comment's honesty, not for correctness: either order leaves a window where one half parses strictly and the other does not, and in that window the behaviour depends on which binary runs.
The arms are the right four
an explicit 0 is an override too
"" "abc" "16GiB" "-1" -> the search/ignore path, default reserve stands
(and the existing eight arms for the scaling itself, unchanged by this diff)
The #451 set already covered 0 and the scaling; this adds the four malformed inputs — which is exactly the delta, no more.
No device, no build here: tests/test-hrx-gtt-reserve.cpp is host-only and my lane holds no configure for it, so I verified the parse, the bound and the arms' arithmetic, not the compiled row.
…cpp#455 review) The review of #455 and engine#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 one literal written the same way on both sides, 2^43 MiB (the MiB-to-bytes product stays inside 64 bits); it was SIZE_MAX >> 20 here and UINT64_MAX >> 20 in the engine. The engine side (engine#3012) documents the accepted form in docs/serve.md. Landing order: this and engine#3012 are one rule. Either can land first; until both are in, the half not yet merged still reads "" as 0. This half reaches the engine with the next llama.cpp pin bump. test-hrx-gtt-reserve: 20 ok, all cases ok. Refs engine#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 41e0322: READY
I extracted pool_reserve_bytes from this head verbatim into a harness with the PR's own test file and ran it: 20 checks, 20 ok, 0 fail (the body's "15/15" is stale — the file now asserts 20). Then I probed the inputs the arms do not cover.
The arms
$ g++ -std=c++17 -I. test-hrx-gtt-reserve.cpp extracted.cpp -o t455 && ./t455
... an explicit 0 is an override too
... whitespace around the number is accepted on both ends (x3)
... GGML_HRX_GTT_RESERVE_MB="" is ignored: an 8 GiB pool keeps its 2 GiB
... "abc" / "16GiB" / "-1" / "\n" / "512 MiB" is ignored (same shape)
all cases ok
Edges I drove by hand, because a numeric guard is where the hole usually is
With an 8 GiB pool (default reserve 2 GiB):
"99999999999999999999" -> 2147483648 (2 GiB: ignored; strtoll saturates at LLONG_MAX and the bound rejects it)
"8796093022208" -> 9223372036854775808 (2^43 MiB, accepted — the bound itself)
"8796093022209" -> 2147483648 (rejected, the bound works)
"+512" -> 536870912 (accepted: strtoll takes a leading plus)
"0x10" -> 2147483648 (ignored: base 10, so the trailing x rejects it)
"--1" -> 2147483648 (ignored)
" " -> 2147483648 (ignored)
The 2^43 acceptance is safe because the subtraction that uses it saturates: const size_t ceiling = memory_total > reserve ? memory_total - reserve : 0; (hrx-1bit-safety.cpp:152). So the largest accepted override gives a ceiling of zero — a refusal — not a wrapped enormous budget. That is the direction this PR is about, and it holds at the boundary. The product also stays inside 64 bits: 2^43 × 2^20 = 2^63.
std::isspace needs <cctype> and std::strtoll needs <cstdlib>; the file includes both, so the new parse compiles on its own headers rather than by accident.
Two notes, neither blocking
-0is accepted and sets the reserve to zero, while-1is ignored. That is consistent with the deliberate "an explicit 0 still works", so it is not a defect — only worth knowing that the guardrail can be removed by a value a human would read as negative.- The sibling knob in the same file,
GGML_HRX_POOL_LIMIT_MB, still parses withstd::atollinpool_budget_terms/pool_budget_is_fixed_limit. Its failure direction is the closed one (a mistyped value becomes a small budget, which refuses), so it is not this defect — but after this PR the file reads two knobs by two rules, which is the kind of asymmetry the next reader will assume away.
The pairing claim holds: I diffed this hunk against engine#3012's app/mem_admit.h change and the rule, the bound and the whitespace handling are the same code, so the planner and the backstop still compute the same reserve.
|
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: READY -- strict parse, range check, and the arms cover both directions. One thing a typo still hides
The defect is a good catch: std::atoll("") is 0, so an empty GGML_HRX_GTT_RESERVE_MB set the reserve to zero and removed the guardrail while looking like configuration. The fix is the right shape --
const long long mb = std::strtoll(env, &end, 10);
const bool digits = end != env;
while (digits && std::isspace(...)) ++end;
if (digits && *end == '\0' && mb >= 0 && mb <= kMaxOverrideMiB) { return mb * kMiB; }-- whole-value consumption, signed rejection, and an upper bound (1 << 43 MiB) that stops mb * kMiB from overflowing size_t on the way out. The arms are the pair I would have asked for: "512\n", " 512 ", "\t512" accepted; "", "abc", "16GiB", "-1", "\n", "512 MiB" rejected. Rejecting "512 MiB" is the right call and worth one comment line, because units are exactly what a person types.
The ask: say something when a value is ignored. Today a mistyped override falls through to the scaled default silently, so an operator who set GGML_HRX_GTT_RESERVE_MB=16GiB, or =8G, believes they have a 16 GiB reserve and actually has the computed one -- and the guardrail line in the log (reserve N GiB) is the only clue, one line away from nothing. A single fprintf(stderr, "GGML_HRX_GTT_RESERVE_MB=%s is not a number of MiB; using the computed reserve\n", env) on the reject path makes the config honest, and it is the same rule this round keeps landing on: an input that was not understood must not be indistinguishable from one that was.
Cross-repo note: this pairs with engine ggml-org#3012 (same defect, app/mem_admit.h). Worth landing together and saying so, for the reason the previous pair gave -- a planner and a backstop that disagree about what the operator asked for is worse than either.
Refs 1bit-MONSTER/engine#2986.
The reserve override was read with
atoll. That turns""and"abc"into 0 and"16GiB"into 16 MiB, so setting the variable empty, or mistyping it, removed the guardrail. Measured on d79524d with an 8 GiB pool.Now only a whole, non-negative number of MiB counts as an override. Anything else is ignored and the quarter-pool default stands; an explicit 0 still works. The engine applies the same rule in its planner, so the two keep computing the same number.
test-hrx-gtt-reserve: 15/15 cases pass.test-hrx-pool-refusal-budgetstill passes. No device needed.🤖 Generated with Claude Code