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

Method forward

funasr/models/whisper_lid/model.py:164–255  ·  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

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

Callers

nothing calls this directly

Calls 4

encodeMethod · 0.95
_calc_ctc_lossMethod · 0.95
_calc_att_lossMethod · 0.95
force_gatherableFunction · 0.90

Tested by

no test coverage detected