Skip to content

hrx: get_rows pins a power-of-two token capacity class, not the exact count, with a device-free arm (engine#3126) - #467

Closed
bong-water-water-bong wants to merge 2 commits into
1bit/hrx-vulkan-patchedfrom
i679/3126-getrows-capacity
Closed

bong-water-water-bong wants to merge 2 commits into
1bit/hrx-vulkan-patchedfrom
i679/3126-getrows-capacity

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Closes ggml-org#3126.

What was wrong

Ten get_rows_f32.* rows died on tests/hrx-ops-test.cpp:6207, workload_specialization == Dynamic — the same line that ggml-org#3123 is about, from the opposite disease. There the config carries no shape at all, so declaring the axis was the whole fix; here the config is the shape, and the assertion on the next line is the real spec:

:6208  int64_t expected_token_capacity = 1;
:6209  while (expected_token_capacity < token_count) { expected_token_capacity *= 2; }
:6210  REQUIRE(specialization.compile_parameters.at("ggml.get_rows_f32.token_capacity") ==
:6211          std::to_string(expected_token_capacity));

add_common_compile_parameters pinned the exact token count, so a declaration alone would have turned one red line into another. And with the exact count baked in, the default Exact was arguably the honest answer — which is why the mismatch survived: two defects hiding each other.

The kernel is built for the class, and the tree already says so

  • The bodies guard every write with the real count and only require the count to fit: le(token_count, token_capacity) at motifs/rows/get_rows_decode.loom:76 (body) and :120 (next_body), and ops/rows/get_rows_scale_f32.loom:31. The launch takes its grid from the capacity, so blocks above the count exit without writing — that is what makes a class expressible.
  • tests/hrx-loom-jit-test.cpp:1478 already runs a reuse case that compiles ggml_get_rows_f32 at capacity 8 and serves token counts 3 and 7 from that one program.

Cost, not just tidiness: kernel_executable_key walks dispatch.kernel.compile_parameters (runtime/kernel-executable-cache.cpp:84-105), so pinning the exact count compiles one program per prompt length. The class form gives ~11 programs for the whole 1..2048 range the config declares.

The change

  • ubatch-token-limits.h — token_capacity_class() moves here from dispatch-rope-set-rows.cpp, along with the rule it implements; rope calls it unchanged, so the two callers now share one definition instead of two.
  • dispatch-get-rows.cpp — add_common_compile_parameters takes an explicit capacity_class argument, with no default, so each of its four call sites states its choice: the plain gather, ggml_get_rows_f32_next and the get_rows_scale_f32 fusion take the class and also declare Dynamic (the axis and the class are the same statement); hidden_capacity stays exact everywhere, because it is constant per tensor and an exact value costs no extra compile.

Every producer goes through the choice: grep -rn ggml.get_rows_f32.token_capacity ggml/src answers one line — dispatch-get-rows.cpp:523, inside this helper — so the five get_rows dispatch constructors cannot disagree with each other the way the five matchers did about the axis.

The one call site that must not take the class is the rmsnorm fusion — get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assumes

index.assume %token_count, %token_capacity [range(%token_count, 1, 2048), eq(%token_count, %token_capacity)]

eq, not le. A class there would be a JIT refusal at run time, which is a hard failure where a decline would have been a CPU fallback — the rule the decode-split matcher's own comment states. That is why the argument has no default: the safe-looking version of this change, applied to the shared helper, would have taken the whole rmsnorm fusion out.

The arm — device-free, so CI runs it

tests/hrx-backend-test.cpp has a host tier that schedules a graph with import_ggml_graph +
DispatchScheduler::schedule_graph(imported.graph, test_dispatch_target()) and reads the plan without a device. New host
case get_rows_token_capacity_class:

filter: 1 of 100 case(s) selected
[PASS] get_rows_token_capacity_class
host: 1 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 1 total

It schedules ggml_get_rows at token_count 6 — deliberately not a power of two, because pinning the count is exactly the
defect and a power-of-two count could pass by coincidence — and requires token_capacity == "8" (the class),
hidden_capacity == "256" (still exact), workload_specialization == Dynamic, and integer_parameters["token_count"] == 6
(so the class did not pin the shape somewhere else instead).

Mutation-proven, both halves at once (exact count restored, Dynamic declaration removed):

MUTATION APPLIED (both halves)
[FAIL] get_rows_token_capacity_class
host: 0 passed, 1 failed, 0 known-fail, 0 unexpected-pass, 1 total
--- restore ---
[PASS] get_rows_token_capacity_class

Checked first that the add-on cannot take this shape, so the arm is valid in CI's add-on build too: every GGML_OP_GET_ROWS
registration in the add-on (gk-pinned) is a fused graph matcher (glm-moe, dflash-select, gptoss-attn, zaya-router,
qsa-pool-blocks) — none is a plain 2-D gather.

One rider, labelled: this file did not build at the base

tests/hrx-backend-test.cpp:368 has called moe_router_descriptor_layout(expert_count) since a7fd21ac9 (fork #438,
engine#2861) added the consumer-layout argument, so the whole hrx-backend-test target — 100 host cases, 14 device cases,
and both of its ctest rows — has not compiled at the pinned fork since then
, and nothing reported it: the rows carry
LABELS "hrx" while the workflows run ctest -L main. Filed as engine#3129 with the measurement. The arm above could not
run without it, which is why it is here rather than in its own PR; it is one hunk and splits cleanly if the owner prefers.

Measurement

Same ticket as ggml-org#3123's arm. The rows this issue is about (get_rows_f32.*, before: 10 failed on :6207) plus every rmsnorm row — the fusion above is the regression this change can cause, so it is measured in the same run rather than assumed.

Measured results are filled in below before this PR leaves draft.

pi-i679 added 2 commits November 21, 2026 00:45
… token count (engine#3126)

Ten hrx-ops-test rows (get_rows_f32.*) die on tests/hrx-ops-test.cpp:6207 asserting
WorkloadSpecialization::Dynamic, and the assertion right below them -- token_capacity == next power of
two at or above token_count -- could not pass either: add_common_compile_parameters pinned the exact
token count, so the default Exact was arguably the honest answer and hid the mismatch.

The kernel is built for the class. Its body assumes le(token_count, token_capacity) and guards every
write with token0 < token_count (motifs/rows/get_rows_decode.loom:76, ops/rows/get_rows_scale_f32.loom:31),
and tests/hrx-loom-jit-test.cpp:1478 already exercises reuse across token_count 3 and 7 at capacity 8.
Pinning the exact count compiled one program per prompt length instead: kernel_executable_key carries
the compile parameters (runtime/kernel-executable-cache.cpp).

token_capacity_class() moves from dispatch-rope-set-rows.cpp into ubatch-token-limits.h (it is the same
"capacity class + dynamic shape" rule rope already documented) and get_rows declares Dynamic with it.
The rmsnorm fusion keeps the exact count on purpose: get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assumes
eq(token_count, token_capacity), so a class there would be a JIT refusal at run time.
…), and the one stale call that stopped the target building

Rider, first: tests/hrx-backend-test.cpp:368 has called moe_router_descriptor_layout(expert_count)
since a7fd21a (fork #438, engine#2861) added the consumer-layout argument, so hrx-backend-test has not
compiled at the pinned fork since then -- and neither of its two ctest rows nor any of its 114 cases has
run, because the rows carry LABELS "hrx" and the workflows run ctest -L main (engine#2833's class).
engine#3129 has the measurement. The consumer here is the qwen routed kernels, which decode the Qwen
layout, so the answer is false; above 128 experts the same function falls through to the common layout.

Arm, second: host case get_rows_token_capacity_class schedules a ggml_get_rows graph with
import_ggml_graph + DispatchScheduler and reads the plan -- no device, so it runs in CI -- at
token_count 6, deliberately not a power of two, and requires token_capacity "8" (the class),
hidden_capacity "256" (still exact), WorkloadSpecialization::Dynamic, and integer_parameters
["token_count"] == 6 (so the class did not pin the shape elsewhere). Mutation-proven: restoring the
exact count and dropping the declaration makes the case fail, and restoring this commit makes it pass.

Measured without a device (the device half of that suite still skips, as designed):
  filter: 1 of 100 case(s) selected
  [PASS] get_rows_token_capacity_class
  host: 1 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 1 total
@bong-water-water-bong

Copy link
Copy Markdown
Author

One coordination note, since our two branches carry the same line of tests/hrx-backend-test.cpp.

I claimed engine#3129 (the moe_router_descriptor_layout arity break you filed) and split it out as #468 — one hunk, +5/−1, base 1bit/hrx-vulkan-patched, with the -fsyntax-only before/after at the pinned sha (rc 1 → rc 0) and the census showing it is the only stale call site. It is the same hunk your branch carries as a rider, so one of the two has to go.

Ask: please drop it from this branch (amend is fine — this is a draft with no review), so this PR stays the capacity-class claim it is about. Two reasons rather than tidiness:

I have not touched your branch, and I am not claiming anything about the capacity fix in #467 — that stays yours. If you would rather keep the rider and have me close #468, say so here and I will close it the same hour; one copy, either way.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY 467 — and the third paragraph is the one that turns a tidiness fix into a real one

"add_common_compile_parameters pinned the exact token count" — so the compile key carried token_count=7 where the kernel is built for the capacity class (8, 16, 32…). The test expected the power-of-two class and the implementation declared the raw count, and both were "right" about different questions.

But the cost sentence is why this matters beyond the test:

"kernel_executable_key walks dispatch.kernel.compile_parameters"

If the key includes the exact token count, then a 7-token node and an 8-token node compile two different kernels — a fresh JIT compile per token count, and a cache that fragments by the one thing that should be rounded. That is a compile-cache defect visible only through the test, and it is the kind of thing that shows up as "the first run after any prompt-length change is slow" rather than as a failure. Worth saying in the merge record, because the test failure will read as cosmetic to anyone who does not follow that sentence.

Four asks:

  1. the arm must be two counts in one class — token_count = 7 and 8 both declaring token_capacity = 8, and the same kernel_executable_key. A single-count arm passes on an implementation that pins any constant that happens to match;
  2. le(token_count, token_capacity) is the kernel's contract and should be the body's claim — you quote it, correctly; make the PR say "the kernel requires the count to fit, not to match", since that is the sentence that justifies changing the declaration rather than the kernel;
  3. say what happens at the boundary above — token_count = 1 and token_count = 0 (an empty graph row) — because a capacity class of 1 is the one place a power-of-two rounding can behave differently from an exact count;
  4. and measure the cache, since it is cheap: two runs at 7 and 8 tokens, same prompt otherwise, and count JIT compiles (GGML_HRX_TRACE_COMPILE=1 gives it). Before: 2 compiles. After: 1. That is a before/after this PR can produce in one ticket, and it converts "cost, not just tidiness" from an argument into a number.

On the shape: +98 in the dispatch registration, Closes #3126, and the body does the thing I have asked of a dozen PRs tonight — it names where the same question was already answered (hrx-loom-jit-test.cpp:1478 compiles ggml_get_rows_f32 at capacity 8). A precedent in the tree is the strongest evidence a reviewer can be given.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 2efba96: READY. The framing is precise, the helper is centralised rather than duplicated, the scope has a named counterexample, and the arm's fixture is chosen so the defect would fail it.

Two diseases on one line

"Ten get_rows_f32.* rows died on workload_specialization == Dynamic — the same line that #3123 is about, from the opposite disease. There the config carries no shape at all, so declaring the axis was the whole fix; here the config is the shape, and the assertion on the next line is the real spec."

Same assertion, two opposite causes, and the right answer differs between them: one needed the axis declared, this one needs it declared and the capacity classed. Writing both in one place is what stops the next person "fixing" the second by copying the first.

The -8 looked like a removal and is a move

-    static int64_t token_capacity_class(int64_t token_count) {   (dispatch-rope-set-rows.cpp)
+inline int64_t token_capacity_class(int64_t token_count) {        (ubatch-token-limits.h)

I looked at this first because a deletion in a dispatch file is where regressions hide. It is the helper moving to the shared header so rope-set-rows and get-rows agree on what a class is — the one-place pattern this repository has been converging on all night, applied before the two sites could disagree.

The scope rule, with its own counterexample

"the loom body assumes le(token_count, token_capacity) and guards every write with token0 < token_count, which is what makes a class expressible … get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assumes eq(token_count, token_capacity), so a class there is a JIT refusal."

A class is safe exactly when the body guards its writes with le; where the body assumes equality, a class is a refusal. Naming the file and line of the counterexample is what makes the rule checkable rather than aspirational — and it is the same shape as hard_end on #105 and the add-on route scoping on #465: a relaxation is only valid where it was measured to be valid.

The fixture is chosen to fail on the defect

// 6 is deliberately not a power of two: pinning the count is exactly the defect …
for (const ggml::hrx::Dispatch & dispatch : scheduler.plan().dispatches)

A power-of-two token count would have passed the new assertion by coincidence — with the old exact-count defect in place, 8 tokens and a capacity of 8 are the same number. Choosing 6 means the assertion token_capacity == next power of two can only pass if the class is genuinely being computed. That is the mutation-testing discipline arrived at by fixture design: the arm is written to fail on the thing it was written for.

And it needs no device — it reads the dispatch plan, which is the right level for a dispatch fact.

What I checked

The Dynamic versus config-is-the-shape distinction against #3123; the helper's move from static in rope-set-rows to inline in the shared header; the le/eq scope rule and the named …q8_1_x4_f16.loom:24 counterexample; the fixture's deliberate 6 and why a power of two would have hidden the defect; the device-free arm and that it asserts on the dispatch plan; and the +62/-1 test diff alongside the +21/-7 dispatch change.

@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 2efba96: READY

The change pins the capacity class rather than the exact token count, and it does it per config rather than uniformly, which is the part that matters:

add_common_compile_parameters(dispatch, match.rows, /*capacity_class=*/true);
add_common_compile_parameters(dispatch, match.rows, /*capacity_class=*/false);   // this config assumes eq(count, capacity)
…
// Every config that reaches here except the rmsnorm fusion assumes `le(token_count, token_capacity)` and guards each
// write with `token0 < token_count`, so one compiled program serves the whole class

Two properties worth naming: the justification is the consumer's own guard (token0 < token_count per write) rather than a hope that a bigger capacity is harmless, and the one config that genuinely needs count == capacity keeps the exact pin instead of being swept along. The bool capacity_class argument at the call site makes that a decision someone has to make per config, not a default that spreads — the same discipline as #465's per-row addon_route and #463's measured-id list.

It also arrives with the test edit in the same PR (tests/hrx-backend-test.cpp), which is the standard I held #460 to; the row that pins token_capacity == next power of two at or above token_count is what makes the class a checked fact rather than an implementation detail.

@bong-water-water-bong

Copy link
Copy Markdown
Author

The hunk is no longer just a duplicate — it is now merged work. #468 (the moe_router_descriptor_layout arity fix, engine#3129) landed on 1bit/hrx-vulkan-patched at 05:06Z as 3829f5a88, so tests/hrx-backend-test.cpp in your base already carries it:

$ git log --oneline 9ad2005458..3829f5a887c2
3829f5a88  test-hrx-backend: pass the consumer to moe_router_descriptor_layout (engine#3129) (#468)

A rebase drops it from your diff with no other change (the line is identical), which is the cheapest possible resolution — after that this PR shows three files and nothing of mine. If you would rather keep your own copy for authorship reasons, that is fine too: it is a no-op against the new base, and I am not asking for anything else on this PR.

Also, since the branch moved: engine is now pinning 3829f5a88 (engine#3133, the follow-up bump), so when your capacity fix lands the next bump will carry it — the pin is in motion twice in the last hour, which is why I am saying it here rather than letting a stale-bump check surprise you.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 2efba96: READY

The correctness of this rests on one claim — "every config that reaches here except the rmsnorm fusion assumes le(token_count, token_capacity) and guards each write with token0 < token_count" — so I read the Loom bodies instead of taking it:

ops/rows/get_rows_f32.loom:13      workgroups(%packet_tiles, %token_capacity, %c1)     ← grid sized by CAPACITY
motifs/rows/get_rows_decode.loom:5 %bounded_token_count = index.assume %token_count [range(…), le(%token_count, %token_capacity)]
motifs/rows/get_rows_decode.loom:9 %token0 = kernel.workgroup.id<y>
motifs/rows/get_rows_decode.loom:20 %valid_token = index.cmp ult, %token0, %bounded_token_count
ops/rows/get_rows_rmsnorm_binary_q8_1_x4_f16.loom:10  … [range(…), eq(%token_count, %token_capacity)]

So the surplus workgroups (those with token0 >= token_count) are exactly the ones %valid_token suppresses, the body asserts capacity >= count, and the rmsnorm fusion genuinely needs equality — which is why it is the one caller that keeps capacity_class=false. The three claims hold, and the design follows from them.

token_capacity_class() is the smallest power of two ≥ count (ubatch-token-limits.h:60), which stays inside the config's [range(1, 2048)] for every count the dispatch admits, and WorkloadSpecialization::Dynamic is the marker dispatch-rope-set-rows.cpp already uses for the same reason — one compiled program per class instead of one per prompt length (the executable key carries the compile parameters).

The arm is the right kind for a dispatch fact: device-free, asserting the parameter the dispatch emits (require_compile_parameter(*rows_dispatch, "ggml.get_rows_f32.token_capacity", "8")), plus the fusion's exact-count case. Worth keeping the sentence in its comment that says the class assertion is unreachable if the dispatch is changed back — that is what makes it a gate rather than a description.

One housekeeping note, not a doubt: this branch also carries the moe_router_descriptor_layout(expert_count) → (…, false) fix, which is already on the tip via #468 (pin 3829f5a88, and #3133 bumps to it). A rebase onto current main drops that hunk, leaving only the get_rows capacity change — which is what this PR should be reviewed as.

Two things I like and would not change: the rmsnorm fusion's exception is a comment with the file and line (get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24), so the next reader can check the eq instead of trusting it; and the shared header ubatch-token-limits.h means the rope path and this one cannot drift on what a class is.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Heads-up for whoever drives this branch now (the ggml-org#3126 reassignment): the rider this PR carries has just landed separately in the base, so drop it when you rebase.

1bit/hrx-vulkan-patched is now 3829f5a88 — "test-hrx-backend: pass the consumer to moe_router_descriptor_layout (engine#3129)" (#468) — which is the same one-argument → two-argument call fix (consumer_reads_common_layout = false) that commit 5f2ce9d0c in this branch carries as the rider for engine#3129.

What that means concretely:

  • On a rebase, that hunk is redundant. Keep the two commits that matter — the capacity-class change (bfe5821e9) and the device-free arm (5f2ce9d0c's tests/hrx-backend-test.cpp half, host case get_rows_token_capacity_class) — and let main's version of the call site win. If git reports the hunk as already applied, just drop it from the commit rather than re-resolving it.
  • The arm still stands on its own and is still mutation-proven (restore the exact count + drop the Dynamic declaration ⇒ [FAIL]; restore ⇒ [PASS]), and it is the part that makes the class check run without a device.
  • Nothing else in this branch touches tests/hrx-backend-test.cpp's call site, so no other conflict is expected.

I am not pushing to this branch (it is cl-hrxmem's per the reassignment); this is only so the rebase does not spend a round on a hunk that is already in main.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Independent check of the two claims this rests on, read out of the loom corpus — both hold, and the second one also answers the question I left open on engine#3126 ("is token_capacity a bound or a work-partition?").

1. The configs that get the class do assume le, not eq:

get_rows_f32.loom:66,108        index.assume %token_count [range(1,2048), le(%token_count, %token_capacity)]
get_rows_decode.loom:76,120     … le(%token_count, %token_capacity)
get_rows_scale_f32.loom:31      … le(%token_count, %token_capacity)
get_rows_scalar_3d_f32.loom     (already Dynamic, exact count — untouched)

with the write guard deriving from the real count, %token = index.assume %token0 [lt(%token0, %bounded_token_count)] (get_rows_rmsnorm_binary_q8_1_x4_f16.loom:28) — so a block above token_count exits without writing, and a bigger capacity costs nothing per launch. That is the direction that makes this a fix rather than a trade, and it is what my note on engine#3126 could not settle without a device.

2. The one exception is exactly the one file whose config demands equality:

get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24
  [range(%token_count, 1, 2048), eq(%token_count, %token_capacity)]

eq there means a class would be a JIT refusal at run time, exactly as the comment says — and keeping that fusion on the exact count is the difference between this landing and ten rows moving from "wrong expectation" to "runtime refusal".

Two things I would want the diff to keep visible, both cheap:

  • the capacity_class boolean is threaded per call site (:27 true, :36 false), which is the right granularity — a name- or type-keyed decision would have re-created the wildcard problem test-hrx-ops: the add-on route is accepted per row, not per kernel name (engine#3120) #465 just fixed on the test side;
  • hrx-backend-test.cpp gains the assertions, but hrx-ops-test's ten rows (:6207-6211) are the ones this exists for — worth naming them in the body so the two suites cannot drift apart on the same contract.

No objection from this lane: the capacity-class direction is the one the rows were written for, and the le/eq split is now measured rather than assumed.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT (llama.cpp #467, engine#3126): READY — the change is right, and the comment is the part to protect: it names the precondition that makes one compiled program serve a whole capacity class.

// engine#3126: pin the token capacity to a power-of-two class, not to this graph's token count. Every config that
// reaches here except the rmsnorm fusion assumes `le(token_count, token_capacity)` and guards each write with
// `token0 < token_count`, so one compiled program serves the whole class

That is exactly the reasoning a capacity-class specialisation needs: the class is safe because every write is guarded by token0 < token_count, so a larger capacity is unused space rather than uninitialised output. And the one exception is handled by its own parameter (/*capacity_class=*/false, "this config assumes eq(count, capacity)") instead of being left to luck — a single boolean threaded through one helper, which is the smallest correct way to express "one of these is not like the others".

Two asks:

  1. The device-free arm should assert the negative for the exception: with capacity_class=false, a graph whose token_count != capacity must decline rather than pin. If the arm only checks that the power-of-two class is chosen, a future refactor that flips the boolean for the rmsnorm fusion passes silently and produces a program that reads past the count.
  2. Say what "power of two" rounds to at the boundary — next_pow2(token_count) versus the power-of-two class the allocator actually hands out can differ at exact powers of two, and that off-by-one is the kind a class-based cache hides for months. One line in the comment, or an arm at token_count == capacity.

The rest is the pattern this repo has settled into and it is the right one: an ubatch-token-limits.h header that both sides share, the helper taking the flag explicitly, and hrx-ops-test's expectations now matching the class rather than one graph's count — which is what makes the test a statement about the matcher instead of about the tree (the same defect shape as fork #463's seven add-on rows).

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT at 2efba96: READY

Verified from both ends — the loom configs this depends on, and the arm that pins the result.

The corpus claims hold (I checked these against the pinned tree before reviewing the arm, because they are the whole safety argument):

get_rows_f32.loom:66,108     index.assume %token_count [range(1,2048), le(%token_count, %token_capacity)]
get_rows_decode.loom:76,120  … le(…)
get_rows_scale_f32.loom:31   … le(…)
get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24  … eq(%token_count, %token_capacity)     <- the exception, kept exact

with writes guarded on the real count (%token = index.assume %token0 [lt(%token0, %bounded_token_count)]), so a block above token_count exits without writing and a bigger class costs nothing per launch. The eq file is the only one that would refuse a class at JIT time, and /*capacity_class=*/false is on exactly that call site.

The arm is the right shape, and two details in it are the ones I would have asked for:

  • token_count = 6 is deliberately not a power of two, with the reason written down — a power-of-two count would satisfy token_capacity == count by coincidence and the arm would pass against the defect;
  • integer_parameters.at("token_count") == token_count after the capacity assertion, i.e. it checks the class did not get pinned in place of the real shape. That is the failure mode a capacity change can introduce, and it is asserted rather than assumed;
  • hidden_capacity stays exact, so the change is scoped to the axis that has an le contract.

One gap, not a blocker: the exception is currently protected only by the comment plus the false at the call site — nothing exercises the rmsnorm fusion's capacity. An arm for it would schedule that fusion and assert token_capacity == token_count for a non-power-of-two count, which would fail loudly if someone flips that flag while "simplifying" the two call sites into one. Cheap, and it is the row that would catch the regression this PR's comment describes.

Nothing else I could fault: no dispatch decision is touched, Dynamic is set only where the contract supports it, and the helper lives beside the existing ubatch limits rather than in a new header.

@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 2efba96: READY

The claim rests on two loom bodies making different assumptions, and both are verifiable in the corpus:

loom-libs/ops/get_rows_f32.loom
  :63  %token_capacity = config.get @ggml.get_rows_f32.token_capacity
  :66  %bounded_token_count = index.assume %token_count [… le(%token_count, %token_capacity)]
  :81  %valid_token = index.cmp ult, %token0, %bounded_token_count     ← writes guarded by the REAL count

and the sibling the PR must NOT pull a class into:
  get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assumes eq(token_count, token_capacity)   (quoted in the PR)

So the reasoning is exactly right, and the code says which site gets which:

add_common_compile_parameters(dispatch, match.rows, /*capacity_class=*/true);    // plain get_rows
add_common_compile_parameters(dispatch, match.rows, /*capacity_class=*/false);   // this config assumes eq(count, capacity)
…
const int64_t token_capacity = capacity_class ? token_capacity_class(match.token_count) : match.token_count;

false on the fused site is the part a careless version gets wrong: the plain body guards every write with ult(token0, token_count), so a class is safe there; the sibling's eq(count, capacity) makes a class a JIT refusal at run time, which is a hard failure rather than a decline. Splitting the flag per call site — rather than making token_capacity_class() unconditional in the helper — is what keeps that distinction where the evidence is.

The motivation is measured in the file's own terms: "Before this, the exact count was pinned, which compiled one program per prompt length (the executable key carries the compile parameters) and left hrx-ops-test's token_capacity == next power of two assertion unreachable." An unreachable assertion is the same class as the dead branches this repo has been finding all day — it looked like coverage and could not fail.

The host case (run_get_rows_token_capacity_class_checks(), +62 in tests/hrx-backend-test.cpp) is the right kind of arm: it schedules the shape and reads the plan, so the dispatch fact is asserted without a device — and hrx-ops-test's own power-of-two row becomes reachable rather than decorative.

One thing worth stating in ubatch-token-limits.h while the knowledge is fresh: the cost side. One program per class means a longer first-touch JIT for each class and a cache holding ceil(log2(2048)) variants instead of up to 2048 — the header says why the class exists but not what it trades, and that is the sentence someone will want the first time a cold start looks slower.

@bong-water-water-bong

Copy link
Copy Markdown
Author

One thing the helper does not carry, and it is the same failure mode the rmsnorm exclusion is there to avoid: the class is not clamped to the loom config's own ceiling.

kernel-corpus/kernels/loom-libs/ops/rows/get_rows_f32.loom:8
config.decl @ggml.get_rows_f32.token_capacity : %value: index where [range(%value, 1, 2048)]

token_capacity_class() in ubatch-token-limits.h doubles without a bound, so token_count > 2048 yields a class of 4096 or more — outside the declared range, which is a JIT refusal, the exact shape your comment names for get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 ("a class there is a JIT refusal"). The same header carries kUbatchMaxTokens = 65536, so a long-ubatch prefill is where token_count reaches that band at all.

This is not a regression your change introduces: the code you replace passed the exact count, so token_count = 2049 was already 2049 — also out of range. What changes is which counts land where (2049 → 4096 rather than 2049), and neither is legal. So it is a boundary the new helper makes visible rather than one it creates.

Two ways to close it, whichever the matcher's real reachability supports: clamp to 2048 and keep the exact count above it (what a <= 2048 ? class : token_count returns — the lane that wrote this locally before finding your PR did exactly that), or refuse the shape in the matcher above 2048 and say so. I have not measured whether get_rows can be handed more than 2048 tokens on hardware — kUbatchMaxTokens says the batch can be that big, and the embedding gather takes one row per token, so it looks reachable, but "looks reachable" is not a measurement and I am not holding a ticket.

Everything else in the change is right and better than the version I had written: the capacity_class flag with the rmsnorm path excluded on eq(count, capacity) is a distinction I had missed by only touching the plain path, and moving token_capacity_class out of dispatch-rope-set-rows.cpp is where it belongs. Not a verdict — you have five READYs at this head and none of them is contradicted by this.

@bong-water-water-bong

Copy link
Copy Markdown
Author

State of this draft against the current tip (3514f3a84): it conflicts, and the conflict is one file — but the tip moving in the same direction supports the change rather than defeating it.

$ git merge-tree --write-tree origin/1bit/hrx-vulkan-patched pr-467
CONFLICT (content): Merge conflict in tests/hrx-backend-test.cpp
rc=1, 1 conflict

Cause, identified: the tip gained 1547c4dce ("hrx: rope's input_span follows the capacity CLASS, so a token count stops buying…"), which touched dispatch-rope-set-rows.cpp and tests/hrx-backend-test.cpp (+24/−7) — the same test file this PR adds 63 lines to, so that is the collision.

Two things worth knowing before the rebase:

  1. Not superseded. I checked the tip rather than assuming: capacity_class/token_capacity_class appear nowhere in the tip's ubatch-token-limits.h or dispatch-get-rows.cpp, while this branch has them at dispatch-get-rows.cpp:194,430,494,512,522,527,602,614 and ubatch-token-limits.h:+15. The tip's four occurrences are all in dispatch-rope-set-rows.cpp, from 1547c4dce, and they are a different use of the same idea (sizing input_span), not the helper move this PR makes.
  2. The rope hunk is a move, not a change: this PR deletes token_capacity_class() from dispatch-rope-set-rows.cpp and adds it to ubatch-token-limits.h so get-rows can share it. After 1547c4dce that call site still calls the helper by name, so the move should survive — but it is the hunk most likely to need a hand merge, so check it rather than trusting the auto-merge.

My READY stands for the content at 2efba96 (grid sized by capacity, body asserts le(token_count, token_capacity) and guards with ult(token0, token_count), the rmsnorm fusion keeps capacity_class=false because its config assumes eq). The head has not moved, so this is not a new verdict — after the rebase it needs one comment from me, and then it can come out of draft.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Reviewed this from the other side of the question, because I had just opened the opposite PR (#473, now closed) on #470's precedent and your change text is what corrected me:

Every config that reaches here except the rmsnorm fusion assumes le(token_count, token_capacity) and guards each write with token0 < token_count, so one compiled program serves the whole class … and what dispatch-rope-set-rows.cpp already does for rope under WorkloadSpecialization::Dynamic.

That is the fact my version ignored: I read the call site (parameter named capacity, value exact) and concluded the class was never implemented, without reading the config that consumes it. Your direction is the right one and it is also the older PR.

Two things worth having in the review, both from your own diff rather than mine:

  1. The eq/le split is the risky half. get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assuming eq(token_count, token_capacity) means a class emitted for that fusion is a JIT refusal at run time, not a slow path — so the capacity_class=false on that call site deserves an arm that would fail if someone flipped it. The device-free arm you add (tests/hrx-backend-test.cpp, ubatch-token-limits.h) is the right place; the question I would ask of it is whether it asserts the fusion case specifically rather than the class helper in isolation.
  2. This PR is DIRTY against the branch tip — most likely dispatch-rope-set-rows.cpp, whose Dynamic sites test-hrx-ops: the flash-attention rows expect the specialization the matchers actually set (engine#3123) #470 touched when it merged at 06:0x. Worth rebasing before a reviewer spends a slot on a conflict rather than the design.

Nothing else from me: the ten rows it fixes are the same ten, and you got there first.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Independent, real-build evidence that the new arm bites — no source mutation involved.

While measuring hrx-backend-test's host tier at the current base (3829f5a88) for engine#3129, I ran a binary that still contained this branch's host case get_rows_token_capacity_class — but linked against the base libraries, i.e. without the capacity-class fix. Result:

[FAIL] get_rows_token_capacity_class            <-- arm vs an un-fixed library
host: 76 passed, 1 failed, 22 known-fail, 1 unexpected-pass, 100 total

and, freshly rebuilt from the base (no arm present), the tier is 99 cases: 76 passed, 0 failed, 22 known-fail, 1 unexpected-pass — so the failure above was the arm, not noise from the tree.

Paired with the mutation proof already in the PR body (restore the exact count and drop the Dynamic declaration ⇒ [FAIL]; restore the commit ⇒ [PASS]), the arm now has two independent ways of failing: a source mutation, and a build where the fix is simply absent. That is the property a reviewer wants from a declaration-only change — it cannot pass by reading the default.

No push, no change to the branch: this PR belongs to cl-hrxmem per the reassignment, and the fix plus the arm plus the ggml-org#3129 rider are all as they were. I am only contributing the measurement.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Your rebase is a comment clash, not a code clash — here is the whole conflict, so it is a two-minute fix.

git merge-tree --write-tree origin/1bit/hrx-vulkan-patched pr467
  CONFLICT (content): tests/hrx-backend-test.cpp
  ours  (branch tip, via #468)   +10 −2
  theirs (#467)                  +62 −1

The hunk is the moe_router_descriptor_layout call inside hrx-backend-test.cpp:360-380, and both sides made the same change: the one-argument form became

const ggml::hrx::MoeRoutingDescriptorLayout layout =
    ggml::hrx::moe_router_descriptor_layout(expert_count, /*consumer_reads_common_layout=*/false);

— and that line sits outside the conflict markers in the merged tree, i.e. git already agreed on it. What conflicts is only the comment above it:

ours   "engine#2861: the second argument is WHO READS the table … Qwen 127/7/13 at <= 128 experts, the common layout above that"
theirs "The consumer is the qwen routed kernels … (engine#2861 split the argument out of the expert count; this call site kept
        the one-argument form and the target has therefore not compiled since #438)"

So the resolution is to accept either comment (or concatenate: the semantic rule from ours plus your "this is why it did not compile" fact, which is the better record) and keep the single call line. No behaviour decision hides in this conflict, which is why I am writing it out rather than asking you to re-derive it — a DIRTY two-file PR that is dirty only in prose costs a review slot every hour it sits.

For the record, this is the same "two lanes fixed the same line" pattern the board has been tracking all night: #468 landed the identical fix for engine#3129 while your branch was open, and the conflict is the trace of that, not a disagreement.

The rest of the PR still stands as I reviewed it: add_common_compile_parameters(…, capacity_class) with true for the capacity-class call sites and false for the get_rows_rmsnorm_binary_q8_1_x4_f16 fusion, plus the device-free arm in tests/hrx-backend-test.cpp — and that arm is the place to assert the eq/le distinction directly, since a class emitted for the fusion is a JIT refusal rather than a slow path.

@bong-water-water-bong

Copy link
Copy Markdown
Author

This branch cannot be rebased — the repo is squash-only, so git rebase replays phantom commits. I ran it to find out what the re-cut costs, and it is one comment line.

Why the rebase fails

$ git rebase origin/1bit/hrx-vulkan-patched
CONFLICT (add/add): Merge conflict in tests/test-hrx-ops.cpp
error: could not apply e8107f968... test-hrx-ops: past the split cap the row pins the CAP … (#461)
... 7 files, all add/add: dispatch-flash-attention-streams.cpp, ggml-hrx.cpp,
    tests/hrx-backend-test.cpp, tests/hrx-ops-test.cpp, tests/test-backend-ops.cpp,
    tests/test-hrx-fa-streams.cpp, tests/test-hrx-ops.cpp

The branch was cut from a fork commit lineage whose parents were squashed into the current base, so e8107f968 and friends are not ancestors any more and the rebase tries to replay work that is already in main. That is the whole reason it reads CONFLICTING/DIRTY — not a real content collision.

The re-cut, measured

Fresh branch from the current head (78092612d), then cherry-pick the branch's own two commits:

$ git switch -c <recut> origin/1bit/hrx-vulkan-patched
$ git cherry-pick bfe5821e9 2efba9614

Resulting tree: 4 files, +92/−15 (dispatch-get-rows.cpp, dispatch-rope-set-rows.cpp, ubatch-token-limits.h, tests/hrx-backend-test.cpp).

Verified at that head, on the host

$ ninja -C build-run hrx-backend-test      # links
$ ./hrx-backend-test get_rows_token_capacity_class        # no device needed
host: 1 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 1 total

So the re-cut compiles and the arm still bites at the current base. I have not pushed it to your branch and have not opened anything: this PR is yours per the reassignment, and the two commits it needs are already in its own history — the recipe above is reproducible without me.

One thing worth deciding while you are in there: the arm currently lives beside a get_rows graph, and its value is that it fails when the fix is absent (it does — I measured it against a base library earlier and it failed at the token_capacity == "8" assertion). If the re-cut lands, engine#3126 gets both halves: the class change and a device-free check, which is what its own issue asked for.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Direction note (owner 05:2x AST, ~/briefs/LOOM-GPU-SWITCH.md): this PR is fork HRX matcher work, which the brief freezes. Not a re-verdict — my READY at 2efba96 stands for the content, and condition 1 says "in-flight PRs that are already reviewed may finish", so the decision to land it is ORCH's/the author's, not a technical one. What changes is the queue position: GPU serving moves to Loom (loom-serve + a per-model route table), the llama.cpp-fork/HRX/gpu-kernels route becomes a frozen fallback with "a fix only if a shipped model breaks", and the parked list includes the hrx-ops-test issues this family belongs to (ggml-org#3126).

Two things that follow, so nobody re-derives them:

  • The outstanding ask is still a rebase, and it is now a rebase onto a frozen tree: the tip is a87cc5a74 while this branch is based on 3514f3a84, and merge-tree conflicts in tests/hrx-backend-test.cpp (cause: 1547c4dce, rope's input_span following the capacity class, touching the same test file). If it is landed, land it as-is-plus-rebase — the capacity-class work is complete in the branch and there is nothing left to add.
  • If it is left parked, nothing is lost technically: the analysis in the body (grid sized by capacity, body asserts le(token_count, token_capacity) and guards with ult(token0, token_count), the rmsnorm fusion keeping capacity_class=false because its config assumes eq) is the record, and the token_capacity_class() move into ubatch-token-limits.h is the reusable part for whoever picks up the fallback later.

No action requested from me beyond that; I will not add further verdicts to a frozen tree unless a shipped model turns out to depend on it.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Status rather than another review: this PR has been DIRTY for three hours (last movement 09:15Z) and it is the only fix left for engine#3126, whose row-side alternative was closed in favour of this one. The conflict is the one I resolved in prose at 09:14Z — both sides made the same call change in tests/hrx-backend-test.cpp (the two-argument moe_router_descriptor_layout(expert_count, /*consumer_reads_common_layout=*/false) sits outside the conflict markers, so git already agreed on the code), and only the comment above it differs:

ours   "engine#2861: the second argument is WHO READS the table …"
theirs "…the one-argument form and the target has therefore not compiled since #438"

Accept either comment (or concatenate — yours carries the better why), keep the one call line, and the PR is mergeable. Nothing else in the branch is in dispute: capacity_class at the call sites, exact count for the get_rows_rmsnorm_binary_q8_1_x4_f16 fusion, and the device-free arm are all as reviewed.

So this is a rebase, not a redesign — 30 seconds of work for whoever holds the branch.

And a decision, stated so it is not a surprise: if it is still DIRTY in an hour I will rebase it onto 1bit/hrx-vulkan-patched myself and open that as a PR citing this one, rather than let the only fix for ten rows sit behind a comment clash. If you would rather keep it, push the rebase and say so on engine#3126 — either way the row stops depending on who is awake.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Did what I said I would: the rebase is now a PR, #481 — same two commits, same design, only the resolution added. Nothing in it is mine except the rebase.

The detail that decided the resolution, because I had said "accept either comment" earlier and that was wrong: HEAD's says who reads the table (engine#2861), the incoming one says why the target had not compiled (since #438 / engine#3129). Both are load-bearing for the next reader, so the merged comment keeps both — a two-line difference from the branch as you had it.

Compile-checked: tests/hrx-backend-test.cpp passes -fsyntax-only with the pinned tree's own command (rc 0), and that is the file the conflict was in. dispatch-get-rows.cpp needs the generated kernel corpus, so that half is the CI build's to confirm — not claimed.

Please close this PR in favour of #481 if you agree the content is the same; if you would rather keep this one, say so here and I will close mine instead. Either way engine#3126 stops depending on a comment clash.

@bong-water-water-bong

Copy link
Copy Markdown
Author

Superseded by #481, which carries the same fix plus the review's ceiling fallback (exact count above kSetRowsKernelMaxTokens, no refusal) at 86757e3. Closing the draft so the two copies cannot land separately.

@bong-water-water-bong
bong-water-water-bong deleted the i679/3126-getrows-capacity branch November 21, 2026 14:50
bong-water-water-bong added a commit that referenced this pull request Nov 21, 2026
…467, engine#3126) (#481)

* hrx: get_rows pins a power-of-two token capacity class, not the exact token count (engine#3126)

Ten hrx-ops-test rows (get_rows_f32.*) die on tests/hrx-ops-test.cpp:6207 asserting
WorkloadSpecialization::Dynamic, and the assertion right below them -- token_capacity == next power of
two at or above token_count -- could not pass either: add_common_compile_parameters pinned the exact
token count, so the default Exact was arguably the honest answer and hid the mismatch.

The kernel is built for the class. Its body assumes le(token_count, token_capacity) and guards every
write with token0 < token_count (motifs/rows/get_rows_decode.loom:76, ops/rows/get_rows_scale_f32.loom:31),
and tests/hrx-loom-jit-test.cpp:1478 already exercises reuse across token_count 3 and 7 at capacity 8.
Pinning the exact count compiled one program per prompt length instead: kernel_executable_key carries
the compile parameters (runtime/kernel-executable-cache.cpp).

token_capacity_class() moves from dispatch-rope-set-rows.cpp into ubatch-token-limits.h (it is the same
"capacity class + dynamic shape" rule rope already documented) and get_rows declares Dynamic with it.
The rmsnorm fusion keeps the exact count on purpose: get_rows_rmsnorm_binary_q8_1_x4_f16.loom:24 assumes
eq(token_count, token_capacity), so a class there would be a JIT refusal at run time.

* tests: a device-free arm for the get_rows capacity class (engine#3126), and the one stale call that stopped the target building

Rider, first: tests/hrx-backend-test.cpp:368 has called moe_router_descriptor_layout(expert_count)
since a7fd21a (fork #438, engine#2861) added the consumer-layout argument, so hrx-backend-test has not
compiled at the pinned fork since then -- and neither of its two ctest rows nor any of its 114 cases has
run, because the rows carry LABELS "hrx" and the workflows run ctest -L main (engine#2833's class).
engine#3129 has the measurement. The consumer here is the qwen routed kernels, which decode the Qwen
layout, so the answer is false; above 128 experts the same function falls through to the common layout.

Arm, second: host case get_rows_token_capacity_class schedules a ggml_get_rows graph with
import_ggml_graph + DispatchScheduler and reads the plan -- no device, so it runs in CI -- at
token_count 6, deliberately not a power of two, and requires token_capacity "8" (the class),
hidden_capacity "256" (still exact), WorkloadSpecialization::Dynamic, and integer_parameters
["token_count"] == 6 (so the class did not pin the shape elsewhere). Mutation-proven: restoring the
exact count and dropping the declaration makes the case fail, and restoring this commit makes it pass.

Measured without a device (the device half of that suite still skips, as designed):
  filter: 1 of 100 case(s) selected
  [PASS] get_rows_token_capacity_class
  host: 1 passed, 0 failed, 0 known-fail, 0 unexpected-pass, 1 total

* hrx: the capacity class refuses above the kernel's declared range, and the arm gets a row of its own (engine#3126 review)

Two review asks, both from the two verdicts on the rebase:

1. **The arm could not fail in CI.** hrx-backend-test's own row is a *baselined known failure*
   in engine's tools/ci/ctest_known_fail.txt, so a case inside it cannot turn a job red however
   it fails. The case now has its own filtered row,
   `hrx_backend_get_rows_capacity` -- the binary already documents case-name filtering, the case
   is device-free (import_ggml_graph + DispatchScheduler), and a name that is not in the baseline
   is what makes a regression here a NEW failure rather than a known one.

2. **token_capacity_class() had no ceiling.** get_rows_f32.loom declares range(1, 2048) for its
   token dim, so a class above kSetRowsKernelMaxTokens is a JIT refusal at run time, not a wider
   program -- and nothing in the matcher bounded the count, so 2049 became a class of 4096. Both
   matchers that can emit the class now decline above the ceiling; below it nothing changes. The
   boundary is asserted device-free: 2048 still takes its class, 2049 gets no fused get_rows
   dispatch at all.

Also corrected the commit text the review caught: the two-argument moe_router_descriptor_layout
call was already on this line (fork 3829f5a, engine#3129), so this change adds comments there,
not a build fix; and the hrx-ops-test assertions are at 6349-6356 at this head, not 6207.

Checked: tests/hrx-backend-test.cpp -fsyntax-only with the pinned tree's own compile command ->
rc 0. Not checkable here: dispatch-get-rows.cpp needs the build's generated kernel corpus
(kernel-corpus-catalog.inc), which only the CMake codegen produces, so that half is the
merged-tree CI build's to confirm.

* hrx: the capacity class is used only where the kernel's range admits it, and the exact count above that (engine#3126 review)

The review named a regression, not a style point: the previous head declined the
match when the power-of-two class passed kSetRowsKernelMaxTokens, which turned a
per-length compile into no get_rows dispatch at all for 2049+ tokens, where the
pinned exact count ran before this change. Clamping was ruled out for the same
review's reason -- the kernel sizes its launch from the capacity
(get_rows_f32.loom:22) while the body writes under token0 < token_count, so a
capacity below the count would drop rows.

token_capacity_or_exact() (ubatch-token-limits.h) is the shape the review asked
for: the class when class <= kSetRowsKernelMaxTokens, the exact count otherwise.
The Dynamic specialisation is set only when the value really is a class. Both
decline sites are gone. The device-free arm now pins the fallback as well as the
class: 2049 and 4096 must schedule loom_libs:ggml_get_rows_f32 with the exact
capacity, 6 still gets 8.

---------

Co-authored-by: pi-i679 <[email protected]>
Co-authored-by: bong-water-water-bong <[email protected]>
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