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

Method forward

funasr/models/transducer/model.py:190–274  ·  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

188 self.ctc_weight = 0.0
189
190 def forward(
191 self,
192 speech: torch.Tensor,
193 speech_lengths: torch.Tensor,
194 text: torch.Tensor,
195 text_lengths: torch.Tensor,
196 **kwargs,
197 ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]:
198 """Encoder + Decoder + Calc loss
199 Args:
200 speech: (Batch, Length, ...)
201 speech_lengths: (Batch, )
202 text: (Batch, Length)
203 text_lengths: (Batch,)
204 """
205 if len(text_lengths.size()) > 1:
206 text_lengths = text_lengths[:, 0]
207 if len(speech_lengths.size()) > 1:
208 speech_lengths = speech_lengths[:, 0]
209
210 batch_size = speech.shape[0]
211 # 1. Encoder
212 encoder_out, encoder_out_lens = self.encode(speech, speech_lengths)
213 if (
214 hasattr(self.encoder, "overlap_chunk_cls")
215 and self.encoder.overlap_chunk_cls is not None
216 ):
217 encoder_out, encoder_out_lens = self.encoder.overlap_chunk_cls.remove_chunk(
218 encoder_out, encoder_out_lens, chunk_outs=None
219 )
220 # 2. Transducer-related I/O preparation
221 decoder_in, target, t_len, u_len = get_transducer_task_io(
222 text,
223 encoder_out_lens,
224 ignore_id=self.ignore_id,
225 )
226
227 # 3. Decoder
228 self.decoder.set_device(encoder_out.device)
229 decoder_out = self.decoder(decoder_in, u_len)
230
231 # 4. Joint Network
232 joint_out = self.joint_network(encoder_out.unsqueeze(2), decoder_out.unsqueeze(1))
233
234 # 5. Losses
235 loss_trans, cer_trans, wer_trans = self._calc_transducer_loss(
236 encoder_out,
237 joint_out,
238 target,
239 t_len,
240 u_len,
241 )
242
243 loss_ctc, loss_lm = 0.0, 0.0
244
245 if self.use_auxiliary_ctc:
246 loss_ctc = self._calc_ctc_loss(
247 encoder_out,

Callers

nothing calls this directly

Calls 8

encodeMethod · 0.95
_calc_transducer_lossMethod · 0.95
_calc_ctc_lossMethod · 0.95
_calc_lm_lossMethod · 0.95
get_transducer_task_ioFunction · 0.90
force_gatherableFunction · 0.90
remove_chunkMethod · 0.80
set_deviceMethod · 0.80

Tested by

no test coverage detected