Repository navigation
hrx: don't stage a binding that no command in the program references (engine #914) - #176
Conversation
…(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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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 thatcarries host data, because
requires_materialization()is justhost_data != nullptr. The upload/download decision,though, is derived from
collect_graph_value_access()(:519-576), which only records values that appear ininitialization_commandsorcommands. A value that no command references gets a default-constructedGraphValueAccess{}at:580-581—read = false, write = false— and is still allocated a staging buffer at:637,with
staging.upload = 0andstaging.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 codealready does when
allocate_host_staging_bufferfails (:638-643). Nothing reads or writes such a value, so there isnothing to stage and nothing to bind.
Deliberately not
if (!staging.upload && !staging.download): a value that a kernel writes and that is also agraph_inputhas 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=1plus theamdgpu_bo_createtracepoint; both arms back to back under onegpu-holdticket, base = the pin's staged build(
b0-d8bd0ff6052), fix = this branch (b1-d8bd0ff).upload=0 download=08388608 Bdevice BOs, plain (visible=512)8388608 Bdevice BOs, coherent (visible=8704)2097152 Bdevice BOs, plain (slab side effect)drm-total-gttOkay, the user is asking…Reproducibility, same box state, three arm-pairs in two tickets: peak
7679 -> 5789at-n 8twice and at-n 16,and the same creation-sum delta of exactly
-1890.0 MiBboth times (-n 16: 18 029.8 → 16 139.8 MiB, 2294 → 1992BOs). The saving decomposes exactly:
216 x 8 MiB = 1728 MiBof staging that is no longer allocated, plus81 x 2 MiB = 162 MiBof 2 MiB-class slabs those allocations had been pulling in — 1890 MiB, which is the whole of the differencein both accounting paths (bo_trace's creation sum and the process's own
drm-total-gttpeak).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 8both arms emitOkay, the user is asking; at-n 16both emit the same 12-token continuationOkay, the user is asking for the capital of France. I need.Scope and what is not claimed
-ngl 0, where the KV cache lives in host memory and therefore takes the staging path at all. At-ngl 99the same bindings are device-resident and are not staged, so this change does not apply there.ggml_rope_set_rows_f32/ggml_set_rowsprogram the commands bind only theReadWriteviews (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'sbinding list. Value ids are per program, so this is "the two 8 MiB bindings this shape does not reference", not a
fixed id.
materialize_host_bindingsis a static function that needs a live device and a realCommandProgram; 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.
clang-formatbinary 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.