Skip to content

hrx: a write retargeted to its external storage keeps its own offset once (engine #679) - #336

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/679-external-write-offset
Nov 13, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
cl-hrxmem/679-external-write-offset

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Refs engine ggml-org#679 (the QSA mask chain; item 4 of the lead's list, and cl-npuperf's hip.add_f16 audit).

The bug

storage_binding_target() (dispatch/command-program.cpp) moves a write through a Transient view onto the External value that covers the written range. That External is the split output the scheduler owns when the consumer runs on another backend. The retarget offset is

graph_value->storage_offset + binding_offset - external_target->storage_offset

so it already includes the binding's own offset. append_command() then did command_binding.offset += binding_target.offset, which counts the binding offset twice. Any binding with a non-zero offset on that path lands at twice its place.

Where it showed up

qwen4exp's sparse-attention mask with GGML_HRX_HIP_F16_FILL=1 and the mask ADD on the CPU. That makes the SET_ROWS result a split output, and the batched SET_ROWS writes one slice per query row at i02 * 5120.

  • Model. Flash-Next IQ3_XXS at 18.8k: the second prefill ubatch was refused with binding value=674 … range=[5232640, 5237760) is outside runtime binding length 2621440 (256 such ranges), then llama_decode returned -3. Window cl-hrxmem-679-fill-fn, 11-13 09:47Z.
  • Program dump. cl-npuperf's command-program dump showed the cache binding's slice offsets at 0, 10240, 20480 (GraphValue) against 0, 5120, 10240 (Transient) with the add also on HRX.
  • Matcher. A probe in match_hip_set_rows showed both arms hand over the same cache Value: nb=[2,2,5120,2621440]. The doubling happened after the matcher, in this lowering.
  • Decode. Decode has one slice at offset 0, so it never showed.

The change

The retarget marks its offset absolute, and the caller assigns it instead of adding it. The root-alias path (offset = the alias's offset into its root, to which the binding offset still adds) is unchanged.

Verified (Strix Halo gfx1151, gpu-hold tickets; add-on at gpu-kernels d37fa7a)

check before after
new hrx-backend-test external_write_retarget_offset (write at byte 128 of a Transient view an External view covers; expects offset 128) FAIL (mutant: this change reverted) PASS
cl-npuperf's maskchain, fill only, 512 2048 2560 / 512 2051 5120 / 1 2051 18944 aborted (binding overrun) identical to the CPU graph, 0 differ ×3
maskchain, fill+add identical identical ×3
maskchain, add only / neither SET_ROWS refused SET_ROWS refused, unchanged (that's the separate placement case)
hrx-backend-test, no add-on — host 86/86, device 13/13
hrx-backend-test, add-on compiled in 22 host fails (pre-existing) host 64/86, the same 22 names (diffed), device 13/13
test-backend-ops -b HRX0 SET_ROWS / CPY / GET_ROWS — 73/73, 65/65, 64/64

Not run: a model-level 18.8k arm with this build and GGML_HRX_HIP_F16_FILL=1. The windows needed for it were skipped twice (MemAvailable 79G and 72G after stopping coder-llm). That run is the model gate, so the flag's default isn't touched here.

🤖 Generated with Claude Code

…once (engine ggml-org#679)

storage_binding_target() moves a write through a transient view onto the
external value that covers the written range (the scheduler's split output,
when the consumer runs on another backend). That retarget offset already
folds in the binding's own offset (storage_offset + binding_offset -
external->storage_offset), but append_command() added it to
command_binding.offset again. Every binding with a non-zero offset on that
path landed at twice its place.

qwen4exp's sparse-attention mask hits it whenever hip.fill_f16 is on and the
mask ADD stays on the CPU: the batched SET_ROWS writes one slice per query
row at i02 * 5120, the second half of the slices went past the 2,621,440-byte
mask, and the 18.8k prefill's second ubatch was refused ("outside runtime
binding length"). Decode has one slice at offset 0 and never showed it.

The retarget now marks its offset absolute and the caller assigns it;
the root-alias path keeps adding its storage offset as before.
hrx-backend-test external_write_retarget_offset pins a write at byte 128 of
a transient view that an external view of the same storage covers: it
passes with this change and fails without it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at 1cb48cc — a double-applied offset, and the body states exactly which term was folded where.

The arithmetic, spelled out

"storage_binding_target() moves a write through a transient view onto the external value that covers the written range … That retarget offset already folds in the binding's own offset (storage_offset + binding_offset - external->storage_offset), but append_command() added it [again]"

That is the clearest statement of this bug class I have read: the offset is a sum of three terms computed in one place, and a second place adds one of them again. The fix is to make the fold happen once — which is the same rule as ggml-org#2271's "one resolution, shared", ggml-org#2333's single seam, and every "second set of rules" in this queue.

And it explains why it only bites in one configuration: "the scheduler's split output, when the consumer runs on another backend" — a retargeted write is the only path where the transient view exists, so a test that writes into a plain output never sees the double-add.

Two asks

  1. state the wrong-offset symptom — an offset that is too large by binding_offset writes to the wrong place: silent corruption of a neighbouring row, or an out-of-bounds access? The two are different severities and the fix's test depends on which: a corruption test needs a canary in the neighbour, an OOB test needs the guard to fire. One line;
  2. say whether the fold moved into a helper — if storage_binding_target() now returns the final offset and append_command() uses it verbatim, the class is closed; if the fix subtracts in append_command(), the subtraction is a second place that knows the formula, and the next edit to the first place desynchronises them. That distinction is the whole content of the fix and it should be visible in the body.

And the arm needs the configuration, not just the case

The bug requires a split output whose consumer is on another backend. A test that constructs the retarget by hand proves the arithmetic; a test that runs a graph that actually splits proves the wiring. If only the first exists, say so — it is the difference between testing the formula and testing that the scheduler produces it.

@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 1cb48cc

The arithmetic in the comment is the arithmetic in the code, and the arm asserts the one number that distinguishes the two behaviours.

-            target.offset = graph_value->storage_offset + binding_offset - external_target->storage_offset;
+            target.offset = graph_value->storage_offset + binding_offset - external_target->storage_offset;
+            target.absolute = true;
…
-        if (binding_target.offset > 0) {
+        if (binding_target.absolute) {
+            command_binding.offset = binding_target.offset;      // already includes binding_offset: assign
+        } else if (binding_target.offset > 0) {
             command_binding.offset += binding_target.offset;     // the alias path still adds

REQUIRE(output.offset == 128) is the assertion I would have asked for, and it is exact rather than relational: the bug makes it 256 (128 from the retarget plus the binding's own 128), so a test that checked "the write lands inside the binding" would pass at both values -- the binding is 2621440 bytes wide, which is why the real failure was a range overrun 256 times over rather than an immediate abort.

The two-path design is the right one and it is explicit: absolute defaults false, so the alias-into-storage-root case keeps its additive offset, and only the retarget sets it. That is exactly the distinction the comment states, and it is the reason a blanket "assign instead of add" would have broken the other path.

The symptom is measured where it happened, not reconstructed: "the second prefill ubatch was refused with binding value=674 … range=[5232640, 52…] outside runtime binding length 2621440 (256 such ranges), then llama_decode returned -3", and "decode has one slice at offset 0, so it never showed" -- which is the sentence that explains why this survived: the single-slice case is the one every earlier test used.

One thing bundled in this diff that the body does not name, and it is not wrong just unannounced: the SET_ROWS matcher now accepts q4_0 as well as q8_0 (output->type != GGML_TYPE_Q8_0 && output->type != GGML_TYPE_Q4_0), and the comment above it changed from "q8_0: the quantized KV cache" to "q8_0 / q4_0". If that is part of the same fix it belongs in the body; if it rode along, it needs its own line, because it widens what HRX claims to dispatch and the table's arms are all about the mask chain.

Not run: the new hrx-backend-test arm and the maskchain rows -- -DGGML_HRX=ON needs the private runtime, which this worktree does not have (same constraint as the other HRX PRs I reviewed tonight). So this verdict rests on the code, the arithmetic, and the assertion being the discriminating number rather than on my execution of it.

@bong-water-water-bong
bong-water-water-bong merged commit 128125e into 1bit/hrx-vulkan-patched Nov 13, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-hrxmem/679-external-write-offset branch November 13, 2026 12:38
@bong-water-water-bong

Copy link
Copy Markdown
Author

Ran it, and mutated it -- the arm passes on your head and fails on the bug

-DGGML_HRX=ON builds on this box after all (the reference tree at ~/amd-oss/hrx-system is older than the pin: its loomc/link_index.h has 0 of the 4 builder symbols), so I can now execute what I previously only read.

$ cmake -S . -B ~/.cache/scratch-prs2/bhrx2 -DGGML_HRX=ON \
        -DHRX_SOURCE_DIR=/home/bcloud/wt/prs2-review/third_party/hrx-system -DLLAMA_BUILD_TESTS=ON …
$ cmake --build … --target hrx-backend-test          BUILD-RC=0
$ hrx-backend-test external_write_retarget_offset
host: 1 case(s)
[PASS] external_write_retarget_offset
host: 1 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 1 total

$ <mutant: `command_binding.offset = binding_target.offset` -> `+=`, i.e. the bug this fixes>
[FAIL] external_write_retarget_offset
       tests/hrx-backend-test.cpp:1926: requirement failed: output.offset == 128

output.offset == 128 is exactly the assertion that separates the two behaviours -- the doubling gives 256 -- and the failure message names it rather than a generic mismatch. That is what I said in my verdict from reading; it is now measured.

Two things worth knowing, since they are the difference between a test that runs and one that does not:

  • the host tier runs without a device (device: skipped, no HRX devices available -- hrx_gpu_initialize(0) failed on the iree dylib path), so 85 of the arms are executable on any box with the toolchain;
  • and the whole host suite is 85/85 on your base (fdb…: host: 85 passed, 0 failed), so this arm is added to a green set rather than to a partly-skipped one.

Still not run by me: anything on the device tier (needs the iree library on LD_LIBRARY_PATH plus a gpu-hold ticket), and the maskchain rows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant