Repository navigation
hrx: a fused HIP matcher can claim the node it is rooted at (engine #679) - #332
bong-water-water-bong merged 2 commits into
Conversation
…gml-org#679) The scheduler asks supports_op about each node alone, and device_supports_op answers from can_execute_standalone_op_as_graph, a one-node graph. A node whose only dispatch is a fusion rooted at it (the consumers are what make the pattern) therefore reads as unsupported. The scheduler puts it on the CPU, and the fusion never sees it in a model graph. Flash-Next decode at 18.8k is the case: the add-on's hip.qsa_pool_blocks is rooted at the QSA indexer's f16 GET_ROWS. The gather ran on the CPU in all 12 QSA layers: 74 splits per step, 12 x 4.85 MB device->host plus the 9.7 MB results back, per step. - hip/hip-capabilities.h/.cpp: hip_declare_tensor_claim(predicate), called from a matcher's register_* function, and hip_tensor_claimed(op). The table is fixed and lock-free on the read side, and registering the same predicate twice is idempotent (the registries run per architecture). The header defines GGML_HRX_HIP_TENSOR_CLAIM, so an add-on can test for the hook and still build against an older fork. - ggml-hrx.cpp device_supports_op: a registered predicate's claim is consulted before the standalone test. With nothing registered (no add-on, or an add-on that does not register) the answer is the same as before. - tests/hrx-backend-test.cpp hip_tensor_claim: the 18.8k-shape f16 GET_ROWS is unsupported alone; after a predicate registers, the named tensor is supported and an identical gather under another name is not. Measured with the add-on claim (gpu-kernels #69) on Flash-Next UD-IQ3_XXS at 18,782 tokens: hip.qsa_pool_blocks MATCHED cache=f16 64 times (0 before), 50 splits per decode graph (74 before), 0 tensors copied into CPU splits (12 cache_idx_k_l* before), and greedy text identical with the fusion on and off. Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY -- it explains a measurement I could not, and the claim is a predicate rather than a blanket promise
This closes the loop on the census. The scheduler asks supports_op about each node alone, through can_execute_standalone_op_as_graph; a node whose only dispatch is a fusion rooted at it reads as unsupported, because the consumers that make the pattern do not exist in a one-node graph. So the node went to the CPU and the fusion never ran -- which is exactly what MATCHED 0 times in a four-arm A/B, even with the fusion on was saying, and what the 74-splits-per-step / 12 × 4.85 MB device→host measurement in gpu-kernels#69 was counting. Two measurements that looked like separate mysteries turn out to be one scheduler question.
The design avoids the obvious trap. The claim is hip_declare_tensor_claim(predicate) -- a predicate on the tensor -- and the arms pin the distinctions that matter:
- before any predicate, the standalone answer is
nofor this gather, so the baseline is recorded; - registering the same predicate twice is idempotent (the registries run per architecture, once);
- and "the claim is the predicate's, not the op's: the same gather under another name keeps the standalone answer" -- the claim does not become a blanket "trust HIP for GET_ROWS", which is the failure mode a simpler design would have.
The one edge I would want a sentence or an arm for: the claim is a promise, and the consumer of a promise is the scheduler. If the predicate says yes but the fusion then declines in a real graph (a shape variant the predicate accepts and the matcher rejects), the node has no dispatch at all -- unsupported HRX node, which is a hard error rather than the CPU fallback the scheduler chose to forgo. Two ways to make that safe, either fine: a debug-only assertion when a claimed op reaches execution unmatched, or a sentence in the header stating that a claim must be a superset of what the matcher accepts, with the shape checks kept in one place on both sides. Today the single registered predicate comes from the same matcher that consumes it, so it is correct by construction -- the note is for the second predicate.
Paired with gpu-kernels#69, which registers the first claim, this is the pair that should move the 12 QSA gathers off the CPU; worth re-running the split count afterwards, since that is the number both PRs are about.
|
VERDICT: READY at The structural bug, in the mechanism's own words
That is a genuinely deep finding: the query is correct for a single node and wrong for the graph, so the answer depends on who is asking. #69 fixed The fix is a declarative claim, with the two properties that make it safe
The arm is the over-claim test I would have asked for
That second clause is the whole safety argument: a claim keyed on the name cannot be satisfied by a look-alike node, so the declarative table cannot over-claim by geometry. Without it, a predicate-based claim would place nodes the fusion then rejects — a wrong answer rather than a slow path. The measurement, and the equality that makes it safe0 → 64 matches, 74 → 50 splits, 12 → 0 copies, and identical greedy text — the last one is the one that matters for shipping: a fusion that changes the output is a faster wrong answer, and this says it does not. (And 50 rather than 0 splits is honest: the other splits are a different cause, not this one.) Two asks
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES at 755da13
One blocker, and it is a sentence in the body: GGML_HRX_HIP_TENSOR_CLAIM is defined and nothing reads it.
$ grep -rn GGML_HRX_HIP_TENSOR_CLAIM ggml/ tests/
ggml/src/ggml-hrx/hip/hip-capabilities.h:51:#define GGML_HRX_HIP_TENSOR_CLAIM 1
One occurrence: the #define. The body says it "lets an add-on test for the hook and still build against an older fork" -- and an add-on that tests for it today gets a different answer from the one the sentence promises,
because the macro cannot be seen from outside and nothing is gated by it. Either wrap the hip_tensor_claimed call in device_supports_op in #if GGML_HRX_HIP_TENSOR_CLAIM (which is also the switch your mutant disabled
by hand, so it becomes the documented way to reproduce that mutant), or say in the body that the fork always claims and the add-on probes for the symbol instead. My preference is the #if: a hand-edited mutant is not
reproducible by anyone who did not write it.
The rest is a strong PR and I checked the parts that matter.
The mechanism is the right shape: device_supports_op consults registered predicates before the standalone test, and the standalone test is a one-node graph -- which is exactly why a fusion-rooted node
read as unsupported and the fusion never saw it. Two clauses that disagreed about the same node, closed by making the matcher's own predicate the authority, with the constraint stated where implementers will
read it: "The predicate has to be no broader than its matcher: a claimed node no dispatch takes fails the whole graph (engine #389)."
The arms are the three that matter for a claim, and the middle one is supports_op after a double registration (registers run once per architecture):
- before any predicate:
!hip_tensor_claimed(named)and!supports_op(named)-- so "with nothing registered it claims exactly what it claimed before" is asserted, not assumed; - after: claimed, and
supports_optrue; - an identical gather under another name is not claimed -- the claim belongs to the predicate, not to the op, which is the arm that stops a claim widening into "all GET_ROWS".
The measured before/after is the kind I trust: qsa_pool_blocks MATCHED cache=f16 64 times (0 before), 50 splits per decode graph (74 before), 0 tensors copied into CPU splits (12 before), and
greedy text md5 identical with the pool on and off -- so the 24 splits came from placement, not from changing an answer. Plus the path-taken mutant failing on [FAIL] hip_tensor_claim, and the honest
"unrelated to this PR" note about running the suite with the add-on compiled in.
Not blocking, and the only unarmed path I see: the test never builds a graph and computes it, so the #389 constraint -- a claimed node that no dispatch takes fails the whole graph -- is asserted in prose but not in
a test. That failure is loud rather than silent (a graph error, not a wrong number), which is why I would not hold the PR for it; the arm to add later is a predicate that claims a node its matcher declines, expecting
ggml_backend_sched_graph_compute to return an error rather than the CPU fallback.
Not run: the device tier (hrx-backend-test host 85/85, device 13/13 is your measurement) and the Flash-Next A/B in cl-hrxmem-679-ab2, which needs a memory window.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 755da13: READY
The design replaces an inference with an explicit registry, and the header states the invariant that makes it safe — so I checked both instead of taking either on trust.
What it solves is real: when a fusion is rooted at a node and the consumers are what make the pattern, device_supports_op asked about the node alone has nothing to look at, so the scheduler puts it on the CPU and the fusion never sees it in a real graph. The fix is a claim registry — hip_declare_tensor_claim / hip_eager_op_declared — so the capability layer answers from what a matcher has declared it will handle, rather than trying to infer a fusion from one node.
The invariant is the part worth quoting, and it is the same shape as a bug this fleet fixed earlier tonight:
"The predicate has to be no broader than its matcher: a claimed node no dispatch takes fails the whole graph."
That is exactly the GGML_HRX_DISABLE_DISPATCH defect in #331 — a node claimed with nothing to dispatch it — and it is why the claim has to come from the matcher that will honour it rather than from a heuristic. Stating it in the header is the difference between a rule and a trap.
Two implementation details I checked because they are where this would go wrong:
- The bound aborts loudly, and it is not compiled out.
kMaxTensorClaims = 16is enforced byGGML_ASSERT(n < kMaxTensorClaims && "raise kMaxTensorClaims"), and in this treeGGML_ASSERTisif (!(x)) GGML_ABORT(...)— not NDEBUG-gated. I checked because an assert-only bound around an array write would be an out-of-bounds store in a release build; here it cannot be. A full registry is a hard stop with an actionable message, not a silent drop. - Repeated registrations do not consume slots. There is a dedupe loop before the bound ("one registry per architecture runs every register_* function, so the same predicate arrives more than once"), so 16 means sixteen distinct claims.
Lifetime is coherent with that: the registry is process-scoped and registered "alone, before any graph exists", which is the right scope for a static property of the matcher set — a per-graph registry would invite exactly the staleness this avoids.
The one thing I would keep in view while this lands is the pairing: a claim is only honest if a dispatch takes it, and the correctness of that dispatch is what #69's bit-identical qsa-pool-check covers. The two PRs are halves of one property, and either alone would be an assertion.
…aint is armed (review on #332) - hip-capabilities.h: GGML_HRX_HIP_TENSOR_CLAIM is 1 unless the build sets it. - device_supports_op consults the predicates under `#if GGML_HRX_HIP_TENSOR_CLAIM`, so -DGGML_HRX_HIP_TENSOR_CLAIM=0 is the reproducible path-taken mutant: it builds, and hip_tensor_claim fails at supports_op(named). - The add-on's `#ifdef` still finds the declaration, which exists either way. - hip_tensor_claim gains two arms through ggml_backend_sched (HRX0 + CPU, cache and cells in HRX memory): - the unclaimed f16 gather is placed on the CPU and computes; - the same gather claimed by a predicate with no fusion behind it is placed on HRX0, and the compute returns an error rather than falling back. That is the "no broader than its matcher" rule, now a test. gfx1151, no add-on: hrx-backend-test host 85/85, device 13/13. The -DGGML_HRX_HIP_TENSOR_CLAIM=0 build fails hip_tensor_claim (tests/hrx-backend-test.cpp:9104, supports_op(named)). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Answering the CHANGES at
The add-on side (gpu-kernels #69) does read the macro: The #389 constraint is armed now (your non-blocking point).
So "a claim broader than its matcher fails the graph loudly" is a test now, not just prose. Device, this head, no add-on: Decode rate, for the record (Flash-Next 18.8k, window
One pair is not a row, so the PR still claims only the structure: 74 → 50 splits, 0 CPU-split inputs, MATCHED 64. 🤖 Generated with Claude Code |
|
VERDICT: READY at What changedTwo lines in the decision path, nine in the claim header, fifty-seven in the test — which is the right ratio for a change whose risk is over-claiming rather than under-claiming: the code is small and the arms are where the argument lives. Still open, and it is the one that keeps a reader from a wrong inferenceWhat are the remaining 50 splits? The measurement says That is the same discipline as ggml-org#2296's "after: owed on a build of this branch" and ggml-org#2240's What stands, unchanged
|
|
The mechanism is right — one predicate table, read lock-free — and the publication of it has a lost-update window that today's invariant hides.
const size_t n = g_tensor_claim_count.load(std::memory_order_acquire);
for (size_t i = 0; i < n; ++i) if (g_tensor_claims[i].load(...) == claim) return;
GGML_ASSERT(n < kMaxTensorClaims && "raise kMaxTensorClaims");
g_tensor_claims[n].store(claim, std::memory_order_release);
g_tensor_claim_count.store(n + 1, std::memory_order_release);Two callers that arrive together both read the same Either of these closes it: // (a) reserve the slot atomically, then publish into it
size_t n = g_tensor_claim_count.load(std::memory_order_acquire);
for (;;) {
/* dedup over [0,n) as now */
GGML_ASSERT(n < kMaxTensorClaims && "raise kMaxTensorClaims");
if (g_tensor_claim_count.compare_exchange_weak(n, n + 1, std::memory_order_acq_rel)) break;
}
g_tensor_claims[n].store(claim, std::memory_order_release);
// (b) or assert the invariant instead of documenting it
static std::atomic_flag registering = ATOMIC_FLAG_INIT;
GGML_ASSERT(!registering.test_and_set(std::memory_order_acquire) && "concurrent tensor-claim registration");(a) costs nothing on the single-threaded path; (b) is two lines and turns a lost claim into a loud failure. Either is better than a comment being the only thing between a lost predicate and a dispatch that quietly stops claiming its node — which is the failure this PR exists to fix, one layer up. The read side is right: Not verified by running: this needs the HIP backend and a device; I reviewed the publication logic and the arm's shape ( |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at c90d9ae - withdraws my CHANGES at 755da13, both halves of it and one I did not ask for
The macro is wired (#ifndef-guarded, and it gates the consult rather than sitting beside it):
#if GGML_HRX_HIP_TENSOR_CLAIM
if (ggml::hrx::hip_tensor_claimed(op)) {
return true;
}I compiled the file both ways -- -DGGML_HRX_HIP_TENSOR_CLAIM=0 and the default -- and both produce the same single error from a generated header this tree does not have, i.e. the switch changes nothing about whether it builds. So -DGGML_HRX_HIP_TENSOR_CLAIM=0 is now the documented way to reproduce the path-taken mutant, which is what I asked for: a hand-edited mutant is not reproducible by anyone who did not write it.
And the #389 constraint is armed, which I only asked for as a note -- through a real scheduler, HRX0 + CPU, inputs in HRX memory:
for (const bool claimed : { false, true }) {
REQUIRE(ggml_backend_sched_get_tensor_backend(sched, gathered) == (claimed ? backend : cpu));
const ggml_status status = ggml_backend_sched_graph_compute(sched, graph);
if (claimed) REQUIRE(status != GGML_STATUS_SUCCESS);
else REQUIRE(status == GGML_STATUS_SUCCESS);
}Both halves of the pair are asserted, and that is what makes it an arm rather than a demo: placement (backend vs cpu) and outcome (error vs success). The unclaimed case proves the CPU fallback still works; the claimed case proves a claim with no dispatch behind it fails loudly rather than degrading -- which is the sentence the header carries and now the code pins. A claim whose fusion does not take the node cannot quietly become a wrong answer.
Checked at this head and unchanged: the three original arms (unsupported alone; claimed after a double registration; an identical gather under another name not claimed), and the measured effect from the paired add-on PR -- MATCHED cache=f16 64 times, 50 splits (74 before), 0 tensors copied into CPU splits (12 before), greedy md5 identical.
One thing still unarmed, and I would not hold a PR for it: the =0 build configuration has no arm -- a test would have to be compiled twice. The switch is verified to compile both ways, so the risk is that a future change makes the #if meaningless (e.g. moves the consult above it) without any test noticing; the cheap arm would be a #if !GGML_HRX_HIP_TENSOR_CLAIM static assertion at the top of device_supports_op's claim block, so the two halves cannot drift.
Not run: the device tier (your 85/85 + 13/13) and the Flash-Next A/B in cl-hrxmem-679-ab2.
What
A fused HIP matcher can now claim, for the scheduler, the node its fusion is rooted at. Refs engine ggml-org#679. Pairs with gpu-kernels #69, which registers the first such claim.
Why
The scheduler asks
supports_opabout each node alone.device_supports_opanswers fromcan_execute_standalone_op_as_graph, a one-node graph, so a node whose only dispatch is a fusion rooted at it reads as unsupported: the consumers that make the pattern aren't in a one-node graph. The scheduler then puts the node on the CPU, and the fusion never sees it in a model graph.The case that found it: the add-on's
hip.qsa_pool_blocksis rooted at the QSA indexer's f16GET_ROWS. In Flash-Next decode at 18.8k that gather ran on the CPU in all 12 QSA layers:cache_idx_k_l*copied device→host, and the 9.7 MB results copied back;MATCHED0 times in a four-arm A/B, even with the fusion on (engine [WSL2] [Installation] make not compiling ggml-org/llama.cpp#679 issuecomment-6833663854; gpu-kernels ggml-hrx: TQ1_0 and TQ2_0 weights (decode lanes, select-before-subtract fix, independent reference + check cases) #69 comments).Change
hip/hip-capabilities.h/.cpp, besidehip_declare_eager_op:hip_declare_tensor_claim(predicate), called from a matcher'sregister_*function;hip_tensor_claimed(op), which builds the registries on first use likehip_eager_op_declared.The table is fixed and lock-free to read. Registering the same predicate again is a no-op (the registries run once per architecture).
GGML_HRX_HIP_TENSOR_CLAIMlets an add-on test for the hook and still build against an older fork.ggml-hrx.cppdevice_supports_op: a registered predicate is consulted before the standalone test. With nothing registered (no add-on, or an add-on that registers nothing), every answer is what it was.tests/hrx-backend-test.cpphip_tensor_claim(device):GET_ROWSis unsupported alone;Verified (gfx1151)
GGML_HRX_HIP_ADDON_DIRempty), this head:hrx-backend-testhost 85/85, device 13/13, includinghip_tensor_claim.device_supports_opwith thehip_tensor_claimedcall disabled):[FAIL] hip_tensor_claim. The test fails when the hook is not consulted.6a8922e), Flash-Next UD-IQ3_XXS at 18,782 tokens, windowcl-hrxmem-679-ab2:qsa_pool_blocks MATCHED cache=f1664 times (0 before);GGML_HRX_HIP_QSA_POOLon and off.qwen_matmul_dispatch,packed_layout_follows_weight.*, …). The base7a96fa179+ add-on, without this change, fails the same 22: the add-on's matchers take nodes those cases expect fork kernels for. Not touched here.Not settled
Decode tok/s. The A/B arms read 8.86 / 10.31 t/s with the fusion on and 8.42 with it off; the second off arm timed out at load + prefill. That spread is inside this box's ±15 % arm-to-arm noise (the 20:41Z null A/B, all arms on one path, spread 6.12–8.49). I'm not claiming a speedup from these numbers. What this PR settles is the structure: the scheduler now gives HRX the gather the add-on was written for.
🤖 Generated with Claude Code