MCPcopy Create free account
hub / github.com/apple/axlearn / sample_decode

Method sample_decode

axlearn/audio/decoder_asr.py:517–558  ·  view source on GitHub ↗

CTC sample decoding. The output hypotheses will have blanks and repeats removed (via `_map_label_sequences`). To perform greedy decoding, provide `top_k_logits(1)` as the logits modifier. Args: input_batch: See `beam_search_decode`. num_decodes: See

(
        self,
        input_batch: Nested[Tensor],
        *,
        num_decodes: int = 1,
        logits_modifier: Optional[ConfigOr[LogitsToLogitsFn]] = None,
    )

Source from the content-addressed store, hash-verified

515 )
516
517 def sample_decode(
518 self,
519 input_batch: Nested[Tensor],
520 *,
521 num_decodes: int = 1,
522 logits_modifier: Optional[ConfigOr[LogitsToLogitsFn]] = None,
523 ) -> DecodeOutputs:
524 """CTC sample decoding.
525
526 The output hypotheses will have blanks and repeats removed (via `_map_label_sequences`).
527 To perform greedy decoding, provide `top_k_logits(1)` as the logits modifier.
528
529 Args:
530 input_batch: See `beam_search_decode`.
531 num_decodes: See `beam_search_decode`.
532 logits_modifier: An optional logits modifier to apply prior to softmax.
533 If None, do not modify the logits.
534
535 Returns:
536 See `beam_search_decode`.
537 """
538 cfg: CTCDecoderModel.Config = self.config
539 paddings: Tensor = input_batch["paddings"]
540 # Add 1 so we can drop EOS while ensuring decodes can be up to `num_frames`.
541 max_decode_len = paddings.shape[-1] + 1
542 sample_decode_outputs = sample_decode(
543 inputs=jnp.zeros_like(paddings),
544 time_step=jnp.zeros(paddings.shape[0], dtype=jnp.int32),
545 cache={"time_step": jnp.array(0)},
546 tokens_to_scores=self._tokens_to_scores(
547 input_batch, num_decodes=num_decodes, logits_modifier=logits_modifier
548 ),
549 num_decodes=num_decodes,
550 prng_key=self.prng_key,
551 max_decode_len=max_decode_len,
552 stop_decoding_condition=StopOnSubsequence([[cfg.vocab_size]]), # Dummy EOS token.
553 )
554 return self._postprocess_outputs(
555 sequences=sample_decode_outputs.sequences,
556 paddings=paddings,
557 scores=sample_decode_outputs.token_scores,
558 )
559
560 def greedy_decode(self, input_batch: Nested[Tensor]) -> DecodeOutputs:
561 """CTC greedy decoding.

Callers 1

sample_decodeMethod · 0.45

Calls 4

_tokens_to_scoresMethod · 0.95
_postprocess_outputsMethod · 0.95
sample_decodeFunction · 0.90
StopOnSubsequenceClass · 0.90

Tested by

no test coverage detected