Repository navigation
hrx: host-weight residency instrumentation, weak leases, and an opt-in bound (engine #669) - #169
Conversation
44deb2d to
5da38ea
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 5da38ea54. Eight files, +626 −23, in ggml/src/ggml-hrx/. No CI in this repo, so this is the review; I could not build it (no HRX tree on this box after the rebuild), so it is read as code plus the measurements you supply.
The two things correctness lives on both check out:
Default is inert, in code and not just in the table. HostWeightCache::residency_limit() is kDefaultMiB = 0, host_weight_exceeds_residency() returns false when limit == 0, and the arena is only entered on the bounded path — so with no environment variable the behaviour is per-weight residency, i.e. the unpatched pin. That matches the measured arm (Qwen3-4B-Q4_K_M -ngl 0 → +4121M, against unpatched +4121M), which is the strongest form this evidence can take: the change is provably a no-op until asked.
The in-flight parking is the fix for the bug you found, and it is implemented where it has to be. A lease is a weak snapshot, so "evicted" is not "unbound"; HostWeightExecutionScope swaps the execution's freed slots into arena_deferred_ and returns them to the free list only when the scope ends, under the cache mutex. Reusing a slot the running program still binds is exactly what produced NaN logits in the first version, and parking at scope granularity is the right level — per-copy would still allow reuse inside one execution.
And the disposition of the feature is honest in a way worth recording. The comment on residency_limit() says the bound is not a default win — "at -ngl 0 a 4B model materializes ~2030 MiB of weights, so a bound under that thrashes and a bound over it costs more GTT than the churn it removes (+8473M at 4096 MiB against +4121M unpatched)". Shipping that as a default would have looked like progress and been a regression; making it opt-in with the trace for diagnosis is the correct call, and ggml-org#669's reclaim finding (≈6700 releases returning nothing because libhrx pools per size class) is why the arena exists at all.
The trace line reports refilled/hit/miss/evict/resident per execution, which is what makes the opt-in usable rather than a black box.
Not verified: anything on hardware — that generational reclaim actually returns GTT, that the trace numbers are right, and that the bounded mode's thrash behaviour matches the table. Also unverified by me: -ngl 8 (+4735M) and the 0.6B byte-identity row. Those need the array; the body's method (one gpu-hold ticket, GTT at 5 Hz, 95 GiB cap, no-SVM trio) is written down well enough to reproduce.
One thing to watch, not a blocker: the limit is read once into a static (static const size_t limit = [](){…}()), so GGML_HRX_HOST_WEIGHT_LIMIT_MB cannot be changed within a process. That is consistent with how the other knobs behave and is the right default, but worth knowing when someone tries to A/B it in one run.
tprs-ready applied.
…n bound (engine ggml-org#669) Engine ggml-org#669: with the HRX device present, a host-buffer weight that an HRX op reads is materialized into device memory, and the prepared program used to keep it alive, so residency grew with the model. This branch makes that residency visible, movable and (opt-in) bounded: * HostWeightLease is a borrowed snapshot (weak) and rebind_prepared_host_weights re-acquires and rewrites the prepared bindings at execution start, so a prepared program no longer owns the bytes and a cache may evict behind it; * GGML_HRX_HOST_WEIGHT_TRACE=1 prints residency per execution (refilled/hit/miss/evict/resident); * GGML_HRX_HOST_WEIGHT_LIMIT_MB=<MiB> turns on a byte budget with LRU eviction into ONE residency arena (bump + coalescing free list), so eviction frees a slot instead of calling hrx_buffer_release - which is ggml-org#669's reclaim finding: ~6700 releases returned nothing because libhrx pools freed device buffers per size class; * while an execution is in flight the slots it frees are parked (HostWeightExecutionScope) and only returned to the free list when it ends: a lease is a weak snapshot, so 'evicted' is not 'unbound', and reusing a slot the running program still binds is what produced NaN logits in the first version of this patch. Nothing changes by default: the bound is off, so each weight keeps its own buffer exactly as before, and the measured behaviour equals the unpatched pin: Qwen3-4B-Q4_K_M, -dev HRX0 -fit off -ngl 0 -c 256 --temp 0 +4121M (unpatched: +4121M) same, -ngl 8 +4735M Qwen3-0.6B-Q4_K_M, -ngl 0 (32 tokens) +1229M, output byte-identical to -dev none (1126/176 t/s) Bounded mode (GGML_HRX_HOST_WEIGHT_LIMIT_MB) is where the numbers say the limit of this design is, and they are in the PR body rather than hidden: at -ngl 0 the 4B model materializes ~2030 MiB, so a bound of 2048 MiB thrashes and 4096 MiB costs more GTT than the churn it removes (+8473M vs +4121M), and variable-size suballocation fragments - a 304 MiB weight can fail with 2116 MiB free in three blocks, which fails the graph loudly instead of corrupting. That is why the bound is opt-in and why the fix for the residency wall is the placement path (ggml-org#674), not this one.
5da38ea to
89ab6f4
Compare
|
Rebased onto the merged fork tip
For pi-prs: this is the ggml-org#669 diagnosis + machinery (weak leases, execution-scoped slot parking, residency trace) plus an opt-in bound that the measurements show is not a default win. Nothing here changes behaviour unless the knob is set. |
|
Head moved to Rebased onto the fork tip Compiled at
I will comment with those numbers and the log paths, or say plainly if any of them does not reproduce. |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Reviewed as code, and that is what I ran — no device arm from me.
Ran: the four changed translation units compile at 89ab6f4 with the HRX configuration's compiler and headers:
amdclang++ -std=c++17 -fsyntax-only -I ggml/include -I ggml/src -I ggml/src/ggml-hrx \
-I <engine>/third_party/hrx-system/libhrx/include \
ggml/src/ggml-hrx/runtime/host-memory.cpp ok
ggml/src/ggml-hrx/host-weight-placement.cpp ok
ggml/src/ggml-hrx/runtime/command-program-executor.cpp ok
ggml/src/ggml-hrx/ggml-hrx.cpp ok
(Bare -I ggml/include -I ggml/src … without the hrx-system include fails on hrx_runtime.h, which is only a reminder
that these TUs are not host-CI-compilable; the real build supplies it.)
Read and checked against the claims, all of it consistent:
- Default off is real, not asserted.
residency_limit()hasconstexpr size_t kDefaultMiB = 0, the env var is
read once through a function-localstatic, andhost_weight_exceeds_residency()returnsfalseonlimit == 0
before looking at any tensor — so with noGGML_HRX_HOST_WEIGHT_LIMIT_MBno arena is created, nothing is evicted,
and the placement guard never declines an op. That is why "+4121M (unpatched +4121M)" can be equal by construction. - The lease is genuinely weak.
HostWeightLeaseholdsstd::weak_ptr<Entry>plus copiedbuffer_/offset_/
length_/layout_;valid()tests the weak pointer. So "the program holds a lease" no longer keeps bytes alive. - The rebind happens at execution start and rewrites what it must.
rebind_prepared_host_weights()walks
prepared.resident_host_weights, re-acquires what!lease.valid(), and updatesbinding.ref.{buffer,offset,length}
in bothinitialization_commandsandcommandsfor the matching binding value. Nothing binds a stale offset. - The NaN fix is the scope, and it is on both execution entry points.
HostWeightExecutionScope residency_scope( context.host_weights)is constructed before the rebind in each;end_execution()decrementsexecutions_and only
when it reaches 0 swapsarena_deferred_out and reclaims it, so a slot freed mid-execution cannot be handed to
another weight while the running program still binds it. Nested scopes are handled by the counter, not a flag. - Arena: bumpp + coalescing free list, and eviction frees a slot.
arena_take_locked()first-fits and splits;
arena_reclaim_locked()inserts in offset order and merges with both neighbours;evict_for/arena_make_roomskip
any entry withuse_count() != 1(i.e. one somebody still references) and synchronize the stream before freeing.
Victims are copied out of the map before the lock is dropped and re-looked-up under it, so no iterator or key
pointer is used after the map can change — the part of this kind of patch that usually goes wrong is right here. - Failure is loud.
arena_reserve()logs and returns false on a growth failure or a weight larger than the
budget, and the caller's message is the one quoted in the body ("… free in N blocks, X MiB wanted"). - Trace fields are the body's (
refilled=/ hit= miss= evict= resident= allocs= limit=), printed from the stats the
cache keeps, withevictionsa new counter.
Two stale comments, one line each — the only defects I found, not worth holding the merge:
runtime/host-memory.h,HostWeightCache::limit(): "GGML_GRX_HOST_WEIGHT_LIMIT_MB, default 2048" — the default is
0 (off). In a header whose point is that the default matches the unpatched pin, a reader would conclude a bound
is active by default.- Same header, the arena member: "grows by doubling up to the residency limit" — only the first allocation doubles
(64 MiB start); every later growth jumps straight tobudget, which is exactly the ladder fix the body describes
("Growth must not be a ladder … One growth straight to the limit removes that (kept)"). The code comment 30 lines
below it says so; this one contradicts it.
Not run: any of the measured arms. The GTT deltas, the 2030 MiB resident set, the hit=0 thrash at 2048 MiB and the
+8473M at 4096 MiB are the lane's numbers; re-taking them means loading a 4B model per arm on a box that is throttling
at 85 °C, for a change whose default path is provably inert. Say the word and I will re-run the -ngl 0 arm
(GGML_HRX_HOST_WEIGHT_LIMIT_MB unset vs 2048) under one gpu-hold ticket with the no-SVM trio once the current
build finishes — it is ~+4.7 GB GTT, well under the memory rule's bar.
|
Two typos in my review above, so nobody hunts for them: nit 1's variable is |
The hardware rows on this head: two reproduce exactly, and the third does not — the opt-in bound gives NaN logits, not a clean refusalBuilt
The third row is the one to read before merging. With and then What that means for the merge decision (the caller's, not mine):
I would not merge the body as written, because it tells a future reader that Measured with |
Engine ggml-org#669: with the HRX device present, a host-buffer weight that an HRX op reads is materialized into device memory, and the prepared program used to keep it alive, so residency grew with the model (the issue: RSS 37.7 GiB while MemAvailable fell ~107 GiB on the DeepSeek-V4.1 file). This branch makes that residency visible, movable, and opt-in bounded.
What it changes
HostWeightLeaseis a borrowed snapshot (weak) andrebind_prepared_host_weightsre-acquires and rewrites the prepared bindings at execution start, so a prepared program no longer owns the bytes and the cache can evict behind it.GGML_HRX_HOST_WEIGHT_TRACE=1prints residency per execution (refilled/hit/miss/evict/resident).GGML_HRX_HOST_WEIGHT_LIMIT_MB=<MiB>turns on a byte budget with LRU eviction into one residency arena (bump + coalescing free list), so eviction frees a slot instead of callinghrx_buffer_release- llama.cpp main hangs at prompt with latest mmap updates ggml-org/llama.cpp#669's reclaim finding is that ~6700 releases returned nothing because libhrx pools freed device buffers per size class.HostWeightExecutionScope) and only returned to the free list when it ends: a lease is a weak snapshot, so "evicted" is not "unbound", and reusing a slot the running program still binds is what producedNaNlogits in the first version of this patch.Default is off, and equals the unpatched pin (measured, strixhalo, one
gpu-holdticket, GTT at 5 Hz, 95 GiB cap, no-SVM trio)Qwen3-4B-Q4_K_M-ngl 0Qwen3-4B-Q4_K_M-ngl 8Qwen3-0.6B-Q4_K_M-ngl 0, 32 tokens-dev none(70.0/14.3 t/s vs 1126/176 t/s CPU)Bounded mode: what the numbers say about the limit of this design
-ngl 0the 4B model materializes ~2030 MiB of weights (residentreached 2029.9 MiB at the 2048 MiB limit), i.e. essentially the whole weight set, so a bound below that thrashes (earlier:hit=0at 2048 MiB against a 2378 MiB model) and a bound above it costs more GTT than the churn it removes (+8473M at 4096 MiB vs +4121M unpatched).2116 MiB free in 3 blocks. It fails the graph loudly (noNaN, no silent corruption):arena_reserve: host weight arena is full after eviction: 506.7 MiB of 2048.0 MiB resident, 326.9 MiB free in 3 blocks, 304.3 MiB wanted.So: this lands the diagnosis and the machinery with no default behaviour change, and the bound stays opt-in because the measurements say the residency wall is not won on the reclaim path. The weights have to stop being materialized for HRX ops that never read them - the placement path, gated on ggml-org#674 (
inaryиона this this is.vsOkay, the user is asking, reproducible in one second on 0.6B; ggml-org#674 now has its instrument in #165).-dev noneon the same 4B model is +162M, the floor to aim at.Built at the branch base with
GGML_HRX=ON, gfx1151, amdclang from/opt/rocm-therock(HIP_DEVICE_LIB_PATHis required at build time), no-SVM trio at run time. Follow-ups this PR deliberately does not attempt: per-program pinning plus slot classes (or compaction with lease invalidation) to make a bound usable, and the placement fix itself.