mtp: read the target hidden states from a device buffer (#31927)

This commit is contained in:
Aman Gupta authored and GitHub committed 2026-11-09 20:52:32 +08:00
1 parent b39680c41a
commit a8fad21e99
31 files changed
+453 -61

No files matched your search

+49
View File
@@ -223,6 +223,12 @@ void llm_graph_input_embd_h::set_input(const llama_ubatch * ubatch) {
ggml_backend_tensor_set(embd, ubatch->embd, 0, n_tokens*n_embd*ggml_element_size(embd));
}
if (h_rows) {
GGML_ASSERT(ubatch->data && ubatch->data->embd_state_row.size() == (size_t) n_tokens);
ggml_backend_tensor_set(h_rows, ubatch->data->embd_state_row.data(), 0, n_tokens*ggml_element_size(h_rows));
return;
}
GGML_ASSERT(ubatch->embd_state && "this graph requires a state embedding, see llama_batch_ext_set_embd_state()");
GGML_ASSERT(n_embd_state == h->ne[0]);
@@ -236,6 +242,10 @@ bool llm_graph_input_embd_h::can_reuse(const llm_graph_params & params) {
res &= (!params.ubatch.embd) || (embd && embd->ne[1] == params.ubatch.n_tokens);
res &= (!params.ubatch.embd_state) || (h && h->ne[1] == params.ubatch.n_tokens);
const bool has_rows = params.ubatch.data && !params.ubatch.data->embd_state_row.empty();
res &= has_rows == (h_rows != nullptr);
res &= !has_rows || h_rows->ne[0] == params.ubatch.n_tokens;
return res;
}
@@ -1588,6 +1598,8 @@ llm_graph_context::llm_graph_context(const llm_graph_params & params) :
hdmd (params.hdmd),
layer_inp_dev (params.layer_inp_dev),
layer_inp_src (params.layer_inp_src),
h_nextn_dev (params.h_nextn_dev),
h_nextn_src (params.h_nextn_src),
samplers (params.samplers),
cb_func (params.cb),
res (params.res),
@@ -2883,6 +2895,24 @@ ggml_tensor * llm_graph_context::build_inp_embd(ggml_tensor * tok_embd, float to
return cur;
}
ggml_tensor * llm_graph_context::build_inp_embd_state(llm_graph_input_embd_h * inp, int64_t n_embd_state) const {
if (ubatch.data && !ubatch.data->embd_state_row.empty()) {
GGML_ASSERT(h_nextn_src != nullptr && h_nextn_src->ne[0] == n_embd_state);
inp->h_rows = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
ggml_set_input(inp->h_rows);
ggml_set_name(inp->h_rows, "mtp_h_rows");
return ggml_get_rows(ctx0, h_nextn_src, inp->h_rows);
}
ggml_tensor * h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_state, n_tokens);
ggml_set_input(h);
ggml_set_name(h, "mtp_h_input");
return h;
}
ggml_tensor * llm_graph_context::build_inp_pos() const {
auto inp = std::make_unique<llm_graph_input_pos>(hparams.n_pos_per_embd(), hparams.rope_section_order);
@@ -4396,6 +4426,25 @@ void llm_graph_context::build_layer_inp_dev() const {
res->add_input(std::move(inp));
}
void llm_graph_context::build_h_nextn_dev() const {
ggml_tensor * src = res->t_h_nextn;
if (h_nextn_dev == nullptr || src == nullptr) {
return;
}
// the device buffer is only enabled for the unmasked extraction, which has a row per batch token
GGML_ASSERT(src->ne[0] == h_nextn_dev->ne[0] && ggml_nrows(src) == n_tokens);
auto inp = std::make_unique<llm_graph_input_batch_idxs>();
inp->idxs = ggml_new_tensor_1d(ctx0, GGML_TYPE_I64, n_tokens);
ggml_set_input(inp->idxs);
ggml_build_forward_expand(gf, ggml_set_rows(ctx0, h_nextn_dev, ggml_reshape_2d(ctx0, src, src->ne[0], n_tokens), inp->idxs));
res->add_input(std::move(inp));
}
void llm_graph_context::build_sampling() const {
if (samplers.empty() || !res->t_logits) {
return;