Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<llama_context *>(user_data);

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/llama-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
28 changes: 28 additions & 0 deletions src/llama-moe-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> lookup_mem;

impl(const llama_model & model, const std::vector<ggml_backend_t> & backends, const std::vector<ggml_backend_buffer_type_t> & 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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/llama-moe-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading