Skip to content

hrx: a KV-cache binding that no command in the shape references still gets an 8 MiB staging buffer — 576 MiB per 4B/ctx-4096 run allocated and never transferred #914

Closed

Description

@bong-water-water-bong

Symptom

In a plain -ngl 0 run of a 4B at -c 4096, half of the KV-sized staging buffers the executor allocates are never
uploaded and never downloaded
. They are bound to the program, occupy device memory (GTT) for the lifetime of the
cached shape, and carry no data in either direction.

Measurement (this box, pin d8bd0ff6052a, no-SVM trio, gpu-hold)

GGML_HRX_HOST_STAGING_TRACE=1, Qwen3-4B-Q4_K_M, -ngl 0 -fit off -c 4096 -n 8:

$ grep -o 'value [0-9]* [0-9]* B upload=[01] download=[01] mapped=[01]' <run>.err \
    | awk '$4==8388608' | sort | uniq -c
     72 value 7  8388608 B upload=0 download=0 mapped=0
     72 value 8  8388608 B upload=1 download=1 mapped=0
     36 value 12 8388608 B upload=0 download=0 mapped=0
     36 value 13 8388608 B upload=1 download=1 mapped=0
  • 144 staging buffers of 8 388 608 B in that run, in 36 prepared programs (4 per program: the K and V cache tensors bound
    in two views each).
  • Two of those four per program (values 7 and 12) are upload=0 download=0: 16 MiB per prepared program, 576 MiB
    per run
    of device memory that is allocated, bound, and never touched in either direction.
  • The per-batch summary line says the same thing arithmetically: 11 buffers, 33562664 B staged, 16785448 B uploaded, 16777216 B downloaded — the transfers account for exactly two of the four big buffers.
  • The amdgpu_bo_create histogram of the same run shows the device side of it: 8388608 B x 144 BOs, i.e. the
    never-transferred half is 72 BOs = 576 MiB of the run's 4705 MiB peak GTT.

Root cause

materialize_host_bindings (ggml/src/ggml-hrx/runtime/command-program-executor.cpp:569) decides per binding, and it
stages every binding that carries host data (requires_materialization() is host_data != nullptr) — regardless of
whether any command in the program refers to the value:

  • :519-576 collect_graph_value_access() builds access_by_value only from the bindings that appear in
    initialization_commands and commands, so a value that no command references has no entry.
  • :580-581 falls back to a default-constructed GraphValueAccess{} for it, i.e. read = false, write = false.
  • :637 then allocates the staging buffer anyway, and :646-647 sets staging.upload = access.read (false) and
    staging.download = access.write && !binding.graph_input (false).

So the two flags are the symptom; the cause is that "no command references this value" is not a reason to materialise it.

What the extra two buffers are, stated only as far as the dump proves it: they are 8 MiB, exactly the KV tensor size at
this shape (n_head_kv 8 x key_length 128 x 2 B x n_ctx 4096), and in the ggml_rope_set_rows_f32 / ggml_set_rows
program the commands bind only the ReadWrite views (program.json, command_shape:
b:cache:8:0:0:8388608:2 and b:cache:13:0:0:8388608:2). Values 7 and 12 appear in no command's binding list at all
— they are graph values that ggml hands to the program, so the executor stages them, but nothing in the program refers to
them. Value ids are per program, so the pair is "the two 8 MiB bindings this shape does not reference", not a fixed id.
(Both are host bindings because at -ngl 0 the KV cache stays in host memory — which is also why the staging path runs
at all.)

Note the guard is not upload == 0 && download == 0: a value that a kernel writes and that is also a graph input has
those flags too and must be device-writable. The safe test is "absent from access_by_value".

Expected

A binding that no command in the program references should be passed through unchanged, exactly as the code already does
when allocate_host_staging_buffer fails (:638-643). Nothing reads or writes that value, so no device copy is needed.

Impact

