From a29e679f7b25967885cc9b29f1ca00d18085acd4 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Thu, 26 Nov 2026 08:46:36 -0600 Subject: [PATCH] Distinguish token and embedding inputs when reusing graphs --- src/llama-context.cpp | 3 ++- src/llama-graph.cpp | 6 +++++- src/llama-graph.h | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 20168ade5f1c..9fa670937a36 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -3066,7 +3066,8 @@ ggml_cgraph * llama_context::graph_reserve( auto * res = gf_res_reserve.get(); - const auto gparams = graph_params(res, ubatch, mctx, ctx_type_to_graph_type(cparams.ctx_type)); + auto gparams = graph_params(res, ubatch, mctx, ctx_type_to_graph_type(cparams.ctx_type)); + gparams.reserve_mixed_inputs = true; res->reset(); diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp index 93e299f9f326..45ae8c4ac7fe 100644 --- a/src/llama-graph.cpp +++ b/src/llama-graph.cpp @@ -1693,6 +1693,7 @@ llm_graph_context::llm_graph_context(const llm_graph_params & params) : hparams (model.hparams), cparams (params.cparams), ubatch (params.ubatch), + reserve_mixed_inputs(params.reserve_mixed_inputs), prec_policy (model.prec_policy), n_embd (hparams.n_embd), n_layer (hparams.n_layer()), @@ -2757,7 +2758,10 @@ ggml_tensor * llm_graph_context::build_inp_embd(ggml_tensor * tok_embd, float to // mixed path (ubatch.is_mixed()): set_rows the token rows into a copy of the embd rows, with its own inputs as select branches must not share tensors // TODO: use inp->tokens and inp->embd once ggml_build_forward_select allows it - const bool has_mixed = llm_arch_supports_mixed_batch(model.arch) && cparams.ctx_type == LLAMA_CONTEXT_TYPE_DEFAULT; + // Reserve for a mixed batch, but only put its row writes into an execution graph + // when the batch supplies the slot indices consumed by those writes. + const bool has_mixed = (reserve_mixed_inputs || ubatch.is_mixed()) && + llm_arch_supports_mixed_batch(model.arch) && cparams.ctx_type == LLAMA_CONTEXT_TYPE_DEFAULT; const int64_t n_tok_rows = has_mixed ? llm_graph_n_tok_rows(ubatch) : 0; diff --git a/src/llama-graph.h b/src/llama-graph.h index ec9247456395..f99a8dc9e76b 100644 --- a/src/llama-graph.h +++ b/src/llama-graph.h @@ -911,6 +911,7 @@ struct llm_graph_params { // rows (n_shadow_rows of them); a ubatch with no label never applies it, so serving is the stable set's graph const llama_adapter_lora * shadow = nullptr; uint32_t n_shadow_rows = 0; + bool reserve_mixed_inputs = false; // the physical attention stream of each of the ubatch's unique sequences (seq_id_unq order): the graph's K/V // views are built at these streams, so a graph is reused only when they match std::vector streams = {}; @@ -1157,6 +1158,7 @@ struct llm_graph_context { const llama_hparams & hparams; const llama_cparams & cparams; const llama_ubatch & ubatch; + const bool reserve_mixed_inputs; const llama_prec_policy & prec_policy;