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
8 changes: 7 additions & 1 deletion ggml/src/ggml-cpu/ggml-cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,13 @@ static void ggml_compute_forward_mul_mat_id(
for (int id = 0; id < n_ids; ++id) {
const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);

assert(i02 >= 0 && i02 < n_as);
// a negative id selects no expert, its row is zero (the other experts are computed elsewhere)
if (i02 < 0) {
memset((char *) dst->data + id*nb1 + iid1*nb2, 0, ne0*sizeof(float));
continue;
}

assert(i02 < n_as);

MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
matrix_row_counts[i02] += 1;
Expand Down
2 changes: 2 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ extern "C" {
enum ggml_type type_v; // data type for V cache [EXPERIMENTAL]

size_t moe_cache_size; // device cache in bytes for the experts kept in host memory, split among the devices like the layers, 0 = disabled [EXPERIMENTAL]
int32_t moe_cache_cpu; // experts missing from the MoE cache: -1 = uploaded, N >= 0 = computed on the CPU, at most N of them per layer and batch still uploaded for later batches [EXPERIMENTAL]

// Abort callback
// if it returns true, execution of llama_decode() will be aborted
Expand Down Expand Up @@ -1175,6 +1176,7 @@ extern "C" {
// 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
#define LLAMA_HAS_MOE_CACHE_CPU 1 // llama_context_params::moe_cache_cpu
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);

//
Expand Down
4 changes: 3 additions & 1 deletion src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ llama_context::llama_context(
cparams.op_offload = params.op_offload;
cparams.kv_unified = params.kv_unified;
cparams.moe_cache_size = params.moe_cache_size;
cparams.moe_cache_cpu = params.moe_cache_cpu;

// initialized later
cparams.pipeline_parallel = false;
Expand Down Expand Up @@ -559,7 +560,7 @@ llama_context::llama_context(
}

if (cparams.moe_cache_size > 0) {
moe_cache = std::make_unique<llama_moe_cache>(model, backend_ptrs, backend_buft, cparams.moe_cache_size);
moe_cache = std::make_unique<llama_moe_cache>(model, backend_ptrs, backend_buft, cparams.moe_cache_size, cparams.moe_cache_cpu);
}

sched_reserve();
Expand Down Expand Up @@ -4022,6 +4023,7 @@ llama_context_params llama_context_default_params() {
/*.type_k =*/ GGML_TYPE_F16,
/*.type_v =*/ GGML_TYPE_F16,
/*.moe_cache_size =*/ 0,
/*.moe_cache_cpu =*/ -1,
/*.abort_callback =*/ nullptr,
/*.abort_callback_data =*/ nullptr,
/*.embeddings =*/ false,
Expand Down
1 change: 1 addition & 0 deletions src/llama-cparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct llama_cparams {
bool training; // set by llama_opt_init()

size_t moe_cache_size;
int32_t moe_cache_cpu;

std::vector<bool> embeddings_layer_inp; // [n_layer()] extract input embeddings for layer

Expand Down
66 changes: 57 additions & 9 deletions src/llama-graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,13 +1709,17 @@ ggml_tensor * llm_graph_context::build_lora_mm_id(
ggml_tensor * cur, // ggml_tensor * b
ggml_tensor * ids,
ggml_tensor * w_s,
ggml_tensor * slots) const {
ggml_tensor * slots,
ggml_backend_t backend) const {
// as in build_lora_mm: the folded weight reads the transformed input, the LoRA delta reads `cur`
ggml_tensor * cur_mm = build_hadamard_input(w, cur);
// the experts in the MoE cache are selected by their slots
ggml_tensor * res = slots == nullptr ?
ggml_mul_mat_id(ctx0, w, cur_mm, ids) :
ggml_mul_mat_id(ctx0, moe_cache->get_experts(w), cur_mm, slots);
if (backend) {
ggml_backend_sched_set_tensor_backend(sched, res, backend);
}

if (prec_policy) {
prec_policy->apply(res);
Expand Down Expand Up @@ -2355,7 +2359,8 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
ggml_build_forward_expand(gf, weights);

// the experts of host-resident layers may be read from the MoE cache
ggml_tensor * slots = build_moe_cache_slots(selected_experts, up_exps, gate_exps, down_exps, gate_up_exps, il);
moe_cache_cpu cache_cpu;
ggml_tensor * slots = build_moe_cache_slots(selected_experts, up_exps, gate_exps, down_exps, gate_up_exps, il, weight_before_ffn ? nullptr : &cache_cpu);

cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);

Expand All @@ -2366,12 +2371,26 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
cb(cur, "ffn_moe_weighted", il);
}

ggml_tensor * up = nullptr;
ggml_tensor * experts = nullptr;

// with the missing experts of the MoE cache on the CPU, the experts are built twice: on the device from the cache and on the CPU
// from the host weights, each weighted by the router only for the experts it holds, so each row of the sum comes from one of them
// the body of the loop is not indented, to keep this change small
ggml_tensor * const cur_in = cur;
ggml_tensor * experts_sum = nullptr;
const int n_pass = cache_cpu.cpu_ids ? 2 : 1;
for (int pass = 0; pass < n_pass; ++pass) {
ggml_tensor * const mm_ids = pass == 0 ? selected_experts : cache_cpu.cpu_ids;
ggml_tensor * const mm_slots = pass == 0 ? slots : nullptr;
ggml_backend_t const mm_backend = pass == 0 ? nullptr : backend_cpu;
ggml_tensor * const mm_weights = n_pass == 1 ? weights : ggml_mul(ctx0, weights, pass == 0 ? cache_cpu.dev_mask : cache_cpu.cpu_mask);
cur = cur_in;

ggml_tensor * up = nullptr;

if (gate_up_exps) {
// merged gate_up path: one mul_mat_id, then split into gate and up views
ggml_tensor * gate_up = build_lora_mm_id(gate_up_exps, cur, selected_experts, up_exps_s, slots); // [n_ff*2, n_expert_used, n_tokens]
ggml_tensor * gate_up = build_lora_mm_id(gate_up_exps, cur, mm_ids, up_exps_s, mm_slots, mm_backend); // [n_ff*2, n_expert_used, n_tokens]
cb(gate_up, "ffn_moe_gate_up", il);

if (up_exps_s) {
Expand All @@ -2390,7 +2409,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
cb(up, "ffn_moe_up", il);
} else {
// separate gate and up path
up = build_lora_mm_id(up_exps, cur, selected_experts, up_exps_s, slots); // [n_ff, n_expert_used, n_tokens]
up = build_lora_mm_id(up_exps, cur, mm_ids, up_exps_s, mm_slots, mm_backend); // [n_ff, n_expert_used, n_tokens]
cb(up, "ffn_moe_up", il);

if (up_exps_s) {
Expand All @@ -2403,7 +2422,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
}

if (gate_exps) {
cur = build_lora_mm_id(gate_exps, cur, selected_experts, gate_exps_s, slots); // [n_ff, n_expert_used, n_tokens]
cur = build_lora_mm_id(gate_exps, cur, mm_ids, gate_exps_s, mm_slots, mm_backend); // [n_ff, n_expert_used, n_tokens]
cb(cur, "ffn_moe_gate", il);
} else {
cur = up;
Expand Down Expand Up @@ -2515,7 +2534,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
cur = amax_src1.res;
}

experts = build_lora_mm_id(down_exps, cur, selected_experts, down_exps_s, slots); // [n_embd, n_expert_used, n_tokens]
experts = build_lora_mm_id(down_exps, cur, mm_ids, down_exps_s, mm_slots, mm_backend); // [n_embd, n_expert_used, n_tokens]
//ggml_prec_set_src(experts, GGML_PREC_BF16, 1); // (see TODO above)
cb(experts, "ffn_moe_down", il);

Expand All @@ -2541,10 +2560,14 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
}

if (!weight_before_ffn) {
experts = ggml_mul(ctx0, experts, weights);
experts = ggml_mul(ctx0, experts, mm_weights);
cb(experts, "ffn_moe_weighted", il);
}

experts_sum = experts_sum ? ggml_add(ctx0, experts_sum, experts) : experts;
}
experts = experts_sum;

ggml_build_forward_expand(gf, experts);

ggml_tensor * cur_experts[LLAMA_MAX_EXPERTS] = { nullptr };
Expand Down Expand Up @@ -2587,7 +2610,8 @@ ggml_tensor * llm_graph_context::build_moe_cache_slots(
ggml_tensor * gate_exps,
ggml_tensor * down_exps,
ggml_tensor * gate_up_exps,
int il) const {
int il,
moe_cache_cpu * cpu) const {
if (moe_cache == nullptr) {
return nullptr;
}
Expand Down Expand Up @@ -2617,6 +2641,30 @@ ggml_tensor * llm_graph_context::build_moe_cache_slots(
ggml_backend_sched_set_tensor_backend(sched, slots, moe_cache->backend(il));
cb(slots, "ffn_moe_slots", il);

// the missing experts on the CPU: the maps are looked up on the device like the slot map, so the copy callback plans before they are read
ggml_tensor * maps[3];
if (moe_cache->get_cpu_maps(il, &maps[0], &maps[1], &maps[2])) {
if (cpu == nullptr) {
return nullptr;
}
// all or none: a lookup in the graph makes the copy callback plan the layer
ggml_tensor * lookups[3];
for (int i = 0; i < 3; ++i) {
lookups[i] = ggml_get_rows(ctx0, maps[i], ids); // [1, n_expert_used*n_tokens]
if (!ggml_backend_supports_op(moe_cache->backend(il), lookups[i])) {
return nullptr;
}
}
for (ggml_tensor * lookup : lookups) {
ggml_backend_sched_set_tensor_backend(sched, lookup, moe_cache->backend(il));
ggml_build_forward_expand(gf, lookup);
}
cpu->cpu_ids = ggml_reshape_2d(ctx0, lookups[0], selected_experts->ne[0], selected_experts->ne[1]);
cpu->dev_mask = ggml_reshape_3d(ctx0, lookups[1], 1, selected_experts->ne[0], selected_experts->ne[1]);
cpu->cpu_mask = ggml_reshape_3d(ctx0, lookups[2], 1, selected_experts->ne[0], selected_experts->ne[1]);
cb(lookups[0], "ffn_moe_cpu_ids", il);
}

// add the lookup now: the transform of a Hadamard-folded expert reads host weights and would start its split first
ggml_build_forward_expand(gf, slots);

Expand Down
15 changes: 13 additions & 2 deletions src/llama-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1114,12 +1114,14 @@ struct llm_graph_context {

// do mat_mul_id, while optionally apply lora and per-expert scale
// if slots is set, the experts are read from the MoE cache at these slots (see build_moe_cache_slots)
// if backend is set, the mat_mul_id runs on it
ggml_tensor * build_lora_mm_id(
ggml_tensor * w, // ggml_tensor * as
ggml_tensor * cur, // ggml_tensor * b
ggml_tensor * ids,
ggml_tensor * w_s = nullptr,
ggml_tensor * slots = nullptr) const;
ggml_tensor * slots = nullptr,
ggml_backend_t backend = nullptr) const;

// rescale `cur` (the src1 of a matmul) by a power of two, computed from its maximum absolute
// keep both individual values and Q8 activation block sums within F16 range
Expand Down Expand Up @@ -1225,14 +1227,23 @@ struct llm_graph_context {
ggml_tensor * post_exp_norm = nullptr,
float post_exp_norm_eps = 1e-5f) const;

// the experts missing from the MoE cache, computed on the CPU (see llama_moe_cache::get_cpu_maps)
struct moe_cache_cpu {
ggml_tensor * cpu_ids = nullptr; // [n_expert_used, n_tokens], -1 for the experts in the cache
ggml_tensor * dev_mask = nullptr; // [1, n_expert_used, n_tokens]
ggml_tensor * cpu_mask = nullptr; // [1, n_expert_used, n_tokens]
};

// the slots of the selected experts in the MoE cache, nullptr if the experts of the layer are not read from the cache
// if the cache computes its missing experts on the CPU, cpu receives their lookups, or the cache is not used if cpu is nullptr
ggml_tensor * build_moe_cache_slots(
ggml_tensor * selected_experts,
ggml_tensor * up_exps,
ggml_tensor * gate_exps,
ggml_tensor * down_exps,
ggml_tensor * gate_up_exps,
int il) const;
int il,
moe_cache_cpu * cpu) const;

//
// inputs
Expand Down
Loading
Loading