Skip to content

hrx: a TOP_K claim requires the registration, not only the shape (engine#2704) - #415

Merged
bong-water-water-bong merged 4 commits into
1bit/hrx-vulkan-patchedfrom
cl-856/2704-top-k-claim
Nov 16, 2026
Merged

bong-water-water-bong merged 4 commits into
1bit/hrx-vulkan-patchedfrom
cl-856/2704-top-k-claim

Conversation

@bong-water-water-bong

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

Copy link
Copy Markdown

device_supports_op answered TOP_K with the shape predicate alone:

if (op->op == GGML_OP_TOP_K) {
    return ggml::hrx::hip_top_k_tensor_supported(op);   // early return: no registry, no capability question
}

and that predicate says nothing about whether the op is registered. register_hip_top_k_dispatch returns before hip_declare_eager_op when the hip_top_k_f32 code object is not embedded, so on such a build every well-shaped TOP_K is claimed while no matcher can execute it — engine#2639's class again (claimed, then unsupported HRX node … TOP_K, graph_compute -1, nothing generated) instead of a CPU fallback.

Measured, on a build constructed for it

The fork compiles the code objects from its own hip/kernels/*.hip glob, so "the object is absent" is a configuration anyone can reproduce: same commit, kernel source moved aside, embedded table has no hip_top_k_f32 entry (0 vs 1 in the shipped table).

TOP_K f32/i32 cols 1024 k 8 rows 512, one node, no device
                                              shape  eager_declared  registry
no-object build (kernel source absent)          yes        no           no
shipped build                                   yes        yes          yes

The shape predicate is yes on both, so before this change the early return claimed the node in the first row, where registry=no means nothing dispatches it. eager_declared is the flag hip_declare_eager_op sets — the very call the registration path skips when the object is missing — so it is exactly the missing half of the question.

The change

return eager_capability_declared(GGML_OP_TOP_K) && ggml::hrx::hip_top_k_tensor_supported(op);

One conjunction, and deliberately not the alternative of deleting the early return and letting the generic tail decide: the tail ends in can_execute_standalone_op_as_graph, which builds a one-node graph and runs a registry match — a per-node cost that the MoE router pays once per layer, to answer a question this flag answers in a load. GGML_HRX_HIP_TENSOR_CLAIM=0 builds are unaffected (hip_eager_op_declared is not under that macro), and GGML_HRX_DISABLE_DISPATCH=hip.top_k_f32 was already handled by the predicate's own dispatch_name_disabled check.

Before / after, same two builds

                                             shape  eager_declared  registry | device_supports_op
                                                                             | BEFORE     AFTER
no-object build (kernel source absent)        yes        no           no    | CLAIM      decline
shipped build                                 yes        yes          yes   | CLAIM      CLAIM

Row 1 is the defect and its closure: the claim now agrees with the registry instead of asserting the opposite. Row 2 is the
control — on the build that ships, before and after are identical, so this costs nothing where the object exists.

Reproducing the second build (six commands, no device)

git -C <fork> worktree add --detach ~/wt/notopk <this sha>
mv ~/wt/notopk/ggml/src/ggml-hrx/hip/kernels/hip_top_k_f32.hip ~/top-k.hip.aside   # the only change
cmake -S ~/wt/notopk -B ~/wt/notopk-b -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_C_COMPILER=<rocm>/bin/amdclang -DCMAKE_CXX_COMPILER=<rocm>/bin/amdclang++ \
  -DHRX_SOURCE_DIR=<hrx-system> -DGGML_HRX=ON -DGGML_VULKAN=OFF -DGGML_HIP=OFF -DGGML_CPU=ON \
  -DLLAMA_BUILD_SERVER=OFF -DLLAMA_CURL=OFF -DLLAMA_BUILD_TESTS=OFF
cmake --build ~/wt/notopk-b -j 4 --target ggml-hrx        # ~7 min, thermal-run here
grep -c hip_top_k_f32 ~/wt/notopk-b/ggml/src/ggml-hrx/ggml-hrx-hip-code-objects.inc   # 0 (the shipped table: 1)

Then link any small program against that bin/libggml-hrx.so and print hip_top_k_tensor_supported(op),
hip_eager_op_declared(GGML_OP_TOP_K) and can_execute_standalone_op_as_graph(op, "<arch>") for one TOP_K node. The
arch string matters: find_dispatch_registry only answers for the architectures it builds, so a wrong one makes the
registry column say no for a reason that has nothing to do with this change (it cost me one wrong reading).

Rebuilt both libggml-hrx at this commit to take the numbers (~/wt/cl-856-notopk-b and the shipped build), probe source in the
PR text above: one node (f32 in, i32 out, cols 1024, k 8, rows 512), no device, target gfx1151.

Why there is no unit arm

The line is inside a static function (device_supports_op), so a host test cannot call it, and an arm on the conjunction would be tautological. The instrument that can tell the difference is the two-build probe above, which is why it is in the PR text with the exact configuration that reproduces it. If a reviewer wants it in-tree, the natural home is a small tools/ probe with the kernel-omission recipe documented, not a ctest.

Refs engine#2704

bong-water-water-bong added 4 commits November 16, 2026 12:28
…tchers (engine#2704)

device_supports_op decided placement from the op code while the decision that matters at compute
time was made by the registered matcher, so every disagreement became another hand-written clause in
the placement path: a batched IQ3_S source (llama.cpp#407) and a strided VIEW source (engine#2639)
each cost one. The second was also narrower than the family it stood for -- it checked the source's
contiguity and not the ids' or the output's, so a GET_ROWS with a strided ids tensor was claimed
here and executed by nobody.

The family has two forms with different conditions, so the fix is not one conjunction:

  * the contiguous form (match_get_rows_f32 and the scale_f32 / rmsnorm_binary forms that call it)
    needs weight, ids and output contiguous;
  * the route-table form (match_get_rows_scalar_3d_f32, the qwen3.6-35b routed experts) reads the
    ids through a row stride, so its ids are deliberately NOT contiguous.

A conjunctive "ids contiguous" in the placement predicate would therefore have moved every routed
GET_ROWS to the CPU while every per-matcher test stayed green. So the facts are written once
(dispatch-get-rows-support.h), the two forms are two named predicates that their own matchers call,
and placement asks the union: "can SOME form execute this node".

The new test is host-only (no device, no graph, no libhrx) because the defect is two callers
disagreeing, which neither caller's own test can catch; 9 arms, including the routed shape that a
conjunctive predicate would break and the two shapes that each cost a decline.

Refs engine#2704
#408 and this branch rewrite the same GET_ROWS block, so the resolution keeps #408's derivation
(source contiguity is what every matcher gates; ids must not be constrained as a conjunction,
because match_get_rows_scalar_3d_f32 admits strided ids on purpose) and states it in the new place
instead of repeating it: the facts carry a per-form predicate, so the ids constraint lives with the
form that has it and the union still claims the routed shape. The batched-IQ3_S clause folded into
the same predicate, so the placement path no longer carries two hand-written GET_ROWS clauses.

Rebuilt at bb71f1b: test-hrx-get-rows-facts 9/9, ctest #75 Passed.
#412)

Three text corrections after the review at d273fb3, no behaviour change:

1. The direction was inverted. An over-approximation in PLACEMENT is the unsafe direction -- it is
   engine#2639 itself, a claimed node executed by nobody. Said so plainly.
2. The header claimed "a condition cannot be in one caller and not the other". That is true of the
   conditions moved into the facts (contiguity, type, batch) and false of the ones that need the
   graph. Narrowed to what the code does, and recorded WHY the rest is not a hole: the tail of
   device_supports_op asks the registry (can_execute_standalone_op_as_graph ->
   DispatchScheduler::supports_node), measured on the box at this pin -- an F16 destination gets no
   standalone dispatch (so the tail refuses it) while a 3-D destination gets one (so declining it
   here would be the #407 error of refusing a path that works).
3. The same two facts are recorded next to the comment that says GET_ROWS is "no longer stated here
   at all", so a reader cannot conclude the un-moved conditions are unguarded.

test-hrx-get-rows-facts 9/9 after the edit (comments only, but the row is the PR's proof and it runs
in the same target).
…ine#2704)

device_supports_op answered TOP_K with hip_top_k_tensor_supported(op) alone, and that
predicate says nothing about whether the op is registered: register_hip_top_k_dispatch
returns BEFORE hip_declare_eager_op when the hip_top_k_f32 code object is not embedded.
On such a build every well-shaped TOP_K was claimed with no matcher to execute it --
engine#2639's class, "unsupported HRX node ... TOP_K", graph_compute -1, nothing
generated, instead of a CPU fallback.

Measured on a build constructed for it (same commit, hip/kernels/hip_top_k_f32.hip moved
aside, so the embedded table has no object; 0 entries against 1 in the shipped table),
one TOP_K node, no device, target gfx1151:

                       shape  eager_declared  registry | before  after
  no-object build       yes        no           no     | CLAIM   decline
  shipped build         yes        yes          yes    | CLAIM   CLAIM

eager_capability_declared is the flag hip_declare_eager_op sets, i.e. exactly the call the
registration path skips when the object is missing, so it is the half of the question the
early return was missing. The alternative -- deleting the early return and letting the
generic tail decide -- was rejected: that tail ends in can_execute_standalone_op_as_graph,
a one-node graph build plus a registry match per node, which the MoE router pays once per
layer. GGML_HRX_HIP_TENSOR_CLAIM=0 builds are unaffected (hip_eager_op_declared is not
under that macro) and GGML_HRX_DISABLE_DISPATCH=hip.top_k_f32 was already handled by the
predicate's own dispatch_name_disabled check.

No unit arm: the line is inside a static function, and an arm on the conjunction would be
tautological. The two-build probe is the instrument that distinguishes them and it is in the
PR text with the recipe.

Refs engine#2704
@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 9a4d320: READY — and the audit this PR enabled says two more ops have the same hole open right now.

The measurement is the strongest form: build the absence

"The fork compiles the code objects from its own hip/kernels/*.hip glob, so 'the object is absent' is a configuration anyone can [construct]"

TOP_K f32/i32 cols 1024 k 8 rows 512, one node, no device
                                shape  eager_declared  registry
no-object build (kernel source absent)  yes       no          no

Three predicates disagreeing in one row is the whole bug, and constructing the missing-object build is how you find out that register_hip_top_k_dispatch returns before hip_declare_eager_op. Nobody has to imagine the failure mode: it is a build flag.

The structural finding, which is worth more than the TOP_K patch

device_supports_op ends by asking the registry (can_execute_standalone_op_as_graph → DispatchScheduler::supports_node), and the file documents a three-tier design where tier 3 is "eager_capability_declared(op->op) on its own, for everything else". So the class is precisely: an op that early-returns before the tail, with only a shape predicate.

With that lens, the audit is one pass:

1755  GGML_OP_TOP_K   -> guarded by this PR ✓ (eager_capability_declared && shape)
1761  GGML_OP_ARGSORT -> return small_rows_argsort_tensor_supported(op);        no capability, no registry
1773  GGML_OP_DUP     -> type/contiguity decision, returns before the tail      no capability, no registry
1817  GGML_OP_NONE    -> early return                                          (probably exempt; say why)

ARGSORT and DUP have exactly the hole this PR fixes. It is latent rather than active for the same reason TOP_K's was — their code objects are embedded in this build (my trace census shows ggml_argsort_rows_f32 specialising 5 times) — and it becomes real in the same configuration: a hip/kernels/*.hip glob that does not contain them. That is one build flag away, not a hypothetical.

The ask, and it is two lines plus a clause

Either guard them the same way (eager_capability_declared(op->op) && shape), or state in the comment why they cannot hit it — a per-op capability flag that is set unconditionally, for instance. And one clause for GGML_OP_NONE, so the next reader does not have to work out whether it is exempt or forgotten.

That converts this PR from "the third instance fixed" into "the class closed", and it is the difference between a sequence of patches (#412/#413 for GET_ROWS, this for TOP_K, and then whoever hits ARGSORT next) and one place where the rule lives.

Credit where the sequence deserves it

The comment measures the GET_ROWS registry too ("8 GET_ROWS registrations … an F16 destination"), so this PR is written with its siblings in view rather than in isolation — the three of them now describe one defect from three ops.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Two more rows for the table, both measured on the builds already at this commit — the GGML_HRX_DISABLE_DISPATCH sentence in the body was a claim until now:

                                                        shape  eager  registry | BEFORE     AFTER
shipped build, default                                   yes    yes     yes     | CLAIM      CLAIM
shipped build, GGML_HRX_DISABLE_DISPATCH=hip.top_k_f32    no     yes     no      | decline    decline
no-object build, same env                                 no     no      no      | decline    decline

The middle row is the one worth reading: the registration is disabled, so the shape predicate itself returns no (it calls dispatch_name_disabled) while eager_declared stays yes (the declaration happens before the disable check in register_hip_top_k_dispatch). The conjunction is yes && no = decline, which is why adding it cannot widen anything: it only ever removes a claim, never adds one. That is the property I most wanted a measurement for, because the risk of a change like this is always "did it claim something new".

GGML_HRX_HIP_TENSOR_CLAIM=0 is the last unmeasured sentence in the body; that build is running now and I will add its row here when it lands (it should read yes/yes/yes -> CLAIM/CLAIM, i.e. identical to the default, since hip_eager_op_declared is not under that macro).

@bong-water-water-bong

Copy link
Copy Markdown
Author

The last unmeasured sentence in the body, now a row — GGML_HRX_HIP_TENSOR_CLAIM=0, i.e. the build the fork uses as its "answer as it did before the HIP hook" mutant:

                                                          shape  eager  registry | BEFORE   AFTER
shipped, default                                           yes    yes     yes     | CLAIM    CLAIM
shipped, GGML_HRX_HIP_TENSOR_CLAIM=0                       yes    yes     yes     | CLAIM    CLAIM
shipped, GGML_HRX_DISABLE_DISPATCH=hip.top_k_f32            no     yes     no      | decline  decline
no-object build                                           yes    no      no      | CLAIM    decline
no-object build + same env                                 no     no      no      | decline  decline

Rows 1 and 2 are identical, which is what the body asserted and had not checked: hip_eager_op_declared is not under GGML_HRX_HIP_TENSOR_CLAIM, so that mutant build answers exactly as the default does and the conjunction changes nothing there. Row 4 stays the one behaviour change in this PR, and it changes a claim that nothing could execute into a CPU fallback.

That is every environment the diff touches: default, the macro mutant, the env-disabled registration, and a build without the code object. The remaining row is the device one (test-backend-ops -b HRX0 -o TOP_K), queued in a gpu-hold ticket — on a build with the object the claim is unchanged, so it should report the same cases as before the diff; I will post it here when the ticket runs.

@bong-water-water-bong

Copy link
Copy Markdown
Author

The sibling question this change invites: are the other early returns in device_supports_op the same shape? Swept, with measurements, so a reviewer does not have to:

early return does its predicate consult the registration? verdict
TOP_K (this PR) no — shape only the bug
ARGSORT → small_rows_argsort_tensor_supported yes — it starts with dispatch_name_disabled("common.argsort_rows_f32"), and that registration is a Loom kernel from the corpus, so it exists unconditionally (no code object to be missing) no equivalent bug
VIEW Q8_0 (a quantized KV-cache alias) a view is an alias, never dispatched; the function already treats leaf NODE nodes as covered not a claim of work
zero_output_elision_supported requires `nelements == 0
the supported_*_tensor family these are the "guarded by a predicate" mechanism, and they are hand-written shape rules not swept by this PR; they are the graph-level question ggml-hrx.cpp:1697 names

Measured for the ARGSORT row (placement predicate vs registry, three shapes): contiguous f32 512x2 → yes/yes; strided source → no/no; cols 1024 (its own bound) → no/no. Agree everywhere, which is what "the predicate already asks" predicts.

So TOP_K is the only early return in that function whose answer can be yes while nothing can execute the node, and the difference is why: it is the only one fronting a HIP code object, and code objects are the one registration input that a build can lack.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 16, 2026 18:57
@bong-water-water-bong

Copy link
Copy Markdown
Author

Promoted from draft: ready for review at 9a4d320. The reason it was a draft is gone — llama.cpp#412 merged as 5450c8054, and the engine pin bump that carries it is engine ggml-org#2739. This PR is independent of both (its base is 1bit/hrx-vulkan-patched at bb71f1be0 and it touches only the TOP_K early return), so it needs no stacking.

Summary of what a reviewer should weigh, in one place:

  • the defect: device_supports_op returned the shape predicate for TOP_K with nothing consulting the registration, so on a build without the hip_top_k_f32 code object every well-shaped TOP_K was claimed and none could execute — engine#2639's class;
  • the fix: one conjunction (eager_capability_declared(GGML_OP_TOP_K) && hip_top_k_tensor_supported(op)), which can only ever remove a claim;
  • the evidence: five configurations measured (default / GGML_HRX_HIP_TENSOR_CLAIM=0 / env-disabled / no-object build / no-object+env), of which exactly one changes behaviour (the no-object build: CLAIM -> decline), plus the sibling sweep showing ARGSORT does not have the same bug;
  • what is missing: the device control row (test-backend-ops -b HRX0 -o TOP_K) has not run — its ticket died queued, and I chose not to re-queue it while older rows' work held the GPU queue. It is a control, not the evidence: rows 1-2 of the table already show the change is inert when the code object exists. Say the word and it goes back in the queue.

@bong-water-water-bong
bong-water-water-bong merged commit 2af6b0d into 1bit/hrx-vulkan-patched Nov 16, 2026
@bong-water-water-bong
bong-water-water-bong deleted the cl-856/2704-top-k-claim branch November 16, 2026 19:09
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