finalizeEmbedding function

List<double> finalizeEmbedding(
  1. EmbeddingOutputContract contract,
  2. ForwardResult result, {
  3. List<int>? attentionMask,
})

Turns a raw ForwardResult into the final vector per contract.

pooledFinal is copied verbatim after a [1, dim] shape check — see EmbeddingOutputContract.pooledFinal for why it must never fall through to meanPoolAndNormalize. tokenLevel is mean-pooled over attentionMask (the EFFECTIVE mask: the one the pass echoed in ForwardResult.attentionMask, else the tokenizer's) and L2-normalised, so padding a pass added never leaks into the mean.

Throws StateError for a pooledFinal result that is not [1, dim], and whatever meanPoolAndNormalize throws for a malformed token-level one.

Implementation

List<double> finalizeEmbedding(
  EmbeddingOutputContract contract,
  ForwardResult result, {
  List<int>? attentionMask,
}) => switch (contract) {
  EmbeddingOutputContract.pooledFinal => _copyPooledFinal(result),
  EmbeddingOutputContract.tokenLevel => meanPoolAndNormalize(
    result,
    attentionMask: attentionMask,
  ),
};