How a Router Bias Ramp Hid an MoE Offload Bug in llama.cpp's Tests

While working on llama.cpp issue #32710 (Inkling-2-Mini gives NaN when the experts of its looped layers stay in CPU memory), I added the model’s architecture to the test suite’s existing host-experts check. On a tree with the bug, the test passed. The reason turned out to be in the test fixtures: the router bias in the MoE test models is a steep ramp, so every token picks the same top-k experts (two in these fixtures), and a bug about which experts get copied to the GPU has nothing to trip on.

Short version:

  • The bug: with experts in host memory and micro-batches of 32+ tokens, llama.cpp copies only the experts a layer’s routing selects to the GPU. A looped layer runs several passes over one set of expert weights, and the later passes read the copy made for pass 1, which is missing the experts they pick.
  • The test model reached exactly that code path and still passed, because with the fixtures’ ramp bias every top-k call selects the same 2 experts. Pass 2 never needs an expert pass 1 didn’t copy.
  • The existing host-experts test (deepseek2) has the same gap: it gives the model 64 experts so the selective copy gets exercised, and the ramp limits it to 2.
  • A same-GPU comparison with the router bias at zero, experts in GPU memory vs in host memory, catches the bug (NaN on the broken tree in 5 of 5 seeds) and matches exactly on master and the fix (NMSE 0 in 5 of 5).
  • My first version of that check passed the NaN anyway, because NaN > 1e-4 is false.

Scope: one GPU (RTX 5060 Ti, CUDA). The copy-callback trace and the CPU counts come from debug-only instrumentation, not part of the fix or the test patch.

Setup

  • Code: llama.cpp master a8ad690 (2026-11-25), in three local trees: master with #32807’s workaround removed (the bug), master as is, and a fix that generalizes #32807.
  • Test: tests/test-llama-archs.cpp builds small random-weight models for each architecture and compares their logits across devices. Its “Host experts” config keeps the MoE expert tensors in host memory and runs micro-batches of 64 tokens, so the CUDA backend offloads MUL_MAT_ID and llama.cpp copies the selected experts to the GPU per split (llama_context::sched_copy_experts).
  • Hardware: RTX 5060 Ti 16 GB (sm_120), CUDA 12.9.1; CPU measurements on a Ryzen 9 5950X.

The bug on the real model

On the real model (Inkling-2-Mini IQ4_NL, llama-perplexity -f wiki.test.raw -c 512 --chunks 4 -ngl 99 -ncmoe 8), the tree without #32807’s workaround gives NaN on all 4 chunks in each of three configs. Master, where #32807 copies Inkling’s experts in full, gives finite perplexity:

configwithout #32807master
-fa on -b 512 -ub 512nan ×46.6218 ± 0.52015
-fa off -b 2048 -ub 512nan ×46.6211 ± 0.52147
-fa on -b 512 -ub 64nan ×46.6087 ± 0.51952

The test that passed anyway

I added LLM_ARCH_INKLING2_LOOP to host_experts_test() next to LLM_ARCH_DEEPSEEK2. On the broken tree, the Host-experts row came back OK (NMSE vs CPU 4.04e-07) and the summary said “all 8 test(s) passed”.

A trace of the copy callback on that tree showed the test did reach the buggy path. Pass 1 of the looped layer copied only its selected experts. Pass 2 got no copy of the expert weights at all and read pass 1’s device copy. That’s the #32710 path, and the test still passed.

Why: a ramp in the router bias

The test fixtures fill every MoE router bias with 2.0f*i:

// a random router bias can tie within the noise between the backends, a ramp keeps the top-k selection the same
static void fill_exp_probs_b(std::vector<float> & dst, const char *, const tensor_data_params &) {
    for (size_t i = 0; i < dst.size(); i++) {
        dst[i] = 2.0f*i;
    }
}

That came from PR #30818 (merged 2026-10-20), which fixed a real problem: test-llama-archs -a beam failed for about one seed in ten because a CPU-to-Metal difference of 9.1e-05 in router scores exceeded a 2.6e-05 margin and flipped a selection. A ramp with step 2 makes the top-k a function of the expert index alone, so backends can’t disagree about it.

