Skip to content

hrx: keep the small-rows matcher bounds exclusive, and decline ARGSORT when no dispatch covers it (engine #1259) - #222

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/small-rows-bounds
Nov 6, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/small-rows-bounds

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

What this fixes

Two bugs of the class that #221 fixed for the 32-expert router: a matcher that accepts what its kernel refuses, and a claimed node that has no dispatch.

1. Matcher bounds were inclusive of the kernel's own limit

Every kernel in kernel-corpus/kernels/loom-libs/ops/small_rows_f32.loom carries index.assume %column_count [range(%column_count, 1, LIMIT)], and a Loom range excludes its upper bound, so the kernel refuses LIMIT itself:

registration matcher accepted kernel assumes
common.softmax_rows_f32 ne[0] <= 4096 [1, 4096)
common.sum_rows_f32 ne[0] <= 4096 [1, 4096)
common.argsort_rows_f32 ne[0] <= 1024 [1, 1024), and range(%rank, 0, 1023)
common.norm_rows_f32 ne[0] <= 65536 [1, 65536)
common.get_rows_small_f32 w,s,b,r <= 65536 [1, 65536) x4

A shape at exactly the limit was claimed and then rejected at prepare time instead of falling back. All bounds are now exclusive. get_rows_small also bounds id_stride, which its kernel assumes is < 1048576 and which the matcher did not bound at all.

2. ARGSORT was claimed unconditionally

device_supports_op claimed GGML_OP_ARGSORT while the only registration that produces ids is common.argsort_rows_f32. Any ARGSORT shape that matcher declines — and, in particular, the registration switched off with GGML_HRX_DISABLE_DISPATCH for a measurement — stayed claimed with no dispatch and failed the whole graph:

graph_compute: unsupported HRX node 2: ARGSORT output=3:i32[31,22,1,1]<-ARGSORT inputs=[1:f32[31,22,1,1]<-SOFT_MAX]

instead of splitting to the CPU. device_supports_op now asks small_rows_argsort_tensor_supported(), the same bounded-claim pattern TOP_K already uses in the same function (hip_top_k_tensor_supported, engine ggml-org#679). This also repairs every ARGSORT with more than 1024 columns, which was claimed, declined and failing the graph before.

Measured

Pin ced49c61, full test-backend-ops test -b HRX0, TOPK_MOE waiver switched off so the ids are actually compared, one gpu-hold claim:

arm result
before fix, nothing disabled 4279/4599, [VIEW] 320, every other node OK
after fix, nothing disabled 4275/4595, [VIEW] 320, every other node OK — no regression
after fix, common.argsort_rows_f32 disabled 4233/4233 PASS, [VIEW] 0, 0 mismatch dumps

The totals move by 4 because four ARGSORT cases with ne[0] >= 1024 used to be claimed and are now honestly declined, so the harness reports them not supported rather than running them. The third row is the point: with the argsort registration switched off the ids now fall back to the CPU and the suite passes, where before the same arm died on unsupported HRX node ARGSORT and merely looked like 40 failures — which is what made that debug switch unusable for the measurements in engine ggml-org#1259.

What this does NOT do

The kernels are untouched. On the evidence in engine ggml-org#1259 the ids they produce are correct: with the waiver off, the routed weights — gathered on device from exactly those ids — match the CPU in all 320 cases, and with the ids produced on the CPU instead the whole suite passes. The remaining TOPK_MOE difference is the host readback of a non-contiguous VIEW of the argsort output, which is what #221's waiver names and what a follow-up should materialise.

…T when no dispatch covers it (engine ggml-org#1259)

Two bugs of the same shape as the 32-expert router reject that #221 fixed.

1. The matchers accepted the kernel's own upper bound. Every kernel in
   kernel-corpus/kernels/loom-libs/ops/small_rows_f32.loom carries
   `index.assume %column_count [range(%column_count, 1, LIMIT)]`, and a Loom
   range is exclusive of its upper bound, so the kernel refuses LIMIT itself:

     common.softmax_rows_f32   matcher `<= 4096`   kernel range [1, 4096)
     common.sum_rows_f32       matcher `<= 4096`   kernel range [1, 4096)
     common.argsort_rows_f32   matcher `<= 1024`   kernel range [1, 1024),
                                                   and range(%rank, 0, 1023)
     common.norm_rows_f32      matcher `<= 65536`  kernel range [1, 65536)
     common.get_rows_small_f32 matcher `<= 65536`  kernel range [1, 65536) x4

   A shape at exactly the limit was therefore claimed and then rejected at
   prepare time instead of falling back. The bounds are now exclusive, and
   get_rows_small also bounds id_stride, which its kernel assumes is
   < 1048576 and the matcher did not bound at all.

2. ARGSORT was claimed unconditionally by device_supports_op, but the only
   registration that produces ids is common.argsort_rows_f32. So any ARGSORT
   shape that matcher declines - and, in particular, the one switched off with
   GGML_HRX_DISABLE_DISPATCH for a measurement - stayed claimed with no
   dispatch and failed the WHOLE graph:

     graph_compute: unsupported HRX node 2: ARGSORT output=3:i32[31,22,1,1]
                    <-ARGSORT inputs=[1:f32[31,22,1,1]<-SOFT_MAX]

   instead of splitting to the CPU. device_supports_op now asks
   small_rows_argsort_tensor_supported(), the same bounded-claim pattern TOP_K
   already uses (hip_top_k_tensor_supported). This also repairs every
   ARGSORT with more than 1024 columns, which was claimed, declined and
   failing the graph before.

Effect on the suite (pin ced49c6, full `test-backend-ops test -b HRX0`, the
TOPK_MOE waiver switched off so the ids are actually compared, one claim):

  before fix, nothing disabled : 4279/4599, [VIEW] 320, every other node OK
  after  fix, nothing disabled : 4275/4595, [VIEW] 320, every other node OK  (no regression)
  after  fix, argsort disabled : 4233/4233 PASS, [VIEW] 0, 0 mismatch dumps

The totals move by 4 because four ARGSORT cases with ne[0] >= 1024 used to be
claimed and are now honestly declined, so the harness reports them as not
supported instead of running them. The second line is the point: with the
argsort registration switched off, the ids now fall back to the CPU and the
whole suite passes, where before the same arm died on "unsupported HRX node
ARGSORT" and only looked like 40 failures - which is what made the debug switch
unusable for measurements like the one in engine ggml-org#1259.

The kernels themselves are untouched: the ids they produce on device are
correct (the routed weights gathered FROM those ids match the CPU in all 320
cases), so nothing here "fixes" a store. The remaining TOPK_MOE difference is
the host readback of a non-contiguous VIEW of the argsort output, which is
what #221's waiver names.

@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 2aba7216.

The foundation of the fix checks out in the kernel's own source, which is where this has to be decided:

small_rows_f32.loom:32,82   range(%column_count, 1, 4096)      softmax, sum
small_rows_f32.loom:120     range(%column_count, 1, 1024)      argsort
small_rows_f32.loom:155     range(%rank, 0, 1023), lt(%rank, %cols)      <- the line that settles the exclusivity argument
small_rows_f32.loom:280     range(%column_count, 1, 65536)     norm
small_rows_f32.loom:177     range(%id_stride, 1, 1048576)      get_rows_small

The argsort one does not even need a ruling on whether a Loom range excludes its upper bound: the kernel bounds the rank to
below 1023 and requires lt(%rank, %cols), so a 1024-column row cannot be ranked whatever the interval semantics — the
matcher's ne[0] <= 1024 was accepting a shape the body refuses, exactly as you describe. The other four are the same
one-byte change, and get_rows_small gaining the id_stride < 1048576 bound is the one I would have missed: it was
unbounded in the matcher and assumed in the kernel.

The ARGSORT half is wired the way the comment says: small_rows_argsort_tensor_supported() is declared in
dispatch-small-rows.h:28, defined at .cpp:701, and called from ggml-hrx.cpp:1350 in device_supports_op — so a shape
the matcher declines (or the registration disabled with GGML_HRX_DISABLE_DISPATCH=common.argsort_rows_f32) now declines at
the claim instead of being claimed and then failing the whole graph. The predicate's own bounds match the kernel
(cols < 1024, rank-shape equality, contiguity, f32 -> i32), and <cstdlib>/<string> are included for the getenv.

It compiles as part of the backend: syntax-checked dispatch-small-rows.cpp with the exact command my working
GGML_HRX=ON build uses for it (paths rewritten to this head) — clean, rc 0.

Not re-run here: the full-suite A/B (4279/4599 -> 4275/4595 with [VIEW] 320, and the 4233/4233 arm with the
argsort registration disabled). That needs the HRX suite on the device in one claim, and it is the row that matters — the
third arm is the one that says the debug switch is usable again. What I can say is that the mechanism is the one your
numbers describe, and the -4 is consistent with four ne[0] >= 1024 ARGSORT cases moving from "claimed" to honestly
not supported.

One sequencing note for the queue, not a request on this PR: engine ggml-org#1267 bumps the pin to ab79894d, which is before
this commit. If ggml-org#1267 lands first the engine gets the build fix without #222, and #222 landing afterwards means a second
engine pin bump to pick it up. If both are going in for c31, taking #222 into the fork first and bumping once to the
resulting tip saves a bump (and the registry_pins line that has to move with each one).

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