Skip to content
1 min read · 256 words

@nhtio/adk/batteries/embeddings/transformers_js/pooling

Deterministic, battery-owned pooling + L2-normalization for the transformers.js embeddings battery.

Remarks

Used when poolingOwner: 'battery'. The pipeline is asked for raw pooling:'none' token states — a [batch, seq, hidden] tensor — and this module pools them into one vector per input in pure JS, identically across Node and the browser. That removes the per-runtime post-processing variance that widens the node↔browser cosine gap; only the irreducible ONNX-Runtime kernel floor remains.

Pooling strategies mirror the transformers.js feature-extraction options:

  • mean — attention-unaware mean over the sequence axis (matches the pipeline's 'mean', which also means over all positions for a single ungrouped sequence).
  • cls / first_token — the first token's hidden state.
  • eos / last_token — the last token's hidden state.
  • none — no pooling (caller already has the vector); returned unchanged per row.

Type Aliases

Type AliasDescription
Pooled2DA 2-D [batch, hidden] nested array (already-pooled vectors).
TokenStates3DA 3-D [batch, seq, hidden] nested array (raw token states from pooling:'none').

Variables

VariableDescription
defaultPoolAndNormalizeDefault poolAndNormalize.

Functions

FunctionDescription
l2NormalizeL2-normalize a vector in place-safe fashion (returns a new array). A zero vector is returned unchanged (no divide-by-zero).
poolAndNormalizePool raw [batch, seq, hidden] token states into [batch, hidden] vectors and optionally L2-normalize — the deterministic battery-owned path (poolingOwner: 'battery').