Skip to content

hrx: order the host copies with the compute dispatches (engine #696) - #167

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/ordered-host-copies
Nov 1, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/ordered-host-copies

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

hrx_synchronous_h2d/d2h submit on libhrx's separate transfer_queue with an empty wait list (hrx_hal_queue_transfer_and_wait passes iree_hal_semaphore_list_empty()) and then wait only for their own signal, while command programs complete asynchronously on the backend stream. buffer_set/buffer_get are exactly the cross-backend copies ggml_backend_sched inserts, so a copy could overtake the dispatch that produces or consumes those bytes.

The fix: the copies now go through the stream's command buffer (hrx_stream_copy_buffer on the device buffer stream, then hrx_stream_synchronize). Stream commands are recorded and submitted on the single dispatch queue, so they queue behind the dispatches by construction instead of relying on a device-wide stall - which is what hrx_stream_synchronize before every copy cost (6x on a 4B -ngl 0 run when it was measured in the staging path, see ggml-org#669).

The host-mapped staging buffer is kept per device and grown on demand, not allocated per copy: per-copy allocate/release is what libhrx's per-size allocator pools retain (engine ggml-org#669: ~6700 releases returned nothing, GTT sat at 7393 MiB with 61 MiB resident).

Built and verified on strixhalo (GGML_HRX=ON, gfx1151, amdclang from /opt/rocm-therock, no-SVM env trio): 0.6B -ngl 0 and 4B -ngl 8 both run, output matches the CPU reference word for word on the small model. PR-READY.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Verified on this branch alone (build from 4a58da9b0 + this patch only, GGML_HRX=ON, gfx1151, no-SVM env trio, one gpu-hold ticket):

arm result
Qwen3-0.6B-Q4_K_M -dev none (CPU reference), -c 256 --temp 0 -n 32 Okay, the user is asking about the capital of France. Let me think. I remember that France is a country in Europe, and its capital is
same, -dev HRX0 -ngl 0 byte-identical text
Qwen3-4B-Q4_K_M -ngl 8 -c 256 -n 32 runs, 19.5 t/s prompt / 19.0 t/s generation, GTT 43281M -> 48323M (+5041M)

No Compute error, no NaN logits, and the GTT delta is the same order as the unpatched pin (+4121M at -ngl 0), so the ordered copies do not buy memory either way - they remove a race.

…rg#696)

hrx_synchronous_h2d/d2h submit on libhrx's separate transfer_queue with an empty wait list
and then wait only for their own signal, while command programs complete asynchronously on
the backend stream. buffer_set/buffer_get are exactly the cross-backend copies
ggml_backend_sched inserts, so a copy could overtake the dispatch that produces or consumes
those bytes.

The copies now go through the stream's command buffer (hrx_stream_copy_buffer on the device
buffer stream, then synchronize): stream commands queue behind the dispatches on the single
dispatch queue, so ordering is by construction rather than by a device-wide stall. The
host-mapped staging buffer is kept per device and grown on demand instead of being
allocated per copy, because per-copy allocate/release is what libhrx's per-size pools retain
(engine ggml-org#669).

@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 24789594. Two files, +69 −2. No CI in this repo, so this is the review.

The change makes host copies (buffer_set / buffer_get) go through the stream instead of hrx_synchronous_h2d/d2h, so a copy cannot overtake the dispatch that produces or consumes those bytes. The reasoning in the comment is specific and checkable at the API level: the synchronous helpers submit on transfer_queue with an empty wait list, while hrx_stream_copy_buffer records into a stream command buffer that queues behind the dispatches on the one dispatch queue.

Read as code, the parts that would bite are handled:

  • per-device reusable staging (copy_staging / copy_staging_size / copy_staging_mapped) instead of per-copy allocate/release, which the comment ties to libhrx's per-size pools retaining (engine ggml-org#669) — and ensure_copy_staging only reallocates when copy_staging_size < size, releasing the old buffer after the new allocation succeeds, so a failed realloc leaves the previous staging intact rather than dangling;
  • released in the registration-context destructor, before buffer_stream;
  • mutex-guarded (buffer_stream_mutex) across allocation, memcpy and the stream copy, so two threads cannot share one staging buffer's contents;
  • the host_visible fast path (memcpy(context->base + off, …)) is untouched above the change, so direct-host bindings do not pay for this.

One consequence worth stating plainly rather than discovering in a profile: every upload and download now ends in hrx_stream_synchronize, i.e. it drains the whole stream, so a copy costs a full pipeline sync. That is the correct price for ordering (the alternative is the race this PR removes), but it makes any hot-loop buffer_get — per-token logit downloads, for instance — considerably more expensive than a transfer-queue copy was. If such a loop exists on the decode path, it deserves its own look rather than inheriting this.

What I could not do: compile or run it — ggml-hrx needs the HIP/HRX build and there is no HRX tree on this box after the rebuild. This verdict is a code review, not a build.

tprs-ready applied.

@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 merged commit f55e176 into 1bit/hrx-vulkan-patched Nov 1, 2026
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/ordered-host-copies 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