Skip to content

hrx: GGML_HRX_GRAPH_PROGRAM_CACHE_MB sees ~16 MiB per cached program while the cache grows 0.4-2.3 GiB per shape — the 64-program count is the only bound that bites #841

Closed

Description

@bong-water-water-bong

hrx: GGML_HRX_GRAPH_PROGRAM_CACHE_MB sees ~16 MiB per cached program while the cache grows 0.4-2.3 GiB per shape — the 64-program count is the only bound that bites

Measured at fork d8bd0ff6052a (ggml/src/ggml-hrx), hrx-system 82804197bc, on strixhalo (gfx1151, 120 GiB UMA),
zaya1-8b-Q4_K_M, llama-server -ngl 99 -fit off -c 4096 -np 1, #715 no-SVM trio, gpu-hold, one request per new
prompt shape with cache_prompt=false. Raw logs: ~/amd-oss/hrx-study/evidence/memory/graphcache-8b-20261102-1030.log
(default bounds = arm A) and graphcache-bound-20261102-1044.log (both bounds exercised, 28 trims).

The measurement

Arm A, default bounds (GGML_HRX_GRAPH_PROGRAM_CACHE=64, ..._CACHE_MB=8192), GTT delta after each request:

shape (prompt tokens) GTT delta trim lines
load, no request +6033 MiB 0
33 +6461 0
480 +7092 0
512 +7738 0
1024 +9247 0
1152 +11556 0
2048 +12991 0
2963 +15447 0
4011 +18450 (unbounded arm; default arm trims to +11480) 0 / 28

So the cache grows 0.4-2.3 GiB per new shape, totalling ~12.4 GiB over 6 shapes at the point where the count bound
starts trimming. The trim line reports what the byte budget is looking at:

W trim_graph_programs: HRX graph program cache trimmed (1 of 66 programs, 1031 -> 1031 MiB of arenas,
                       budget 8192 MiB, 64 max): kept the current one, trim 25

1031 MiB accounted for 66 programs ≈ 16 MiB each, against ~8192 MiB of budget — 12.6 % of the budget while the cache
already held ~18 GiB of GTT. The count bound (64) trips on the 65th shape; the byte budget never will (it would need
~500 programs at this per-program rate).

Why: the byte budget only counts the transient arena

graph-program-cache.cpp:798

size_t GraphProgram::retained_bytes() const {
    return commands_ != nullptr ? commands_->transients.arena_size : 0;
}

and that is the only quantity fed to the budget (budget_.note(graph.uid, cached_program->retained_bytes()) at :685,
also :854 in retain_only). A cached GraphProgram also keeps its imported Graph (with the HRX buffers recorded
into it) and its prepared command programs — and trim_graph_programs clears exactly those prepared programs on every
trim (graph-program-cache-limit.cpp:64) while the budget never counted them. The ~1.2 GiB/shape difference is not
attributed
to a specific member here; what is established is that transients.arena_size is not it.

The header comment states the opposite expectation: graph-program-budget.h:21-22 — "A cached program keeps its
transient arena alive, about 1 GiB for a large prefill shape", which is why an 8192 MiB budget was assumed to bound
~8 shapes. Measured at 8B/ctx4096 it is ~16 MiB of arena per program and the other ~1.2 GiB per shape is invisible to the
knob.

Impact (operator-facing)

  • The knob that sounds like the memory bound (..._CACHE_MB, "budget in MiB") does nothing at realistic model sizes;
    the lever that actually bounds the cache is GGML_HRX_GRAPH_PROGRAM_CACHE (count), whose own documentation
    (graph-program-cache-limit.h:24-27) presents it as the weaker of the two.
  • Setting ..._CACHE_MB low to rein in a chat server is a silent no-op: no trim, no warning, growth continues until the
    count trips.
  • Not a correctness bug: with both bounds default the cache does get trimmed (28 trims observed) and everything is
    returned on exit; the report is about which knob is load-bearing and what its accounting covers.

Suggested direction

  • Account the prepared command programs and the graph-retained buffers in retained_bytes() (they are alive exactly as
    long as the program is cached), or
  • rename/re-document the knob as an arena-only bound and make the count bound the documented lever.

Reproduction scripts: ~/amd-oss/hrx-study/tools/scripts/mem-gc2.sh / mem-gc3.sh (the cache_prompt=false and
same-length designs), GTT read box-wide per request. Context: FACTS-memory.md §6.2/§7.3 in the same directory —
including the companion result that growth is per shape, not per request and not KV (10 identical-length requests
plateau after two, differing content).

