Skip to content

hrx: don't stage a binding that no command in the program references (engine #914) - #176

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/i914-skip-unreferenced-staging
Nov 4, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/i914-skip-unreferenced-staging

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

hrx: don't stage a binding that no command in the program references (engine ggml-org#914)

Root cause

materialize_host_bindings (ggml/src/ggml-hrx/runtime/command-program-executor.cpp:569) stages every binding that
carries host data, because requires_materialization() is just host_data != nullptr. The upload/download decision,
though, is derived from collect_graph_value_access() (:519-576), which only records values that appear in
initialization_commands or commands. A value that no command references gets a default-constructed
GraphValueAccess{} at :580-581 — read = false, write = false — and is still allocated a staging buffer at :637,
with staging.upload = 0 and staging.download = 0 (:646-647).

So those buffers are allocated, bound into the program, keep device memory for as long as the prepared program is
cached, and carry nothing in either direction.

The fix

One guard: if the value is absent from access_by_value, pass the binding through unchanged — the same thing the code
already does when allocate_host_staging_buffer fails (:638-643). Nothing reads or writes such a value, so there is
nothing to stage and nothing to bind.

Deliberately not if (!staging.upload && !staging.download): a value that a kernel writes and that is also a
graph_input has those same two flags and must stay device-writable. The safe test is "no command references it".

Before / after

Qwen3-4B-Q4_K_M, -ngl 0 -fit off -c 4096, no-SVM trio, GGML_HRX_HOST_STAGING_TRACE=1 plus the
amdgpu_bo_create tracepoint; both arms back to back under one gpu-hold ticket, base = the pin's staged build
(b0-d8bd0ff6052), fix = this branch (b1-d8bd0ff).

base fix delta
8 MiB host staging buffers 432 216 −216
… of which upload=0 download=0 216 0 −216
8388608 B device BOs, plain (visible=512) 432 216 −1728 MiB
8388608 B device BOs, coherent (visible=8704) 721 721 0
2097152 B device BOs, plain (slab side effect) 148 67 −162 MiB
BO creation sum 13 421.8 MiB 11 531.8 MiB −1890.0 MiB
own peak drm-total-gtt 7679 MiB 5789 MiB −1890 MiB
BO count 1717 1417 −300
greedy answer Okay, the user is asking… identical —

Reproducibility, same box state, three arm-pairs in two tickets: peak 7679 -> 5789 at -n 8 twice and at -n 16,
and the same creation-sum delta of exactly -1890.0 MiB both times (-n 16: 18 029.8 → 16 139.8 MiB, 2294 → 1992
BOs). The saving decomposes exactly: 216 x 8 MiB = 1728 MiB of staging that is no longer allocated, plus 81 x 2 MiB = 162 MiB of 2 MiB-class slabs those allocations had been pulling in — 1890 MiB, which is the whole of the difference
in both accounting paths (bo_trace's creation sum and the process's own drm-total-gtt peak).

Answer identity was checked on the generated text (not on stdout: the trace summary shares that stream — a first
version of the harness did exactly that and reported a false "DIFFERS"): at -n 8 both arms emit
Okay, the user is asking; at -n 16 both emit the same 12-token continuation Okay, the user is asking for the capital of France. I need.

