Skip to content

hrx: standalone SET_ROWS dispatch for the QSA mask rebuild (engine #679) - #196

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/hrx-set-rows
Nov 4, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/hrx-set-rows

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 4, 2026 •

Copy link
Copy Markdown

ggml-hrx: standalone SET_ROWS for the QSA mask rebuild (engine ggml-org#679)

Branch 1bit/hrx-set-rows @ 658b99492, base 1bit/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_ROWS dispatch 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:

$ GGML_HRX_LOG_DISPATCH=1 test-backend-ops -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 test-backend-ops -o SET_ROWS -b HRX0
  Skipping ... 2/2 backends passed          # declines to the CPU side, does not fail a graph

Re-running the gate needs the four environment variables from the top_k PR's Repro section (IREE_HAL_AMDGPU_LIBHSA_PATH
and the no-SVM trio for device enumeration, HIP_DEVICE_LIB_PATH for amdclang); without the first, test-backend-ops
registers zero HRX devices and silently tests the CPU backend.

Re-verified 2026-11-04 after the c16 hardware reboot (this head, unchanged)

658b99492 is still the branch head and is what was gated again today on a freshly built test-backend-ops (gfx1151, one
gpu-hold -l attn claim, die 52 C, 0 gfxhub faults):

$ 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 test-backend-ops test -o SET_ROWS -b HRX0
  556 cases: not supported [HRX0]   -> clean decline, no node failure, harness OK

What was missing, and why it is worth a kernel

GGML_OP_SET_ROWS is already in eager_capability_declared (ggml-hrx.cpp:865), but the two matchers that own it
are narrower than what src/models/qwen4exp.cpp build_attn_qsa builds:

matcher shape it takes QSA mask rebuild
common.set_rows_scatter (priority 200) hidden == 1, i64 indices, f16 cache i32, f32
common.set_rows (100) i64 indices, hidden >= 4, <= 2048 tokens hidden == 1, 2051 cells

So 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):

graph_compute: unsupported HRX node 44: SET_ROWS output=51:f32[1,18816,1,1]<-SET_ROWS
graph_compute: also: SET_ROWS matcher rejected node: input/output shape or storage is unsupported

with the indexer, hip.top_k_f32 (merged #161) and the flash-attention decode split all taking their nodes. It is the
last node of that path with nowhere to run on the device.

The change

hip/kernels/hip_set_rows.hip — cache[indices[i], :] = rows[i, :]:

  • f32/f16 on both sides, independent (f16 rows are widened for an f32 cache, exactly as the CPU reference does);
  • i32 or i64 indices;
  • packed rows (nb[1] == ne[0] * type_size) — a stride the kernel cannot express is declined, not guessed;
  • out-of-range indices are skipped, not written (the CPU reference asserts; a device kernel must not fault);
  • one thread per (token, packet), 4-element packets when the rows are 16-byte aligned, else one element per thread — so
    the element-wise hidden == 1 case is one thread per selected cell instead of a workgroup;
  • amdclang gfx1151: 10 VGPRs, 18 SGPRs, 0 spills, 0 scratch, no LDS, 16 waves/SIMD.

hip/dispatch-hip-set-rows.cpp — hip.set_rows, SingleOp, priority 50:

  • fused registrations are tried before every single-op one, and 50 is below both Loom matchers, so this matcher
    only ever sees a node the existing kernels declined. No shape that runs on the device today changes backend;
  • no hip_declare_eager_op and no extra bound in device_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 claim
    for 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);
  • a quantized cache is still the add-on's (set_hip_set_rows_hook);
  • dims 2 and 3 are sliced: 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 suite's cases reachable — without it there is no test-backend-ops case in this kernel's contract at all;
  • GGML_HRX_DISABLE_DISPATCH=hip.set_rows turns it off, which is the A/B switch for the model row.

hip/README.md gains "Worked example 3", which is the genuinely new part of the recipe: when the op is already in the
capability 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-hold claim per step, 0 gfxhub faults, trio + libhsa path)

Host reference first, no device — ~/wt/hrx-probe/set_rows_check.cpp 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/ops.cpp'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, 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.cpp runs the same graph on HRX0 and on the CPU backend and
compares element by element: 8 cases, 0 mismatches, hip.set_rows taken:

case shape device CPU
QSA mask @ 18,816 hidden 1, 2051 cells, i32, f32→f32 53.9 us 57.5 us
QSA mask @ 2,048 same, n_kv 2048 68.2 us 18.7 us
QSA mask, i64 indices as above, i64 37.2 us 42.5 us
QSA mask, f16 cache hidden 1, f32→f16 22.1 us 5.4 us
wide 256x8 hidden 256, 8 tokens, 512 rows 40.5 us 6.0 us
wide 2560x17 hidden 2560, 17 tokens 40.7 us 11.5 us
wide 257x5 (odd width) hidden 257, scalar packets 37.2 us 4.7 us
wide 256x8 f16→f16 f16 rows and cache 21.1 us 0.2 us

(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:

arm HRX0 cases hip.set_rows in the dispatch log CPU
matcher enabled 59/59 yes (1 line, de-duplicated) —
GGML_HRX_DISABLE_DISPATCH=hip.set_rows 3/3 no —
-b CPU — — 559/559 unchanged

i.e. 56 cases that used to be not supported [HRX0] now run on the device and match the CPU reference

The QSA probe with the dispatch in (~/wt/hrx-probe/qsa_layer.cpp, stages indexer → +topk → +rebuild → +fa):

n_kv indexer +top_k +rebuild +fa CPU nodes / splits
2,048 360.4 us 421.0 436.4 538.6 0 / 0
4,096 457.1 477.4 496.1 621.2 0 / 0
8,192 556.3 523.4 641.9 684.8 0 / 0
18,816 570.6 627.2 696.8 877.8 0 / 0

+rebuild costs 69.6 us at 18,816 and the whole measured QSA decode step is 877.8 us per layer, with
GGML_SCHED_DEBUG=2 reporting 0 nodes on the CPU backend and 0 splits (before: two SET_ROWS nodes unsupported
per 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 vs
GGML_HRX_DISABLE_DISPATCH=hip.set_rows, same binary, prompts 1000..7000 including the 2051 ± pair) needs the 76.33 GiB
Flash-Next file at -c 19456, i.e. ~92 GiB MemAvailable; this box has not had that since 2026-10-31 (GTT is
DRAM-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, one convert_hf_to_gguf.py commit that does not touch this
tree). The commit is now 684648c69 on base a5c32fbf8, 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.
  • Control on the same head: GGML_HRX_DISABLE_DISPATCH=hip.set_rows -> every case not supported [HRX0], i.e. the
    switch declines cleanly and the 59 passes above are this dispatch doing the work.
  • The QSA-stage numbers in the table above are not re-measured here: the kernel is byte-identical after the rebase and
    the box drifts 13-15 %/hour, so a fresh single sweep would be worse evidence than the interleaved rows already quoted.

@bong-water-water-bong

Copy link
Copy Markdown
Author

#196 ready for review at 658b994

@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 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:

  1. hip/README.md says 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.
  2. kSetRowsMaxSlices = 64 is checked as cache->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.
  3. set_rows_packet_width() reads rows.storage_offset/cache.storage_offset for alignment but the rows binding is
    rows->storage_root with 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.
@bong-water-water-bong

Copy link
Copy Markdown
Author

Rebased onto a5c32fbf8 and re-gated on the rebased head 684648c69: hip.set_rows (single), 59/59, Backend HRX0 OK; with GGML_HRX_DISABLE_DISPATCH=hip.set_rows every case is not supported [HRX0] (556 declines, 3/3 CPU-only), so the 59 passes are this dispatch doing the work.

@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

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.

@bong-water-water-bong
bong-water-water-bong merged commit 7709e59 into 1bit/hrx-vulkan-patched Nov 4, 2026
bong-water-water-bong pushed a commit that referenced this pull request Nov 11, 2026
…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]>
bong-water-water-bong added a commit that referenced this pull request Nov 11, 2026
…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]>
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/hrx-set-rows branch November 12, 2026 21:15
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