Skip to content

llama : expose the MoE cache's slot map (experimental) - #1

Closed
Helldez wants to merge 1 commit into
bmoe/expert-ready-hook-2210-dlfrom
bmoe/moe-cache-slots
Closed

Helldez wants to merge 1 commit into
bmoe/expert-ready-hook-2210-dlfrom
bmoe/moe-cache-slots

Conversation

@Helldez

@Helldez Helldez commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Experimental, for BigMoeOnEdge's expert streamer. One commit on top of the expert-ready hook.

What

llama_moe_cache_slots(ctx, il, n_tokens, n_expert_used) returns the MoE cache's host slot map for layer il (one int32 per expert, -1 = not on the device), or NULL when a batch of that shape does not read the layer's experts from the cache. The conditions mirror build_moe_cache_slots: the layer is cached, the batch is at most 32 tokens and fits the slots, and the device supports the lookup. LLAMA_HAS_MOE_CACHE_SLOTS lets a caller build against a llama.cpp without it.

Why

The streamer keeps the host copy of the experts and has to make every routed expert resident before the copy callback uploads the misses. It cannot see what the device already holds, so its RAM cache duplicates the VRAM cache. With this accessor it leaves the device's experts out.

Correctness (the eviction question)

The streamer reads the map from the eval callback at the layer's ffn_moe_topk node. That is after selection and before the lookup split whose copy callback runs moe_cache_slru::plan. No other plan can run in between, because the lookup split of layer il is the next one to copy a slot map. Could the plan for il evict a hit that the streamer skipped?

  • plan sets seen[e] = seen_gen for every selected expert before it does anything else.
  • Hits are only touched (moved to the protected segment, and possibly demoted back to probation by the 50/50 rule). Neither changes key_of or the slot map.
  • Every miss gets its slot from victim(il). victim skips any slot whose key is in layer il and whose expert is seen in this generation. That covers every hit of the batch and every fill made earlier in the same plan. It aborts rather than return such a slot.

So a selected expert that has a slot when the layer's lookup starts keeps it through that plan, and its host weights are never read for that batch. A plan for a later layer can evict it, so the map says nothing about later layers or the next token. The caller must read it per layer, at that point.

Add llama_moe_cache_slots(ctx, il, n_tokens, n_expert_used), which returns
the host slot map of layer il (one int32 per expert, -1 if the expert is not
on the device) when a batch of that shape reads the layer's experts from the
MoE cache, and NULL otherwise, under the same conditions as the graph's own
lookup (build_moe_cache_slots), including the device's support for it.

An expert streamer that keeps the host copy of the experts uses it to leave
out the experts the device already holds. Read between the layer's expert
selection and its lookup, a selected expert that has a slot keeps it through
that layer's uploads: moe_cache_slru::plan marks every selected expert as seen
before it picks a victim, and victim() never returns a slot of the same layer
holding a seen expert, so neither a hit nor an earlier fill of the batch is
evicted, and the host weights of a hit are never read.

LLAMA_HAS_MOE_CACHE_SLOTS 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-slots 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant