Skip to content

hrx: GGML_HRX_DISABLE_DISPATCH=top_k / =argsort_rows fall back to the CPU instead of failing the graph (engine #2146) - #331

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2146-disable-claim
Nov 12, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2146-disable-claim

Conversation

@bong-water-water-bong

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

Copy link
Copy Markdown

Refs 1bit-MONSTER/engine#2146 (found while working it; the issue is closed by engine#2296).

What this does

GGML_HRX_DISABLE_DISPATCH skips a registration when one of its comma-separated patterns is a substring of the registration's name. The two supports_op claims that have to decline along with their only registration did the reverse: they searched the variable for the full name (hip.top_k_f32, common.argsort_rows_f32). So =top_k removed the matcher but kept the claim. The node then stayed on HRX with no dispatch, and the graph failed with unsupported HRX node: TOP_K instead of running TOP_K on the CPU. On Flash-Next at 18.8k tokens this made the =top_k arm of ggml-org#2146 unusable.

dispatch_name_disabled(name, kind) is now the one rule. The registry calls it for every registration, and both claims call it with their registration's name. Behaviour without the variable is unchanged.

Before / after (gfx1151, test-backend-ops -b HRX0 -o <op>, one gpu-hold ticket each)

op GGML_HRX_DISABLE_DISPATCH before (c762975, branch head) after (27c9153)
TOP_K unset 488/488 passed 488/488 passed
TOP_K top_k 0/488 passed: unsupported HRX node 0: TOP_K 0 run, 533 not supported (CPU fallback)
TOP_K hip.top_k_f32 0 run, 533 not supported 0 run, 533 not supported
ARGSORT unset 42/42 passed 42/42 passed
ARGSORT argsort_rows 0/42 passed: unsupported HRX node 0: ARGSORT 0 run, 98 not supported (CPU fallback)
ARGSORT common.argsort_rows_f32 0 run, 98 not supported 0 run, 98 not supported

"Not supported" is what a disabled kernel should give: the op goes to the CPU backend. test-backend-ops exits 1 when it ran no test, in both columns.

Not covered

  • tests/test-hrx-top-k-declined (added at 9b61cf9) is a ctest entry that runs test-hrx-top-k-repeat with =top_k. It requires, for each case: no HRX claim, the node scheduled on the CPU, and indices equal to the CPU backend's. It fails 7/7 without the fix and passes 7/7 with it. In that standalone graph the scheduler puts TOP_K on the CPU either way, so what fails without the fix is the claim. The graph failure is the test-backend-ops row above.
  • ARGSORT has no ctest arm; its before/after is the test-backend-ops row.
  • fused (the category form from hrx: say so when DISABLE_DISPATCH=fused means the category, not the substring (engine#2146) #326) goes through the same function with kind. Both claims are single-op registrations, so =fused does not disable them. That is the same as before this change.

🤖 Generated with Claude Code

…own rule (engine ggml-org#2146)

The registry skips a registration when a pattern is a substring of its name. The two
supports_op claims that must decline with their only registration checked the other
way round: they looked for the full name ("hip.top_k_f32", "common.argsort_rows_f32")
inside the variable. So =top_k removed the matcher but kept the claim, and on
Flash-Next at 18.8k the request failed with "unsupported HRX node 1: TOP_K" (the
sparse-attention indexer's selection) instead of running TOP_K on the CPU.

dispatch_name_disabled() is now the one rule; the registry and both claims call it.

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

@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 -- one rule instead of three, and the "after" column is the correct behaviour rather than merely "no failure"

The bug is the same shape as the switch problem I spent today on: GGML_HRX_DISABLE_DISPATCH matches substrings of registration names, but the two supports_op claims searched for the full name. So =top_k removed the matcher and kept the claim -- the node stayed on HRX with nothing to run it and the graph died with unsupported HRX node: TOP_K instead of falling back to the CPU. A switch whose two readers disagree about what it selects is a defect even when nothing is wrong with either reader.

dispatch_name_disabled(name, kind) is the right shape of fix: one rule, called by the registry for every registration and by both claims with their own registration's name, so a third claim cannot drift from them. And the "fused"-implies-Fused special case lives in that one place, which is where it belongs.

The before/after table is the evidence I would want and rarely get: test-backend-ops -b HRX0 -o TOP_K, one ticket per arm, 488/488 passed unset both before and after; =top_k goes from 0/488 passed: unsupported HRX node 0: TOP_K to 0 run, 533 not supported (CPU fallback), which is the correct outcome rather than the absence of a failure; and =hip.top_k_f32 unchanged, so the fix does not perturb the path that already worked. That third row is the one that shows the change is scoped: the same switch, the same op, spelled the other way, still behaves.

One thing worth tightening, because it is how this bug comes back. Both claim sites call the one-argument form:

if (dispatch_name_disabled("hip.top_k_f32")) {          // kind defaults to SingleOp
if (dispatch_name_disabled("common.argsort_rows_f32")) {

The default is DispatchMatchKind::SingleOp, and with it a Fused registration's claim would not be disabled by =fused -- the claim would survive while the matcher was skipped, which is precisely the defect this PR fixes, one category over. Today those two registrations are single-op, so nothing is wrong; the default is the part that will be wrong later. Either pass the kind at both sites (the registration's real one) or drop the default so a new call site has to state it -- the compiler then asks the question the reviewer would.

Also worth noting for engine#2146: this is what makes the =top_k arm of that issue usable at all, so it belongs in the trail of that row rather than only in this PR -- an A/B arm that answers "unsupported node" is not an A/B, and one of the arms I could not use before is now available.

@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 27c9153

The defect is stated exactly and the one-rule fix is the right shape, and the before/after table is what makes it a fix rather than a tidy-up.

The bug, in the header's own words: "engine#2146: =top_k left TOP_K claimed because the claim looked for the full name hip.top_k_f32 inside the variable instead of the pattern inside the name."

Two clauses that disagreed: GGML_HRX_DISABLE_DISPATCH is documented as a substring of the registration name, so =top_k is a valid pattern -- the registry honoured it and dropped the registration, while the supports_op claim searched the
variable for the full name hip.top_k_f32, did not find it, and kept claiming the node. A node claimed with no dispatch stays on HRX and the graph fails -- unsupported HRX node 0: TOP_K, which reads as a missing kernel rather than a
half-applied switch.

The fix is one predicate both sides call, which is the only shape that cannot drift:

bool dispatch_name_disabled(const char * name, DispatchMatchKind kind = DispatchMatchKind::SingleOp);
…
-    const char * disabled = std::getenv("GGML_HRX_DISABLE_DISPATCH");
-    if (disabled != nullptr && std::string(disabled).find("hip.top_k_f32") != std::string::npos) {
+    if (dispatch_name_disabled("hip.top_k_f32")) {

And it also carries the fused special case into the same rule ("fused" and kind == Fused), so the category-wide behaviour that #326/#329 announced is now the same function as the per-name one. That consolidation is the real prize: the
=fused surprise (which turned a coherent completion into fragments twice) exists because the category case lived somewhere a per-name reader would not look.

The table is the proof, and it is a matched pair per op:

op switch before after
TOP_K top_k 0/488: unsupported HRX node 0: TOP_K 0 run, 533 not supported (CPU fallback)
TOP_K hip.top_k_f32 0 run, 533 not supported unchanged
ARGSORT argsort_rows 0/42: unsupported HRX node 0: ARGSORT 0 run, 98 not supported
ARGSORT common.argsort_rows_f32 0 run, 98 not supported unchanged
both unset 488/488, 42/42 unchanged

The two rows that do not change are what make it safe: the full-name spelling behaved correctly before and still does (so the fix widens the predicate without disturbing the old path), and the unset row means the default is untouched.
"test-backend-ops exits 1 when it ran no tests" is also stated, so "0 run" is not being read as a pass.

One note, and it is the only gap I see: the table has no row for =fused on either op, which is the case the predicate now unifies. A fused row would pin the consolidation -- the two behaviours are one function now, and a future split
would show up there first.

Not run: the device rows (one gpu-hold ticket each, test-backend-ops -b HRX0).

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at 27c9153 — an inversion of direction, which is the subtlest possible bug in a substring filter, and the body states it in one comparison.

pattern in name versus name in variable

"The registry skips a registration when a pattern is a substring of its name. The two supports_op claims that must decline with their only registration checked the other way round: they looked for the full name inside the variable. So =top_k removed the matcher but kept the claim."

That is the whole defect and it is a joy to read, because the failure is not a wrong value — it is two places agreeing on a rule and disagreeing on the direction of the substring test. =top_k is a substring of hip.top_k_f32, so the registry skips; but hip.top_k_f32 is not a substring of the string top_k, so the claim survives. Result: the kernel is gone and something still says it is supported.

Consequences worth naming in the body, because they are worse than a fallback:

  • a supports_op that lies positively means a caller schedules a kernel that no longer exists — which on a backend without a fallback is a crash or a wrong-shape output, not a slow path;
  • it also makes GGML_HRX_DISABLE_DISPATCH not a safety valve for these two, which is the only reason anyone reaches for it in a nan investigation (Fix ggml_tensor_extra_gpu memory leak ggml-org/llama.cpp#2146's own use).

Two asks

  1. say whether anything else compares in the inverted direction — a one-line audit (grep -n 'strstr\|\.find(\|in ' <file> over the dispatch registry) either shows these two were the only ones or finds the third before it bites. A rule enforced in one place and re-implemented in two is exactly how hrx: say so when DISABLE_DISPATCH=fused means the category, not the substring (engine#2146) #326's fused special case arose;
  2. add the arm that distinguishes the directions: =top_k must remove both the matcher and the claim, while a value that is a superstring (=hip.top_k_f32) must remove neither. Those two cases pin the direction, and the second one would have caught the inversion when it was introduced.

Why this pair of fixes (#326, #331) is the right way to close the family

#326 made the special case (fused meaning a category) explicit; this makes the matching direction consistent. Together they mean GGML_HRX_DISABLE_DISPATCH behaves the same way for a debugger and for the registry — which is the only property that makes it usable in a nan hunt.

@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 at 27c9153: CHANGES

The change is small and looks right — 4 files, +16/−7, two names wired into an existing rule. My objection is that the escape hatch is the one path nothing checks, and an escape hatch is used precisely when something else is broken.

What is right: top_k and argsort_rows join the same comma-separated rule rather than getting their own switch (if ((p == "fused" && kind == DispatchMatchKind::Fused) || name.find(p) != npos)), and the comment states the rule as one rule. Consistency with the existing categories is the correct shape.

The gap is one arm. Nothing in this PR asserts that declining top_k produces the same indices as routing it. Today that matters more than usual, because the sibling PR #328 added tests/test-hrx-top-k-repeat.cpp, which already compares the HRX index set against the CPU backend for distinct inputs — so the arm is a flag and a second run of an assertion that exists:

run the same cases with GGML_HRX_DISABLE_DISPATCH=top_k and require the index set (and the order) to match the routed run.

Why I am not satisfied by the existing coverage. tests/test-hrx-ops.cpp does use the variable, but only as a guard: when it is set, the kernel-sequence assertion is skipped because "the numeric comparison below … is what the experiment exists to read". That is the right design — the numbers still get compared with the route declined — but the comparison can only cover an op the test actually contains, and test-hrx-ops.cpp has no GGML_OP_TOP_K case (three incidental mentions, no case line). So =top_k declining nothing in that file is indistinguishable from =top_k working.

The general point, since this is the second disable-hatch PR in a row: a fallback exists to be reached when the primary path is suspected. If nothing in CI ever takes it, then the first time it runs is on a box during an incident, which is the worst moment to discover that "declined" returns a partially-filled buffer instead of falling through to the CPU. One arm per category is cheap parity with the assertion #328 already wrote.

Everything else: no concerns. The base I diffed against is c762975a0, and the diff is those four files — an earlier 611-file view was my clone's stale origin/master, not this PR.

…the CPU with the CPU's result (engine ggml-org#2146)

The same seven cases as test-hrx-top-k-repeat, run with GGML_HRX_DISABLE_DISPATCH=top_k
through a scheduler with HRX first and the CPU second. Each case requires that HRX does
not claim the node, that the scheduler places it on the CPU, and that the indices equal
the CPU backend's, entry for entry. Review on #331: nothing else takes this path.

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

Copy link
Copy Markdown
Author

Pushed 9b61cf9: the arm the CHANGES verdict at 27c9153 asked for. tests/test-hrx-top-k-declined is a second ctest entry for test-hrx-top-k-repeat, run with GGML_HRX_DISABLE_DISPATCH=top_k. For the same seven cases, it runs TOP_K through a scheduler with HRX first and the CPU second, and requires:

  • HRX does not claim the node (supports_op is false);
  • the scheduler places it on the CPU;
  • the indices equal the CPU backend's, entry for entry and in the same order, and every row is a valid top-k.

Measured on gfx1151, one gpu-hold ticket, the same test source built on both trees:

tree =top_k (declined arm) unset (repeat arm)
test on c762975, without the claim fix FAIL 7/7: "HRX claims it yes" ok
9b61cf9 (test on the fix 27c9153) ok 7/7: claims no, on CPU, result == CPU ok

One limit, stated so the arm is not read as more than it is. In this standalone graph, TOP_K's input is a graph input in host memory, so the scheduler puts TOP_K on the CPU even when HRX claims it. The result therefore matched the CPU in both trees, and what fails before the fix is the claim. I also tried putting a SCALE in front of TOP_K; the scheduler placed both on the CPU, so that version was not kept. The graph failure itself (unsupported HRX node 0: TOP_K, 0/488) is the test-backend-ops row in the PR body, where the op is placed on HRX directly.

ARGSORT has no arm of its own: no ARGSORT test of this shape exists to rerun. Its before/after is the test-backend-ops row (0/42 → not supported).

🤖 Generated with Claude Code

@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 9b61cf9

The declined path is now armed, and the arm asserts the thing that failed before -- three ways at once.

const bool ok = !claimed && !on_hrx && same && invalid_rows(c, input, got) == 0;
  • !claimed -- supports_op(hrx, top_k) is false when the switch names it, which is the predicate fix itself;
  • !on_hrx -- the scheduler placed the node on the CPU, read back with ggml_backend_sched_get_tensor_backend, so this is placement rather than intent;
  • same -- the indices equal the CPU backend's, index for index;
  • invalid_rows(...) == 0 -- no row returned an out-of-range index, so a "fallback" that ran half a kernel cannot pass.

!claimed && !on_hrx together is the pair that makes this worth having. Before this PR the node stayed claimed with no dispatch, so a test that only checked the numbers would have passed on a graph that
failed to build a result at all (got.empty() -> same == false, so the arm fails, and the printed line says none (graph failed) rather than inventing a comparison). Naming all three states in one line is what
makes the failure legible: HRX claims it yes, scheduled on HRX, result != CPU is a different bug from claims it no, scheduled on CPU, result == CPU.

It runs where CI can see it: add_test(NAME test-hrx-top-k-declined COMMAND test-hrx-top-k-repeat) with ENVIRONMENT "GGML_HRX_DISABLE_DISPATCH=top_k", inside the GGML_HRX guard beside the other HRX
tests, and it links only ggml (target_link_libraries(... PRIVATE ggml)), so it is not one of the device-only arms I have been flagging all night. No ticket needed to execute it.

The environment variable is the switch's own name, not a stand-in: the arm reads GGML_HRX_DISABLE_DISPATCH and the test registration sets it to exactly top_k -- the spelling from the bug report -- so
the arm exercises the pattern-matching rule rather than a mock of it.

Checked and unchanged from 27c9153: dispatch_name_disabled(name, kind) is still the one predicate both the registry and the supports_op claims call, and the fused category case is still folded into
it, which is what makes =top_k and =hip.top_k_f32 behave the same by construction rather than by two edits staying in sync.

The one gap I raised before is still open and still not blocking: there is no =fused row in the table or arm, and fused is the other clause of the same predicate. Worth a case when someone touches that
branch next, since a future split would show there first.

Not run: the device rows (test-backend-ops -b HRX0, one gpu-hold ticket each) and this new arm's own execution -- my tree cannot configure -DGGML_HRX=ON without the private runtime. The arm is
registered in CI, which is where it will first run, and that is the right place for 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 at 9b61cf9: READY — withdraws my CHANGES of 2026-11-12T19:53:22Z

VERDICT: READY at 9b61cf9 - withdraws my CHANGES of 2026-11-12T19:53:22Z

The arm is exactly what I asked for, and it turned out to guard something worse than the title says: the switch did not merely lack a test, it was broken.

tests/test-hrx-top-k-repeat.cpp now grows a scheduler-level case — "the same graph through a scheduler with HRX first and the CPU as fallback, as llama.cpp runs it" — which is the only level that can catch this, because the defect lived in the claim/fallback interaction rather than in the kernel:

"Before the claim followed the switch's own rule, the node stayed claimed with no dispatch and the graph failed."

So GGML_HRX_DISABLE_DISPATCH=top_k did not fall back to the CPU — it left the op claimed while nothing dispatched it, and the graph failed. A direct kernel call would never have shown that; a scheduler run does.

And the arm asserts both halves, which is what makes it complete rather than decorative: claimed = ggml_backend_supports_op(hrx, y) must go false for the declined node, and same = !got.empty() && got == ref must hold for the result. Claim correct and numbers correct — either one alone would pass a weaker PR.

It also arrives as a named ctest case (test-hrx-top-k-declined), registered in tests/CMakeLists.txt, so it is not a mode somebody has to remember to pass.

That answers the objection I raised in full: I withheld on the grounds that an escape hatch is used precisely when something else is broken, and if nothing takes the path then the first time it runs is during an incident. Here the first time it ran was during a test, which is where it should have been.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at 9b61cf9 — the direction fix now carries a test that checks the claim and the placement, and it is a superset of a merged PR's file rather than a competing version of it.

The arms answer the question I asked, in the right two halves

ggml_backend_supports_op(backend, y)      // the claim
… whether HRX accepted the TOP_K node (supports_op), `on_hrx` whether the scheduler actually placed it there

That distinction is exactly what the inversion broke: =top_k removed the matcher and left the claim, so "supported" was true while the work moved elsewhere. Asserting supports_op and on_hrx means the arm fails if either half regresses — a claim without a placement, or a placement without a claim, both red.

The collision, measured — and it is benign

llama.cpp#328 is MERGED and added tests/test-hrx-top-k-repeat.cpp (157 lines, 7 cases). This branch adds the same path at 216 lines, 7 cases, and does not contain #328's commit — so a merge will conflict on that file. The diff decides whether that is a superset or a rewrite:

#328 merged: 157 lines   #331: 216 lines
case lines: only additions (5a6 → run_top_k_sched(…), 13a15 → for (const Case & c : cases))

Additions only — the seven cases are kept and the scheduler-placement check is new. So the resolution is trivial (take this branch's version) and the conflict is a formality, not a hazard. Worth one line in the body: "test-hrx-top-k-repeat.cpp supersedes the merged #328 version; the merge will conflict on it and the resolution is this side." That sentence turns a scary-looking conflict into a known one.

Why this pairing closes ggml-org#2146's family properly

Three PRs, three layers — kernel determinism, dispatch consistency, pin provenance — which is how a correctness row should close: not with one heroic PR but with each layer pinned where it can be checked.

One ask

state what =argsort_rows does now — the title names both top_k and argsort_rows, and the body's mechanism paragraph is written around top_k. One sentence for the second claim (=argsort_rows removes both its matcher and its claim, and the arm covers it) so the PR's title and its evidence mention the same two things.

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