From a09c1ddcd8fd74b0a68c2314944bc1de5db6edf8 Mon Sep 17 00:00:00 2001 From: Helldez <74499579+Helldez@users.noreply.github.com> Date: Tue, 27 Oct 2026 18:53:39 +0100 Subject: [PATCH] llama : expose the MoE cache's slot map (experimental) 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. --- include/llama.h | 9 +++++++++ src/llama-context.cpp | 8 ++++++++ src/llama-context.h | 2 ++ src/llama-moe-cache.cpp | 28 ++++++++++++++++++++++++++++ src/llama-moe-cache.h | 4 ++++ 5 files changed, 51 insertions(+) diff --git a/include/llama.h b/include/llama.h index 3af6e22233ef..f79b4d0e0bad 100644 --- a/include/llama.h +++ b/include/llama.h @@ -1168,6 +1168,15 @@ extern "C" { // otherwise: float[n_embd] (1-dimensional) LLAMA_API float * llama_get_embeddings_seq(struct llama_context * ctx, llama_seq_id seq_id); + // [EXPERIMENTAL] The slot map of the MoE cache (see moe_cache_size) for layer il: one int32 per expert, in host + // memory, the expert's slot on the device or -1 if it is not there. Returns NULL unless a batch of n_tokens tokens + // that selects n_expert_used experts per token reads the experts of layer il from the cache. + // Meant for the eval callback, between the node that selects the experts of layer il and the cache's lookup: + // an expert the batch selects that has a slot then is not evicted by that layer's uploads, so its host weights + // are not read for this batch. The map changes as later layers upload their experts. + #define LLAMA_HAS_MOE_CACHE_SLOTS 1 + LLAMA_API const int32_t * llama_moe_cache_slots(struct llama_context * ctx, int32_t il, int64_t n_tokens, int64_t n_expert_used); + // // backend sampling API [EXPERIMENTAL] // note: use only if the llama_context was created with at least one llama_sampler_seq_config diff --git a/src/llama-context.cpp b/src/llama-context.cpp index ca84fc405bce..10b8a36494ab 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -2775,6 +2775,10 @@ ggml_status llama_context::graph_compute( return status; } +const int32_t * llama_context::moe_cache_slots(int32_t il, int64_t n_tokens, int64_t n_expert_used) const { + return moe_cache ? moe_cache->get_slots(il, n_tokens, n_expert_used) : nullptr; +} + bool llama_context::sched_copy_experts(ggml_backend_t backend, const ggml_tensor * src, ggml_tensor * dst, ggml_cgraph * graph, void * user_data) { auto * lctx = static_cast(user_data); @@ -4269,6 +4273,10 @@ float * llama_get_embeddings_seq(llama_context * ctx, llama_seq_id seq_id) { return ctx->get_embeddings_seq(seq_id); } +const int32_t * llama_moe_cache_slots(llama_context * ctx, int32_t il, int64_t n_tokens, int64_t n_expert_used) { + return ctx->moe_cache_slots(il, n_tokens, n_expert_used); +} + void llama_set_embeddings_nextn(llama_context * ctx, bool value, bool masked) { ctx->set_embeddings_nextn(value, masked); } diff --git a/src/llama-context.h b/src/llama-context.h index 6938fb103cde..73759f618b5e 100644 --- a/src/llama-context.h +++ b/src/llama-context.h @@ -262,6 +262,8 @@ struct llama_context { bool set_sampler(llama_seq_id seq_id, llama_sampler * sampler); + const int32_t * moe_cache_slots(int32_t il, int64_t n_tokens, int64_t n_expert_used) const; + private: llm_graph_result * get_gf_res_prev(); diff --git a/src/llama-moe-cache.cpp b/src/llama-moe-cache.cpp index 4d3596b244cb..53f505283c5e 100644 --- a/src/llama-moe-cache.cpp +++ b/src/llama-moe-cache.cpp @@ -252,6 +252,9 @@ struct llama_moe_cache::impl { // views used by copy_experts ggml_context_ptr ctx_views; + // the lookup that get_slots checks with the backend + mutable std::vector lookup_mem; + impl(const llama_model & model, const std::vector & backends, const std::vector & bufts, size_t size) : n_expert_used(model.hparams.n_expert_used_max()), layers(model.layers.size()) { for (size_t i = 0; i < backends.size(); ++i) { @@ -506,6 +509,27 @@ struct llama_moe_cache::impl { return l.slot_map; } + // the same conditions as build_moe_cache_slots, which falls back to the host experts if the device cannot look up the slots + const int32_t * get_slots(int32_t il, int64_t n_tokens, int64_t n_expert_used) const { + ggml_tensor * slot_map = get_slot_map(il, n_tokens, n_expert_used); + if (slot_map == nullptr) { + return nullptr; + } + lookup_mem.resize(2*ggml_tensor_overhead()); + ggml_init_params params = { + /*.mem_size =*/ lookup_mem.size(), + /*.mem_buffer =*/ lookup_mem.data(), + /*.no_alloc =*/ true, + }; + ggml_context_ptr ctx(ggml_init(params)); + ggml_tensor * ids = ggml_new_tensor_1d(ctx.get(), GGML_TYPE_I32, n_tokens*n_expert_used); + ggml_tensor * slots = ggml_get_rows(ctx.get(), slot_map, ids); + if (!ggml_backend_supports_op(backend(il), slots)) { + return nullptr; + } + return groups[layers[il].ig].slru.slot_map[il]; + } + ggml_tensor * get_experts(const ggml_tensor * w) const { const auto it = bindings.find(w); return it != bindings.end() ? it->second.cached : nullptr; @@ -647,6 +671,10 @@ ggml_tensor * llama_moe_cache::get_slot_map(int32_t il, int64_t n_tokens, int64_ return pimpl->get_slot_map(il, n_tokens, n_expert_used); } +const int32_t * llama_moe_cache::get_slots(int32_t il, int64_t n_tokens, int64_t n_expert_used) const { + return pimpl->get_slots(il, n_tokens, n_expert_used); +} + ggml_tensor * llama_moe_cache::get_experts(const ggml_tensor * w) const { return pimpl->get_experts(w); } diff --git a/src/llama-moe-cache.h b/src/llama-moe-cache.h index 9baf063909bc..cbe2c6e8e9d5 100644 --- a/src/llama-moe-cache.h +++ b/src/llama-moe-cache.h @@ -22,6 +22,10 @@ class llama_moe_cache { // the slot map of layer il, if its experts can be read from the cache for n_tokens tokens, nullptr otherwise ggml_tensor * get_slot_map(int32_t il, int64_t n_tokens, int64_t n_expert_used) const; + // the data of the slot map of layer il if the graph reads its experts from the cache for n_tokens tokens, nullptr otherwise + // an expert selected by the batch that has a slot here when the layer's lookup starts keeps it through that layer's plan + const int32_t * get_slots(int32_t il, int64_t n_tokens, int64_t n_expert_used) const; + // the experts of w in the cache, nullptr if w is not cached ggml_tensor * get_experts(const ggml_tensor * w) const;