Activity

  1. added
    bug
    hrx
    on Nov 2, 2026
  2. bong-water-water-bong commented on Nov 2, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    **Triaged — bucket A, owner the memory / hrx-study lane that filed it. Verified at fork d8bd0ff6052a; the paths are right but the files live one directory deeper than the issue says, and the arithmetic turns "does nothing at realistic sizes" into something stronger: at this geometry the byte budget cannot trip.

    Verified at d8bd0ff6052a (all four, exact lines)

    claim where
    the budget sees only the transient arena ✔ ggml/src/ggml-hrx/runtime/graph-program-cache.cpp:798-800 — retained_bytes() { return commands_ != nullptr ? commands_->transients.arena_size : 0; }
    and that is the only quantity fed to it ✔ exactly two call sites in the file: :685 budget_.note(graph.uid, cached_program->retained_bytes()) and :854 budget_.note(keep->uid(), keep->retained_bytes()) — nothing else calls budget_.note
    trim clears what the budget never counted ✔ ggml/src/ggml-hrx/runtime/graph-program-cache-limit.cpp:63-64 — evict(victims); context.prepared_programs.clear();
    the header states the opposite expectation ✔ ggml/src/ggml-hrx/runtime/graph-program-budget.h:18-25 — "about 1 GiB for a large prefill shape", and the default 8192 MiB is justified there as "a 27B model's hot shapes plus another model's fit"

    Path correction so nobody hunts for it: the issue's graph-program-cache.cpp / graph-program-budget.h / graph-program-cache-limit.cpp are all under ggml/src/ggml-hrx/runtime/. Your line numbers are exact.

    The arithmetic that makes it stronger than reported

    Your trim line accounts 1031 MiB for 66 programs ≈ 16 MiB each. With the count bound at GGML_HRX_GRAPH_PROGRAM_CACHE=64, the maximum the byte budget can ever see at this geometry is

    64 programs × ~16 MiB  ≈  1 GiB   against a default budget of 8192 MiB
    

    so the 8192 MiB default is about 8× larger than the highest number the count bound allows it to reach. The byte budget is not merely ineffective at realistic sizes, it is unreachable by construction while (a) the count bound is 64 and (b) per-program arenas stay ~16 MiB — which is why you saw the count trip and the budget never fire. Two consequences worth writing into the issue: the documented rationale for the 8192 default (graph-program-budget.h:22-24) is off by roughly that factor, and every operator who lowers ..._CACHE_MB to rein in a server is adjusting a knob whose threshold sits far above the ceiling the other knob imposes.

    Also: both call sites have the blind spot — retain_only() at :854 re-notes the same retained_bytes(), so a fix that only touches the trim path leaves the retain path under-counting. Whatever is decided, it has to be inside retained_bytes() (or a second quantity threaded to both sites).

    Suggested direction, with the cheap half named

    The minimal change that makes budget and trim agree by construction: count the prepared command programs in retained_bytes() — prepared_programs.clear() is exactly what a trim frees (graph-program-cache-limit.cpp:64), so the quantity the budget watches becomes the quantity the trim releases. The graph-retained buffers are the larger and harder half and can be a second step (or a stated exclusion), because the point that has to stop being wrong first is "the number the knob watches is not the number the trim frees".

    Whichever way it goes, the doc line is mandatory: either the knob counts what it releases, or it is renamed/documented as an arena-only bound with the count bound named as the real lever (graph-program-cache-limit.h:24-27 currently presents the count as the weaker one).

    Next action, one: add the prepared programs to retained_bytes(), then re-run your own arm B and re-read the same trim line — the assertion is that "MiB of arenas" becomes a figure of the same order as the GTT growth you measured (≥ a few hundred MiB per shape, not 16). Closes when the byte budget is the bound that bites for at least one realistic shape set, or when the knob is renamed and documented as arena-only.

    Links: #840 (same lane, same pin — the sibling gap on the APU side), #669/#674 (the residency accounting this cache growth sits under), #680 (the GTT ceiling the trim exists to keep clear).

  3. bong-water-water-bong commented on Nov 3, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Second component of the per-shape growth, measured at the pin (d8bd0ff60), 2026-11-02 21:11-21:18 AST:

    Each cached graph program also retains a KV-cache staging set that retained_bytes() cannot see either.

    • With GGML_HRX_HOST_STAGING_TRACE=1 at 4B / -ngl 0 -c 4096 -fit off -n 8 (trio), the executor prepares 181 programs
      and allocates 686 staging buffers; exactly 144 of them are 8 388 608 B, in 36 programs, 4 per program.
      8 388 608 = n_head_kv(8) x key_length(128) x 2 B x n_ctx(4096) = one K or V cache tensor of one layer.
    • The amdgpu_bo_create tracepoint shows the same 144 x 8 MiB as device BOs (VRAM_WIPE_ON_RELEASE), i.e. the staging
      buffers are real GTT, plus a coherent sibling class of the same size for the write-back.
    • Naming: the executor's own dump (GGML_HRX_DUMP_COMMAND_PROGRAM_DIR) shows the only bindings of length 8 388 608 are
      named cache, access ReadWrite — the KV cache, not a norm or a transient.
    • Retention is per prepared program, not per layer: dumping disables structural reuse (graph-program-cache.cpp:711), the
      program count goes 181 → 362, and the plain 8 MiB class doubles 144 → 288 while the coherent class stays 145. Moving
      -ub 512 → 64 at the same ctx does not move the class at all.
    • So each cached shape carries 4 x 8 MiB = 32 MiB of KV staging at 4B/ctx 4096 (64 x 32 MiB = 2 GiB at the 64-program
      bound), and again none of it is in retained_bytes() (transient arena only, graph-program-cache.cpp:798).

    Two smaller observations from the same arms:

    1. Two of the four per program are staged with upload=0 download=0 — allocate_host_staging_buffer is called before the
      access flags are consulted (command-program-executor.cpp:636-657), so ~16 MiB per program is allocated and never
      copied to or from.
    2. With -ctk q8_0 -ctv q8_0 the whole class disappears (the 8 MiB coherent class drops to its single bring-up BO),
      i.e. the staging only happens for the f16 KV path. Mechanism not chased; recorded because it is the cheapest way to
      see the class vanish.

    Evidence (raw rows, dumps and the three arm logs) is in ~/amd-oss/hrx-study/evidence/memory/:
    bo-kvshape-20261102-2111.log, bo-kvshape-20261102-2114.log, bo-kvname-20261102-2118.log,
    dump-20261102-2118/. Method and numbers will also land in FACTS-memory.md §7.12.

  4. bong-water-water-bong commented on Nov 3, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Fixed in llama.cpp#175 — the accounting change, with a before/after on this issue's own reproducer

    Since this is an engine issue and the code is in the fork, the PR (1bit-MONSTER/llama.cpp#175, branch
    1bit/i841-cache-bytes, head 3135dba6b67, MERGEABLE/CLEAN, base 1bit/hrx-vulkan-patched) cannot auto-close this
    issue - closing it is a human step once #175 is merged and the pin moves past it.

    Root cause, exactly as this issue's "Suggested direction" predicted: retained_bytes() returned
    transients.arena_size only. The bytes the budget could not see are the prepared command programs a cached program
    keeps alive - their host staging set and per-program constant buffers - and trim_graph_programs drops them on every
    trim, so it was accounting for exactly what it freed and nothing else. Confirmed at the pin with your reproducer:

    W trim_graph_programs: ... (1 of 66 programs, 1032 -> 1032 MiB of arenas, budget 8192 MiB, 64 max)
    

    1032 MiB across 66 programs = 15.6 MiB each against an 8192 MiB budget, while GTT had grown +11.5 GiB.

    Before/after (same tree, same binary, same env, both arms from one gpu-hold ticket; zaya1-8b, -ngl 99 -fit off -c 4096 -np 1, one request per new shape, cache_prompt=false), GGML_HRX_GRAPH_PROGRAM_CACHE_MB=2048:

    shape base #175
    load +6033 +6033
    64 / 128 +6569 / +7190 +6569 / +7190
    256 +8434 +7106
    384 +10622 +7528
    512 +12991 +7629
    768 +10493 +7713
    1024 +11480 +7828
    • The base at a 2048 MiB budget is byte-identical to the base at the default 8192 MiB - the knob was a silent
      no-op
      , which is the operator-facing claim in this issue, reproduced.
    • With the accounting fixed the budget bites from shape 256, the cache plateaus, and the peak is -3652 MiB
      (-32 %)
      , -5362 MiB (-41 %) at shape 512.
    • At the defaults the two builds behave the same, because 64 programs retain ~5.9 GiB and the count bound still
      trips first. What changes there is that the trim line reports the truth: 5903 -> 5903 MiB retained. So the honest
      scope: this makes the byte budget correct and load-bearing; it does not change which bound trips first at the
      default. If the byte knob should be the primary lever at realistic model sizes the default needs to come down (2048
      is where it starts to bite on an 8B/4k) - a default change I did not put in a fix PR.

    Now covered by a host test (tests/test-hrx-graph-program-budget.cpp, second commit 3135dba6b67): resize() must
    move the byte count without moving the LRU, and it is a no-op on an uncached uid. Verified by mutation - a copy whose
    resize() also bumps the recency counter makes the test abort at victims[0] == 1, so the check bites.

    Not attributed, said rather than implied: RecordedCommandGraph's retained buffer/executable handles carry no byte
    sizes, so that slice is still invisible to the budget (it is dropped by the same trim). Tracking it is a follow-up.

    Thanks for the two follow-up comments on this issue - the KV-staging measurement (144 x 8 MiB in 36 programs) is what
    made the fix's target obvious rather than a guess at the 1.2 GiB/shape.

  5. bong-water-water-bong commented on Nov 3, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    The "unattributed slice" caveat, closed: buffers must stay out of the budget (they are aliases), executables are upstream-blocked

    Follow-up to the PR body's honest-scope note ("RecordedCommandGraph's retained buffer/executable handles carry no byte
    sizes, so that slice stays unattributed"). I went back to see whether that slice can be attributed. Answer: for buffers it
    must not be, and for executables it cannot be yet. Details, because both are the kind of thing that gets "fixed" into a
    double count later:

    1. RecordedCommandGraph::retained_buffers are refcount aliases, not allocations. resolve_command_binding()
    (ggml/src/ggml-hrx/dispatch/command-program-resolver.cpp:18-71) produces a ResolvedBufferRef from exactly two places:

    • a concrete CommandProgramBindings entry — which is the model's device BO, or the prepared program's own host-staging /
      per-program constant buffer, or
    • transient_arena->buffer.

    retain_prepared_command_list_resources() then just hrx_buffer_retain()s those handles and records each once
    (command-program-executor.cpp:1515-1536). So every byte behind them is already owned and already counted by someone else:
    the model BO by the loader, staging and constants by PreparedCommandProgram::retained_bytes(), the arena by
    transients.arena_size. Summing hrx_buffer_get_size() over them would add the arena once per retaining program — 36
    programs = 36x — which is precisely the over-count that makes a byte budget stop meaning anything. Nothing to add; the
    right change is a comment at the declaration saying so.

    2. retained_executables has no size to read. libhrx's hrx_executable_export_info_t (hrx_runtime.h:288-295)
    carries name, flags, constant_byte_length, binding_count, parameter_count, workgroup_size — and there is no
    hrx_executable_get_size(). Compiled-kernel bytes are therefore invisible to any budget on this runtime until hrx-system
    grows an accessor. An upstream gap, not an omission in the fix.

    Both are recorded here rather than as a comment-only commit on the PR because that PR is sitting at a reviewed head
    (3135dba6b67, "READY" from bong-water-water-bong) and re-heading it for prose would throw away the SHA the review built.

    State of the fix

    • PR llama.cpp#175 (1bit/i841-cache-bytes @ 3135dba6b67) — the accounting fix + the resize() host test. Reviewed
      READY; no changes requested. Still MERGEABLE/CLEAN against 1bit/hrx-vulkan-patched as of 2026-11-03 08:0x.
    • Host test re-verified on the box after the 07:4x reboot at the same head, with a different compiler than the reviewer's
      (g++ 15.2 -std=gnu++23; reviewer used amdclang++/C++26): all checks passed, rc 0.
    • Not fixed by this issue, stated so nobody assumes otherwise: at the default budget (8192 MiB / 64 programs) the
      64-program count still trips first. The byte knob only becomes the binding bound when it is set below the real
      footprint (2048 MiB starts biting on 8B/4k). Changing that default is an owner decision and is not in the fix PR.
  6. bong-water-water-bong commented on Nov 4, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Status note (2026-11-04 17:2x AST): llama.cpp#175 is merged (c4948cc387, base head 428937ae). In a build of the
    merged tree I re-ran the PR's own host test in place — test-hrx-graph-program-budget: all checks passed — and confirmed
    the merged retained_bytes() (runtime/graph-program-cache.cpp:798-810) adds prepared_.retained_bytes() under
    prepared_mutex_, i.e. the retention a trim actually frees, as reviewed.

    Nothing here changes the issue's conclusion: the byte budget now prices what it frees, but at the shipped defaults
    (8192 MiB / 64 programs) the count bound still trips first, so the operator lever remains
    GGML_HRX_GRAPH_PROGRAM_CACHE. Making the byte knob primary would need a default change — recorded as an owner decision,
    deliberately not in the PR.

    Written up as §6.2/§7.21 of ~/amd-oss/hrx-study/FACTS-memory.md.

  7. bong-water-water-bong commented on Nov 5, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Measurement appended so this row's remaining half is only the default question: the byte lever now works, and it is
    a ceiling on retention rather than a footprint plateau.

    Isolation (GGML_HRX_GRAPH_PROGRAM_CACHE=0 = no count limit, so the byte budget is the only thing that can trim;
    plan_evictions: max_programs == 0 is unbounded), before/after pair from the same tree lineage differing only by
    #175 (verified by strings on the trim message: "… MiB of arenas …" vs "… MiB retained …"), zaya1-8B
    -ngl 99 -fit off -c 4096 -np 1, cache_prompt=false, six distinct prompt lengths (111/231/351/471/611/755 evaluated
    tokens, distinct content), no-SVM trio, own drm-total-gtt from /proc/<pid>/fdinfo:

    arm after load Δ after each of the 6 shapes trims peak own GTT
    before, default (8192 MiB / 64) +6035 +445 / +988 / +1613 / +2257 / +3527 / +4478 0 10 091 (last point 10 513)
    before, MB=1024, count off +6035 identical to the row above, to the MiB 0 10 513
    after, default +6035 identical to before-default 0 10 513
    after, MB=1024, count off +6035 +445 / +988 / +1019 / +1171 / +1021 / +1748 13 7 783
    1. Before #175 the knob was a silent no-op at a quarter of the observed growth: MB=1024 with no count bound is
      byte-identical to the 8192 MiB default. After, the same flags give 13 trims and −2 730 MiB of peak (−26 %).
    2. It bounds retention, not the footprint: all 13 trim lines read 1049 -> 1036 MiB retained, budget 1024 MiB, 0 max,
      i.e. retention held at ~1 020-1 040 MiB across 17-22 cached programs, and the last (755-token) shape still allocates
      +730 MiB on top of it. Growth is bounded by budget + the executing shape.
      (0 max in that line is max_programs == 0, the count bound switched off — not a bug.)
    3. A trim does not return the bytes to the driver: own GTT moves −150 MiB while a trim in the same window frees ~450 MiB
      of retention, which is what FACTS-memory §6.1 predicts (the libhrx pool keeps its slabs, max_count = UINT32_MAX,
      hrx_mem_pool_trim never called). Consistent, not proven — one request cannot separate the trim from the new shape.
    4. At the shipped defaults both builds behave the same, the count bound trips first, so the operator advice in the issue
      body stands. Making the byte knob primary is still a default change — owner decision, deliberately not in #175.

    Detail: ~/amd-oss/hrx-study/FACTS-memory.md §7.22 (DRAFT 17), evidence evidence/memory/gc4-20261105-0525/, script
    tools/scripts/mem-gc4.sh. Leaving this issue open only for that default decision.

  8. bong-water-water-bong commented on Nov 5, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Re-checked on the shipped tree (428937ae, llama-server built for this ticket), because everything this row quotes was
    measured at the pin. Same reproducer shape: 8B, -ngl 99 -fit off -c 4096 -np 1, cache_prompt=false, 8 growing prompt
    lengths, and — one method change — the server's own drm-total-gtt per reading rather than the box-wide counter, which
    this box contaminates with two other lanes' serves.

    shape (evaluated tokens) default bounds both bounds off
    load 6 035 MiB 6 035 MiB
    151 +107 +107
    351 +421 +421
    659 +1 051 +1 051
    851 +1 574 +1 574
    1 091 +513 (15 trims) +2 331
    1 427 +484 (28 trims) +2 948
    1. The lever is unchanged: bounds-off grows monotonically with zero trims; the default arm trims at 66 programs and every
      line still reads … budget 8192 MiB, 64 max. At the shipped defaults the count is still what bites.
    2. The per-shape constant is 4-5x lower than at the pin — first shape (program count cannot confound it): 183 tokens →
      +536 MiB at the pin vs 151 tokens → +107 MiB shipped (2.93 vs 0.71 MiB per token of the new shape); cumulative 1 392 vs
      369 MiB per cached shape. Candidates for the difference are named in FACTS-memory §7.25 and deliberately not attributed
      (#176 removes 2 of the 4 staging buffers per prepared program; the pin arm's box-wide sampling may also flatter it —
      its load delta still matched to 2 MiB).
    3. A skew the bound does not exploit, which is the actionable half of this comment. The first trims in the shipped run read
      1934 -> 1933 MiB retained and 1092 -> 1092, i.e. evicting one of 66 programs frees ~0-1 MiB, because retention is
      heavily skewed: most cached programs hold almost nothing and a few hold nearly everything. Under the count bound that is
      harmless (the bound is about how many shapes you keep). Under the byte bound it is not: with
      GGML_HRX_GRAPH_PROGRAM_CACHE=0 GGML_HRX_GRAPH_PROGRAM_CACHE_MB=1024 the observed trims were 1049 -> 1036, 1058 -> 626,
      1276 -> 830 — LRU sometimes frees 13 MiB, sometimes 448, i.e. it keeps evicting small old programs while the cache stays
      near budget.
      So a candidate improvement, offered as a decision and not as a patch: when eviction is triggered by bytes, choose by size
      (largest first) rather than strictly by age
      , keeping LRU for the count bound. It would make each trim actually move the
      number, and it would keep the expensive-to-rebuild large shapes only if they are also recent — which is the trade nobody has
      measured here (hit rate vs footprint). I have not filed it as its own issue; it belongs to this row's open default question.
  9. bong-water-water-bong commented on Nov 5, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Draft: engine#841 comment 4 — the hit-rate side, measured (2026-11-05 14:4x AST)

    Posted at:

    1. What I set out to measure, and the retraction I have to lead with

    My third comment here proposed that when eviction is triggered by the byte bound, the cache should evict
    largest-first instead of by age, and said plainly that the hit-rate side was unmeasured. That measurement ran today on
    this box (zaya1-8B, -ngl 99 -fit off -c 4096 -np 1, cache_prompt=false, count bound off via GGML_HRX_GRAPH_PROGRAM_CACHE=0
    so only the byte budget can trim, three prompt lengths repeated three times with identical text per length).

    First result is that my own first reading was wrong, and it is worth stating plainly because it is the trap in this
    experiment.
    A within-arm comparison (does cycle 2-3 get slower than cycle 1 in the thrashing arm?) says no — the
    repeats got faster (2.2x / 1.7x / 3.1x), even with 53 trims over 9 requests. I nearly wrote "discarding the programs costs
    nothing". A constant per-request tax and a warm-up ramp have the same within-arm signature (both rise cycle over cycle).
    The only way to see the tax is an arm that does not pay it, at the same time, in both orders:

    arm (run order) L1 563 tok L2 947 tok L3 1 379 tok
    _MB=64, ran first of the ticket 1 827 ms 878 1 234
    _MB=1024 1 447 628 909
    unbounded 466 611 867
    unbounded (2nd pass) 436 606 857
    _MB=1024 (2nd pass) 438 593 866
    _MB=64, ran last 636 815 1 150

    min-of-3 per arm/length, all cycles (the box was contaminated: individual prefills stretched to 18/25/76 s against a
    0.6-1.2 s baseline, so no means are used anywhere).

    2. The bound does cost hit rate — as a rate on every request, not a collapse

    With the order controlled, _MB=64 is +34-46 % of prefill (636/815/1 150 ms against 436/606/857 at best case in the
    pass where it ran last; the first pass's other two lengths agree at +44 %/+42 %), while _MB=1024 tracks the unbounded arm
    within 3 % in both positions. So:

    • the first-arm penalty (cold JIT cache) is real — the same arm's first prefill was 2 189 ms when it opened the ticket and
      1 010 ms when it ran last — and the tight arm is still slower when it runs last, so ordering does not explain its cost;
    • two candidates for the cost, which this ticket does not separate: re-materialising the evicted program, and the trim's own
      walk (53 trims x ~4 ms is the same order as the 200-300 ms measured). Both are consequences of the bound.

    The footprint half reproduces and is order-insensitive: _MB=64 ends +100 MiB over load in both passes,
    _MB=1024/unbounded +1 976 / +2 000 MiB in both.

    3. The important part: this design was degenerate for the policy question

    At 64 MiB the trim leaves 1 of 4 programs (3 of 4 programs, 364 -> 23 MiB retained, budget 64 MiB, 0 max), i.e.
    nothing is cacheable — so "LRU by age vs largest-first" is not observable there; the arm thrashes for every request
    regardless of policy. At 1 024 MiB almost nothing is evicted (2 trims, retention 1 033 of 1 024 MiB). Retention per program
    in this run is ~90 MiB, so the band where the policy question actually lives is a budget that holds some but not all
    of three shapes — roughly 100-400 MiB.

    So: the motivation for size-aware eviction (footprint) is sound and reproduces; the policy choice itself still has no
    measurement behind it, because neither end of the knob exercises it. The cheap decisive experiment is named — budget
    ~100-400 MiB, three shapes cycling, and check which program each trim drops relative to the next request — and I would rather
    say that than let a 64 MiB thrash stand in for it.

    4. What this changes for the default question

    Nothing about the shipped default yet: at 8 192 MiB / 64 programs the count bound still trips first, so this knob is not
    binding for operators today. What it does add is that when the byte bound binds it charges throughput on every request,
    which is an argument for keeping the default high unless the footprint is the constraint — the operator-facing advice in
    FACTS-memory §7.26/§7.27 and KNOBS.md.

    Raw evidence: evidence/memory/gc6-20261105-1350/ (first pass) and evidence/memory/gc7-20261105-1415/ (order-reversed),
    scripts tools/scripts/mem-gc6.sh / mem-gc7.sh, offline reader tools/gc7-parse.py.

  10. bong-water-water-bong commented on Nov 5, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    engine#841 comment 5 (posted) — retracting the +34-46 % prefill tax from comment 4

    Correcting my own number from comment 4. I wrote there that a byte budget too small to cache any shape costs
    +34-46 % of prefill, order-controlled. Two follow-up runs say that magnitude is wrong: the cost is ~9-11 % at
    best, and it is not separable from clock starvation on this box.

    Why the first number was too big: the original run compared arm positions in a ticket where individual prefills
    stretched to 18/25/76 s, and prefill here is clock-dominated — pp_dpm_sclk moved 728 → 2 320 MHz inside single
    arms
    . A fresh llama-server per arm cannot hold the clock still, so "tight ran last and was still slower" was a weak
    control, not a strong one.

    The two runs that replace it (8B, -ngl 99 -fit off -c 4096 -np 1, count bound off so only the byte bound can trim,
    three shapes 563/947/1379 tokens, cache_prompt=false):

    1. Six-budget dose-response (64/384/128/256/192/1024 MiB, interleaved, warm-up arm discarded). Clock-normalised, the
      two extremes are indistinguishable: 53 trims → 0.966/0.660/0.746 ms/token vs 18 trims → 0.936/0.671/0.691
      (L1/L2/L3) at ~2 100 MHz. The apparent 2x splits tracked sclk, not the budget.
    2. Paired A/B, six pairs, order alternating (A = tight 64 MiB, B = unbounded), counting a pair only when the two arms'
      clocks are within 150 MHz: pooled median ratio A/B = 1.11, mean 1.37, 5 of 7 pairs slower with the bound; without
      one 3.0x contention outlier (same clock, tight arm starved) the median is 1.09 and the mean 1.10.

    The limit, stated rather than hidden: the paired run achieved only 7 of 18 clock-matched requests, so it is
    underpowered for a ≤10 % effect, and pinning the clock is a box-config change I do not make on a shared host. So the
    honest statement is "about a tenth, wide uncertainty", not a precise figure.

    What got stronger, and it is the part the eviction-policy proposal actually needs. The footprint half reproduced
    byte-identically in all six pairs: tight ends +100 MiB over load with 17 trims, unbounded +1 880 MiB with 0
    trims — a deterministic 1.78 GiB at three shapes, across six fresh servers in one ticket. So the case for a
    size-aware eviction (or for lowering the default) rests on footprint, which is reproducible, and not on a throughput
    argument, which is not.

    Method note for anyone repeating this: record pp_dpm_sclk per request, compare only clock-matched pairs, and never
    compare two arms measured at different clock states — the effect you are chasing is smaller than the clock's own swing.

  11. bong-water-water-bong commented on Nov 6, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Resolved (accounting) and measured out of the policy proposal. Both halves were taken to the device:

    The accounting fix is shipped

    Fork #175 (c4948cc38, "count the prepared command programs in the graph-program cache byte budget") makes retained_bytes() include the prepared command programs and re-tallies before evicting. It is an ancestor of the engine's current pin — d70b67bf0, merged by #1233 (verified: c4948cc38 is 36 commits behind it), so the engine now has it. Verified at tip on strixhalo: at a 64 MiB budget the trim lines report real retention — 95 -> 68 MiB retained, budget 64 MiB, 104 -> 77 MiB retained (the kept executing program excluded), 14-19 trims over 9 requests. The budget bites at tens of MiB per program, not the old "16 MiB/program plus an unreachable 8192" picture.

    The eviction-policy half does not earn a PR

    The issue's size-aware proposal was implemented and measured (1bit/i841-cache-policy @ 231430f9d, pushed for the record only): byte-bound trims evict largest-first, the count bound keeps LRU, GGML_HRX_GRAPH_PROGRAM_EVICT=lru restores the old order, and the host test is extended. At 64 MiB it does fewer trims (14 vs 19) but leaves a slightly higher peak (+319 vs +313 MiB over load) and no throughput gain — so it was not opened as a PR.

    What the device measurement says about the premise

    At tip (strixhalo, zaya1-8B Q4_K_M, -ngl 99 -fit off -c 4096 -np 1, cache_prompt=false, shapes 552/958/1393 tokens, 3 cycles): unbounded growth saturates at ~0.32 GiB (peaks +321/+323/+323 MiB over load), not the campaign's 1.78 GiB; a 64 MiB budget gives +319/+319/+319 with 14 trims and trims do not return bytes to the driver (the libhrx pool keeps its slabs), consistent with this issue's own measurement note. Generated content is byte-identical across every arm; no separable budget effect on throughput (prompt_ms with pp_dpm_sclk 1.0-2.1 GHz per request, the cycle-2/3 speedup tracks sclk).

    The 1.78 GiB case did not reproduce here and was not attributed: candidates are the fork commits after 428937ae and/or this build's prebuilt hrx-system runtime (not the engine's pinned one). If that case matters for a real payload, re-running it against the pinned runtime + shipped binary is the next step — and the slab-retention behaviour is a runtime property, not a cache-accounting one.

    Closing on the accounting fix being shipped and the policy proposal measured out. The fork-side lane recommends no further change.

  12. bong-water-water-bong commented on Nov 7, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    The order question, measured: below ~192 MiB age is right, from 256 MiB up largest-first roughly halves the rebuilds, and they converge once the budget covers the cycle

    Follow-up to comments 3, 5 and 6. Comment 3 proposed that when the byte bound trips, the cache should evict
    largest-first instead of oldest-first, and said plainly that the hit-rate side was unmeasured; comment 5 retracted the
    throughput framing (prefill on this box is clock-dominated). So the question is decided by counting rebuilds, which
    needs (a) a workload that revisits shapes and (b) a log that says which order produced it. Both now exist:

    • GGML_HRX_GRAPH_PROGRAM_EVICT=size selects the byte-bound order (count bound stays oldest-first); unset is age, the
      shipped behaviour. The trim line now carries evict=<policy> builds=<N> hits=<M>, so each arm is self-describing and
      the count comes from the binary's own lifetime counters rather than from a clock. Fork PR llama.cpp#230.
    • workload: A B C / A B C / A B C over three prompt shapes (24/96/192 words = 111/471/1043 evaluated tokens), 8B
      -ngl 99 -fit off -c 4096 -np 1, cache_prompt=false, fresh server per arm, count bound off so only the byte
      bound can trim, own GTT from /proc/<pid>/fdinfo, every run inside gpu-hold, MemoryMax=24G (peak own-GTT 7.1 GiB).

    builds = graph programs rebuilt for the same nine requests (lower is better); trims/dropped are the policy's own
    work. One full 9-arm sweep, and a second sweep that confirms 256/384 before the 25-minute hold cut it:

    byte budget age: trims / dropped / builds size: trims / dropped / builds
    off (control) 0 / 0 / – (retention +1088 MiB over load, never returned) same arm
    128 MiB 20 / 119 / 121 154 / 161 / 194
    192 MiB 11 / 119 / 121 84 / 97 / 132
    256 MiB 9 / 115 / 118 40 / 50 / 88
    384 MiB 9 / 112 / 116 8 / 17 / 56
    512 MiB 3 / 28 / 59 7 / 10 / 51
    768 MiB 1 / 8 / 38 1 / 1 / 38

    Three things fall out of that shape, and they are the answer rather than a winner:

    1. age's rebuild count is flat in the budget until it is nearly big enough. 121 → 121 → 118 → 116 for 128 → 384 MiB:
      tripling the budget buys five rebuilds. That is the revisit-cycle pathology measured, not asserted: on an A B C
      cycle the oldest program is always the one about to be needed, so age evicts the next-needed program and spends the
      rest of the request rebuilding it. Extra bytes cannot help an order that does that.
    2. largest-first converts the same bytes into rebuilds. 194 → 132 → 88 → 56 across the same range, i.e. less
      than half of age's rebuilds at 384 MiB
      (56 vs 116) and 46 % fewer when the bound is below the working set at 256
      MiB (88 vs 118) — while returning more bytes per trim (244 MiB vs 132 MiB at 384) and touching fewer programs
      (17 dropped vs 112).
    3. they converge exactly once the budget covers the cycle, which the control arm sizes at 1088 MiB above load:
      at 768 MiB both orders are 38 builds / 1 trim, so the order stops mattering before the bound stops binding.

    The inversion at the tight end is real and is why the shipped default stays age: at 128 MiB size rebuilds 60 % more
    (194 vs 121) and at 192 MiB 9 % more. Mechanism: with a budget far below the cycle, dropping the largest frees room
    that the next shape fills with several small programs, and the following shape re-evicts them all — one big eviction
    becomes many re-admissions. Largest-first pays off exactly when the freed room is enough to keep the rest of the cycle
    resident, which is why its advantage grows monotonically from 256 to 384 MiB and vanishes at 768.

    What I would do with it: the default does not change (a policy that is worse below a fifth of the working set is not
    a safe default for an unknown workload), but any serve whose byte bound sits in the 256–512 MiB band — which is the band
    comment 3 measured at 15.6 MiB × 66 programs on an 8B/4k serve — should set GGML_HRX_GRAPH_PROGRAM_EVICT=size and
    expect roughly half the rebuilds for the same retention ceiling.

    Caveats stated rather than implied: one workload shape family (word-count-varied prompts, one model, one -c), one
    complete sweep plus a partial confirming sweep (builds/trims/dropped are identical between them where both ran, which is
    what makes single-sample rows quotable here), and no timing claim of any kind — comment 5 stands, this measures counts
    only. hits is reported by the same line but its total (builds + hits) varies with the budget, so I do not score on it.

  13. bong-water-water-bong commented on Nov 7, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Correction to my own table in comment 6713555438, made before anyone has to find it: the builds column is a snapshot, not an arm total. The trim line prints the cache's lifetime counters at the moment of the trim, so an arm whose last trim lands early reports a smaller number than it ended on. Per arm, with the lag from the last trim to the arm's final request:

    arm trims builds at last trim lag to arm end
    age 256 9 118 0.1 s (exact)
    age 384 9 116 0.1 s (exact)
    age 512 3 59 42.6 s (lower bound)
    age 768 1 38 74.7 s (lower bound)
    size 256 40 88 7.7 s (lower bound)
    size 384 8 56 15.8 s (lower bound)
    size 512 7 51 0.4 s (exact)
    size 768 1 38 27.3 s (lower bound)

    What that changes and what it does not:

    • The headline pair survives, and the direction is safe. The two rows the conclusion rests on are the exact ones: at 384 MiB age rebuilds 116 (exact) against size >= 56, and at 256 MiB age 118 (exact) against size >= 88. A lower bound on size's rebuilds can only widen the gap, so "largest-first roughly halves the rebuilds in the 256-384 MiB band" is if anything understated, not overstated.
    • One sentence was too strong and I am withdrawing it as written. I said the two orders "converge exactly" at 768 MiB. What the data shows is that each order trims exactly once there, at the same point and with the same counts (38 builds / 335 hits), and neither trims again - so the order stops mattering before the bound stops binding. It does not show the two arms ended on the same total, because that snapshot is 27-75 s early.
    • trims and dropped were never snapshots (they are greps over every trim line of the arm), so the "17 programs dropped vs 112 at 384 MiB" statement stands as published.
    • The "below ~192 MiB age is better" inversion is unaffected: those rows are exact (age at 128/192 was a separate 3-sweep stage whose counters were identical across sweeps) or bounded in age's favour the same way.

    The PR body (llama.cpp#230) now says this in the table caption, and age stays the default, so none of this moves shipped behaviour. If the number matters beyond a factor of two, the fix is one harness line - print the counters after the last request instead of relying on the last trim - which I will do if this row gets picked up again.

  14. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    The >= rows from my correction (6713999145) are now measured at arm end, and the answer moves the recommendation upward, not downward: largest-first's advantage survives at 512 MiB and is gone by 768.

    Method: TAIL=1 in the harness sends one request with a shape the passes never used, after a marker, so its build trips the byte bound again and the trim line printed there carries near-final counts instead of a snapshot. Same 8B, -ngl 99 -fit off -c 4096 -np 1, count bound off, three shapes × three passes, one extra 160-word shape at the end. Lag from the last trim to the arm's final request is now 0.2 / 0.9 / 0.2 / 0.3 s for the four arms — i.e. these are end-of-arm numbers, not snapshots.

    budget age: pre-tail trims / dropped / builds size: pre-tail trims / dropped / builds
    512 MiB 3 / 28 / 77 7 / 10 / 61
    768 MiB 1 / 8 / 56 1 / 1 / 55

    What changes:

    1. 512 MiB does not converge. Snapshot said 59 vs 51; the arm-end numbers are 77 vs 61, so age still rebuilds 26 % more at a 512 MiB budget. My recommendation was "set EVICT=size in the 256-512 MiB band"; the band is not bounded at 512 — the advantage is still there at 512 and only disappears by 768.
    2. 768 MiB is where they converge, and now that sentence is earned rather than asserted. Earlier I withdrew "they converge exactly at 768" because both 38s were early snapshots. Measured at arm end it is 56 vs 55 — one build apart, with both arms trimming once. So the withdrawn sentence can be restored in substance: the order stops mattering somewhere between 512 and 768, which brackets the control arm's 1088 MiB working set from below.
    3. The snapshots were low for both orders, roughly proportionally, which is why the direction of every earlier conclusion survived the correction while no absolute number did. Anyone quoting 59/51/38/38 from comment 6713555438 should replace them with the four numbers above.

    Caveat that stays: the 384 MiB row is still mixed — age 116 is exact, size >= 56 is a lower bound from the longer-running stage, so size's true 384 figure is probably a little higher than 56 (which would make the 384 gap a bit smaller than "2x", not larger). If that row matters, it is the same one-line change and one 2-arm ticket.

    The lever and the instrument are merged (llama.cpp#230, 5d4d960); the counters that the trim line prints are made lifetime-monotonic by llama.cpp#235. Neither changes a default.

  15. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    One more thing the arm-end numbers give, and it retires a caveat I put on my own table: builds + hits is exactly 1428 in every arm, for both policies, at every budget measured this way.

    age  512:  builds  77 + hits 1351 = 1428
    size 512:  builds  61 + hits 1367 = 1428
    age  768:  builds  56 + hits 1372 = 1428
    size 768:  builds  55 + hits 1373 = 1428
    

    That is the number of graph-program lookups the workload performs (ten requests: three shapes × three passes plus the tail shape), and it being constant across policies and budgets is the check that the two counters are complete and complementary rather than independently noisy — a rebuild is a lookup that missed, so for a fixed workload the pair must trade one-for-one, and it does to the unit.

    It also explains the thing I flagged in comment 6713555438 and then refused to score on: the earlier hits column appeared to swing from 335 to 1147 between arms. That spread was not behaviour, it was the same snapshot artefact as builds — the trim line prints the counters at the trim, so an arm whose last trim landed early reported a small hits too. At arm end the four arms sit within 22 counts of each other and sum identically. So hits is safe to quote now, with the same rule as builds: only from a trim near the arm's end.

    Corollary for anyone re-running this: builds + hits is a free self-check on whether a gc10 arm produced a usable reading at all. If the sum is not the workload's lookup count, the last trim was a snapshot and the row is a lower bound — which is how the 128/192/256 stage was certified exact and how the 512/768 rows failed certification until the tail was added.

  16. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Correcting my own corollary from 6714986812, and it matters: the builds + hits sum is a check, not an invariant. I wrote that it "is exactly 1428 in every arm... constant across policies and budgets". It is constant in the arms where the last trim lands at the arm's last lookup, and not in the ones where it lands early. Checking every stage's sums rather than only the four arms in front of me:

    stage complete (sum = workload lookups) short
    128/192/256 age128, age192, age256, size128, size192 → 1265 size256 → 1203 (−62)
    512/768 (arm-end) age512, size512, age768, size768 → 1428 —
    384 (arm-end) age384 131 + 1297 = 1428, size384 70 + 1358 = 1428 —

    So "the sum is constant" is exactly what a complete reading looks like, and a short sum means that arm's counters are still catching up — which is the useful half of the claim, and the half I should have written. Concretely it downgrades one published row: size256's 88 is a lower bound, because that arm's sum is 62 short of the workload's 1265 lookups.

    The 384 MiB row, measured at arm end (new):

    budget age: pre-tail trims / dropped / builds size: pre-tail trims / dropped / builds
    384 MiB 9 / 112 / 131 8 / 17 / 70

    Both sum to 1428, so this pair is complete and comparable: largest-first rebuilds 47 % less at 384 MiB (70 vs 131), and drops 17 programs instead of 112. The earlier 116 / ≥ 56 in comments 6713555438 and 6713999145 were both incomplete readings — the arm-end numbers are higher for both orders, and my prediction that the correction would shrink the 384 gap was right in direction (2.07× → 1.87×) and wrong in size (both counts moved a lot, not a little).

    That leaves one row outstanding: 256 MiB, where age 118 is complete and size >= 88 is not. Running that pair at arm end now. Current state of the table, complete rows only:

    128 MiB   age 121  vs  size 194     size worse
    192 MiB   age 121  vs  size 132     size worse
    256 MiB   age 118  vs  size >= 88   (incomplete row)
    384 MiB   age 131  vs  size  70     size -47%
    512 MiB   age  77  vs  size  61     size -21%
    768 MiB   age  56  vs  size  55     ~equal
    

    The recommendation narrows rather than disappears: the size order pays off from ~384 to ~512 and is a wash by 768; below 192 it is the wrong order. Anyone who quoted the earlier numbers from me should take these.

  17. bong-water-water-bong commented on Nov 8, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Last row closed: 256 MiB at arm end is age 133 vs size 119 (both sum to 1428, so both complete). The earlier reading of that row (118 vs ">= 88") had size short by 62 lookups; with a complete reading its advantage at 256 MiB is ~11 %, not the ~34 % the incomplete numbers implied.

    All six rows, and one methodological correction that matters for reading them:

    budget measured how age builds size builds size vs age
    128 MiB 9-request workload, complete (sum 1265) 121 194 +60 % worse
    192 MiB same, complete (sum 1265) 121 132 +9 % worse
    256 MiB arm-end + tail, complete (sum 1428) 133 119 −11 %
    384 MiB arm-end + tail, complete (sum 1428) 131 70 −47 %
    512 MiB arm-end + tail, complete (sum 1428) 77 61 −21 %
    768 MiB arm-end + tail, complete (sum 1428) 56 55 ~0

    The rows are not comparable across the two measurement styles, and I should say so before anyone subtracts them. The arm-end runs carry one extra request (a 160-word shape, sent after a marker so its build trips the byte bound and the trim line prints near-final counts); that request builds its own programs, so its counts include roughly +15 builds over the same arm without it. The check that the two styles agree in substance: the 256 MiB arm reads 118 on the 9-request workload and 133 with the tail — the same "~15" appears at 384 (116 → 131) and 512 (59 → 77). Compare within a row, never across rows.

    What the complete table says, and it is less clean than either of my earlier summaries:

    • largest-first is the wrong order below ~192 MiB (it rebuilds 9-60 % more);
    • it wins from 256 up, but not monotonically: −11 % at 256, −47 % at 384, −21 % at 512, ~0 at 768. The 384 MiB peak is one sample per arm and I am not going to invent a mechanism for it; the 512 and 768 runs are single samples too.
    • the one statement that survives every measurement style: it is a wash by 768 MiB (56 vs 55, one build apart), which is where the two orders stop trimming different amounts.

    So the recommendation that survives all the corrections is the modest one: GGML_HRX_GRAPH_PROGRAM_EVICT=size is worth setting for a serve whose byte budget sits in the 256-512 MiB band — expect a real but workload-dependent cut in rebuilds, up to roughly half around 384 MiB in this workload — and never below 192 MiB. Both PRs that make it available are merged (llama.cpp#230 5d4d960 for the lever and the trim-line counts, #235 6bf2162 for counters that cannot go backwards).

    Measurements in this thread: 9-request stage ×3 sweeps, arm-end stage for 512/768, 384 and 256 — every row above quotes a sum check, and rows that failed that check were re-run rather than averaged.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughrx

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions