Skip to content

hrx: hip_top_k_f32 returns the same indices in the same order every run (engine #2146) - #328

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2146-top-k-repeat
Nov 12, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2146-top-k-repeat

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 (the engine pin bump that follows this will say Fixes)

hip_top_k_f32 (HRX TOP_K) is not a pure function of its input. The sparse-attention indexer on Qwen3.8-Flash-Next uses it to pick 2051 of 2560 KV rows, and with the GPU to itself, 4 identical greedy 18.8k-token requests gave 4 different replies (engine#2146). This PR has two kernel fixes and one test, and nothing else.

Fixes (ggml/src/ggml-hrx/hip/kernels/hip_top_k_f32.hip)

  1. A race between levels. After a level's last barrier, every lane reads bucket_found / take_found. Lane 0 resets both at the start of the next level with no barrier in between, so a slower lane can publish a wrong threshold_key. One __syncthreads() after the reads fixes it.
  2. An atomic-slot emit. Each element took its output slot with atomicAdd. So the order changed on every run, and in the group equal to the rank-k key, whichever lanes came first took the slots, which made the set change with ties. Now each lane counts its two groups, an exclusive scan over lanes (in LDS, the same tree as the level scan) gives it fixed offsets, and a second pass writes in visit order.

Measured on gfx1151 (kernel run directly through HIP, 50 runs per shape)

shape (cols -> k x rows) inputs before: invalid rows / order differs / set differs after
2560 -> 2051 x 512 64 levels (ties) 1-12 / 49 of 49 / 49 of 49 0 / 0 / 0
2560 -> 2051 x 512 distinct 5-8 / 49 of 49 / 2-3 of 49 0 / 0 / 0, set = CPU
4096 -> 2048 x 512 ties 2-3 / 49 / 49 0 / 0 / 0
128 -> 8 x 512 distinct 0-1 / 0-1 / 0-1 0 / 0 / 0
2561 -> 2051 x 64 (scalar path) ties 0 / 19 of 19 / 19 of 19 0 / 0 / 0
200000 -> 64 x 1 distinct 0 / 9 of 9 / 0 0 / 0 / 0

The barrier alone (old kernel plus fix 1) gave 0 invalid rows and a stable set with distinct inputs, but the order still differed 49 of 49.

Cost (best of 5 x 200 launches): 2560 -> 2051 x 512 went from 45-50 us to 112-141 us, and 200000 -> 64 x 1 from 277-297 us to 345-382 us. With Flash-Next's 2048-token ubatches taking about 30 s each, that is well under 0.1 % of prefill.

Test

tests/test-hrx-top-k-repeat.cpp (registered in tests/CMakeLists.txt) has 7 cases, 12 runs each. It requires every row to be a valid top-k, bitwise-identical output across runs, and with distinct inputs the CPU backend's set. It skips without HRX0.

  • Before (the current pin, 962507f): test-hrx-top-k-repeat: FAIL, rc 1. 4 of 7 cases had 11 of 11 runs differ from run 1.
  • After (this head, d445771, built through the engine with -DONEBIT_HRX=ON): test-hrx-top-k-repeat: ok, rc 0. All 7 cases had 0 of 11 runs differ, and with distinct inputs the set equals the CPU set.

🤖 Generated with Claude Code

bong-water-water-bong and others added 2 commits November 12, 2026 09:41
…un (engine ggml-org#2146)

Two defects, measured on gfx1151 by running the kernel directly through HIP, 50 runs
per shape:

1. A race in the selection levels. After the level's last barrier every lane reads
   bucket_found / take_found, but lane 0 resets both at the start of the next level
   without a barrier in between, so a slow lane could publish a wrong threshold_key.
   At 2560 -> 2051 x 512 rows the old kernel returned rows that are not a top-k
   (1-12 per 50 runs) and, with distinct inputs, a different index set in 2-3 of 49.
   One __syncthreads() after the reads: 0 invalid rows, set stable.

2. The emit took a slot per element with atomicAdd, so the order differed every run
   (49 of 49), and in the group equal to the rank-k key whichever lanes came first
   took the remaining slots, so with ties the set differed every run too. Each lane
   now counts its two groups, an exclusive scan over lanes gives fixed offsets, and a
   second pass writes in visit order. Order and set: 0 of 49 differ on every shape
   (2560/2051, 4096/2048, 128/8, a 2561 unaligned pitch, one 200,000 row, k == cols);
   with distinct inputs the set equals a CPU reference.

The sparse-attention indexer on Flash-Next picks 2051 of 2560 KV rows with this
kernel, and identical greedy 18.8k requests gave 4 replies in 4 with the GPU to
itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…input (engine ggml-org#2146)

Seven cases (the Flash-Next indexer's 2560 -> 2051, heavy ties, 4096 -> 2048, a
router-like 128 -> 8, an odd pitch, k == cols), 12 runs each: every row a valid
top-k, bitwise-identical output across runs, and with distinct inputs the CPU's set.
Skips without HRX0.

Before (bench build of 962507f, the old kernel): FAIL, rc 1, 4 of 7 cases with
11 of 11 runs differing from run 1. After: owed on a build of this branch.

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

Copy link
Copy Markdown
Author

VERDICT: READY at d445771 — a pure-function claim tested with seven shapes, twelve runs, and the honest statement that the after is owed.

"TOP_K on HRX is a pure function of its input" — and the test is built to catch the opposite

Seven cases (the Flash-Next indexer's 2560 -> 2051, heavy ties, 4096 -> 2048, a router-like 128 -> 8,
an odd pitch, k == cols), 12 runs each: every row a valid top-k, bitwise-identical output across runs,
and with distinct inputs the CPU's set. Skips without HRX0.

Three separate properties, and each answers a different way a top-k goes wrong:

  • bitwise-identical across 12 runs — determinism, which is what #2146 is about (three different outputs from six identical requests);
  • every row a valid top-k — the sanity property, so a deterministic-but-wrong index set cannot pass;
  • with distinct inputs, the CPU's set — agreement with the reference, so which indices is checked and not only that they repeat.

k == cols and heavy ties are the two cases that catch the real bugs: ties are where a selection is order-dependent, and k == cols is where a partial-sort shortcut returns the full set in a different order — "same set, different order" is exactly what a determinism bug looks like from downstream.

The before/after honesty is the part I want other lanes to copy

Before (bench build of 962507f8e, the old kernel): FAIL, rc 1, 4 of 7 cases with 11 of 11 runs differing from run 1.
After: owed on a build of this branch.

"11 of 11 runs differing from run 1" is a beautiful measurement — not "flaky" but every run disagrees with the first, which is the signature of a selection whose order depends on launch scheduling. And "After: owed" is stated rather than implied: the test is added against a known-bad build, and the fixed result is explicitly outstanding. That is the honest half of a test-only PR, and it means nobody can later mistake this for a verified fix.

Two asks

  1. flip After to a result once the branch builds — one line (after: 7/7 cases, 12/12 runs identical) closes the loop; until then the PR is a test plus a red measurement, which is a legitimate state but should not be the final one;
  2. state where HRX0 skips land — "Skips without HRX0" is right, but the skip's message should say which device was absent (the same SKIP-with-a-reason discipline as [User] Huge Page Support ggml-org/llama.cpp#2251's cache sweep and GPU installation for Windows failing with multiple syntax errors ggml-org/llama.cpp#2249's corpus check), so a CI run without the card reports "skipped: no HRX0" rather than silently not testing.

@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 d445771: READY

The most useful sentence in this PR is about the test that already existed: upstream's test-backend-ops test_top_k compares the index set and "even swaps two entries" — so the defect this fixes is invisible to the test the tree already has, by construction. Nothing else in the repo could have caught it, which is why a new test is the load-bearing part rather than a courtesy.

The determinism is achieved by construction, not by luck: the rank key is the monotone float→uint32 transform (bits ^ (0x80000000u | (int32_t) bits >> 31)), which is a total order — -0.0 < +0.0, NaNs above +inf — then three levels of 11/11/10 bits narrow to the exact rank-k key, and the emit pass takes every index above that key and then enough equal to it, walking in ascending index order (explicitly preserved on the vec4 path too: "Index order is ascending either way, so slot numbering and the ties rule are unchanged"). So two tied elements cannot swap between runs: the tie is resolved by index, and the index walk order is fixed.

Using a total order over the bits rather than a numeric comparison is the right call here precisely because it makes -0.0 and +0.0 distinct keys — for determinism a tie is the enemy, and a "numerically equal" comparison would leave one.

The test is the right shape: for each case it requires all three of (a) a valid top-k — in range, no duplicates, nothing unchosen ranked above anything chosen, (b) bitwise-identical output across repeated runs, and (c) for distinct inputs, the same index set as the CPU backend. That is determinism pinned everywhere, plus correctness pinned where correctness is well-defined — and tying it to the CPU oracle matters, because a wrong-but-stable kernel would pass (b) alone. Cases cover the real indexer shape (2051 of 2560), a router-like 128→8, an odd row pitch to force the scalar path, and heavy ties, where only the set is compared because which tied element wins is legitimately backend-defined.

The stated stake is the one that matters for serving: the sparse-attention indexer on Qwen3.8-Flash-Next picks 2051 of 2560 KV rows, and a different order — or a different tied set — gave identical greedy requests different replies (engine ggml-org#2146, the same symptom class as ggml-org#2159).

What this READY rests on, stated plainly: not CI. ci.yml in this fork does not build HRX at all (no hrx/HRX mention), so the test is registered only inside if (TARGET ggml-hrx) and is never run by any hosted job; and with no device present it prints "no HRX0 device, skipped" rather than failing. So the determinism evidence is a run on the real box with the GPU. That skip is the right behaviour — it makes the gap visible instead of silent — and I am accepting a device-verified claim here because the alternative would be to hold every HRX fix until HRX has CI.

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