MCPcopy Create free account
hub / github.com/modelscope/FunASR / forward

Method forward

funasr/models/transformer/model.py:175–286  ·  view source on GitHub ↗

Encoder + Decoder + Calc loss Args: speech: (Batch, Length, ...) speech_lengths: (Batch, ) text: (Batch, Length) text_lengths: (Batch,)

(
        self,
        speech: torch.Tensor,
        speech_lengths: torch.Tensor,
        text: torch.Tensor,
        text_lengths: torch.Tensor,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

173 self.beam_search = None
174
175 def forward(
176 self,
177 speech: torch.Tensor,
178 speech_lengths: torch.Tensor,
179 text: torch.Tensor,
180 text_lengths: torch.Tensor,
181 **kwargs,
182 ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]:
183 """Encoder + Decoder + Calc loss
184 Args:
185 speech: (Batch, Length, ...)
186 speech_lengths: (Batch, )
187 text: (Batch, Length)
188 text_lengths: (Batch,)
189 """
190 if len(text_lengths.size()) > 1:
191 text_lengths = text_lengths[:, 0]
192 if len(speech_lengths.size()) > 1:
193 speech_lengths = speech_lengths[:, 0]
194
195 batch_size = speech.shape[0]
196
197 # 1. Encoder
198 encoder_out, encoder_out_lens = self.encode(speech, speech_lengths)
199 intermediate_outs = None
200 if isinstance(encoder_out, tuple):
201 intermediate_outs = encoder_out[1]
202 encoder_out = encoder_out[0]
203
204 loss_att, acc_att, cer_att, wer_att = None, None, None, None
205 loss_ctc, cer_ctc = None, None
206 stats = dict()
207
208 # decoder: CTC branch
209 if self.ctc_weight != 0.0:
210 loss_ctc, cer_ctc = self._calc_ctc_loss(
211 encoder_out, encoder_out_lens, text, text_lengths
212 )
213
214 # Collect CTC branch stats
215 stats["loss_ctc"] = loss_ctc.detach() if loss_ctc is not None else None
216 stats["cer_ctc"] = cer_ctc
217
218 # Intermediate CTC (optional)
219 loss_interctc = 0.0
220 if self.interctc_weight != 0.0 and intermediate_outs is not None:
221 for layer_idx, intermediate_out in intermediate_outs:
222 # we assume intermediate_out has the same length & padding
223 # as those of encoder_out
224 loss_ic, cer_ic = self._calc_ctc_loss(
225 intermediate_out, encoder_out_lens, text, text_lengths
226 )
227 loss_interctc = loss_interctc + loss_ic
228
229 # Collect Intermedaite CTC stats
230 stats["loss_interctc_layer{}".format(layer_idx)] = (
231 loss_ic.detach() if loss_ic is not None else None
232 )

Callers

nothing calls this directly

Calls 6

encodeMethod · 0.95
_calc_ctc_lossMethod · 0.95
_calc_att_lossMethod · 0.95
cr_ctc_lossFunction · 0.90
force_gatherableFunction · 0.90
log_softmaxMethod · 0.45

Tested by

no test coverage detected