Not a correctness bug (the buffer is never used), but it makes every large-context shape cost 2 x n_kv_tensor_bytes
more GTT than it needs: 576 MiB at 4B/ctx 4096 for the run above, and, because a staging buffer is retained per cached
graph program, up to ~1 GiB at the 64-program bound. It also inflates what GGML_HRX_GRAPH_PROGRAM_CACHE_MB has to
account for (#841).

Fix and before/after numbers in the PR that closes this.

Activity

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

    @bong-water-water-bong
    CollaboratorAuthor

    Verified at the pin (third_party/llama.cpp = d8bd0ff6052a), and the code path is exactly as reported — with one addition that makes the fix cheaper than the report implies.

    ggml/src/ggml-hrx/runtime/command-program-executor.cpp:

    • :576 access_by_value = collect_graph_value_access(commands) — built only from the commands' bindings.
    • :579-581 a value with no entry gets a default-constructed GraphValueAccess{}, i.e. read=false, write=false.
    • :581-585 the early "keep it as-is" branch is binding.length == 0 || (!requires_materialization() && !materialize_weight) — note neither term can be true for an unreferenced binding that carries host data, so it cannot be skipped here.
    • :646 allocate_host_staging_buffer(...), :652-654 staging.upload = access.read, staging.download = access.write && !binding.graph_input.

    So the flags are the symptom and the allocation is the cost, as the report says. The addition: the missing condition is precisely found_access == access_by_value.end(), and it is available at :578 before anything is allocated — so the smallest fix is one extra term on the :583 skip ("and no command in this program references this value"), no allocator change and no change to the flag semantics. I agree with the report's warning that upload == 0 && download == 0 is not the predicate: a write-only graph input has download=false by construction at :654 and must still be materialised.

    Labels: bug + hrx. The file is the fork's, so this closes on a llama.cpp PR plus an engine pin bump — an A row that is pin-blocked, and it is now the fifth of them (#841, #894, #895, #900). That is the thing to say to the orchestrator: five A rows are queued behind one pin move, not behind review.

    I have recorded the 576 MiB/run on the board row with the report's own arithmetic (72 BOs of 8 388 608 B in a 4 705 MiB peak) so no lane budgets GTT without it.

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

    @bong-water-water-bong
    CollaboratorAuthor

    Merged and re-measured in the shipped tree (2026-11-04 17:2x AST): llama.cpp#176 landed as 6156b8232c, base head
    1bit/hrx-vulkan-patched @ 428937ae. Build of that tree with the pin's CMake configuration (~/wt/postmerge), one
    gpu-hold ticket, 4B Q4_K_M, -dev HRX0 -ngl 0 -c 4096 -fit off -n 8, no-SVM trio, amdgpu_bo_create tracepoint plus
    the executor's staging census in the same run, pin binary (d8bd0ff60) as the before arm:

    pin merged
    8 MiB staging buffers 432 216
    … upload=0 download=0 216 0
    BO creation sum 13 421.8 MiB 11 531.8 MiB (−1890.0)
    own peak drm-total-gtt 7679 MiB 5789 MiB (−1890)
    generated text, -n 8 greedy Okay, the user is asking identical

    Both accounting paths move by the same 1890.0 MiB, so this is the fix and not a sampling difference; the arithmetic
    closes as the PR said (216 × 8 MiB + 81 × 2 MiB of slabs the dropped allocations no longer pull in), the coherent sibling
    class is untouched, and every other BO class ≥ 2 MiB is identical.

    Also, the merged tree's own device-free test passes: test-hrx-graph-program-budget: all checks passed,
    test-hrx-arena-refusal: all cases ok (5 arms) — so nothing in the merge (which also carries other PRs) broke the code
    under review.

    Written up as §7.21(a) of ~/amd-oss/hrx-study/FACTS-memory.md; evidence
    ~/amd-oss/hrx-study/evidence/memory/postmerge-20261104-1721.log.

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

    @bong-water-water-bong
    CollaboratorAuthor

    Closing: fixed by 1bit-MONSTER/llama.cpp#176 (merged 2026-11-04 19:05Z, head 7cbbac1ac9), and the fix was
    re-verified in the merged tree (built 428937ae, its own test-hrx-graph-program-budget passes).

    Measured before/after on this box, Qwen3-4B, -ngl 99, one gpu-hold, staging census + amdgpu_bo_create class +
    own peak drm-total-gtt, generated text identical in every pair:

    base fix delta
    8 MiB staging buffers 432 216 −216
    … with upload=0 download=0 216 0 −216
    8388608 B plain BOs 432 216 −1728 MiB
    BO creation sum 13 421.8 MiB 11 531.8 MiB −1890.0 MiB
    own peak GTT 7 679 MiB 5 789 MiB −1890 MiB
    answer -n 8 / -n 16 Okay, the user is asking / …for the capital of France. I need identical —

    Three arm-pairs on two tickets: same peak to the MiB, delta exactly −1890.0 MiB twice
    (216 x 8 MiB + 81 x 2 MiB). The coherent 8 MiB sibling (8704 flags, 721 BOs) is untouched, which is the check that
    the guard is narrow. Detail: ~/amd-oss/hrx-study/FACTS-memory.md §7.13.

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