Why llama.cpp and ik_llama Give Different Answers

I ran the same model with the same prompts and greedy decoding through mainline llama.cpp and through ik_llama, and they produced different text on 14 of 20 prompts. Neither build was wrong in any obvious way, and each one was bit-identical to itself run after run. I wanted to know where the difference comes from, whether either engine is closer to the “true” answer, and whether it matters.

Short version:

  • The disagreement starts from two specific numerical differences, each confirmed with a small patch: how each engine quantizes activations before matrix multiplies, and (in an older mainline build) fp16 precision inside CPU flash attention.
  • Once any tiny difference exists, the model amplifies it to roughly the same level regardless of where it came from: logits that differ by a median of 0.07–0.08 at every step, and 12–15 of 20 prompts forking where the top two tokens were nearly tied.
  • Measured against an exact fp32 reference on 200 prompts, neither engine is closer. Both flip the top token on about 0.7% of steps.
  • Both engines change their output depending on batch size: ik_llama at 32 rows per micro-batch, mainline at 64 rows with flash attention on.
  • On a quantized reasoning model, this kind of arithmetic difference changed whether a run looped about as often as a new random seed did (an inconclusive result, see below).

Scope: the root-cause work uses one small model (Qwen3-0.6B at Q8_0) on CPU (AVX2, no AVX512). GPU kernels and big MoE models weren’t measured for claims about the engines themselves. Most comparisons use 20 prompts; the “which is closer to exact” result uses 200.

Setup

  • Builds: mainline llama.cpp b062ba7 (2026-08-19) and HEAD e6443bd (2026-11-24); ik_llama ad217a2 (2026-11-24). Out-of-tree CPU builds with AVX2/FMA/F16C, GCC 14.2, Linux 6.12.
  • Model: Qwen3-0.6B Q8_0, F16 KV cache, flash attention on unless noted.
  • Harness: one C++ program, diff_oracle.cpp, compiled against each tree. It does greedy generation, teacher-forced runs that dump full-vocabulary logits, and replays that capture matching tensors at the point two builds fork.
  • Prompts: 20 fixed prompts, 256 generated tokens each. The 200-prompt run uses MATH-500, GSM8K, wiki and code prompts.
  • Exact reference: mainline with the Q8_0 weights converted exactly to F32, F32 KV and flash attention off, teacher-forced for up to 128 steps per prompt (2,073 steps on 20 prompts). Builds are scored on the reference’s token stream, not their own.
  • Controls: each build is bit-identical run to run and at 4 vs 8 threads, and prompt tokens are identical across builds.

The two engines fork at near-ties

On the 20 prompts, b062ba7 and ik_llama produce different greedy text on 14. The median first fork is at step 36.5 (range 12–243). At each fork, the gap between the top two tokens is tiny: the smaller of the two builds’ gaps has a median of 0.019 logit (max 0.09), and the larger a median of 0.095 (max 0.22). No step is bit-identical between the engines, and on shared steps the median difference in the top-1 logit is 0.076 (99th percentile 0.37).

So the engines aren’t making different choices on clear-cut tokens. They’re breaking near-ties differently.

Where the difference comes from

I followed the difference down to layer 0 and patched each cause away to confirm it:

CauseWhat differsPatchLayer-0 error before → after
Activation quantizationik quantizes matmul inputs to Q8_2_X4 with a bf16 block scale; mainline uses Q8_0 with an fp16 scale. About 6% of int8 codes differ.ml_bf16_actscale.patch (4 lines) gives mainline ik’s gridq/k/v relative error 2.0e-3 / 1.8e-3 / 4.5e-3 → 9.3e-8 / 9.1e-8 / 1.5e-7
Flash attention precision (b062ba7)CPU flash attention rounds Q to fp16 and accumulates V in fp16ml_fa32.patch on top of the first patchfa-0 4.3e-4 → 1.3e-6; the other layer-0 tensors ≤ 2.2e-7 (median)

A small numpy emulation of each engine’s activation quantizer reproduces each build’s own matmul output to within 2.1e-7, which confirms the first cause independently. The second was already fixed upstream: HEAD has PR #29810 for the V accumulator and now uses F32 for Q, and HEAD with the bf16 activation grid matches ik at fa-0 to 2.4e-6. These are medians over prompts. With both patches on b062ba7, 3 of the 15 forking prompts still had layer-0 tensors differing by 3e-3 to 6e-3 (HEAD+bf16: 6 of 14, up to 1.2e-2), which is the amplification described next.

