Repository navigation
hrx: standalone SET_ROWS dispatch for the QSA mask rebuild (engine #679) - #196
Conversation
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY on the code — both new TUs compile for gfx1151, and the matcher's index contract is the CPU reference's own
asserts. The 59/59 device row is the attn lane's.
Reviewed at 658b99492.
What I ran myself:
$ amdclang++ -x hip --offload-arch=gfx1151 -O3 --rocm-device-lib-path=... \
-c ggml/src/ggml-hrx/hip/kernels/hip_set_rows.hip # rc 0
$ amdclang++ <recorded GGML_HRX=ON flags> -c ggml/src/ggml-hrx/hip/dispatch-hip-set-rows.cpp # rc 0, 0 warnings
$ g++ -O2 -std=c++17 -o /tmp/set_rows_check ~/wt/hrx-probe/set_rows_check.cpp && /tmp/set_rows_check
306 cases, 0 failures -> OK
That last one is the lane's own host reference, re-run by me; it reproduces the kernel's thread/packet mapping and index
decode and diffs it against a serial transcription of ggml_compute_forward_set_rows_impl. I then diffed the probe's
mapping against the kernel body line by line (token/packet decode, i32/i64 index load, the range skip, both
*_offset expressions, the packet_width loop) — they are statement-for-statement identical, so the 306 cases are
evidence about this code and not about a predecessor.
The matcher's assumptions are the operator's own. I read ggml_set_rows() on the pin
(ggml/src/ggml.c:3961-3969): a->ne[2] == b->ne[2], a->ne[3] == b->ne[3], b->ne[1] == c->ne[0],
b->ne[2] % c->ne[1] == 0, b->ne[3] % c->ne[2] == 0, c->ne[3] == 1. With a = the cache, b = rows, c = indices,
that makes the matcher's un-moduloed rows->nb[2]/nb[3] stepping legal (rows and cache batch together), and its
slices2 = cache->ne[2] % indices->ne[1], slices3 = cache->ne[3] % indices->ne[2] are then always 0 — the same
broadcast the CPU impl computes with i02 % ne11, i03 % ne12. So the slicing is not a new contract, it is the
existing one restated; and ne2/ne3 did not need separate guards, which is what I went looking for first (a matcher
that steps rows->nb[2] for an ne[2] == 1 rows tensor would read outside the slice it bound).
Also checked: the op is already in eager_capability_declared on the pin, so no hip_declare_eager_op is needed, and
priority 50 really is below common.set_rows_scatter (200) and common.set_rows (100) — a shape those two take can
never reach this matcher, so no existing backend choice moves. The add-on hook (hip_set_rows_hook) stays the path for
a quantized cache, as documented.
Not reproduced by me: test-backend-ops -o SET_ROWS -b HRX0 59/59 and the disabled-dispatch control (556 declines).
The build tree at 658b99492 did not survive the reboot and a fresh GGML_HRX=ON configure does not work on this box.
The device half rests on the attn lane's run at this head.
Nits:
hip/README.mdsays the host reference is "301 cases"; the probe on disk runs 306. The number is stale in the one
place a reviewer is told to look for the pre-device proof.kSetRowsMaxSlices = 64is checked ascache->ne[2] > kSetRowsMaxSlices / cache->ne[3], which is a slice count
bound, but the header comment lists it among "the kernel's 32-bit launch parameters" — the kernel has no slice
argument. Worth moving that constant out of the launch-limits paragraph.set_rows_packet_width()readsrows.storage_offset/cache.storage_offsetfor alignment but therowsbinding is
rows->storage_rootwith an explicit offset added later; those agree today because the offset chain starts at the
root, but the two halves of the alignment decision are computed from different objects.
…rg#679) Qwen3.8-Flash-Next's build_attn_qsa rebuilds a full-length mask once per full-attention layer per decode step: fill(-inf) over [1, n_kv], scatter the 2051 selected cells with ggml_set_rows, then add the causal mask. GGML_OP_SET_ROWS is in eager_capability_declared, but the two Loom matchers are narrower than that shape -- common.set_rows_scatter takes hidden == 1 with i64 indices and an f16 cache, common.set_rows takes i64 indices with hidden >= 4 and <= 2048 tokens -- so an i32-index scatter into an f32 cache was declined and ran on the CPU backend, with a graph split on each side of it. Measured model-free in ~/wt/hrx-probe/qsa_layer.cpp: the stage failed with "unsupported HRX node 44: SET_ROWS output=f32[1,18816]" while the indexer, hip.top_k_f32 and the FA decode split all took theirs. hip/kernels/hip_set_rows.hip is cache[indices[i], :] = rows[i, :] with f32/f16 on both sides, i32 or i64 indices, packed rows, and out-of-range indices skipped rather than faulted. One thread per (token, packet) with a 4-element packet when the rows are 16-byte aligned (else one element), so the element-wise hidden == 1 case costs one thread per selected cell. 10 VGPRs, 18 SGPRs, 0 spills, 0 scratch, no LDS, 16 waves/SIMD. hip/dispatch-hip-set-rows.cpp registers hip.set_rows at priority 50: below common.set_rows_scatter (200) and common.set_rows (100), and fused registrations are tried before every single-op one, so it only ever sees a node the existing kernels declined and no shape that runs on the device today changes backend. No hip_declare_eager_op and no extra bound in device_supports_op are needed, unlike TOP_K: the claim for a capability-listed op is decided by can_execute_standalone_op_as_graph, which runs the matcher, so a declined shape keeps its CPU fallback (engine #389). GGML_HRX_DISABLE_DISPATCH=hip.set_rows is the A/B switch. Dims 2 and 3 are sliced, not kernel-handled: ggml_set_rows batches over them with the indices broadcast as dst_dim % indices_dim (ggml-cpu/ops.cpp), so the matcher emits one dispatch per slice with its own binding byte offsets. That is what makes the 3-6-slice test-backend-ops cases reachable -- without it the suite has no case in this kernel's contract at all (HRX0 ran 3 of 559 SET_ROWS cases) -- while the model runs a single slice. Verification, in the order it was done: * host reference (~/wt/hrx-probe/set_rows_check.cpp, plain g++): transcribes the kernel's thread/packet mapping, index decode, four format combinations and the batched slice offsets, and diffs them against a serial transcription of ggml-cpu's set_rows -- 306 cases, 0 failures, including the QSA shape (hidden 1 / 2051 cells / 18,816 cache rows / i32), both index types, hidden 1..2560 with the odd widths that force the scalar packet path, five batched broadcast cases, and an out-of-range case that must write nothing. * device known answer (~/wt/hrx-probe/set_rows_ka.cpp): the same graph on HRX0 and on the CPU backend, 8 shapes, element by element -- 0 mismatches, hip.set_rows taken (GGML_HRX_LOG_DISPATCH). QSA shape at n_kv 18,816: 53.9 us device (rebuild + a 18,816 element add) against 57.5 us on the CPU. * test-backend-ops -o SET_ROWS -b HRX0: 59/59 with hip.set_rows taken, against 3/3 with GGML_HRX_DISABLE_DISPATCH=hip.set_rows, i.e. 56 cases that used to be "not supported [HRX0]" now run and match the CPU reference; the CPU backend itself is unchanged at 559/559. * the QSA probe's +rebuild and +fa stages now complete at n_kv 2048/4096/8192/18816 with the whole stage graph on HRX and GGML_SCHED_DEBUG=2 reporting 0 nodes on the CPU backend and 0 splits (before: two SET_ROWS nodes unsupported per layer). Not measured, and not claimed: the model row. Decode at 18.8k with this dispatch on/off (~/lb/tattn-setrows-ab.sh) needs the 76.33 GiB Flash-Next file at -c 19456, i.e. ~92 GiB MemAvailable, which this box has not had since 2026-10-31. The model-free numbers bound what the dispatch can be worth structurally (0 CPU nodes, 0 splits per QSA layer) but say nothing about tok/s.
658b994 to
684648c
Compare
|
Rebased onto |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Head 684648c69: no change in ggml/src/ggml-hrx/hip/ since my READY at 658b99492
(git diff 658b99492 684648c69 -- ggml/src/ggml-hrx/hip/ is empty), so the compile evidence carries: both new TUs
(hip_set_rows.hip, dispatch-hip-set-rows.cpp) build for gfx1151, the matcher's index contract matches the CPU
reference's own asserts, and the id/name/alias/registration set is consistent across the four files. The 59/59 device row
is the attn lane's run.
…d (engine ggml-org#2068) The test did not compile from #262 until #293, so host rows drifted from changes that were made on purpose. #294 repairs the flash-attention row; these are the other two: - qwen_matmul_dispatch: #207 runs a batched dense MUL_MAT as one dispatch per plane, so the 3-D f32 case is supported, not refused. - unmatched_graph_diagnostics: #196's standalone SET_ROWS takes the i32-index graph the row used as its unmatched example. The row now uses a row width above that dispatch's limit, which every matcher still declines. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…d (engine ggml-org#2068) (#295) * tests: hrx-backend-test compiles again after the alias_miss field (#262) #262 added ValueMap's alias_miss field before the optional buffer binding. Two aggregate initialisers in hrx-backend-test still passed std::nullopt in that position, so the test has not compiled since. They now pass AliasMiss::None. Co-Authored-By: Claude Fable 5.2 <[email protected]> * tests: hrx-backend-test expects the transposed V's spread pitch (engine ggml-org#1989) qwen_flash_attention_dispatch still required the transposed V to be keys x columns long with value_layout 1. Since llama.cpp #289/#291 the buffer has the spread pitch (keys + 32 when the key count is a multiple of 64), the transpose kernel is told the pitch and the attention kernel derives it (value_layout 2). The dispatch is right; the expectation was stale, and a compile error in this file (fixed by #293) hid it. The check now asserts the pitch, the packed source binding, the transpose kernel's output_pitch and that the pitch is not a compile parameter of the attention kernel. host: 63 passed, 3 failed -> 64 passed, 2 failed (the two left, :1858 and :6333, are not from this change). Co-Authored-By: Claude Fable 5.2 <[email protected]> * tests: hrx-backend-test's two other stale host rows follow the backend (engine ggml-org#2068) The test did not compile from #262 until #293, so host rows drifted from changes that were made on purpose. #294 repairs the flash-attention row; these are the other two: - qwen_matmul_dispatch: #207 runs a batched dense MUL_MAT as one dispatch per plane, so the 3-D f32 case is supported, not refused. - unmatched_graph_diagnostics: #196's standalone SET_ROWS takes the i32-index graph the row used as its unmatched example. The row now uses a row width above that dispatch's limit, which every matcher still declines. Co-Authored-By: Claude Fable 5.2 <[email protected]> --------- Co-authored-by: bong-water-water-bong <[email protected]> Co-authored-by: Claude Fable 5.2 <[email protected]>
ggml-hrx: standalone
SET_ROWSfor the QSA mask rebuild (engine ggml-org#679)Branch
1bit/hrx-set-rows@658b99492, base1bit/hrx-vulkan-patched@d8bd0ff60(current tip).6 files, +386/-3, one commit. Nothing else in the fork changes: the matcher, bounds and claim of every existing
SET_ROWSdispatch are untouched.Rebased onto the current base tip and re-gated on this head on 2026-11-03 (see "Verified on this head").
Verified on this head (
658b99492, 2026-11-03)Rebuilt on the rebound base, then on gfx1151:
Re-running the gate needs the four environment variables from the top_k PR's Repro section (
IREE_HAL_AMDGPU_LIBHSA_PATHand the no-SVM trio for device enumeration,
HIP_DEVICE_LIB_PATHfor amdclang); without the first,test-backend-opsregisters zero HRX devices and silently tests the CPU backend.
Re-verified 2026-11-04 after the c16 hardware reboot (this head, unchanged)
658b99492is still the branch head and is what was gated again today on a freshly builttest-backend-ops(gfx1151, onegpu-hold -l attnclaim, die 52 C, 0 gfxhub faults):What was missing, and why it is worth a kernel
GGML_OP_SET_ROWSis already ineager_capability_declared(ggml-hrx.cpp:865), but the two matchers that own itare narrower than what
src/models/qwen4exp.cppbuild_attn_qsabuilds:common.set_rows_scatter(priority 200)hidden == 1, i64 indices, f16 cachecommon.set_rows(100)hidden >= 4,<= 2048tokenshidden == 1, 2051 cellsSo the node was declined and ran on the CPU backend, splitting each full-attention layer's graph around it. Measured
model-free in
~/wt/hrx-probe/qsa_layer.cpp(the probe that rebuilds one QSA decode step at the GGUF's real shapes):with the indexer,
hip.top_k_f32(merged #161) and the flash-attention decode split all taking their nodes. It is thelast node of that path with nowhere to run on the device.
The change
hip/kernels/hip_set_rows.hip—cache[indices[i], :] = rows[i, :]:nb[1] == ne[0] * type_size) — a stride the kernel cannot express is declined, not guessed;the element-wise
hidden == 1case is one thread per selected cell instead of a workgroup;hip/dispatch-hip-set-rows.cpp—hip.set_rows,SingleOp, priority 50:only ever sees a node the existing kernels declined. No shape that runs on the device today changes backend;
hip_declare_eager_opand no extra bound indevice_supports_op(the ggml-hrx: TOP_K on HRX, a radix select for the QSA indexer (engine #679) #161 pattern is not needed here): the claimfor an op already in the capability list is decided by
can_execute_standalone_op_as_graph, which runs the matcher,so a declined shape keeps its CPU fallback (engine hrx: strict refuses a misplacement, not a placement the code calls by design (engine #2450) #389);
set_hip_set_rows_hook);ggml_set_rowsbatches over them with the indices broadcast asdst_dim % indices_dim(
ggml-cpu/ops.cpp), so the matcher emits one dispatch per slice with its own binding byte offsets. That is whatmakes the suite's cases reachable — without it there is no
test-backend-opscase in this kernel's contract at all;GGML_HRX_DISABLE_DISPATCH=hip.set_rowsturns it off, which is the A/B switch for the model row.hip/README.mdgains "Worked example 3", which is the genuinely new part of the recipe: when the op is already in thecapability list and the existing matchers are merely narrower, the claim needs no code and the right position is
below the matchers being extended.
Verification (all measured here, gfx1151, one
gpu-holdclaim per step, 0 gfxhub faults, trio + libhsa path)Host reference first, no device —
~/wt/hrx-probe/set_rows_check.cpptranscribes the kernel's thread/packetmapping, index decode, four format combinations and the batched slice offsets, and diffs them against a serial
transcription of
ggml-cpu/ops.cpp'sset_rows: 306 cases, 0 failures, including the QSA shape(hidden 1 / 2051 cells / 18,816 cache rows / i32), both index types,
hidden1..2560 with the odd widths that forcethe scalar packet path, the four f32/f16 combinations, five batched broadcast cases, and an out-of-range case that must
write nothing.
Device known answer —
~/wt/hrx-probe/set_rows_ka.cppruns the same graph on HRX0 and on the CPU backend andcompares element by element: 8 cases, 0 mismatches,
hip.set_rowstaken:(The device column includes launch overhead and, for the f32 cases, the 18,816-element add the model does next. It is
reported as a correctness vehicle, not as a speed claim; the CPU is faster for these tiny scatters and the win is the
graph placement, not the kernel.)
test-backend-ops -o SET_ROWS -b HRX0— arms in one claim:hip.set_rowsin the dispatch logGGML_HRX_DISABLE_DISPATCH=hip.set_rows-b CPUi.e. 56 cases that used to be
not supported [HRX0]now run on the device and match the CPU referenceThe QSA probe with the dispatch in (
~/wt/hrx-probe/qsa_layer.cpp, stagesindexer → +topk → +rebuild → +fa):+rebuildcosts 69.6 us at 18,816 and the whole measured QSA decode step is 877.8 us per layer, withGGML_SCHED_DEBUG=2reporting 0 nodes on the CPU backend and 0 splits (before: twoSET_ROWSnodes unsupportedper layer, and the stage did not run at all).
Not measured — and why this is not a tok/s claim
The model row. Decode at 18,782 tokens with this dispatch on/off (
~/lb/tattn-setrows-ab.sh, kernel ON vsGGML_HRX_DISABLE_DISPATCH=hip.set_rows, same binary, prompts 1000..7000 including the 2051 ± pair) needs the 76.33 GiBFlash-Next file at
-c 19456, i.e. ~92 GiBMemAvailable; this box has not had that since 2026-10-31 (GTT isDRAM-backed and that arm peaks 83.8 GiB). The numbers above bound what the dispatch can change structurally — no CPU
node and no split in any full-attention layer's decode graph — and they also say what it cannot: the whole QSA step is
877.8 us per layer, so this alone does not explain the 79 ms/step at 18.8k. What it removes is the split, and #161 is
the precedent for what that is worth (3x prefill, 1.16x decode, from a node whose own cost was ~50 us).
Head on the current base, re-gated there
Base moved while this PR was open (
d8bd0ff60->a5c32fbf8, oneconvert_hf_to_gguf.pycommit that does not touch thistree). The commit is now
684648c69on basea5c32fbf8, byte-identical content, and the op gate was re-run on that head:GGML_HRX_LOG_DISPATCH=1 test-backend-ops test -o SET_ROWS -b HRX0->ggml_hrx dispatch: hip.set_rows (single),59/59 tests passed,
Backend HRX0: OK.GGML_HRX_DISABLE_DISPATCH=hip.set_rows-> every casenot supported [HRX0], i.e. theswitch declines cleanly and the 59 passes above are this dispatch doing the work.
the box drifts 13-15 %/hour, so a fresh single sweep would be worse evidence than the interleaved rows already quoted.