incerto.llm.NormalizedSequenceProb#

class incerto.llm.NormalizedSequenceProb[source]#

Bases: object

Length-normalized sequence probability.

Accounts for the fact that longer sequences naturally have lower probabilities. Common in beam search and generation.

Uses the standard length normalization formula:

score = sum(log_probs) / length^alpha

where alpha (length_penalty) controls the strength of normalization:
  • alpha=0: no normalization (favor short sequences)

  • alpha=1: normalize by length (average log prob)

  • alpha>1: favor longer sequences

__init__()#

Methods

__init__()

compute(logits, token_ids[, length_penalty, ...])

Compute length-normalized sequence probability.

static compute(logits, token_ids, length_penalty=1.0, mask=None, dim=-1)[source]#

Compute length-normalized sequence probability.

Parameters:
  • logits (Tensor) – Token logits of shape (batch, seq_len, vocab_size)

  • token_ids (Tensor) – Generated token IDs of shape (batch, seq_len)

  • length_penalty (float) – Length normalization exponent (alpha). Default 1.0.

  • mask (Optional[Tensor]) – Optional mask for padding of shape (batch, seq_len)

  • dim (int) – Dimension to compute softmax over (default: -1)

Return type:

Tensor

Returns:

Normalized probabilities of shape (batch,), in range [0, 1]. When alpha=1, this equals the geometric mean of per-token probabilities.