Forward chunk. Args: xs_pad: TODO. ilens: TODO. cache: State cache dict for streaming inference. ctc: TODO.
(
self,
xs_pad: torch.Tensor,
ilens: torch.Tensor,
cache: dict = None,
ctc: CTC = None,
)
| 477 | return overlap_feats |
| 478 | |
| 479 | def forward_chunk( |
| 480 | self, |
| 481 | xs_pad: torch.Tensor, |
| 482 | ilens: torch.Tensor, |
| 483 | cache: dict = None, |
| 484 | ctc: CTC = None, |
| 485 | ): |
| 486 | """Forward chunk. |
| 487 | |
| 488 | Args: |
| 489 | xs_pad: TODO. |
| 490 | ilens: TODO. |
| 491 | cache: State cache dict for streaming inference. |
| 492 | ctc: TODO. |
| 493 | """ |
| 494 | if cache is None: |
| 495 | cache = {} |
| 496 | xs_pad *= self.output_size() ** 0.5 |
| 497 | if self.embed is None: |
| 498 | xs_pad = xs_pad |
| 499 | else: |
| 500 | xs_pad = self.embed(xs_pad, cache) |
| 501 | if cache["tail_chunk"]: |
| 502 | xs_pad = to_device(cache["feats"], device=xs_pad.device) |
| 503 | else: |
| 504 | xs_pad = self._add_overlap_chunk(xs_pad, cache) |
| 505 | encoder_outs = self.encoders0(xs_pad, None, None, None, None) |
| 506 | xs_pad, masks = encoder_outs[0], encoder_outs[1] |
| 507 | intermediate_outs = [] |
| 508 | if len(self.interctc_layer_idx) == 0: |
| 509 | encoder_outs = self.encoders(xs_pad, None, None, None, None) |
| 510 | xs_pad, masks = encoder_outs[0], encoder_outs[1] |
| 511 | else: |
| 512 | for layer_idx, encoder_layer in enumerate(self.encoders): |
| 513 | encoder_outs = encoder_layer(xs_pad, None, None, None, None) |
| 514 | xs_pad, masks = encoder_outs[0], encoder_outs[1] |
| 515 | if layer_idx + 1 in self.interctc_layer_idx: |
| 516 | encoder_out = xs_pad |
| 517 | |
| 518 | # intermediate outputs are also normalized |
| 519 | if self.normalize_before: |
| 520 | encoder_out = self.after_norm(encoder_out) |
| 521 | |
| 522 | intermediate_outs.append((layer_idx + 1, encoder_out)) |
| 523 | |
| 524 | if self.interctc_use_conditioning: |
| 525 | ctc_out = ctc.softmax(encoder_out) |
| 526 | xs_pad = xs_pad + self.conditioning_layer(ctc_out) |
| 527 | |
| 528 | if self.normalize_before: |
| 529 | xs_pad = self.after_norm(xs_pad) |
| 530 | |
| 531 | if len(intermediate_outs) > 0: |
| 532 | return (xs_pad, intermediate_outs), None, None |
| 533 | return xs_pad, ilens, None |
| 534 | |
| 535 | |
| 536 | class EncoderLayerSANMExport(nn.Module): |
no test coverage detected