Skip to content

Embeddings pooler meanpoolandnormalize

github-actions[bot] edited this page Aug 26, 2026 · 24 revisions

Development build. This page describes main, not a released package. The latest published Lodestar.Embeddings is 0.4.0 — read its documentation.

Pooler.MeanPoolAndNormalize

Mean-pools then L2-normalizes — the full sentence-embedding recipe.

public static float[] MeanPoolAndNormalize(ReadOnlySpan<float> tokenEmbeddings, int seqLen, int dim, ReadOnlySpan<long> attentionMask)

ParameterstokenEmbeddings is the encoder's output, row-major. seqLen is the number of token positions, dim the embedding dimension, and attentionMask marks a real token with a non-zero value and padding with zero. attentionMask has length seqLen.

Returnsfloat[] of length dim, of unit length unless every token was masked out.

ExceptionsArgumentException when the spans do not match the shape the other arguments declare.

Example — one sequence, one sentence embedding.

using Lodestar.Embeddings.Pooling;

float[] tokens = { 3f, 4f, 99f, 99f };
long[] mask = { 1L, 0L };

float[] embedding = Pooler.MeanPoolAndNormalize(tokens, seqLen: 2, dim: 2, mask);
float x = embedding[0];  // => 0.6
float y = embedding[1];  // => 0.8

RemarksThis is the call to reach for. It is MeanPool followed by L2Normalize, which is what sentence-transformers does to one forward pass, and doing the two steps separately gains nothing but the chance to forget the second.

Forgetting it is not a loud failure. An unnormalized vector still scores, still ranks, and ranks wrongly: a longer sentence tends to a longer vector, so a dot-product index quietly prefers it. Normalizing is what makes the score depend on direction alone.

The result is ready for EmbeddingIndex.Add, which is where most of these vectors are going.

Applies to — net10.0, netstandard2.0.

See alsoPooler.MeanPoolAndNormalizeBatch, Pooler.MeanPool, Pooler.

Lodestar

Project

Clone this wiki locally