Scope and what is not claimed

  • Measured at -ngl 0, where the KV cache lives in host memory and therefore takes the staging path at all. At
    -ngl 99 the same bindings are device-resident and are not staged, so this change does not apply there.
  • The two never-transferred buffers are 8 MiB, exactly the KV tensor size at this shape; in the
    ggml_rope_set_rows_f32 / ggml_set_rows program the commands bind only the ReadWrite views (command_shape:
    b:cache:8:0:0:8388608:2, b:cache:13:0:0:8388608:2) and the two other 8 MiB graph values appear in no command's
    binding list. Value ids are per program, so this is "the two 8 MiB bindings this shape does not reference", not a
    fixed id.
  • No throughput claim: other lanes serve on the same GPU and per-request t/s swings by 2x within one arm.
  • No regression test: materialize_host_bindings is a static function that needs a live device and a real
    CommandProgram; the alternative was a multi-page harness for a six-line guard. The reproducer above is the proof,
    as it was for hrx: make the host-weight residency budget mean the budget (engine #788) #172/hrx: refuse to run when the residency budget is below the working set (engine #788) #173.
  • No clang-format binary on this box today, so formatting was checked by hand against .clang-format
    (ColumnLimit: 120, IndentWidth: 4); the added lines are 118 and 75 characters.

Closes ggml-org#914.

…(engine ggml-org#914)

materialize_host_bindings() staged every binding that carries host data, because requires_materialization() is just
host_data != nullptr. The upload/download decision, however, comes from collect_graph_value_access(), which only
records values that appear in the program's command lists: a value that no command references falls back to a
default-constructed GraphValueAccess (read = false, write = false) and was still given a staging buffer, which is then
neither uploaded nor downloaded.

Measured on this box at the pin (Qwen3-4B-Q4_K_M, -ngl 0 -fit off -c 4096, no-SVM trio,
GGML_HRX_HOST_STAGING_TRACE=1 plus the amdgpu_bo_create tracepoint): 432 staging buffers of 8388608 B, of which 216
report upload=0 download=0 -- the two 8 MiB bindings of each prepared program that its commands never reference. 1728
MiB of staging, plus 162 MiB of 2 MiB-class slabs those allocations no longer pull in: 1890 MiB of a 7679 MiB peak GTT.

Pass such a binding through unchanged, exactly as the allocation-failure path already does. The test is "absent from
access_by_value", not "upload == 0 && download == 0": a value that a kernel writes and that is also a graph input has
both flags zero and must stay device-writable.
@bong-water-water-bong

Copy link
Copy Markdown
Author

#176 ready for review at 7cbbac1

@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 7cbbac1ac. Built both arms here and reproduced the staging counts exactly; the generated text is
identical.

The guard, measured on this box. HRX build at this head (~/wt/tprs-176, amdclang, hrx-system from
~/wt/hl-engine/third_party/, libggml-hrx.so sha256 a6110fb4…), Qwen3-4B-Q4_K_M, -ngl 0 -c 4096 -n 8,
GGML_HRX_HOST_STAGING_TRACE=1, each arm under its own gpu-hold -l "pi prs" ticket. Base arm = the same tree with
only ggml/src/ggml-hrx/runtime/command-program-executor.cpp reverted to 1bit/hrx-vulkan-patched (sha256
28cd03ab…, a 6-line difference) and relinked.

                                   base            this head
8 MiB host staging buffers          432                 216
  … of which upload=0 download=0    216                   0
staging summaries                   542                 542   (identical count: no program gained or lost one)
generated continuation              "Okay, the user is asking"   identical in both roots
rc                                  0                   0

So the claim is right in both directions: the fix removes exactly the zero-transfer 8 MiB buffers (216 → 0) and
nothing else moves (the per-program summary count is unchanged, and the answer text is byte-identical). 216 × 8 MiB
= 1728 MiB of staging, which is where that part of the PR's 1890 MiB delta comes from; the remaining 162 MiB is the
2 MiB slab class it names, which I did not measure separately.

One caveat about the PR's GTT row, not a disagreement: mem_info_gtt_used is device-wide on this box, so on a
shared night it also counts whatever other lanes are mapping (my base-arm sample read 39.8 GiB, against a 2.5 GB
model). The per-process staging counts above are the clean signal; the 7679 → 5789 MiB "own peak" is the lane's own
accounting and I did not try to reproduce it here.

Why I accept the code as well as the numbers. The guard is if (found_access == access_by_value.end()) and
collect_graph_value_access() only creates entries while walking initialization_commands/commands
(:519-576), so "absent" is exactly "no command in this program references this value" — not a heuristic on
host_data. The pass-through is the same shape the function already uses for host_data == nullptr and for the
two allocation-failure paths, so a binding with host data but no staging is a case the rest of the executor already
handles. The note about not testing !upload && !download is the important one and it is correct: a write-only
graph input has both flags false and must stay device-writable, while a value no command mentions has neither.

Nits, neither a gate: materialized.reserve() is still the only sizing and the growing vector now takes one more
push per program (irrelevant, noting it because the file is a hot path); and the comment cites engine ggml-org#914 inside
ggml-hrx, which is right for us but will look odd if this ever goes upstream.

@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 4, 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.

READY.

ggml/src/ggml-hrx/runtime/command-program-executor.cpp @ pr/176 (7cbbac1ac): rc=0

Read of the new guard, which is what matters here: passing the binding through unchanged is not a new code path — it is the same thing every other "cannot stage this" branch in materialize_host_bindings() already does (binding.length == 0, no requires_materialization()/materialize_weight, weight layout conflict, failed acquire, failed staging allocation all materialized.push_back(binding); continue;). So the binding still reaches the program with its own buffer/offset; only the staging buffer and its host_staging entry are skipped, and staging.upload/download are never read for a value no command references.

The test chosen is also the right one: access_by_value is built from initialization_commands and commands (append_command_list_access on both), so "absent from the map" really is "no command in the program references it", and the deliberate rejection of if (!upload && !download) avoids the graph-input case the comment names.

Not re-derived here: the 432→216 staging buffers / −1890 MiB. That needs a built HRX binary and a GPU ticket; the compile and the invariant are what I can check.

@bong-water-water-bong
bong-water-water-bong merged commit 6156b82 into 1bit/hrx-vulkan-patched Nov 4, 2026
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/i914-skip-unreferenced-staging branch November 12, 2026 21:16
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