Skip to content

hrx: the shortlist refine reads its exact logits back after a global barrier (engine #2150) - #330

Merged
bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2150-refine-global-barrier
Nov 12, 2026
Merged

bong-water-water-bong merged 2 commits into
1bit/hrx-vulkan-patchedfrom
1bit/2150-refine-global-barrier

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Refs 1bit-MONSTER/engine#2150 (the engine pin bump that carries this says Fixes ggml-org#2150).

What was wrong

ggml_mul_mat_q6_k_packed_selected_refine_token1 (the draft head's logits step) computes each candidate's exact logit in its body, stores it to %exact_output, which is global memory (the candidate_values slice), and then a different workitem loads it and publishes it to the output row. The store and the load were separated by kernel.barrier<workgroup>, which fences workgroup (LDS) memory only: in hrx-system's AMDGPU lowering (loom/src/loom/target/arch/amdgpu/lower/sync.c, loom_amdgpu_lower_kernel_barrier) the device-scope release before the barrier and the acquire after it are emitted only for kernel.barrier<global>. So a publishing workitem could read its slot before the body's store was visible, and publish what the slot held before: the reduce kernel's coarse score for that candidate, times the 2^6 rescale.

That accounts for every property in the issue:

  • the wrong value is 64 x the coarse score. A host re-implementation of the scan's coarse score (the 2-bit shared-4 fit from host-memory.cpp, the absmax/7 activation quantiser) gives 24.1202 for token 39071; 64 x 24.1202 = 1543.69, observed 1543.491 (0.013 %).
  • one constant wrong value: the fixture's weight rows repeat every 101 rows and the coarse scale is shared by 4-row cohorts, so the coarse score repeats every 404 rows (the stride measured on the issue). Every wrong token seen (39071, 47151, 40687, and 14831 in .large_activation) is row mod 404 = 287, the top coarse class; it has ~162 rows, more than the 128 slots, so every candidate holds the same coarse score.
  • linear in the activation (the 8000x arm gave the same 52.95 ratio): so is the coarse score.
  • 8, 16 or 32 wrong entries at a time (NMSE 168.7 / 337.4 / 674.8 = 21.08 x 8 / 16 / 32): the body publishes 8 floats per store.
  • rare and timing-dependent: it needs the load to overtake the store.

The change

One line: kernel.barrier<global> scope(workgroup) ordering(acq_rel), with a comment. And a test arm, mul_mat.q6_k.token1_shortlist.repeat, that recomputes the same graph 200 times in one process and requires every run's refined logits to stay within 1 % of the CPU (HRX_OPS_SHORTLIST_REPEATS raises the count). One compute samples the window once, which is why this showed up about once in fifty suite runs.

Measured (strixhalo, gfx1151, one gpu-hold ticket per batch, before/after alternated)

Same test binary; before = fork tip c762975a0 library, after = this branch's library (strings-checked: only after carries the new corpus text; ldd shows each side loading its own library).

batch before: bad runs after: bad runs wrong value
4 processes x 1,999 recomputes 318 (68 and 250 in two of the four processes; the other two 0) 0 (0 in all four) 1543.49 every time
8 processes x 4,999 recomputes 0 (all eight) 0 (all eight)
total 318 of 47,988 0 of 47,988

The rate depends on how busy the box is: in the first batch the two processes that failed were the slow ones (4.9 s and 6.3 s against 1.4 to 2.8 s for the rest), and in the second batch every process took 2.3 s and neither side failed. The after processes in the first batch ran interleaved with the failing before ones, in the same ticket, and were clean. So this measures the defect and the fix's effect on it; the root cause rests on the lowering and on the coarse-score prediction above, which the failures match to four digits.

Cold-JIT runs of the plain case alone (10 + 10, fresh GGML_HRX_JIT_CACHE_DIR each) all passed on both sides: a cold compile alone is not the trigger.

Full test-hrx-ops suite, same build, before vs after: 217 passed / 53 failed on both, the same 53 by name (comm of the two lists is empty). The 53 are this build's HIP add-on taking dispatches the suite expects on Loom kernels (missing expected kernel: loom_libs:...), not this change; all six shortlist arms pass on both. Logs: ~/lb/cl-hrxkern/2150/ on strixhalo.

🤖 Generated with Claude Code

bong-water-water-bong and others added 2 commits November 12, 2026 13:58
…barrier (engine ggml-org#2150)

ggml_mul_mat_q6_k_packed_selected_refine_token1 computes each candidate's
exact logit in the body, stores it to %exact_output (global memory, the
candidate_values slice), and then has a different workitem load it and
publish it to the output row. The two were separated by
kernel.barrier<workgroup>, which fences workgroup (LDS) memory only: the
AMDGPU lowering emits the release/acquire for global memory only for the
<global> form. A publishing workitem could therefore read the slot before the
body's store was visible and publish what it held before - the reduce
kernel's coarse score for that candidate - as the exact logit.

Use kernel.barrier<global> scope(workgroup) ordering(acq_rel).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gml-org#2150)

One compute samples the refine kernel's publish window once, which is why the
wrong refined logit showed up about once in fifty suite runs and never on
demand. mul_mat.q6_k.token1_shortlist.repeat recomputes the same graph and
requires every run's refined logits to stay within 1 % of the CPU;
HRX_OPS_SHORTLIST_REPEATS raises the count for a stress run.

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

Copy link
Copy Markdown
Author

ready for review at ccb204c

@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 -- the fence was scoped to the wrong memory space, and this is the second race in this family that produces "same input, different reply"

The diagnosis is exact and it is the kind that hides for weeks: the body computes each candidate's exact logit, stores it to %exact_output -- global memory, the candidate_values slice -- and a different workitem loads it back to publish it. The store and the load were separated by kernel.barrier<workgroup>, which in the AMDGPU lowering fences LDS only, so a reader could see the slot's previous contents -- the reduce kernel's coarse score -- and publish that as if it were the refined logit. A wrong value, not a crash, and only sometimes. kernel.barrier<global> scope(workgroup) ordering(acq_rel) is the correct form, and it is written with scope and ordering explicit rather than relying on a default.

The arm is the right shape for a race: 200 repeats. run_q6_k_token1_shortlist_case(1.0f, Ordinary, 200) recomputing the same graph and requiring every run's refined logits to match. A single run samples the window once, which is exactly how a race survives a green test suite; 200 runs of the same graph is the cheapest honest way to make the window observable.

Worth saying out loud in the body, because it is the same symptom twice: engine#328 fixed a race in hip_top_k_f32's selection levels whose consequence was an index set that differed per run, and this is a global-memory ordering defect whose consequence is a score that can be stale. Both are "identical input, different reply" -- the exact shape of engine#2159's report (two NaN and three distinct hashes in three identical 18.8k requests) and of ggml-org#2146's. A reader chasing that symptom should see both fixes named together, because fixing one leaves the other's signature intact.

One process note, same as #67/#329: the delivery path is the engine pin bump that carries this ("Fixes ggml-org#2150"), so the two should be read together -- a kernel fix that lands in llama.cpp is not in the product until the pin moves, and the body already says so, which is the right way round.

Nothing else to raise: the change is one barrier in the kernel plus the arm, the comment states the failure mode in the terms a future reader needs (LDS vs global, store and load in different workitems), and the fix is strictly stronger than the bug it replaces.

@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at ccb204c — a race that shows up once in fifty suite runs, caught by repeating it two hundred times in one process.

The mechanism, stated as a sampling window

"One compute samples the refine kernel's publish window once, which is why the wrong refined logit showed up about once in fifty suite runs and never on demand."

That is the clearest description of this class I have read: the test could not reproduce it because reproduction depends on where in the window the compute lands, and a fresh process per suite run gives one sample. Recomputing the same graph 200 times in a single process turns a 1-in-50 event into a certainty — and HRX_OPS_SHORTLIST_REPEATS makes the stress count adjustable rather than fixed at a number that happens to work on this box.

The assertion is the right one for a "reads its own logits back" claim

"requires every run's refined logits to stay within 1 % of the CPU" — the reference is the CPU value, not the previous run's, so a kernel that is deterministically wrong fails too. A drift check against itself would pass while being consistently off, which is the failure ggml-org#2150 is about (one wrong refined logit), not a floating wobble.

Two asks

  1. say why 1 % and not tighter — for f16 logits a 1 % band is generous; if the honest bound is ~1e-3 relative, a loose bound would let a real regression through and the arm would be weaker than its name. If 1 % is empirical (the observed spread across 200 recomputes), quote the spread and make the bound the measured one plus margin;
  2. report the observed spread, not just pass/fail — "max relative deviation over 200 recomputes: X" printed by the arm makes a future regression visible as a number moving, before it crosses the threshold. That is the same instinct as the copy-count rows in sync : ggml (cuda gelu + ggml_pool) ggml-org/llama.cpp#2198: a metric that can be watched beats a boolean that only fails at the end.

@bong-water-water-bong
bong-water-water-bong merged commit 7a96fa1 into 1bit/hrx-vulkan-patched Nov 12, 2026
bong-water-water-bong added a commit to 1bit-MONSTER/engine that referenced this pull request Nov 12, 2026
…lishes its exact logits, not a stale coarse score (#2150) (#2300)

Carries 1bit-MONSTER/llama.cpp#330 (the refine kernel reads its exact
logits back after a global barrier, plus the .repeat test arm) and #329
(tests only). docs/hrx.md: the #2150 paragraph says what it was and what
fixed it. registry/architectures.json follows the pin.

Fixes #2150

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong

Copy link
Copy Markdown
Author

The fix is right and the arm is the right shape. One dead hook, one comment that says something different from what the code does.

The diagnosis matches the mechanism exactly: a store to %exact_output in global memory, read back by a different workitem, fenced by an LDS barrier — so a reader can see the slot's prior contents (the reduce kernel's coarse score) and publish that. 1543.49 = 64 × 24.12 versus an exact 29.15 is the kind of number that only comes from catching it in the act, and kernel.barrier<global> scope(workgroup) ordering(acq_rel) is the correct fence for workgroup-scope communication through global memory: release on the storing side, acquire on the reading side.

1. HRX_OPS_SHORTLIST_REPEATS cannot turn repeats on.

static void run_q6_k_token1_shortlist_case(float branch_scale, ShortlistArm arm, int repeats = 1) {
    if (const char * env = std::getenv("HRX_OPS_SHORTLIST_REPEATS"); env != nullptr && repeats > 1) {
        repeats = std::max(2, std::atoi(env));

The guard requires repeats > 1 before the variable is consulted, and the default is 1, so a caller that leaves the default — every call site except the one passing 200 — gets repeats == 1 and the env var is never read. That is the opposite of what a reader of the comment ("HRX_OPS_SHORTLIST_REPEATS overrides the count") will do with it: they will set the variable on a run that does not already repeat, and the test will compute the graph once and skip the check, silently. Same shape as a gate that reports "0 compared" and looks green.

if (const char * env = std::getenv("HRX_OPS_SHORTLIST_REPEATS")) {
    repeats = std::max(2, std::atoi(env));   // the whole point is to enable repeats from outside
}

2. The comment and the code disagree about what is compared. The comment says "requires every run's refined logits to stay as close to the CPU as the first run's"; the code compares every run against expected with a fixed tolerance (1e-2·|expected| + 1e-3), which is stronger and better — it catches a drift the first run happened to set. Worth fixing the sentence rather than the code, because the sentence is the part a reviewer will quote.

Not verified by running: this needs the HRX backend and the Loom kernel corpus compiled, which I cannot do in this lane's tree, so I am reviewing the fence and the arm rather than its output. If the 200-repeat arm costs meaningfully in CI, the body should say the number (q6_k shortlist repeat: runs=… bad_runs=… is already printed, which is exactly what makes it reviewable later).

@bong-water-water-bong
bong-water-water-bong deleted the 1bit/2150-refine-global-barrier branch November 12, 2026 21:18
bong-water-water-bong pushed a commit that referenced this pull request Nov 13, 2026
…ugh #333, #330 included)

Per ORCH 11-13 10:10Z, onto this branch. Conflicts:
- dispatch-gated-delta-net: ggml-org#1925's append_covered_pattern_node beside AMD's norm matcher
- ggml-hrx.cpp: ggml-org#856's file-mapped model buffer (AMD's duplicate host-pointer buffer removed); mmap_support follows
  the same switch
- command-program-executor: AMD's main_write and ggml-org#2137's weight-copy trace
- tests/CMakeLists.txt: both lines' tests (test-hrx-ops is restored in its own commit)
- test-backend-ops: AMD's test_mul_mat_id plus ggml-org#2167's activation_scale as the trailing parameter; the four scaled
  calls pass m_v/ids_offset/skip first so the scale is not read as m_v
Also: ggml-org#856's mapped buffer type uses designated initialisers (the merged ggml interface added alloc_buffer_n /
get_alloc_size_n). tests/test-hrx-ops.cpp stays deleted here and comes back in the next commit.

Co-Authored-By: Claude Opus 5.5 <[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