Any difference gets amplified to the same level

With both causes patched out, the engines still differ by about 1e-6 in fp32. That’s enough to flip a rare int8 code during activation quantization. The KV cache carries the flip forward, and re-quantizing at every matmul spreads it. The patched builds still fork on 15 of 20 prompts. Their top-1 logits differ by a median of 0.070 on shared steps, and at the forks the smaller of the two builds’ top-2 gaps had a median of 0.024.

Every non-identical perturbation I tried on the 20-prompt, 256-token design landed in the same range: 12–15 forks out of 20, and a median top-1 logit difference of 0.070–0.081 on shared steps (the forks themselves happen at near-ties, median smaller gap 0.019–0.045). That covers the other engine, flash attention on vs off, the bf16 grid, bf16 plus fp32 flash attention, HEAD vs b062ba7, and HEAD+bf16 vs ik.

The size of the original difference barely matters. Once two runs differ at all, the model amplifies the difference to the same floor. The flip side is that runs which never hit a difference stay identical: in a short 4-prompt test, the 3 prompts under 32 tokens never crossed ik’s batch threshold and matched bit for bit.

Neither engine is closer to exact

On 20 prompts, mainline’s mean KL to the exact reference was about 11% lower than ik_llama’s. On 200 prompts (49,200 steps per build) that didn’t hold up:

BuildMean KL vs exactTop-1 flips per step (95% CI)
mainline HEAD6.79e-40.70% (0.63–0.78)
ik_llama6.85e-40.76% (0.69–0.84)
mainline b062ba76.77e-40.77% (0.69–0.85)

Every paired confidence interval includes zero (HEAD minus ik: −7.8e-6, CI −3.3e-5 to +1.6e-5; HEAD was better on 108 of 200 prompts). Both predictions I’d written down before the run held: a per-step flip rate of 0.5–0.9%, and mainline and ik within 10% of each other.

Most of each build’s distance from exact comes from activation quantization itself. In the 20-prompt run, a configuration with only activation quantization (Q8_0 weights, F32 KV, flash attention off) reached 86–97% of each build’s KL, as a ratio of means (not an additive breakdown), and the F16 KV cache alone contributed almost nothing (6.2e-7). Those two configurations weren’t rerun on 200 prompts.

Treat 20-prompt differences as provisional. The 200-prompt run overturned one; the floor itself is consistent across both.

Batch size changes the output

Both engines produce different output depending on how many rows are in a micro-batch. I measured this with stock llama-perplexity on 2 × 512 tokens of wiki.test, against a base of -ub 16:

  • ik_llama: -ub 31 matches the base (KLD ≈ 0, 100% same top-1). At -ub 32 and -ub 512, KLD is 1.344e-3 (97.65% same top-1). Flash attention off gives 1.384e-3 at -ub 32. 4 and 8 threads agree. From reading the code (not a patched test), the likely site is iqk_mul_mat.cpp:340, which switches to GGML_TYPE_Q8_0_R8 at nrc_y >= 32.
  • Mainline (HEAD): the threshold is 64 rows, and only with flash attention on, which is where the tiled CPU flash-attention kernel kicks in (GGML_FA_TILE_Q 64). At -ub 64 and 512, KLD is 1.180e-3 (98.04% same top-1); at 31, 32 and 63 it matches the base. With flash attention off, 63, 64 and 512 all match.

My own harness confirms both thresholds (12 prompts × 64 forced steps vs token-by-token decoding). -ub 1 is bit-identical in all builds. The group of prompts with 74–97 tokens breaks from -ub 32 in ik and -ub 64 in mainline. It also found something I can’t explain yet: the 455–600-token group differs at every -ub I tested from 8 up (I didn’t test 2 to 7) in all three builds, so there’s a separate non-invariance with long KV caches that I haven’t localized.

This is an instance of a known class of problem (see prior work): outputs that depend on batch shape. PrismML’s Bonsai 2.5 report measures the same effect on GPUs: a kernel still in testing that sums in a different order gave a mean KL of 1.2e-4, which they describe as the same size as changing the batch size. What I haven’t seen reported is the specific thresholds for these two engines. I found no existing ik_llama issue or PR about the 32-row switch, so I filed one, ik_llama.cpp#3010. The maintainer replied that logits differing across engines, hardware and settings such as batch size is expected, and I closed it.

Does it matter?

