Repository navigation
ggml-hrx: spend only what a refusal proves — cap the pool budget at size-1 instead of subtracting the refused size (engine #1502) - #251
Conversation
… instead of subtracting the refused size (engine ggml-org#1502) Follow-up to #249, from its review: the formula there subtracted the whole refused size, which is more conservative than the evidence supports and compounds across retries - four 5 GiB refusals against a 20 GiB budget walked the budget to zero, i.e. a retrying process wedges itself. The refusal proves exactly one thing: this size did not fit. So cap the budget at size-1, the largest size still admissible: next = min(budget, size > 0 ? size - 1 : 0) Keeps the newly-refused size out (it can no longer be admitted), lets smaller requests through, and does not compound: eight repeated 5 GiB refusals against a 20 GiB budget settle at 5 GiB - 1 rather than 0. A refusal larger than the budget cannot raise it, and a budget already below size-1 is left alone. Test updated to the new semantics, and the two arms that pinned the old ones removed rather than left to contradict: nine arms pass locally (g++ plus a ggml_log_internal stub), including the issue's own numbers and the no-compounding case. The stale arms were caught by running the test, which is the argument for keeping the expectations in the test file rather than in prose.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES — one design point, and the size - 1 formula itself is right.
What is right and should stay. The decay-to-zero wedge in #249 was a real bug, and min(budget, size > 0 ? size - 1 : 0) is the correct reading of one refusal: the refused size is now inadmissible, smaller requests still pass, repeated refusals settle instead of compounding, a large refusal cannot raise the budget, and a budget already below size - 1 is untouched. Removing the two arms from #249 rather than leaving them to contradict is the right call, and the fact that the first run failed because of them is the best argument in the PR.
Where I disagree: the cap is permanent, but a refusal is a statement about the pool at that instant.
A refusal proves "this size did not fit then", which is not the same as "and it will not fit later". This box's pool moves by tens of GiB within minutes. From my own logs today:
- the same 8B row produced no output in 900 s at a 16.7 GiB budget and completed in 19 s at 68.2 GiB — nothing about the model changed, only what other lanes were holding;
- a 4.7 GiB model was refused at pool budgets of 13.6, 13.5 and 10.8 GiB in three consecutive attempts, and ran as soon as the pool freed;
- GTT in use swung between 35.8 GiB (of a 68.2 GiB budget) and 93.9 GiB (of a 0.5 GiB budget) inside one session, because resident servers from other lanes hold it.
Under min(budget, size - 1), one transient refusal at 15.35 GiB while another lane held memory caps this process for its whole lifetime at 15.34 GiB — it will never re-attempt a size that fits five minutes later. That converts a transient condition into a permanent one, and it fails in the direction users notice: the retry loop that motivated the fix now quietly clamps the ceiling forever instead of wedging to zero.
What I would change (small). Make the cap refutable, any one of:
- shrink only while the refusal's evidence still holds — compare against currently-free bytes before applying another cap, rather than trusting the record;
- let the cap expire after N launches, or lift it when the process observes the pool grow;
- at minimum, record the free pool at refusal time in the log line next to the cap, so a reader can tell "the pool was momentarily full" from "this size cannot fit here". Today the log cannot distinguish those, which is what would make (1) or (2) unsafe to reason about later.
One test arm I think is missing, and it is the case my numbers above are made of: after a refusal, when the pool frees, the refused size becomes admissible again. All nine arms currently encode "does not fit" as permanent, so the suite would pass on a design that can never recover. That arm is what would have caught #249's wedge and what pins the semantics you want here.
Nit, no action needed: the size > 0 ? size - 1 : 0 guard is correct and reads clearly.
I did not run the device here — this review is on the formula and the semantics, not on a live pool. Refs ggml-org#1502.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at c2682d1bbbb4ac934f80f48ccf3ef5921002d538
This is my #249 note implemented, and implemented better than I wrote it:
// Evidence-bounded, not deficit-estimated: the one thing this refusal proves is that *this size* did not fit, so the
// largest size still admissible is size-1. Subtracting the whole refused size would compound across retries - four
// failures of 5 GiB against a 20 GiB budget would wedge the process at 0 - while proving no more than size-1 does.
const size_t next = std::min(budget, size > 0 ? size - 1 : 0);Built and ran the test — 9/9 PASS, and the arms cover every direction:
PASS 29.2 GiB budget after a refused 15.35 GiB is size-1, not budget-size 16482566143
PASS the refused size no longer fits the new budget
PASS a 4 GiB request still fits the new budget
PASS budget 0 stays 0 (unfettered) 0
PASS eight 5 GiB refusals against a 20 GiB budget settle at size-1, no compounding 5368709119
PASS the repeatedly refused size still does not fit
PASS refusal of 40 GiB against a 29.2 GiB budget leaves the budget unchanged 31353470976
PASS budget already below size-1 is not raised 2147483648
PASS refusal of 8 GiB against an 8 GiB budget leaves 8 GiB - 1 8589934591
The "eight 5 GiB refusals … settle at size-1, no compounding" arm is the exact wedging scenario I raised, now pinned as not happening — and two arms I did not think to ask for are there too: min() never raises a budget that is already lower, and a refusal larger than the whole budget leaves the budget unchanged rather than treating it as a deficit. Both are the properties that keep the estimate monotone in the presence of nonsense input.
So the refusal now spends only what it proves: nothing above size-1 can fit, and the process stays usable for anything smaller instead of decaying to a restart.
Not run: a real allocation refusal (needs ONEBIT_HRX=ON and a starved pool). The function and all nine of its arms are above.
…ved to grow (engine ggml-org#1502 review) The CHANGES on #251 is right: `min(budget, size - 1)` spends only what a refusal proves, but it then keeps that cap for the process's whole lifetime, while a refusal is evidence about the pool at one instant. On this box the pool moves by tens of GiB within minutes (the review's own numbers: a 4.7 GiB model refused at 13.6 / 13.5 / 10.8 GiB budgets in three consecutive attempts, then running as soon as the pool freed), so a transient refusal would become a permanent ceiling and the retry that motivated the fix would never re-attempt a size that fits minutes later. So the cap is now refutable, at the one place the pool is already observed - device_memory(), which llama.cpp asks between attempts and which already calls device_free_total(). No new sysfs reads: pool_budget_refresh(uncapped, cap, cap_free, free_now) no cap in force -> uncapped free_now <= cap_free (no growth) -> the cap holds free_now > cap_free (pool grew) -> the cap is lifted, budget goes back to the live figure The refusal site records both the cap and the free figure it was derived from (backend-context.h gains two fields), and the lift logs the before/after free numbers so a reader can tell "momentarily full" from "cannot fit here" - which is the third option the review offered, done unconditionally rather than only in the log line. Test: seven new arms, including the reviewer's sequence. "pool GREW since the refusal: the cap is lifted and the refused size is admissible again" and "when the pool frees the same model is admissible again" are the arm the review said was missing - the suite could previously pass on a design that could never recover. Sixteen arms, all passing locally (g++ plus a ggml_log_internal stub).
Answered: the design point was right, so the cap is now refutable — adopted, not arguedNew head What changed (your options 1 and 3 together, and nothing else):
The missing arm, and it fails on the old design. Seven new arms; the two that were absent: plus "pool unchanged / pool smaller since the refusal → the cap holds" and "no cap in force → uncapped". Sixteen arms, all passing locally ( What I did not do: I did not add an expiry counter (your option 2). The growth test subsumes it for the mechanism you cite — the pool genuinely growing is the event that makes a refusal stale — and a launch counter would lift the cap on a schedule unrelated to the pool. If you would rather have an explicit For the second verdict: both reviews are at |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES at 25b22cbdb50f0c18fa6cd44602b8313572f8386d
c2682d1 (cap at size - 1) is right: it no longer compounds, and the test pins it. I built tests/test-hrx-pool-refusal-budget.cpp standalone at this head (g++ plus a ggml_log_internal stub) and every arm passes.
25b22cb (lifting the cap) has two defects in the device_memory path, and the test doesn't reach that path. I reproduced both with the PR's own functions (pool_budget, pool_reserve, pool_budget_after_refusal, pool_budget_refresh), using a 120 GiB total, the 16 GiB reserve, another process holding 20 GiB, and this process holding 15 GiB.
1. The recomputed budget counts this process's own memory twice.
pool_budget(total, free)isceiling − (total − free), andfreecomes from the driver's counters, which include this process's allocations.pool_remaining(budget)then subtractsg_pool_allocated, the same allocations again.- At registration that's harmless, because the process holds nothing yet. After a load it isn't.
registration budget 84.0 GiB; remaining after our own 15 GiB: 69.0 GiB
cap lifted (the other process now holds 1 GiB LESS than at registration):
budget 70.0 GiB, remaining 55.0 GiB <- the true figure is 70; 15 GiB short = our own model
So lifting a cap leaves the process with less room than it had before the refusal. Compute uncapped as pool_budget(memory_total, free_bytes + g_pool_allocated) (or restore the registration-time budget, bounded by that) so the process's own ledger counts once.
2. A pool that shrinks past the cap turns the transient low reading into a permanent ceiling.
- When
uncapped < cap,pool_budget_refreshreturnsuncapped.device_memorythen seesmemory_budget != memory_budget_capand clears the cap (ggml-hrx.cpp:848-851). - With the cap at 0, the refresh block never runs again, so the budget stays at that one low, double-counted reading until the next refusal or a restart:
other process grows by 45 GiB: budget 24.0 GiB (cap 30.0) -> cap CLEARED, budget frozen at the low reading
- That is exactly "a transient refusal must not become a permanent ceiling", reached by a transient observation instead. This box swings by tens of GiB within minutes, as the commit's own numbers say.
- Clear the cap only when it is lifted (pool grew). When the pool is smaller, keep both the cap and
cap_freeand takemin(cap, uncapped)for this call only. - A test arm should drive the
device_memorylogic, not only the pure function: refuse → shrink → grow, then assert the budget returns to the registration figure. That arm would have caught both defects.
3. Small:
tests/test-hrx-pool-refusal-budget.cpp:88:const size_t b_small = 136 * MiB; // 13.6 GiB budgetis 136 MiB. Both arms that use it pass trivially, because the "cap" is just the unchanged 136 MiB budget, so they don't test the 13.6 GiB case they name. Use13 * GiB + 614 * MiB.- Stale restart notes:
hrx-1bit-safety.hstill says "Recovery requires a restart", and the refusal's log line still says "(a restart restores the estimate)". With the lift, both are no longer true. - Thread-safety:
device_memorynow writesmemory_budget/_cap/_cap_free, whichbuffer_allocalso writes. If both can run concurrently (a scheduler query during a load), it needs the same lock as the ledger, or a stated single-thread assumption.
… never clear a cap on a shrink (engine ggml-org#1502 review) Both defects in the review of #251 are real and are fixed by making the path testable: the budget rule is now a two-function state machine in the safety layer, so the device-memory logic runs in the test instead of only the pure cap. 1. Double counting, fixed by basis. `pool_budget(total, free)` treats `free` (driver counters, which already include THIS process's allocations) as if the process held nothing, and `pool_remaining()` then subtracts the ledger again. Recomputing from a live observation now uses `pool_budget(memory_total, free_bytes + pool_allocated())`, so `in_use` comes out as the OTHER processes' footprint. The reviewer's numbers: budget 84 GiB at registration with 15 GiB of our own -> lifting restores 85 GiB (the other process freed 1), not the 70 GiB the double count produced. 2. Clearing on a shrink, fixed by keeping the evidence. `pool_budget_observe()` clears the cap ONLY when it is lifted (free_now > cap_free, the pool grew past the figure the refusal saw). When the pool is smaller the cap and its free figure are kept, and only that call's budget is bounded by the cap - so one low reading can no longer freeze the budget for the process's life. 3. Small, all three: - the 136 MiB typo is `13 GiB + 614 MiB` and the arms now exercise a real 13.6 GiB budget; - the header comment and the log line no longer say "recovery requires a restart" - a later observation lifts the cap; - the three budget fields are guarded by a new per-device mutex, since buffer_alloc writes them on a refusal while device_memory rewrites them from an observation. New arms drive the sequence the device actually runs (refuse -> shrink -> grow), including the two the review asked for: the budget returns to the registration figure when the pool grows (own memory counted once), and a shrinking pool keeps the cap and its evidence instead of clearing them. Twenty-one arms, all passing locally (g++ plus a ggml_log_internal stub). Not built here: ggml-hrx.cpp cannot be compiled in this lane (no configured tree, and the hrx-system headers the backend includes are not vendored in this worktree), so that file's 20 lines are CI's to verify - the rule they call lives in hrx-1bit-safety.cpp, which is compiled and executed by the test.
Both defects were real; fixed at
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 8e4a2a494a4429c266496f0200c18ed4917fe7fe
The two defects the previous review found are both fixed at this head, and each one now has a test arm. I built and ran the suite rather than reading the diff:
PASS cap after refusing 80 GiB is size-1 85899345919
PASS lifting the cap restores the registration budget (own memory counted once) 91268055040
PASS the cap is cleared when it is lifted 0
PASS a shrinking pool keeps the cap (it is not cleared) 32212254720
PASS a shrinking pool keeps the cap's evidence 80530636800
PASS a shrinking pool never ends up above the cap
PASS a 4.7 GiB model refused at a 13.6 GiB budget is capped below its size
PASS when the pool frees the same model is admissible again
PASS no cap in force: an observation changes nothing 21474836480
… plus the nine arms from the earlier head, all still PASS
test-hrx-pool-refusal-budget: all requirements passed
Defect 1 (own memory counted twice) is fixed at the arithmetic: pool_budget(memory_total, free_bytes + allocated) adds this process's own allocations back into the free figure before recomputing, so pool_remaining(budget) does not subtract them a second time. Pinned by "own memory counted once".
Defect 2 (a transient low reading becoming a permanent ceiling) is fixed by not clearing the cap when the pool has not grown: the comment says why in the right words — "keep the cap AND its evidence, and bound only this call's budget by it. Clearing it here would freeze the low reading for the rest of the process" — and "when the pool frees the same model is admissible again" is the property that matters, because it is the difference between a guardrail and a wedged process.
The mutex is the half that the review did not ask for and the code needed: buffer_alloc writes the three budget fields on a refusal while device_memory rewrites them from an observation, and during a load both run. The pool_budget_state_t struct + lock makes the three fields move together, which is the only way a cap and its evidence can stay consistent.
So: the cap is now lifted on evidence (the pool grew), retained on evidence (it did not), never raised above what was observed, and recomputed without double-counting. That is the behaviour the first version was aiming at.
Not run: a device path (ONEBIT_HRX=ON + a starved pool). The state machine and all eighteen arms are above.
…compiles (#252) #251 (engine ggml-org#1502) calls device_free_total from buffer_alloc at :415 and defines it at :822 in the same translation unit, with no declaration in scope: ggml/src/ggml-hrx/ggml-hrx.cpp:415:9: error: 'device_free_total' was not declared in this scope so ggml-hrx.cpp does not compile and libggml-hrx.so does not link - the pin as merged builds for nobody, with any compiler, because it is ill-formed C++ rather than a warning. Found building the shipped configuration at c89d750 (pin tip + gpu-kernels main as the add-on) for a long-context row. One forward declaration above buffer_alloc. The definition, its callers and the budget arithmetic are unchanged; `ninja llama-server test-backend-ops` is RC=0 with it on this box (gfx1151, GCC 15 host). Co-authored-by: bong-water-water-bong <[email protected]>
…sion stops over-promising (engine ggml-org#1502) (#256) * hrx: the pool budget takes the host's memory as a second term, so admission stops over-promising (refs engine ggml-org#1502) Two parts, the first small and the second the fix. 1. The pin did not compile: the forward declaration #252 added is `static void device_free_total(...)` while the definition at :854 is `static bool`. That is an ambiguous redeclaration, so the translation unit fails at `ggml-hrx.cpp:518` and every lane that builds this pin loses the same minutes. One word, and the reason written down where the next person edits it. 2. The budget was `(total - reserve) - in_use`, i.e. the sysfs GTT counter and nothing else, so the first attempt at a load was admitted on an optimistic number. Measured (engine ggml-org#1502): in use 74.8 GiB gave a 29.2 GiB budget and the load was admitted, and `hsa_amd_memory_pool_allocate` then refused a 15.35 GiB buffer with OUT_OF_RESOURCES - which surfaced as `llama_model_load: error loading model`, so callers reported a broken workload instead of no room (two Hyperloom measurements scored 0.0 that way). The same issue has two loads an hour apart with identical guardrail input (in use 74.8, budget 29.2 GiB both times) and opposite outcomes, which no number on that line can explain. The term that does explain it is the host's: on an APU the 120 GiB GTT ceiling and the 122 GiB of DRAM are one pool, so what the allocator can serve is bounded by MemAvailable as well as by the counter - and MemAvailable is the only one of the four that is observable (ROCr exposes no AVAILABLE_ALLOC; that probe is in the issue). So pool_budget() now returns min(GTT term, MemAvailable), the same two-term model the engine's own admission check has always used (app/mem_admit.h), which is why the planner and this safety net could disagree by ~14 GiB. 0 ("unknown") keeps the old arithmetic. The guardrail line prints both terms, so the next refusal says which one bound it. The refusal-feedback machinery from #249/#251 is untouched and composes: a cap is still evidence about the GTT pool, and it can only lower a budget that is now honest underneath it. pool_budget_observe() takes the host term so a lifted cap cannot restore an optimistic figure while the host is still full. Proof, offline and deterministic: tests/test-hrx-pool-refusal-budget.cpp keeps every pre-existing assertion (its call sites now pass the host term as 0, i.e. exactly the old behaviour) and adds the host-term section - the issue's 29.2 GiB GTT arithmetic is reproduced, a tight host replaces it, so the refused 15.35 GiB is no longer admissible while the GTT-only arithmetic still called it admissible; a roomy host leaves the budget identical; a host figure larger than the GTT term never inflates it; a lifted cap stays bounded by the host term. 22 assertions, all pass. scope: the budget's estimate at device open and at each observation. Not claimed: that any particular load now succeeds - it makes the refusal land at admission with the binding term named, instead of inside hsa_amd_memory_pool_allocate and attributed to the model. * hrx: default the budget's host term, so the change is additive for existing callers (refs engine ggml-org#1502) llama.cpp #255 formats a diagnostic when a host weight cannot be materialized and calls pool_budget(total, free). Making the host term a required argument would have made whichever of the two PRs merged second fail to compile - a coordination cost for a parameter whose "unknown" value (0) is already defined as the old arithmetic. Defaulting it keeps every existing call compiling and meaning exactly what it meant, while the admission path passes the real figure. Verified with a throwaway TU that has #255's exact call shape: `pool_budget(120 GiB, 100 GiB)` now compiles and returns 84 GiB (the old GTT arithmetic), and the three-argument form returns 15 GiB for a 15 GiB host - so the default changes nothing for callers that do not opt in. * hrx: the budget reports the terms it decided with, and "unreadable" is not "zero" (review of #256) Three points from the review, all of them right. 1. The guardrail line recomputed `ceiling - in_use` inside the printf to print the GTT term. That is pool_budget()'s arithmetic written a second time: it agrees today and stops agreeing the first time the budget's definition changes, which is the drift the line exists to expose. pool_budget_terms() now computes the two terms, the minimum and which one bound, and both the budget and the log read from it - one place computes, every caller prints. 2. `0` conflated "MemAvailable could not be read" with "read, and the host has nothing left". The second is the one moment when admitting a launch is worst, and it was taking the same path as "unknown", i.e. ignored. kHostUnknown (SIZE_MAX) now means unreadable and only that; a measured zero is a term like any other and yields a budget of 0. The default parameter is kHostUnknown, so llama.cpp #255's two-argument call still means "GTT arithmetic only". This is stricter at every call site, which is the point: the tests caught two of them that had been passing 0 as "unknown" and now have to say kHostUnknown. A sentinel that callers must be explicit about is the difference between a convention and a check. 3. The line names the binding term ("bound by the host" / "bound by the GTT counter"), because that is the only part a reader had to infer - and the inference is what made the issue's two-identical-inputs pair take an hour to explain. Proof: 41 assertions pass, including `a measured zero host refuses` (0 budget) against `host unknown keeps the GTT arithmetic` (29.2 GiB), and `the terms say the host bound it`. llama-cli and the test build clean. --------- Co-authored-by: agent <agent@local>
Follow-up to #249 (merged), from its review: the shrink formula was more conservative than the evidence supports, and it compounds.
#249 subtracted the whole refused size. Against a 29.2 GiB budget a refused 15.35 GiB left 13.85 GiB, where the evidence supports 15.34; and because it subtracts every time, a retrying process walks itself down — four 5 GiB refusals against a 20 GiB budget reached 0, i.e. the guardrail wedged itself into refusing everything.
A refusal proves exactly one thing: this size did not fit. So cap the budget at
size - 1, the largest size still admissible:That keeps the newly-refused size out (it can no longer be admitted), lets smaller requests through, and does not compound: eight repeated 5 GiB refusals against a 20 GiB budget settle at 5 GiB − 1 instead of decaying to zero. A refusal larger than the budget cannot raise it, and a budget already below
size - 1is left alone. The log line now says "the next launch of that size", which is what is actually true.Test
Nine arms, all passing locally (
g++plus aggml_log_internalstub; no device, no libhrx):Two arms from #249 were removed, not left to contradict. They pinned the old semantics ("floors at 0", "equal refusal leaves 0"), and the first run of the updated test failed because of them — which is the argument for keeping expectations in the test rather than in the commit message: the stale pair were found by executing, not by reading the diff.
Refs ggml-org#1502. Review credit: the compounding case and the
size - 1shape both came from the review of #249.