Skip to content
Open
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
19 changes: 19 additions & 0 deletions src/llama-graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,25 @@ ggml_tensor * llm_graph_context::build_inp_embd(ggml_tensor * tok_embd) const {
return cur;
}

llm_graph_input_embd_h * llm_graph_context::build_inp_embd_h(ggml_tensor * tok_embd_w, int il) const {
auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd);

inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
ggml_set_input(inp->tokens);

inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens);
ggml_set_input(inp->embd);

inp->tok_embd = ubatch.token ? ggml_get_rows(ctx0, tok_embd_w, inp->tokens) : inp->embd;
cb(inp->tok_embd, "mtp_tok_embd", il);

inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens);
ggml_set_input(inp->h);
ggml_set_name(inp->h, "mtp_h_input");

return (llm_graph_input_embd_h *) res->add_input(std::move(inp));
}

ggml_tensor * llm_graph_context::build_inp_pos() const {
auto inp = std::make_unique<llm_graph_input_pos>(hparams.n_pos_per_embd());

Expand Down
3 changes: 3 additions & 0 deletions src/llama-graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class llm_graph_input_embd_h : public llm_graph_input_i {
ggml_tensor * embd = nullptr; // F32 [n_embd, n_batch]
ggml_tensor * h = nullptr; // F32 [n_embd, n_batch]

ggml_tensor * tok_embd = nullptr; // F32 [n_embd, n_batch] token embeddings resolved from `tokens` or `embd`

const int64_t n_embd = 0;
};

Expand Down Expand Up @@ -1029,6 +1031,7 @@ struct llm_graph_context {
//

ggml_tensor * build_inp_embd(ggml_tensor * tok_embd) const;
llm_graph_input_embd_h * build_inp_embd_h(ggml_tensor * tok_embd_w, int il) const;
ggml_tensor * build_inp_pos() const;
ggml_tensor * build_inp_attn_scale() const;
ggml_tensor * build_inp_out_ids() const;
Expand Down
2 changes: 1 addition & 1 deletion src/llama-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
filter = [&](uint32_t il) { return il >= hparams.n_layer(); };
}

if (arch == LLM_ARCH_STEP35 && hparams.n_layer_nextn > 0) {
if ((arch == LLM_ARCH_STEP35 || arch == LLM_ARCH_GLM_DSA) && hparams.n_layer_nextn > 0) {
if (params.ctx_type == LLAMA_CONTEXT_TYPE_MTP) {
filter = [&](uint32_t il) { return il >= hparams.n_layer(); };
} else {
Expand Down
32 changes: 3 additions & 29 deletions src/models/cohere2moe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,42 +309,16 @@ llama_model_cohere2moe::graph_mtp::graph_mtp(const llama_model & model, const ll

const llm_norm_type cohere2moe_norm_type = hparams.f_norm_rms_eps == 0.0f ? LLM_NORM : LLM_NORM_RMS;

// TODO: extract in a common llm_graph_context::build_inp_embd_h()
auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd);

inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
ggml_set_input(inp->tokens);

inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens);
ggml_set_input(inp->embd);

// TODO: make static using `ggml_build_forward_select()`
// see llm_graph_context::build_inp_embd() for reference
ggml_tensor * tok_embd;
if (ubatch.token) {
ggml_tensor * tok_embd_w = layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd;
tok_embd = ggml_get_rows(ctx0, tok_embd_w, inp->tokens);
} else {
tok_embd = inp->embd;
}
cb(tok_embd, "mtp_tok_embd", il);

inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens);
ggml_set_input(inp->h);
ggml_set_name(inp->h, "mtp_h_input");

ggml_tensor * h_embd = inp->h;

res->add_input(std::move(inp));
auto * inp = build_inp_embd_h(layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd, il);

ggml_tensor * inp_pos = build_inp_pos();
ggml_tensor * inp_out_ids = build_inp_out_ids();
auto * inp_attn = build_attn_inp_kv_iswa();

ggml_tensor * h_norm = build_norm(h_embd, layer.nextn.hnorm, nullptr, cohere2moe_norm_type, il);
ggml_tensor * h_norm = build_norm(inp->h, layer.nextn.hnorm, nullptr, cohere2moe_norm_type, il);
cb(h_norm, "mtp_hnorm", il);

ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, cohere2moe_norm_type, il);
ggml_tensor * e_norm = build_norm(inp->tok_embd, layer.nextn.enorm, nullptr, cohere2moe_norm_type, il);
cb(e_norm, "mtp_enorm", il);

ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);
Expand Down
11 changes: 10 additions & 1 deletion src/models/deepseek2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ llama_model_deepseek2::graph::graph(const llama_model & model, const llm_graph_p
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
}
}
if (il == n_layer - 1 && inp_out_ids) {
// with NextN/MTP layers the full last-layer output feeds the MTP seed, so the gather moves after the final norm
if (il == n_layer - 1 && inp_out_ids && (hparams.n_layer_nextn == 0 || cparams.embeddings_nextn_masked)) {
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
}
Expand Down Expand Up @@ -423,8 +424,16 @@ llama_model_deepseek2::graph::graph(const llama_model & model, const llm_graph_p
}
cur = inpL;

// post-norm hidden state feeds both the LM head and the MTP seed below
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);

cb(cur, "h_nextn", -1);
res->t_h_nextn = cur;

if (hparams.n_layer_nextn > 0 && !cparams.embeddings_nextn_masked && inp_out_ids) {
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
}

cb(cur, "result_norm", -1);
res->t_embd = cur;

Expand Down
Loading