Skip to content

serve: --precise — a follow-up turn comes out bitwise identical to a cold run - #504

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/precise
Oct 22, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/precise

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

The problem

A follow-up turn in the same conversation is not bit-identical to a cold run of that conversation. The follow-up
continues from the KV cache and prefills only the new tokens, so those tokens go through a different batch than
the cold prefill does — and HRX picks a different kernel family for a different token count, with a different
reduction order over the weights. Near-ties at the top of the distribution then flip and the answer takes another
branch.

Measured with the engine's own stack (precise.py, 10 two-turn chats, greedy, cache_prompt true on the
follow-up, one fresh server per model):

With --precise on, the follow-up re-prefills exactly the tokens a cold run does (per chat the prompt token
count is identical, e.g. 88 vs 88):

model chats that differ first mismatch logprob gap there
Qwen3.8-27B 0 / 10 — —
Qwen3-4B 1 / 10 token 26 0.0385 nats

The 4B's single remaining difference cannot be explained by batch size any more (both arms prefilled the same
tokens), so the open question there is whether that path is deterministic at all - a follow-up experiment, not a
gap in this change.

The kernel families involved, from the dispatch registrations: T == 1 takes the q8-activation decode kernels
(token1_requires_q8_activation), T <= 5 the lowtoken_dot ones (lowtoken_contraction, and
use_split_direct_dot when K splits), T = 2..8 our private kqt kernels, 5 < T <= 16 another family, and
T >= 128 (multiples of 128/256) the WMMA prefill tiles. A ~20-token follow-up lands in the 16 < T < 128
band while a 512-token cold ubatch lands in the tile band — different code, so different rounding.

The fix

--precise (or ONEBIT_PRECISE=1) makes every request re-prefill its whole conversation: serve rewrites
cache_prompt to false before forwarding, so the follow-up takes exactly the path a cold run takes. Off by
default; nothing changes for a single-turn request; documented in docs/serve.md.

Cost, measured on the 27B at 2k and 7k conversation tokens (turn 2's own prompt_ms, 75 W cap):

conversation precise off precise on cost
~1.6k tokens 28 tok / 672 ms 1599 tok / 4785 ms +4.1 s
~5.4k tokens 28 tok / 676 ms 5464 tok / 12 348 ms +11.7 s

What it is not

It is not batch invariance. Somebody who wants follow-ups identical to cold runs and the cheap KV-cache prefill
needs the kernels to reduce in the same order for every T. The kernel families differ by token count (T=1 decode,
T<=5 lowtoken, T=2..8 our kqt, 5<T<=16, 16<T<128 generic post-ops, T>=128 WMMA tiles), so a ~20-token follow-up
and a 512-token cold ubatch run different code with different activation quantisation, split-K and combine trees.
A cheap middle step exists - pad every prefill chunk to the tile size so both take one kernel with one split-K
(~0.65 s per short turn instead of 4.8-12.3 s) - and full invariance is weeks across ~10 kernel families with a
-15..-45 % prefill risk, because the WMMA fragments are where prefill's throughput comes from. Both are written
up in ~/briefs/TPRECISE-REPORT.md.

Checks

  • greedy identity: 10 chats x 2 models with --precise on, cold run vs follow-up: 27B 0/10, 4B 1/10
    (the 4B one at a 0.0385-nat tie, both arms having prefilled identical tokens - a determinism question for the
    4B path, filed for a follow-up).
  • scripts/hw-gate.sh full on the binaries this engine ships (build-warm/hrx/llama/bin), seven models, with a
    private snapshot of the kld store: PASS — ops 663/663, and kat + placement + kld PASS on qwen3.8-27b
    (kld 0.002217), zaya1-8b (0.007729), qwen3-coder-30b-a3b (0.000912), qwen3-4b-iq4xs, qwen3-0.6b and its
    -kq8 / -kvq8 variants. 0 amdgpu ring timeouts.
  • every GPU run went through gpu-hold and every build through a claim (build.sh/build-guard.sh now wrap
    gpu-hold -l "build <dir>" -t 30 -- ~/lb/thermal-run.sh), and every script that starts a backend exports
    IREE_HAL_AMDGPU_LIBHSA_PATH — without it the HRX backend silently registers no device.

🤖 Generated with Claude Code

…cold run

A follow-up turn in the same conversation is not bit-identical to a cold run of that conversation: it continues
from the KV cache and prefills only its new user message, where a cold run prefills the whole conversation in
512-token ubatches, and HRX takes a different kernel family for a different token count. Qwen3.8-27B prefills a
cold conversation in the WMMA tile kernels (T >= 128, multiples of 128 or 256) and a 28-token follow-up in the
generic post-ops matmul (16 < T < 128), with a different activation quantisation, a different split-K and a
different combine tree; the last bits differ and a near-tie flips.

Measured on 2026-10-22 (engine main, one fresh serve per arm, temperature 0, cache_prompt true on the follow-up,
10 two-turn chats per model; a follow-up against a cold run of the same conversation):

- Qwen3.8-27B: 0/10 chats differ with --precise on. Prompt tokens per chat are identical between the arms
  (88 vs 88, 104 vs 104), i.e. the follow-up really does take the cold path.
- Qwen3-4B: 1/10 differs, at token 26 with a 0.0385-nat gap at that position, both arms having prefilled the
  same tokens - a determinism question about that path, not about batch size, and a separate follow-up.

The flag rewrites cache_prompt to false before the request is forwarded, so every request re-prefills its whole
conversation and takes exactly the path a cold run takes. ONEBIT_PRECISE=1 does the same from the environment.
Cost, measured on the 27B as the follow-up's own prompt time: 672 ms -> 4785 ms after a ~1.6k-token conversation
(1599 tokens re-prefilled at 334 tok/s) and 676 ms -> 12348 ms after ~5.4k tokens (5464 tokens at 443 tok/s).
Off by default; a single-turn request is unchanged either way.

This is exactness by construction, not batch invariance: a follow-up is identical to a cold run because it is
one. Batch invariance (the same reduction order for T = 1, 2-8 and prefill) would keep the cheap KV-cache
prefill and is written up separately - it means ~10 kernel families with one activation quantisation, split-K
and combine tree, weeks of work and a prefill-throughput risk, because the WMMA fragments are where prefill's
throughput comes from.

docs/serve.md documents the flag, the numbers and the alternative.

Co-Authored-By: Claude Opus 5 <[email protected]>
@bong-water-water-bong
bong-water-water-bong merged commit 6aa9616 into main Oct 22, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/precise branch October 22, 2026 06:23
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Input Validation for --precise

The --precise flag is added to the command-line interface and environment variable handling, but there is no validation or documentation of its interaction with other flags like --cache-prompt. If --cache-prompt is explicitly set to true by the user, it could conflict with the behavior of --precise which sets cache_prompt to false. This could lead to unexpected behavior or confusion for users.

if (precise) body["cache_prompt"] = false;
Potential Performance Regression

The --precise flag forces re-prefill of the entire conversation, which significantly increases the time-to-first-token (TTFT) for follow-up turns. While this is documented, it's important to ensure that the performance impact is clearly communicated to users and that the flag is only enabled when necessary for bit-identical results.

forward(backends[pick].port, held, -1, id, backends[pick].drop_model, backends[pick].set_model, o.precise, q, r, &watch);

bong-water-water-bong added a commit that referenced this pull request Nov 24, 2026
…ishes its Q8_1 alternate only when a consumer reads it (llama.cpp#511; engine#3380) (#3490)

The split-FA decode matcher (match_flash_attention_decode_split_next_q8_dispatch) runs a loom kernel that writes
the attention output AND its Q8_1 x4 packing in one launch so the next projection need not quantize the activation,
and it called append_alternate_value unconditionally. A graph that ends at the attention output -- exactly the
qwen_flash_attention row -- therefore planned a copy nothing can read; the row had been parked as the suite's last
known-fail with that reason (engine#3380).

llama.cpp#511 applies the existing graph-only consumer qualification (dispatch-q8-publication.h's
common_for_each_q8_consumer through full ordered reshape aliases + common_q8_consumer_is_qualified_projection, the
#449/#504 machinery) to that publication. Kernel selection, the q8 transient and bindings[9] are untouched -- only the
alternate metadata is conditional. The qwen_flash_attention row becomes a plan-only host case with the known-fail
label gone; the seven test-hrx-ops fa_decode_split.* rows assert the no-alternate plan and keep asserting the q8
transient binding. Publication-when-a-consumer-exists stays pinned by qwen_decode_split_flash_attention.*.masked.head96.value64
and qwen_decode_attention_output_next_q8.*.

Device evidence (strixhalo gfx1151, add-on, package runtime libs, root, one box-hold both ticket):
  before (d2369fe2): hrx-ops-test 410 passed, 0 failed, 1 known-fail (qwen_flash_attention)
  after  (#511)    : hrx-ops-test 411 passed, 0 failed, 0 known-fail, 411 total; the single row PASSes;
                     test-hrx-ops 295 passed, 0 failed; the 7 publish rows pass; no reset/fault in the window
Host control (no device): pre-fix source + the new host row FAILs (find_alternate_value(...) == nullptr, and the
fa_decode_split rows FAIL the no-alternate assertion); with the fix all 5 rows pass, host hrx-ops-test 18/0,
test-hrx-ops 2/0, check_stale_expectations 488/488 selectable.

registry/architectures.json is regenerated by scripts/registry-regen.sh (llama.cpp source sha).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant