From 51c7089eeca581ee0a8b4aa3961a4bf87480d4a5 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Wed, 18 Nov 2026 22:08:17 -0600 Subject: [PATCH] Avoid allocating the logits graph for pooled-only embedding contexts --- include/llama.h | 1 + src/llama-context.cpp | 7 ++++++- src/llama-cparams.h | 1 + src/llama-model.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/llama.h b/include/llama.h index 0e59fe0df224..2cdf34626029 100644 --- a/include/llama.h +++ b/include/llama.h @@ -399,6 +399,7 @@ extern "C" { // Keep the booleans together and at the end of the struct to avoid misalignment during copy-by-value. bool embeddings; // if true, extract embeddings (together with logits) + bool embeddings_only; // pooled embeddings without vocabulary logits or sampling bool offload_kqv; // offload the KQV ops (including the KV cache) to GPU bool no_perf; // measure performance timings bool op_offload; // offload host tensor operations to device diff --git a/src/llama-context.cpp b/src/llama-context.cpp index 872894d66a56..49cd7fc868a0 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -113,6 +113,10 @@ llama_context::llama_context( cparams.yarn_attn_factor = params.yarn_attn_factor >= 0.0f ? params.yarn_attn_factor : hparams.yarn_attn_factor; cparams.yarn_beta_fast = params.yarn_beta_fast >= 0.0f ? params.yarn_beta_fast : hparams.yarn_beta_fast; cparams.yarn_beta_slow = params.yarn_beta_slow >= 0.0f ? params.yarn_beta_slow : hparams.yarn_beta_slow; + if (params.embeddings_only && (!params.embeddings || params.pooling_type == LLAMA_POOLING_TYPE_NONE || params.pooling_type == LLAMA_POOLING_TYPE_UNSPECIFIED)) { + throw std::runtime_error("embeddings_only requires embeddings and explicit sequence pooling"); + } + cparams.embeddings_only = params.embeddings_only; cparams.embeddings = params.embeddings; cparams.embeddings_nextn = false; cparams.embeddings_nextn_masked = false; @@ -2140,7 +2144,7 @@ uint32_t llama_context::output_reserve(int32_t n_outputs) { const auto n_embd = hparams.n_embd; const auto n_embd_out = hparams.n_embd_out(); - bool has_logits = true; + bool has_logits = !cparams.embeddings_only; bool has_embd = cparams.embeddings; bool has_embd_nextn = cparams.embeddings_nextn; @@ -3558,6 +3562,7 @@ llama_context_params llama_context_default_params() { /*.abort_callback =*/ nullptr, /*.abort_callback_data =*/ nullptr, /*.embeddings =*/ false, + /*.embeddings_only =*/ false, /*.offload_kqv =*/ true, /*.no_perf =*/ true, /*.op_offload =*/ true, diff --git a/src/llama-cparams.h b/src/llama-cparams.h index 9857c99f066d..2a20efd4c2d5 100644 --- a/src/llama-cparams.h +++ b/src/llama-cparams.h @@ -58,6 +58,7 @@ struct llama_cparams { float yarn_beta_slow; bool embeddings; + bool embeddings_only = false; bool embeddings_nextn; // also extract the hidden state before the final output norm bool embeddings_nextn_masked; // extract for only rows where batch.logits != 0 bool causal_attn; diff --git a/src/llama-model.cpp b/src/llama-model.cpp index f3fc3060f187..4acda1716bd2 100644 --- a/src/llama-model.cpp +++ b/src/llama-model.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params & params) { @@ -2663,6 +2664,45 @@ ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const { // TODO: move reranking logic here and generalize llm->build_dense_out(dense_2_out_layers, dense_2_out_layers_b, dense_3_out_layers); + if (params.cparams.embeddings_only) { + if (params.cparams.training || params.cparams.embeddings_nextn || !params.samplers.empty() || + std::any_of(params.cparams.embeddings_layer_inp.begin(), params.cparams.embeddings_layer_inp.end(), [](bool enabled) { return enabled; })) { + throw std::runtime_error("embeddings_only cannot train, sample, or extract auxiliary outputs"); + } + GGML_ASSERT(llm->res->t_embd_pooled != nullptr); + // Cache writes are explicit graph roots, not dependencies of cache reads. + // Preserve those roots and their original ordering when pruning logits. + const auto ancestors = [](ggml_tensor * root) { + std::unordered_set seen; + std::vector pending { root }; + while (!pending.empty()) { + auto * node = pending.back(); + pending.pop_back(); + if (!node || !seen.insert(node).second) { + continue; + } + for (auto * src : node->src) { + pending.push_back(src); + } + } + return seen; + }; + const auto logits = ancestors(llm->res->t_logits); + const auto pooled = ancestors(llm->res->t_embd_pooled); + auto * gf = llm->res->get_gf(); + std::vector roots; + for (int i = 0; i < ggml_graph_n_nodes(gf); ++i) { + auto * node = ggml_graph_node(gf, i); + if (!logits.count(node) || pooled.count(node)) { + roots.push_back(node); + } + } + llm->res->t_logits = nullptr; + ggml_graph_clear(gf); + for (auto * root : roots) { + ggml_build_forward_expand(gf, root); + } + } llm->res->set_outputs(params); return llm->res->get_gf();