On a quantized reasoning model, it can. I reran the 25 math problems where a ternary Qwen3.6-35B-A3B hit its token cap, changing only -ub 1 on the CUDA stack. On the GPU that switches the prompt pass from batched matrix kernels to single-token ones (decode is already one token at a time), so the arithmetic differs while the model and inputs stay the same. All 25 runs diverged from the originals, at a median of token 51. Whether each run ended in a verbatim loop flipped on 9 of 25 problems; a different random seed flipped 8. My pre-registered bar for calling loop labels fragile was 10, so as frozen the result is inconclusive, and 9 vs 8 is within the noise. Problems still lean one way (after the fact: a run that looped originally looped again half the time, against 0.21 for the rest), so a single run’s label is a weak signal, not a coin flip. (More on that in How Quantized Reasoning Models Fail to Stop.)

Prior work

Already known, and not claimed as new here:

  • Batch dependence as the main source of nondeterminism in LLM serving, and batch-invariant kernels as the fix: Horace He and Thinking Machines Lab, “Defeating Nondeterminism in LLM Inference” (September 2025). The batch thresholds above are instances of this.
  • Invariance holding only by accident at certain tensor shapes: arXiv 2611.19135.
  • Numerical noise changing reasoning accuracy and length: arXiv 2506.09501 (“Understanding and Mitigating Numerical Sources of Nondeterminism in LLM Inference”).
  • Greedy outputs differing between BF16 and FP16 on the same hardware, decided by top-1/top-2 margins: arXiv 2609.26621 (“Greedy Decoding Is Not Precision-Invariant”).
  • Prefix-cache reuse making llama.cpp outputs diverge, more so at low bit: 37.4% of agentic episodes at 16-bit vs 82.5% at 4-bit (arXiv 2609.04748, “Same Request, Different Answer”).
  • Batch-invariance work and reports in llama.cpp: PRs #31504, #32081, #28109 and #16016, and issue #28111.
  • Fixing batch geometry instead of kernels: BlockLock (arXiv 2611.06977), on PyTorch/vLLM, with padded batching agreeing on 52.59% of sequences and BlockLock-Rows at 100% for 0.5–0.8% latency.
  • ik’s bf16 activation scale is a known design choice (ik #540), and b062ba7’s fp16 flash-attention accumulator was a known bug, fixed in PR #29810.
  • Differential testing across precisions: arXiv 2604.19790 (PrecisionDiff).

What this adds:

  • the root-cause chain for mainline-vs-ik disagreement, each step confirmed by a patch;
  • the measured floor for this pair of engines at Q8_0 on CPU (about 0.07–0.08 logit, about 0.7% of steps), and that it’s the same whichever difference starts it;
  • the specific batch thresholds (ik at 32 rows, reported in #3010 and answered as expected behaviour; mainline at 64 with flash attention) and the long-KV non-invariance;
  • a direct comparison of how often arithmetic noise and a new seed change a reasoning run’s loop label (inconclusive as pre-registered).

I didn’t find any published numerical comparison of mainline llama.cpp and ik_llama.

Limits

  • One small dense model at Q8_0, on CPU without AVX512. Lower-bit weights raise the floor in preliminary runs (median top-1 difference 0.086 at Q4_0 up to 0.20 at IQ4_XS), with an unexplained Q4_K_M anomaly.
  • The exact reference is mainline’s own fp32 path, so it isn’t fully independent of mainline.
  • The ik code site for the 32-row switch is from reading the code, not a patched test, and only Q8_0 was measured.
  • The loop result is one seed pair on 25 problems picked because they hit the cap.
  • Step 2’s 200-prompt comparison ran with flash attention on only.
  • The CPU results were checked against the raw files and recomputed independently by a second reviewer, but not adversarially red-teamed (the loop result was).

Takeaways

  1. Two correct engines can disagree on most prompts. They’re breaking near-ties differently, not computing wrong answers. A greedy output difference between two engines isn’t evidence that either one has a bug.
  2. The source of the difference barely matters. Any tiny difference gets amplified to the same floor, so fixing one cause doesn’t make two engines agree.
  3. Batch size is a hidden input. If you need reproducible output from either engine, pin -ub. Staying under 32 rows (ik) or 64 with flash attention (mainline) avoids the thresholds I found, but long prompts differed at every -ub of 8 or more, so -ub 1 was the only setting that was bit-identical in every build and prompt length I tested (on CPU, against token-by-token decoding).
  4. Don’t read much into a single run. On a reasoning model, a kernel-level arithmetic change flipped whether a run looped about as often as reseeding did. Compare rates across runs, not individual traces.