Skip to content

hrx: host-weight residency instrumentation, weak leases, and an opt-in bound (engine #669) - #169

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/i669-residency-arena
Nov 1, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/i669-residency-arena

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 1, 2026 •

Copy link
Copy Markdown

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

  • 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 the cache can 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 - 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.
  • 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.

Default is off, and equals the unpatched pin (measured, strixhalo, one gpu-hold ticket, GTT at 5 Hz, 95 GiB cap, no-SVM trio)

arm GTT delta output
Qwen3-4B-Q4_K_M -ngl 0 +4121M (unpatched: +4121M) generates, 35.0/10.3 t/s, GTT fully returned
Qwen3-4B-Q4_K_M -ngl 8 +4735M generates, 37.4/11.5 t/s
Qwen3-0.6B-Q4_K_M -ngl 0, 32 tokens +1229M byte-identical to -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

  • At -ngl 0 the 4B model materializes ~2030 MiB of weights (resident reached 2029.9 MiB at the 2048 MiB limit), i.e. essentially the whole weight set, so a bound below that thrashes (earlier: hit=0 at 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).
  • Variable-size suballocation fragments: with the bound on, a 304 MiB weight can fail with 2116 MiB free in 3 blocks. It fails the graph loudly (no NaN, 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.
  • Growth must not be a ladder: every arena growth releases the previous buffer, and a released buffer is exactly what the per-size pools retain, so 64 -> 128 -> ... -> 4096 MiB leaked the ladder. One growth straight to the limit removes that (kept).

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. vs Okay, the user is asking, reproducible in one second on 0.6B; ggml-org#674 now has its instrument in #165). -dev none on 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_PATH is 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.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 1, 2026 14:57
@bong-water-water-bong bong-water-water-bong changed the title hrx: bound host-weight residency with weak leases, and one residency arena (engine #669) - DRAFT, measured negative hrx: host-weight residency instrumentation, weak leases, and an opt-in bound (engine #669) Nov 1, 2026
@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 1, 2026

@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

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.
@bong-water-water-bong

Copy link
Copy Markdown
Author

Rebased onto the merged fork tip 1991650479d (owner merged #165/#166/#167/#168) and re-verified:

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.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Head moved to 89ab6f46d95; the READY verdict covers the code, and here is what actually changed.

Rebased onto the fork tip 1991650479d (owner merged #165/#166/#167/#168). One conflict, and it is not in
anything the review reasoned about: fork #161 added the GGML_OP_TOP_K shape check inside device_supports_op at
the same point this branch inserts host_weight_exceeds_residency(op). Resolved by keeping both as independent
return false guards, TOP_K first. Nothing else in the eight files moved, no behaviour changed, and the default
is still inert (residency_limit() == 0 on the unmodified path).

Compiled at 1991650479d (GGML_HRX=ON, gfx1151, amdclang /opt/rocm-therock) - compile only, because the GPU
was frozen after the 11:13 reset. The all-clear is in as of 12:13, so the hardware rows you could not take are
now mine to produce on this exact head
, in one gpu-hold ticket with the no-SVM trio and the 95 GiB cap:

  1. Qwen3-4B-Q4_K_M -ngl 0 at the rebased head, bound off (default) - the +4121M row, re-confirmed on the
    merged tip so the table in the body is measured on what would merge, not on the pre-merge pin;
  2. Qwen3-0.6B-Q4_K_M -ngl 0, 32 tokens - the byte-identity row against -dev none;
  3. the opt-in bound at 4096 MiB, which must fail loudly (arena is full after eviction) rather than silently
    change output - that is the one arm where a wrong answer would matter.

I will comment with those numbers and the log paths, or say plainly if any of them does not reproduce.

@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

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() has constexpr size_t kDefaultMiB = 0, the env var is
    read once through a function-local static, and host_weight_exceeds_residency() returns false on limit == 0
    before looking at any tensor — so with no GGML_HRX_HOST_WEIGHT_LIMIT_MB no 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. HostWeightLease holds std::weak_ptr<Entry> plus copied buffer_/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 updates binding.ref.{buffer,offset,length}
    in both initialization_commands and commands for 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() decrements executions_ and only
    when it reaches 0 swaps arena_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_room skip
    any entry with use_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, with evictions a new counter.

Two stale comments, one line each — the only defects I found, not worth holding the merge:

  1. 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.
  2. 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 to budget, 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.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Two typos in my review above, so nobody hunts for them: nit 1's variable is GGML_HRX_HOST_WEIGHT_LIMIT_MB (I typed GGML_GRX_…), and "bumpp" in the arena line is "bump". The substance is unchanged: default limit is 0/off, the header comment says 2048.

@bong-water-water-bong

Copy link
Copy Markdown
Author

The hardware rows on this head: two reproduce exactly, and the third does not — the opt-in bound gives NaN logits, not a clean refusal

Built 89ab6f46d95 on this box (all eight ggml/src/ggml-hrx files verified byte-identical to the PR head, md5 by
md5 against the PR worktree) and ran the three rows in one gpu-hold ticket at 12:54, no-SVM trio, mem-watchdog,
Qwen3-4B-Q4_K_M / Qwen3-0.6B-Q4_K_M, -fit off -ngl 0, --temp 0. GTT sampled at 0.2 s.

row body says measured on 89ab6f46d95
4B -ngl 0, bound off (default) +4121M, generates +4121M, 19.4/17.1 t/s, Okay, the user is asking ✔ reproduced exactly
0.6B -ngl 0, 32 tokens byte-identical to -dev none byte-identical (md5 8d4da6aceff1c98b467479643f7bcf5c both sides) ✔
4B -ngl 0, bound 4096 (opt-in) +5271M, "fails loudly (arena is full after eviction)" +5271M, and it does not fail loudly ✘

The third row is the one to read before merging. With GGML_HRX_HOST_WEIGHT_LIMIT_MB=4096 the run produces

0.01.260.843 E common_sampler_sample: HRX returned NaN logits: count=151936 of 151936,
              index range [0..151935], contiguous=yes, continuing=no (engine#315)
common/sampling.cpp:620: HRX: NaN logits (engine#123)

and then SIGABRT (rc 134, core dumped). There is no arena is full after eviction line anywhere in the log -
the string does not appear in it at all. So on this head the bounded path does not refuse, it computes NaN for the
whole vocabulary and takes the process down, which is exactly the failure mode the HostWeightExecutionScope
parking was supposed to have removed (the 08:05 measurement that produced NaN logits at the 2048 MiB bound).

What that means for the merge decision (the caller's, not mine):

  • The default is inert, and that is now re-confirmed on the merged tip with the numbers above: bound off behaves
    as the unpatched pin (+4121M), and the 0.6B output is byte-identical to the CPU. Merge-as-is changes nothing for
    anyone who does not set the variable.
  • The opt-in bound is not safe and its row in the body is wrong. Either fix the bounded path before it is
    advertised, or take the two honest options: say in the body that the bound is experimental, or drop the bounded
    arm from this PR and land only the instrumentation and the weak-lease machinery (which is what the default uses).

I would not merge the body as written, because it tells a future reader that GGML_HRX_HOST_WEIGHT_LIMIT_MB=4096
fails loudly. It does not: it silently poisons the logits and aborts. The one thing that would settle whether this is
my build or the code is a second opinion on the same head - the command is one line and the log is
~/wt/i669-fix/logs/run-p169C-4b-bound4096.log on strixhalo.

Measured with ~/wt/i669-pr/logs/p169-arms.sh (in the branch's logs/), engine ggml-org#669 lane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant