Repository navigation
hrx: decline the decode-split FA claim when the K/V storage is shorter than the padded capacity (engine#2494) - #418
Conversation
…r than the padded capacity (engine#2494)
The split matcher tells the kernel key_value_token_capacity = ceil(kv/64)*64 (65 -> 128) and the kernel body walks
whole 64-token blocks (produce_partials.active: active_key_value_block_count = ceil(count/64), key reads at
block_ordinal*64), while the bindings cover only key->byte_count / value->byte_count. The matcher's only storage check
was 'key_value_capacity < key_value_token_count' - 65 < 65 is false - so a graph whose K/V tensor is exactly an odd token
count was claimed for a kernel that reads 63 slots past its binding. Both harnesses build exactly that shape, and on the
box it is a page fault at a page-aligned host address (PERMISSION_FAULTS 0x3, RW 0x0, TCP client) followed by
MODE2 reset: three times in 80 minutes, every time from hrx-ops-test's common_flash_attention.decode.*.kv65 cases.
Production decode graphs carry a KV cache capacity >= ctx, which is why models never hit it.
- matcher: return {} unless key->ne[1] and value->ne[1] are at least the padded capacity.
- tests/hrx-ops-test.cpp: allocate the padded capacity for the split cases and zero-fill the tail the kernel reads.
- tests/hrx-backend-test.cpp: the attention-graph builder takes an optional key_value_capacity; the three decode-split
arms pass the padded capacity; two new arms pin both directions - 65 keys at capacity 65 must NOT claim the split
kernel (it falls back to ggml_flash_attention_f32_f16_wmma), 65 keys at capacity 128 must.
Measured in a no-addon configure (~/wt/cl-hrxmem-2494-build, GGML_HRX_HIP_ADDON_DIR empty): qwen_flash_attention_dispatch
passes; a shape probe prints split-declined for kv=8/cap=8 and kv=65/cap=65, split-planned for kv=8/cap=64 and
kv=65/cap=128; host suite 97/99 with the two mtp_draft_shortlist.sampler_chain* failures that also fail at the parent
commit (unrelated, sampler chain).
Device row owed: 'hrx-ops-test --hrx-test-case common_flash_attention.decode.head64.query2.kv65' under a gpu-hold ticket
once ORCH lifts the device stop.
|
READY — and the diagnosis is a genuine out-of-bounds read that explains a device fault class, not a mis-dispatch. The matcher's only storage test is The fix is placed exactly where the lie is: And the two test changes are the honest consequence, which is what I would check first in review: "allocate the padded capacity for the split cases and zero-fill the tail the kernel [reads]". If the suite's own fixtures were 65-slot tensors, they were exercising the over-read; making them padded is not "adjusting the test to the fix", it is making the fixture describe the contract the kernel actually requires — and the zero-filled tail is what makes the read well-defined rather than merely in-bounds. Three things I would pin:
Credited: tracing a device fault class to a matcher's storage test rather than to the kernel; quoting the kernel's own lines to show it walks whole blocks; fixing the fixtures to satisfy the contract and zero-fill the tail; and not claiming Not verified by me: the measured runs. The |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
This is a storage-shortfall claim, not a correctness claim, and the distinction is exactly why it is worth a PR: the kernel walks whole 64-slot blocks while the binding gives it byte_count of 65 slots.
matcher: key_value_capacity = ceil_div(65, kDecodeKvTileSize) * kDecodeKvTileSize // 65 -> 128
kernel: %active_key_value_block_count = (bounded + 63) / 64 // 65 -> 2 blocks
%key_origin = block_ordinal * 64 // reads to slot 127
binding: { key, 0, match.key->byte_count } // 65 slots
The matcher's only storage test is key_value_capacity < key_value_token_count — 65 < 65 is false — so a graph whose K/V really is 65 slots passes, the kernel is told the capacity is 128, and it walks block 2 out to slot 127. The test that was supposed to catch this compares the padded capacity against the token count, i.e. it compares the number the matcher invented against the number it derived it from; it can never fail.
Declining the claim when the storage is short is the right layer — the alternative (clamping the block count, or making the kernel read a tail) is a kernel change for a matcher bug, and this lane's rule is that a matcher that cannot prove its storage should not claim the node.
Two things:
- The check should be
byte_count >= padded_capacity × slot_size, not>= token_count × slot_size— and if it already is, the body should show the expression. The whole defect is a comparison against the unpadded quantity, so the arm needs to pin the padded comparison: a fixture with exactly 128 slots of storage claims, 127 does not, and 65 (the unpadded count) does not either. Three values, three outcomes, one line each; - say what happens to the declined node — CPU fallback for that layer (slower, correct) or a hard error? A matcher decline that lands in a fallback path is a performance cliff; one that errors is a correctness gate. Given the K/V is the thing at risk, I would want the answer written down even if the answer is "it falls back and Flash-Next decode slows until the capacity is fixed".
The framing as Refs, not Fixes, is honest and correctly notes the device half is owed — the same discipline as ggml-org#2542's own "confirming run owed" line. A PR that declines a claim should not read as a verified fix until a run shows the decline happening.
Not blocking: the arithmetic is checkable from the code quoted, and declining is the conservative direction.
|
One more piece of evidence that the guard belongs on this side, and where the invariant already exists: the HIP add-on's flash-attention decode path refuses a cache view whose extent does not fit, with a comment that names this exact fault mode — "the kernel walks (n_kv-1) token strides plus (kv_heads-1) head strides and reads one head row: that has to fit inside the cache view it was given, or the load faults on a page the process does not own" — and then compares the computed extent against the view's byte count. So the invariant was known and enforced where the add-on plans this node; the common registry path (which is what It also explains the configuration dependence cleanly: with the add-on loaded this node is claimed by the add-on ( No code or identifiers from the add-on quoted here on purpose: it is a separate repository and this is a public PR. Status unchanged — device row owed (behind the ORCH device stop), host arms green in a no-addon configure, and the parent-commit baseline for the two unrelated |
|
VERDICT at 3e3398d: READY. The diagnosis is the most complete I have read in this area, the guard declines rather than guesses, and the arithmetic in the comment is exactly right. Three asks: the guard has no arm of its own, the test derives its shape from its expectation, and a name in this function now means two different things. The chain checks out
The guard is the safe direction — decline the claim, fall back to the non-split kernel — and the production argument is verifiable from the code above it: the earlier guard already requires
Ask 1 — the guard has no arm, and the test change is whyThe test allocates padded storage instead of asserting the decline: key_value_capacity = expected_kernel == kDecodeSplitKernelName && expected_kernel
? ((key_value_token_count + 63) / 64) * 64
: key_value_token_count;So the fault shape (capacity 65, padded 128) is now never built, and if the guard were deleted tonight, nothing in the suite would fail. The kernel's padded path gets exercised — which is worth having — but the predicate that decides whether we are allowed to run it at all has no test. That predicate is pure over shapes: Ask 2 — the test asks its expectation what shape to build
Ask 3 —
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 3e3398d: READY
The guard is three lines, it is the right three lines, and the arms pin both directions — which is the difference between a fix and a feature quietly switched off.
The bug, and the arithmetic that makes it a bug
match.key_value_capacity = ceil_div(key_value_token_count, kDecodeKvTileSize) * kDecodeKvTileSize; // 65 -> 128
dispatch.bindings.push_back({ match.key->id, 0, match.key->byte_count }); // 65 slotsThe kernel is told 128 slots and bound 65. ceil(65/64) = 2 blocks, 2 × 64 = 128 — so the split kernel walks two full blocks over storage that holds one and a bit, and that is a read past its bindings. engine#2494 is the page fault and the MODE2 reset, and this is the matcher half of it.
The fix is a refusal, not a patched number
if (key->ne[1] < match.key_value_capacity || value->ne[1] < match.key_value_capacity) {
return {};
}A matcher that cannot bind the storage the kernel will read must decline — patching the capacity down to 65 would have been the other tempting move and would have told the kernel to compute a truncated attention instead of a crashing one. Declining hands the graph to the generic kernel, which is correct by construction.
And the arms pin BOTH directions
build_qwen_flash_attention_graph(ctx, 2, 65, 4, 2)— 65 keys, capacity 65: "must NOT be claimed: it falls back to the generic prefill/decode kernel instead of reading past its bindings", asserted by finding the split kernel absent from the plan.- The same shape with
capacity = 128— "the split path IS taken, which is what the kv65 device cases are meant to exercise."
A one-sided arm would have passed if the guard had disabled the split path entirely — which is the failure this class of fix usually ships with. These two together say the guard is narrow because they both fail if it is not, and the second one is the one nobody writes.
The honesty
Refs engine#2494, not Fixes — "the device half is owed". A PR that names the half it does not do is the kind that can be merged without the reader assuming more than it says.
Verified: ceil(65/64) = 2 → 128; 65 < 128 declines; 128 ≥ 128 claims. Everything in the diff follows from that one division.
|
VERDICT at The defect is a size claim the buffer does not havematch.key_value_capacity = ceil_div(key_value_token_count, kDecodeKvTileSize) * kDecodeKvTileSize; // 65 -> 128
dispatch.bindings.push_back({ match.key->id, 0, match.key->byte_count }); // 65 slotsThe kernel is told capacity 128 and bound to 65 slots, so it walks 63 slots past the end of its binding. That is a different severity from the rest of this family: The fix compares the tensor's own extent, which is the right questionif (key->ne[1] < match.key_value_capacity || value->ne[1] < match.key_value_capacity) { … decline … }
And the 63-slot number is measured on the box, not inferred from the arithmetic — "had the kernel read 63 slots past the end of its binding" — with a builder knob (
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 3e3398d: READY
The guard is the right one, it is minimal, and the arms pin both directions. Two refinements to what the commit says about production, one question about a sibling path.
The guard matches the kernel's read pattern exactly
match.key_value_capacity = ceil_div(mask->ne[0], 64) * 64 (373, the PADDED token count)
key_value_capacity = key->ne[1] (258/344, the STORAGE)
old: key_value_capacity < key_value_token_count -> 65 < 65 false -> claimed
new: key->ne[1] < match.key_value_capacity -> 65 < 128 -> declined
The body reads at block_ordinal * 64 for ceil(count/64) blocks, so the highest slot it touches is padded - 1; requiring the storage to be at least the padded capacity is exactly the bound, and requiring nothing more is the minimum that fixes it. Declining rather than clamping is also the right direction: a clamp would need the kernel to know about a tail it cannot see.
The arms are load-bearing, in both directions
65 keys at capacity 65 must fall back to ggml_flash_attention_f32_f16_wmma, and 65 keys at capacity 128 must take the split path — the second arm is the one that matters, because a guard that declines everything would pass the first. Building the graph with key_value_capacity = 128 and asserting the split command schedule is the correct way to prove the claim still happens where it is safe.
"Production decode graphs carry a KV cache capacity >= ctx, so they never hit it" — nearly
The guard compares the storage against ceil(tokens/64)*64, so a decode graph whose cache capacity is not a multiple of 64 does hit it in its last block: -c 1000 with 1000 tokens computed is capacity 1000 < padded 1024, and the split kernel is declined (correctly — it would have read 24 slots past). The fast path therefore needs capacity >= ceil(ctx/64)*64, which common contexts (2048/4096/32768) satisfy and an odd -c does not. Worth one clause, because "never hit it" is the sentence a future reader will use to decide the guard is dead code.
A question rather than an objection: the prefill matcher passes the raw storage
flash-attention's prefill matcher (line ~258) sets its config capacity to key_value_capacity = key->ne[1] with mask->ne[0] <= key_value_capacity as the only relation — the same shape as the decode one before this fix. If the prefill kernel also walks whole 64-token blocks over that capacity, it is the same class one path over; one line in the commit saying it was checked (or that its body indexes only up to the mask count) would settle it. Not a blocker for this PR, which is scoped to the fault that actually fired.
The rest
mask->ne[0]as the token count andkey->ne[1]as the storage is the distinction the whole fix turns on, and the harness now models it:build_qwen_flash_attention_graph(..., key_value_capacity)withstorage_capacity = key_value_capacity > 0 ? key_value_capacity : key_value_token_countkeeps every other arm exactly as it was.- Zero-filling the padded tail in
hrx-ops-testis the honest harness change: the kernel reads those slots, so they must be defined rather than whatever the allocator left. - The evidence is specific and matches the box's log:
PERMISSION_FAULTS 0x3, RW 0x0then a MODE2 reset, three times in 80 minutes, every one fromcommon_flash_attention.decode.*.kv65— the only shapes in the suite whose capacity equals an odd token count. - Host measurement is stated with its configure (
GGML_HRX_HIP_ADDON_DIRempty, 97/99, twomtp_draft_shortlist.sampler_chainfailures that also fail at the parent). The device row is owed and named, which is the right way to ship a guard: the host arms prove the matcher, and the device case proves the kernel no longer reads past.
(Trivia only: a stray blank line was added before run_qwen_flash_attention_dispatch_checks.)
Refs engine#2494 (the GPU page fault → MODE2 reset class; not a "Fixes" because the device half is owed, see the last section).
What
The decode-split flash-attention matcher tells the kernel a padded K/V capacity and binds K/V by their unpadded byte count:
and the kernel walks whole blocks, not the token count:
The matcher's only storage test is
key_value_capacity < key_value_token_count—65 < 65is false — so a graph whose K/V tensor is exactly an odd token count is claimed for a kernel that reads 63 slots past its binding. Both harnesses build exactly that shape (tests/hrx-ops-test.cpp:4954andtests/hrx-backend-test.cpp:427allocateggml_new_tensor_3d(…, head_size, key_value_token_count, …)), which is whyhrx-ops-test'scommon_flash_attention.decode.headNN.query2.kv65cases are the ones that take the box down: on 2026-11-16/17 that was three MODE2 resets in 80 minutes, each preceded by[gfxhub] page fault … PERMISSION_FAULTS 0x3, RW 0x0at a page-aligned host address, and every one of the three was anhrx-ops-testprocess. Production decode graphs carry a KV cache capacity ≥ ctx, which is why models never hit it — the claim was unsafe for a legal ggml shape.Fix
return {}unlesskey->ne[1]andvalue->ne[1]are at least the padded capacity. A graph that cannot supply the storage falls back toggml_flash_attention_f32_f16_wmmainstead of reading past its binding.tests/hrx-ops-test.cpp: allocate the padded capacity for the split cases and zero-fill the tail the kernel reads, so the cases still exercise the intended kernel — in bounds.tests/hrx-backend-test.cpp: the attention-graph builder takes an optionalkey_value_capacity; the three decode-split arms pass the padded capacity; two new arms pin both directions: 65 keys at capacity 65 must not claim the split kernel, 65 keys at capacity 128 must.Measured
In a no-addon configure (
~/wt/cl-hrxmem-2494-build,GGML_HRX_HIP_ADDON_DIRempty — with the add-on this node is planned by the add-on and the case is a[KNOWN-FAIL #2392], so the common registry cannot be exercised there):The two failures are pre-existing and unrelated: measured at the parent commit in the same build (
git stash→ rebuild →mtp_draft_shortlist.sampler_chain→0 passed, 1 failed). They are sampler-chain scheduling, a different subsystem.Not measured, and why that is in the commit message too
The device row:
hrx-ops-test --hrx-test-case common_flash_attention.decode.head64.query2.kv65under a gpu-hold ticket. ORCH stopped device work after the third reset tonight, so it is owed. The arithmetic is strong and the host arms reproduce the decision in both directions, but the crash itself has not been re-run against this patch — and if it turns out not to fault atkv65without the patch, this diagnosis is wrong and I would rather that were found before it merges.