|
liblloyal 1.0.0
Branched Inference for llama.cpp
|
Namespaces | |
| namespace | detail |
Enumerations | |
| enum class | Normalize : int32_t { None = 0 , L2 = 1 } |
| Normalization modes for embedding vectors. More... | |
Functions | |
| int32_t | dimension (const llama_model *model) |
| Get embedding dimension for model: the width the model outputs, which a model with its own output head (EmbeddingGemma 2) reports apart from its hidden width. | |
| bool | has_embeddings (const llama_model *model) |
| Check if model supports embeddings. | |
| bool | has_pooling (llama_context *ctx) |
| Check if context has pooling enabled. | |
| int32_t | pooling_type (llama_context *ctx) |
| Get pooling type for context. | |
| void | encode (llama_context *ctx, decode::SegmentSource &source) |
| Encode a whole input for embeddings: text, media, or both, as one stream. | |
| void | encode (llama_context *ctx, const llama_token *tokens, int32_t n_tokens, int32_t n_batch) |
| Encode tokens for embedding extraction: the text as a one-segment stream through encode(ctx, source), so every token's output is read from one dispatch. | |
| void | encode (llama_context *ctx, const std::vector< llama_token > &tokens, int32_t n_batch) |
| Convenience overload for std::vector<llama_token> | |
| std::vector< float > | get (llama_context *ctx, Normalize normalize=Normalize::L2) |
| Get embeddings for last decoded batch. | |
| std::vector< float > | get_seq (llama_context *ctx, llama_seq_id seq, Normalize normalize=Normalize::L2) |
| Get embeddings for specific sequence. | |
| std::vector< float > | get_ith (llama_context *ctx, int32_t idx, Normalize normalize=Normalize::L2) |
| Get embeddings for specific token index in last batch. | |
| float | cosine_similarity (const std::vector< float > &a, const std::vector< float > &b) |
| Compute cosine similarity between two embedding vectors. | |
|
strong |
Normalization modes for embedding vectors.
| Enumerator | |
|---|---|
| None | |
| L2 | |
Definition at line 49 of file embedding.hpp.
|
inline |
Compute cosine similarity between two embedding vectors.
| a | First embedding vector (should be L2-normalized) |
| b | Second embedding vector (should be L2-normalized) |
NOTE: For normalized vectors, cosine similarity = dot product
EXAMPLE: auto emb1 = embedding::get(ctx1, Normalize::L2); auto emb2 = embedding::get(ctx2, Normalize::L2); float sim = embedding::cosine_similarity(emb1, emb2);
Definition at line 446 of file embedding.hpp.
|
inline |
Get embedding dimension for model: the width the model outputs, which a model with its own output head (EmbeddingGemma 2) reports apart from its hidden width.
Every reader below takes the width from here.
| model | Llama model |
Definition at line 64 of file embedding.hpp.
|
inline |
Encode tokens for embedding extraction: the text as a one-segment stream through encode(ctx, source), so every token's output is read from one dispatch.
The same constraint on the context applies.
// Create dedicated embedding context ctx_params.embeddings = true; ctx_params.pooling_type = LLAMA_POOLING_TYPE_MEAN; auto embed_ctx = llama_init_from_model(model, ctx_params);
// Embed each text kv::clear_all(embed_ctx); embedding::encode(embed_ctx, tokens, 512); auto emb = embedding::get(embed_ctx);
| ctx | Llama context (embeddings=true; pooled, or a model without memory) |
| tokens | Token array to encode |
| n_tokens | Number of tokens in array |
| n_batch | Most tokens one text may take; a longer text is refused, never truncated |
| std::runtime_error | if the text does not fit or the context would read it in pieces |
| decode::DecodeError | if the dispatch fails |
Definition at line 227 of file embedding.hpp.
|
inline |
Convenience overload for std::vector<llama_token>
Definition at line 244 of file embedding.hpp.
|
inline |
Encode a whole input for embeddings: text, media, or both, as one stream.
The input is a segment stream (an MtmdSource built with add_special, for the model's own BOS and EOS; a decode::TokenSource for text), placed by the segments rail at sequence 0 from position 0, in one dispatch. Read the vector with get() afterwards.
The context must read the input whole: pooled, or without memory, or with attention that is not causal (see decode::StreamReading::whole). An embedding reads every entry of one dispatch, and the rail places a stream a segment at a time on any other context, so one is refused: a model that keeps memory and attends causally, created with embeddings on and pooling NONE. Per-token embeddings from such a context are not supported; give it a pooling type, or use an embedding model.
An input with nothing in it is refused: no dispatch would run, and get() would read back the vector of the input before it.
| ctx | Embedding context (embeddings=true; pooled, or a model without memory) |
| source | The input's segments |
| std::runtime_error | if the input is empty, the context would read it in pieces, or it is malformed or does not fit one dispatch |
| decode::DecodeError | if the dispatch fails |
Definition at line 186 of file embedding.hpp.
|
inline |
Get embeddings for last decoded batch.
| ctx | Llama context (must have pooling enabled) |
| normalize | Normalization mode (default: L2 for cosine similarity) |
| std::runtime_error | if extraction fails |
REQUIRES: Previous llama_decode() call with tokens
EXAMPLE: auto tokens = tokenizer::tokenize(model, "Hello world"); decode::many(ctx, tokens, 0, 512); auto embedding = embedding::get(ctx, Normalize::L2);
Definition at line 266 of file embedding.hpp.
|
inline |
Get embeddings for specific token index in last batch.
| ctx | Llama context |
| idx | Token index in batch |
| normalize | Normalization mode |
| std::runtime_error | if extraction fails |
USE CASE: Per-token embeddings for token-level analysis, kNN-LM
NOTE: Per-token embeddings may work without pooling enabled
Definition at line 395 of file embedding.hpp.
|
inline |
Get embeddings for specific sequence.
| ctx | Llama context |
| seq | Sequence ID |
| normalize | Normalization mode |
| std::runtime_error | if extraction fails |
USE CASE: Multi-sequence embedding extraction (batch embedding different texts)
NOTE: Falls back to get() for seq=0 if sequence-specific API unavailable
Definition at line 336 of file embedding.hpp.
|
inline |
Check if model supports embeddings.
| model | Llama model |
NOTE: This checks dimension only. For proper embeddings, the context must also be created with pooling enabled (LLAMA_POOLING_TYPE_MEAN, etc.)
Definition at line 82 of file embedding.hpp.
|
inline |
Check if context has pooling enabled.
| ctx | Llama context |
NOTE: Context must be created with pooling type != LLAMA_POOLING_TYPE_NONE for embeddings to work correctly.
Definition at line 97 of file embedding.hpp.
|
inline |
Get pooling type for context.
| ctx | Llama context |
Types:
Definition at line 118 of file embedding.hpp.