Skip to content

hrx: hrx_synchronous_h2d/d2h are not ordered against the compute stream #696

Closed

Description

@bong-water-water-bong

Summary

hrx_hal_queue_transfer_and_wait (libhrx/src/libhrx/transfer.c) submits the upload/download on
device->transfer_queue with iree_hal_semaphore_list_empty() as its wait list, then waits only for its own
signal. Command programs complete asynchronously on the backend's stream, so a host transfer issued between two
graph programs can execute before the dispatch that produces or consumes those bytes.

ggml/src/ggml-hrx/ggml-hrx.cpp reaches that path from buffer_set / buffer_get — i.e. every cross-backend
copy ggml_backend_sched inserts — and from the HRX host-buffer memcpy branch, which has no ordering at all.

This is a latent data race on every HRX load with more than one split. It is not the cause of #674 (a
compute-stream synchronize before every buffer-level copy changed that output not at all), but it is the hazard
#674's body flagged in passing and left for its own issue.

Measured cost of the blunt fix

A full hrx_stream_synchronize before every buffer-level copy cost 6× on a 4B -ngl 0 run (5 s → 32 s,
measured while chasing #674; the patch was reverted for that reason). So ordering has to be semaphore-based,
not a full sync.

What a fix looks like

A wait semaphore derived from the stream's last submitted timepoint, or an explicit device-level barrier before
the transfer, so that:

  1. a h2d copy issued after a dispatch is ordered behind it (writes land after the producer's write),
  2. a d2h copy is ordered behind the dispatch that produces the data,
  3. the HRX host-buffer memcpy branch gets the same ordering as the BO path (today it has none).

Until then, correctness on multi-split loads rests on the per-graph synchronizes happening to be far enough apart
— which is true in the configurations we run today and is not a property the executor guarantees.

Why it is worth filing rather than appending to #674

#674 is about a specific wrong output (every weight op on the CPU), and the ordering hazard was exonerated
there: adding a compute-stream sync changed nothing, at 6× cost. That makes the hazard invisible to #674's
reproducer, which is exactly the kind of finding that gets lost inside another issue's negative-result section.
It has its own blast radius (any multi-split graph), its own test (a transfer issued between two dispatches), and
its own acceptance (byte-identical output with the sync removed from the fast path).

Reproduce / prove the fix without a wrong-output reproducer

  • Log ordering: HRX_PROFILE_MODE=dispatch bundle of a multi-split graph (-ngl 0 on a small model), then check
    whether any fill_buffer/copy_buffer record's start time precedes the producing dispatch's end on the
    compute timepoint. If a bundle shows ordering today it is because of an unrelated stall, not a wait.
  • Acceptance for a fix: the same bundle shows every transfer ordered behind its producer, and the 4B
    -ngl 0 end-to-end time is not the 6× of the blunt sync (i.e. the semaphore is not a barrier), and output
    is byte-identical to -dev none.

Related

Source

Drafted by the pi-i669 lane in ~/briefs/TISSUES-669-STATE.md ("ISSUE-DRAFT — transfer-queue ordering hazard"),
filed by the issue keeper unchanged; the lane's wording is its own analysis.

Activity

  1. added
    perf
    hrx
    on Oct 31, 2026
  2. bong-water-water-bong commented on Nov 1, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Status after the 15:53Z fork merges — same bookkeeping as #674, so this does not close on the merge alone.

    • fix: fork #167 → f55e176b5 (host copies ordered with the compute dispatches) on 1bit/hrx-vulkan-patched
    • engine main pins third_party/llama.cpp = 4a58da9b0, 8 commits behind the fork tip 199165047
    • the acceptance here — transfers ordered, byte-identical to -dev none, and not the blunt full sync's 6× (5 s → 32 s) — needs an HRX run at a build that contains the fix

    Closes on the pin bump to 199165047 plus that run. Frozen until the post-reboot all-clear.

  3. bong-water-water-bong commented on Nov 2, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    EVIDENCE — #696 closeable: the host copies are ordered and cost ~2.5 s, not 32 s, and the output is byte-identical

    # by what makes it closed
    696 fork PR #167 → f55e176b5f3, live at the pin 1d32c22609d6 Qwen3-4B -ngl 0 at the pin is byte-identical to -dev none and takes 2.45 s, i.e. it does not carry the ~6x cost of the blunt per-copy hrx_stream_synchronize (5 s → 32 s) that the issue measured as the reason a naive fix was unacceptable

    Measured tonight, one gpu-hold ticket, #715 trio on, -fit off -c 256 --temp 0 -n 8, pin 1d32c22609d6
    (GGML_HRX_169_DECLINE=none, i.e. the pin's own placement):

    arm rc wall generated text
    -dev HRX0 -ngl 0 0 2454 ms Okay, the user is asking
    -dev none 0 954 ms Okay, the user is asking
    byte-identity - - YES, md5 7edd7f2d2c0651a1820a78a5ef61c4e6 on both sides

    The 2.5 s against the CPU's 0.95 s is the normal cross-backend copy cost on this box; the number that matters for the
    close is that it is not the 32 s of the blunt fix, and the text is identical, so the ordering did not have to be
    traded against correctness.

    The one caveat worth keeping in the close: #167 fixes buffer_set/buffer_get (the ordered host copy), and a
    different unordered path remains - buffer_get's host-mapped branch is a bare memcpy, and a read there is not
    ordered against an in-flight dispatch. I built that experiment (1bit/i674-hostread-ordered, 1513e4e8c74) and it is
    not the cause of #674, so it is a latent hazard rather than an open defect; it is written into #674's thread, not
    this one.

    Lane pi-i669.

  4. bong-water-water-bong commented on Nov 2, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Closed on the evidence comment above — it carries the two numbers this issue's acceptance names, and both hold.

    Fix: fork #167 → f55e176b5, live in the engine at the pin 1d32c22609d6 (bumped in #786 → 7ba14cde9).

    The acceptance was "transfers ordered, not 6× (5 s → 32 s), byte-identical to -dev none":

    arm (pin 1d32c22609d6, Qwen3-4B -ngl 0, one gpu-hold ticket, #715 trio, -fit off -c 256 --temp 0 -n 8) rc wall text
    -dev HRX0 -ngl 0 0 2454 ms Okay, the user is asking
    -dev none 0 954 ms Okay, the user is asking
    byte-identity — — identical, md5 7edd7f2d2c0651a1820a78a5ef61c4e6 on both sides

    So the ordering did not have to be bought with the blunt per-copy hrx_stream_synchronize, whose 6× cost (5 s → 32 s) is what made the naive fix unacceptable in the first place. The 2.45 s against the CPU's 0.95 s is ordinary cross-backend copy cost on this box, not a synchronisation tax.

    Caveat carried into the record, not dropped: #167 fixes buffer_set/buffer_get's ordered host copy. A different path is still unordered — buffer_get's host-mapped branch is a bare memcpy with no ordering against an in-flight dispatch. The lane built that experiment (1bit/i674-hostread-ordered, 1513e4e8c74) and showed it is not the cause of #674, so it is a latent hazard rather than an open defect; it lives in #674's thread, which is the right place for it.

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

    hrxperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions