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

Method forward

axlearn/audio/decoder_asr.py:832–905  ·  view source on GitHub ↗

Computes the transducer loss. Args: input_batch: A dict containing: inputs: A Tensor of shape [batch_size, num_frames, dim]. paddings: A 0/1 Tensor of shape [batch_size, num_frames]. 1's represent paddings. target_labels: An int Te

(self, input_batch: Nested[Tensor])

Source from the content-addressed store, hash-verified

830 self._add_child("transducer", transducer_cfg)
831
832 def forward(self, input_batch: Nested[Tensor]) -> tuple[Tensor, Nested[Tensor]]:
833 """Computes the transducer loss.
834
835 Args:
836 input_batch: A dict containing:
837 inputs: A Tensor of shape [batch_size, num_frames, dim].
838 paddings: A 0/1 Tensor of shape [batch_size, num_frames]. 1's represent paddings.
839 target_labels: An int Tensor of shape [batch_size, num_labels]. Prediction target
840 of the transducer decoder.
841 target: A dictionary with input_ids as key, and an int Tensor of shape
842 [batch_size, num_labels] as value. Prediction inputs to the transducer decoder.
843
844 For both target_labels and target["input_ids"], values should be in the range
845 [0, vocab_size). target_labels does not contain BOS and valid label tokens are
846 followed by a EOS token. input_ids starts with a BOS token. Sequences are not
847 truncated. Out-of-range values are excluded from the loss calculation.
848
849 Returns:
850 A tuple (loss, per_example):
851 loss: A scalar of the transducer loss.
852 per_example: A dict containing transducer decoder outputs of the following keys:
853 weight: A tensor of shape [batch_size], the aggregation weight of the
854 per-example loss.
855 loss: A tensor of shape [batch_size] representing per-example loss.
856 """
857 cfg: TransducerDecoderModel.Config = self.config
858
859 # [batch, src_max_len, joint_dim].
860 am_data = self.am_proj(input_batch["inputs"])
861 am_paddings: Tensor = input_batch["paddings"]
862 chex.assert_type(am_paddings, jnp.bool)
863 target_labels: Tensor = input_batch["target_labels"]
864 target_paddings: Tensor = _compute_target_paddings(target_labels, vocab_size=cfg.vocab_size)
865
866 # [batch, tgt_max_len, joint_dim].
867 lm_data = self.lm_proj(self.prediction_network(inputs=input_batch["target"]["input_ids"]))
868
869 _, per_example = self.transducer(
870 am_data=am_data,
871 am_paddings=am_paddings,
872 lm_data=lm_data,
873 lm_paddings=target_paddings,
874 target_labels=target_labels,
875 )
876 per_example_loss, per_example_weight = (
877 per_example["loss"],
878 per_example["is_valid_example"],
879 )
880 per_example_weight = per_example_weight.astype(per_example_loss.dtype)
881
882 # Compute weighted loss.
883 loss = jnp.sum(per_example_loss * per_example_weight) / jnp.maximum(
884 per_example_weight.sum(), 1
885 )
886 aux_outputs = dict(per_example_weight=per_example_weight, per_example_loss=per_example_loss)
887
888 # Add input summaries.
889 input_summary = self._input_stats_summaries(

Callers

nothing calls this directly

Calls 6

WeightedSummaryClass · 0.90
_compute_target_paddingsFunction · 0.85
astypeMethod · 0.80
itemsMethod · 0.80
add_summaryMethod · 0.45

Tested by

no test coverage detected