Skip to content

Commit c51fb5b

Browse files
ggerganovshaobo.xie
authored andcommitted
graph : avoid huge warm-up graphs for MoE models (#14753)
* graph : avoid huge warm-up graphs for MoE models ggml-ci * cont : bump max nodes to 8x model tensors
1 parent c9da22b commit c51fb5b

2 files changed

Lines changed: 6 additions & 23 deletions

File tree

src/llama-context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ uint32_t llama_context::output_reserve(int32_t n_outputs) {
13961396
//
13971397

13981398
uint32_t llama_context::graph_max_nodes() const {
1399-
return std::max<uint32_t>(65536u, 5u*model.n_tensors());
1399+
return std::max<uint32_t>(1024u, 8u*model.n_tensors());
14001400
}
14011401

14021402
llm_graph_result * llama_context::get_gf_res_reserve() const {

src/llama-graph.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,28 +1206,11 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
12061206
}
12071207

12081208
// aggregate experts
1209+
// note: here we explicitly use hparams.n_expert_used instead of n_expert_used
1210+
// to avoid potentially a large number of add nodes during warmup
1211+
// ref: https://github.com/ggml-org/llama.cpp/pull/14753
12091212
ggml_tensor * moe_out = nullptr;
1210-
#ifdef GGML_USE_DLCU
1211-
if (cparams.ops_fusion) {
1212-
if (n_expert_used == 1) {
1213-
moe_out = ggml_reshape_2d(ctx0, experts, n_embd, n_tokens);
1214-
} else {
1215-
moe_out = ggml_moe_sum(ctx0, experts, n_expert_used);
1216-
}
1217-
} else {
1218-
for (int i = 0; i < n_expert_used; ++i) {
1219-
ggml_tensor * cur_expert = ggml_view_2d(ctx0, experts, n_embd, n_tokens,
1220-
experts->nb[2], i*experts->nb[1]);
1221-
1222-
if (i == 0) {
1223-
moe_out = cur_expert;
1224-
} else {
1225-
moe_out = ggml_add(ctx0, moe_out, cur_expert);
1226-
}
1227-
}
1228-
}
1229-
#else
1230-
for (int i = 0; i < n_expert_used; ++i) {
1213+
for (uint32_t i = 0; i < hparams.n_expert_used; ++i) {
12311214
ggml_tensor * cur_expert = ggml_view_2d(ctx0, experts, n_embd, n_tokens,
12321215
experts->nb[2], i*experts->nb[1]);
12331216

@@ -1239,7 +1222,7 @@ ggml_tensor * llm_graph_context::build_moe_ffn(
12391222
}
12401223
#endif // GGML_USE_DLCU
12411224

1242-
if (n_expert_used == 1) {
1225+
if (hparams.n_expert_used == 1) {
12431226
// avoid returning a non-contiguous tensor
12441227
moe_out = ggml_cont(ctx0, moe_out);
12451228
}

0 commit comments

Comments
 (0)