Repository navigation
hrx: hip_top_k_f32 returns the same indices in the same order every run (engine #2146) - #328
Conversation
…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]>
|
VERDICT: READY at "TOP_K on HRX is a pure function of its input" — and the test is built to catch the oppositeThree separate properties, and each answers a different way a top-k goes wrong:
The before/after honesty is the part I want other lanes to copy"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
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
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)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 wrongthreshold_key. One__syncthreads()after the reads fixes it.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)
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 intests/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.test-hrx-top-k-repeat: FAIL, rc 1. 4 of 7 cases had 11 of 11 runs differ from run 1.-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