Repository navigation
hrx: order the host copies with the compute dispatches (engine #696) - #167
Conversation
|
Verified on this branch alone (build from
No |
2e6bc35 to
0545d1a
Compare
…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).
0545d1a to
2478959
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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) — andensure_copy_stagingonly reallocates whencopy_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,memcpyand the stream copy, so two threads cannot share one staging buffer's contents; - the
host_visiblefast 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.
hrx_synchronous_h2d/d2hsubmit on libhrx's separatetransfer_queuewith an empty wait list (hrx_hal_queue_transfer_and_waitpassesiree_hal_semaphore_list_empty()) and then wait only for their own signal, while command programs complete asynchronously on the backend stream.buffer_set/buffer_getare exactly the cross-backend copiesggml_backend_schedinserts, 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_bufferon the device buffer stream, thenhrx_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 whathrx_stream_synchronizebefore every copy cost (6x on a 4B-ngl 0run 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 0and 4B-ngl 8both run, output matches the CPU reference word for word on the small model. PR-READY.