Skip to content

hrx: an empty or mistyped GGML_HRX_GTT_RESERVE_MB no longer removes the GTT reserve (engine#2986 follow-up) - #455

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
cl-2986/reserve-env-strict
Nov 20, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
cl-2986/reserve-env-strict

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

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-budget still passes. No device needed.

🤖 Generated with Claude Code

…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]>
@bong-water-water-bong

Copy link
Copy Markdown
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 fork half; engine#3012 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
Author

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 #3012, with SIZE_MAX >> 20 rather than UINT64_MAX >> 20 — which is the correct constant on this side, because the value is a size_t here. That detail is the whole reason to check a pair rather than one of them: a copy-pasted bound would have compiled and been wrong on a 32-bit size_t, and it is right on both.

The arms match too — 0 is still an override, a bad value still leaves the quarter-pool default (2 * kGiB on an 8 GiB pool) — so the failure mode is closed on the side that the reserve actually protects, and the two repos now compute the same reserve and parse the override the same way. #451/#452 settled the ratio; this settles the knob.

What I checked

The four conditions (end != env, *end == '\0', mb >= 0, the overflow bound before the multiply), the SIZE_MAX choice against this file's types, the preserved explicit 0, the two arms and their values against an 8 GiB pool, and the engine half's predicate for agreement.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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 (strtoll skips 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.
  • +16 is 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 bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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 bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

  • -0 is accepted and sets the reserve to zero, while -1 is 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 with std::atoll in pool_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.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 41e03220: 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 (engine side, same text) 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 merged commit 338dabb into 1bit/hrx-vulkan-patched Nov 20, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-2986/reserve-env-strict branch November 20, 2026 18:25

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

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.

1 participant