Skip to content

llama : compute the experts missing from the MoE cache on the CPU (experimental) - #2

Closed
Helldez wants to merge 1 commit into
bmoe/moe-cache-slotsfrom
bmoe/moe-cache-cpu-misses
Closed

Helldez wants to merge 1 commit into
bmoe/moe-cache-slotsfrom
bmoe/moe-cache-cpu-misses

Conversation

@Helldez

@Helldez Helldez commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Experimental, stacked on the slot-map PR (base branch bmoe/moe-cache-slots). Off by default (moe_cache_cpu = -1).

What

With llama_context_params::moe_cache_cpu >= 0, the MoE cache no longer uploads every selected expert it does not hold.

  • build_moe_ffn builds the experts twice: on the device from the cache bank, and on the CPU from the host weights (the CPU mul_mat_id is pinned to backend_cpu).
  • Each pass is weighted by the router only for the experts it holds: weights * dev_mask and weights * cpu_mask. Each row of the sum is therefore exactly one of the two (x + 0).
  • The CPU pass selects the cached experts with id -1. ggml-cpu's mul_mat_id treats that as no expert and writes a zero row, so the host weights of a cached expert are never read.
  • The device pass gives the CPU's experts slot 0, so it computes them with some weights, and the mask drops the result.
  • At most moe_cache_cpu missing experts per layer and batch are still uploaded, the best scored first, so the cache keeps learning. The same batch reads those from the cache.
  • The copy callback plans a layer once per batch, on the first of its four maps to be copied. It writes the derived maps (slot or 0, expert or -1, the two masks) after the plan. All four are looked up on the device, so data dependency orders every read after the plan.
  • Llama 4 applies the router weights before the experts, so it keeps the uploads.

Measured

  • Over BigMoeOnEdge's loopback RPC device (whose kernels are the CPU's), the greedy output is byte-identical to the resident placement, both over resident experts and over streamed ones (gate G25e).
  • Real-device numbers: see the BigMoeOnEdge PR.

Limits

  • ggml_backend_sched runs the CPU pass after the device pass, not alongside it. Every split synchronizes when an eval callback is installed, and a split with no inputs synchronizes the previous backend. What this removes is the PCIe upload of the misses from the critical path. It does not overlap the two computations.
  • Upstream ggml : allow MUL_MAT_ID -1 index to skip the computation ggml-org/llama.cpp#26631 (Oct 24) already gives MUL_MAT_ID id -1 the same "zero row" meaning on CPU, Metal, Vulkan, OpenCL, SYCL and WebGPU (not CUDA yet). On a bump, this commit's ggml-cpu hunk is dropped in favour of upstream's.

…perimental)

With moe_cache_cpu >= 0, a batch served by the MoE cache no longer uploads
every selected expert the cache does not hold. The experts are built twice:
on the device from the cache, and on the CPU from the host weights. Each pass
is weighted by the router only for the experts it holds (dev_mask, cpu_mask),
so each row of their sum is exactly one of the two, and the CPU pass selects
the cached experts with id -1, which ggml-cpu's mul_mat_id now treats as no
expert (a zero row) instead of reading their host weights. At most
moe_cache_cpu missing experts per layer and batch are still uploaded, the
best scored first, so the cache keeps learning; those are read from the cache
by the same batch.

The copy callback plans the layer once per batch, on the first of its maps to
be copied, and writes the maps after the plan: the slot map gives slot 0 to
the experts computed on the CPU, cpu_ids gives -1 to the others. The maps are
looked up on the device like the slot map, so they are read after the plan.
llama_moe_cache_slots returns the cache's own slots, which tell a caller the
experts whose host weights the batch does not read. -1, the default, keeps
the upload of every missing expert. Llama 4 weights the input of the experts,
so it keeps the uploads.

The scheduler runs the CPU pass after the device pass, not alongside it.
LLAMA_HAS_MOE_CACHE_CPU lets a caller build against a llama.cpp without it.
@Helldez

Helldez commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Closing with Helldez/BigMoeOnEdge#433 and ggml-org#434: the CPU pass ran after the device pass, not beside it, and the measured gain did not justify a second fork commit.

@Helldez Helldez closed this Nov 2, 2026
@Helldez
Helldez deleted the bmoe/moe-cache-cpu-misses branch November 2, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant