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

Method forward

funasr/models/uniasr/model.py:235–352  ·  view source on GitHub ↗

Frontend + 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

233 self.beam_search = None
234
235 def forward(
236 self,
237 speech: torch.Tensor,
238 speech_lengths: torch.Tensor,
239 text: torch.Tensor,
240 text_lengths: torch.Tensor,
241 **kwargs,
242 ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]:
243 """Frontend + Encoder + Decoder + Calc loss
244 Args:
245 speech: (Batch, Length, ...)
246 speech_lengths: (Batch, )
247 text: (Batch, Length)
248 text_lengths: (Batch,)
249 """
250 decoding_ind = kwargs.get("decoding_ind", None)
251 if len(text_lengths.size()) > 1:
252 text_lengths = text_lengths[:, 0]
253 if len(speech_lengths.size()) > 1:
254 speech_lengths = speech_lengths[:, 0]
255
256 batch_size = speech.shape[0]
257
258 ind = self.encoder.overlap_chunk_cls.random_choice(self.training, decoding_ind)
259 # 1. Encoder
260 if self.enable_maas_finetune:
261 with torch.no_grad():
262 speech_raw, encoder_out, encoder_out_lens = self.encode(
263 speech, speech_lengths, ind=ind
264 )
265 else:
266 speech_raw, encoder_out, encoder_out_lens = self.encode(speech, speech_lengths, ind=ind)
267
268 loss_att, acc_att, cer_att, wer_att = None, None, None, None
269 loss_ctc, cer_ctc = None, None
270 stats = dict()
271 loss_pre = None
272 loss, loss1, loss2 = 0.0, 0.0, 0.0
273
274 if self.loss_weight_model1 > 0.0:
275 ## model1
276 # 1. CTC branch
277 if self.enable_maas_finetune:
278 with torch.no_grad():
279
280 loss_att, acc_att, cer_att, wer_att, loss_pre = self._calc_att_predictor_loss(
281 encoder_out, encoder_out_lens, text, text_lengths
282 )
283
284 loss = loss_att + loss_pre * self.predictor_weight
285
286 # Collect Attn branch stats
287 stats["loss_att"] = loss_att.detach() if loss_att is not None else None
288 stats["acc"] = acc_att
289 stats["cer"] = cer_att
290 stats["wer"] = wer_att
291 stats["loss_pre"] = loss_pre.detach().cpu() if loss_pre is not None else None
292 else:

Callers

nothing calls this directly

Calls 6

encodeMethod · 0.95
encode2Method · 0.95
force_gatherableFunction · 0.90
random_choiceMethod · 0.80

Tested by

no test coverage detected