espnet2.legacy.nets.scorers.ctc.CTCPrefixScorer
espnet2.legacy.nets.scorers.ctc.CTCPrefixScorer
class espnet2.legacy.nets.scorers.ctc.CTCPrefixScorer(ctc: Module, eos: int, window_margin: int = 0)
Bases: BatchPartialScorerInterface
Decoder interface wrapper for CTCPrefixScore.
Initialize class.
- Parameters:
- ctc (torch.nn.Module) – The CTC implementation. For example,
espnet2.legacy.nets.pytorch_backend.ctc.CTC - eos (int) – The end-of-sequence id.
- window_margin (int) – Half-width, in encoder frames, of the window the CTC forward recursion is restricted to. 0 (default) walks the whole utterance at every decoding step, which is exact but costs O(T) per step. A positive value centres a window on the frame the prefix has reached and is an approximation.
- ctc (torch.nn.Module) – The CTC implementation. For example,
batch_init_state(x: Tensor, xs_lengths: Tensor = None)
Get an initial state for decoding.
- Parameters:
- x (torch.Tensor) – The encoded feature tensor. Either a single utterance (T, D) or a batch (B, T, D).
- xs_lengths (torch.Tensor) – Encoder output lengths (B,). Required when x is a padded batch; the padded frames are excluded from the CTC prefix scores.
Returns: initial state
batch_score_partial(y, ids, state, x)
Score new token.
- Parameters:
- y (torch.Tensor) – 1D prefix token
- ids (torch.Tensor) – torch.int64 next token to score
- state – decoder state for prefix tokens
- x (torch.Tensor) – 2D encoder feature that generates ys
- Returns: Tuple of a score tensor for y that has a shape (len(next_tokens),) and next state for ys
- Return type: tuple[torch.Tensor, Any]
batch_select_state(state, best_ids: Tensor)
Select states of a whole (n_utt, beam) hypothesis grid at once.
This is the vectorized counterpart of calling select_state() once per hypothesis, used by espnet2.legacy.nets.batch_beam_search.BatchBeamSearch.
- Parameters:
- state – The state returned by
batch_score_partial(). - best_ids (torch.Tensor) – (n_utt, beam) tensor of pruned candidates encoded as beam_index * odim + token_index.
- state – The state returned by
Returns: the reordered state, in the batched form that : batch_score_partial() accepts directly.
extend_prob(x: Tensor)
Extend probs for decoding.
This extension is for streaming decoding as in Eq (14) in https://arxiv.org/abs/2006.14941
- Parameters:x (torch.Tensor) – The encoded feature tensor
extend_state(state)
Extend state for decoding.
This extension is for streaming decoding as in Eq (14) in https://arxiv.org/abs/2006.14941
- Parameters:state – The states of hyps
Returns: exteded state
init_state(x: Tensor)
Get an initial state for decoding.
- Parameters:x (torch.Tensor) – The encoded feature tensor
Returns: initial state
score_partial(y, ids, state, x)
Score new token.
- Parameters:
- y (torch.Tensor) – 1D prefix token
- next_tokens (torch.Tensor) – torch.int64 next token to score
- state – decoder state for prefix tokens
- x (torch.Tensor) – 2D encoder feature that generates ys
- Returns: Tuple of a score tensor for y that has a shape (len(next_tokens),) and next state for ys
- Return type: tuple[torch.Tensor, Any]
select_state(state, i, new_id=None)
Select state with relative ids in the main beam search.
- Parameters:
- state – Decoder state for prefix tokens
- i (int) – Index to select a state in the main beam search
- new_id (int) – New label id to select a state if necessary
- Returns: pruned state
- Return type: state
