Skip to content

Embeddings 0.4.0 onnxtextembedder embed

github-actions[bot] edited this page Aug 21, 2026 · 1 revision

Lodestar.Embeddings 0.4.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

OnnxTextEmbedder.Embed

One vector, from token ids you already have.

public float[] Embed(ReadOnlySpan<long> inputIds, ReadOnlySpan<long> attentionMask)

ParametersinputIds are the token ids for one text, as the model's own tokenizer produces them. attentionMask is the same length, 1 for a real token and 0 for padding.

Returnsfloat[] of length Dimension, the pooled vector for that text.

ExceptionsArgumentException when the two spans differ in length. ObjectDisposedException after Dispose — the type checks a flag of its own, because reaching a disposed ONNX Runtime session surfaces as a null dereference from inside it, naming neither the object nor the mistake.

Example — ids from a tokenizer, one text at a time.

using Lodestar.Embeddings.Onnx;

using var embedder = new OnnxTextEmbedder("model.onnx");

long[] ids = [101, 2054, 2003, 102];
long[] mask = [1, 1, 1, 1];

float[] vector = embedder.Embed(ids, mask);

Remarks — this is the low-level entry: it takes ids rather than text, so the tokenizer is yours to choose and yours to match to the model. Mismatching them produces vectors that are confidently wrong rather than an error, which is the failure worth guarding against — use the vocabulary that shipped with the model.

For text rather than ids, EmbedBatch takes strings and does the encoding. It is also the faster path for more than one text: a session run has a fixed cost that a batch amortises.

The attentionMask matters even for a single unpadded text, where it is all ones — the model reads it, and pooling uses it to ignore padding.

Applies to — net10.0, netstandard2.0.

See alsoOnnxTextEmbedder.EmbedBatch, BatchEncoder.

Lodestar

Project

Clone this wiki locally