Skip to content
Merged
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
1 change: 1 addition & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/llama-cparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/llama-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>

static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params & params) {
Expand Down Expand Up @@ -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<ggml_tensor *> seen;
std::vector<ggml_tensor *> 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<ggml_tensor *> 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();
Expand Down