Are you an LLM? You can read better optimized documentation at /api/@nhtio/adk/batteries/embeddings/transformers_js/pooling.md for this page in Markdown format
@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 Alias | Description |
|---|---|
| Pooled2D | A 2-D [batch, hidden] nested array (already-pooled vectors). |
| TokenStates3D | A 3-D [batch, seq, hidden] nested array (raw token states from pooling:'none'). |
Variables
| Variable | Description |
|---|---|
| defaultPoolAndNormalize | Default poolAndNormalize. |
Functions
| Function | Description |
|---|---|
| l2Normalize | L2-normalize a vector in place-safe fashion (returns a new array). A zero vector is returned unchanged (no divide-by-zero). |
| poolAndNormalize | Pool raw [batch, seq, hidden] token states into [batch, hidden] vectors and optionally L2-normalize — the deterministic battery-owned path (poolingOwner: 'battery'). |