It also means routing ignores the input. I counted distinct experts per top-k call on the CPU with a debug eval callback:

  • inkling2_loop selects exactly 2 distinct experts in all 24 top-k calls per run, and pass 2 of the looped layer needs 0 experts that pass 1 didn’t select, in all 6 pass-2 calls, for seeds 1–8.
  • deepseek2, the architecture already in the host-experts test, selects exactly 2 distinct experts in all 13 top-k calls per run, seeds 1–3.

The test file gives host-experts architectures 64 experts, with the comment that “with more experts than a ubatch uses, the copy of the used experts in host memory skips some of them”. With the ramp, the selective copy is only ever tested on 2 of the 64. I haven’t seen a wrong result for deepseek2; this is a coverage gap, not a known bug.

With the router bias at zero instead, the same CPU count shows pass 2 needing 1–16 experts that pass 1 didn’t select in every one of its 6 calls per run, seeds 1–8 (never 0). The looped layer’s top-k calls then select 8–42 distinct experts.

A check that catches it

The ramp still makes sense for the existing rows, which compare a GPU against the CPU. What #32710 breaks is a different property: experts kept in host memory should give exactly what the same experts in GPU memory give. That can be checked on one device, where the cross-backend noise #30818 was guarding against doesn’t come in.

So the new check builds two extra models with the router bias at zero, one with experts in GPU memory and one with host experts, runs both on the same GPU, and requires matching logits. Over seeds 1–5:

treeinkling2_loop, routed NMSEdeepseek2, routed NMSE
without #32807nan (5/5)0.00e+00 (5/5)
master0.00e+00 (5/5)0.00e+00 (5/5)
fix0.00e+00 (5/5)0.00e+00 (5/5)

When the copy is correct, host and device experts are bit-identical on this GPU, so input-dependent routing doesn’t make the check flaky here.

This depends on the backend op-offloading MUL_MAT_ID for host experts at a micro-batch of 64, as CUDA does. On a backend that runs host-weight MUL_MAT_ID on the CPU instead, the host model’s first MoE layer runs on the CPU and the reference’s on the GPU, the difference feeds the next layer’s zero-bias router, and #30818’s near-tie problem comes back. I only tested CUDA.

A NaN that passed

The first run of that check printed OK on the broken tree, next to “routed nan”. I had written the condition as:

if (nmse_routed > 1e-4) {

NaN > 1e-4 is false, so a NaN never reaches the failure branch. The fixed condition is:

if (!(nmse_routed <= 1e-4)) { // NaN fails too

With that, the broken tree reports “8 test(s) executed, 1 failed” in 5 of 5 seeds, and master and the fix pass. The existing CPU comparison in the same loop, if (nmse_val > 1e-4), has the same hole. I left it alone in this change.

Caveats

  • One GPU (RTX 5060 Ti) and one backend (CUDA). The bit-exact host-vs-device result is 5 seeds on this GPU; Vulkan and Metal are untested.
  • The copy-callback trace and the CPU counts use debug-only instrumentation that isn’t part of the fix or the test patch.
  • The deepseek2 observation is about what the test covers. No deepseek2 output was wrong.

Prior work

  • PR #30818 introduced the ramp to make router selection independent of backend noise.
  • PR #32807 is the arch-specific workaround for #32710 on master; PR #32809 (closed) tried a scheduler-side fix. Comments on #32710 had already narrowed the trigger to looped layers with host experts and micro-batches of 32+ tokens.

What this adds:

  • the ramp limits both host-experts tests in test-llama-archs (deepseek2, and inkling2_loop as added) to 2 of 64 experts;
  • a same-device check with input-dependent routing that catches #32710;
  • the NaN hole in the file’s NMSE checks. The NaN > threshold pitfall itself is well known.

Takeaways

  1. A test fixture that makes a choice deterministic across backends can also make it constant. It’s worth counting how many experts a host-experts test actually touches.
  2. When a property only has to hold on one device, check it on one device (where both paths really run there; CUDA op-offloads host experts, other backends may not), and the cross-backend noise problem goes away.
  3. Write NMSE thresholds as !(x <= limit), so a NaN fails.