Embed positions in tensor. Args: xs: padded input tensor (B, T, D) xs_lens: input length (B) decoding_chunk_size: decoding chunk size for dynamic chunk 0: default for training, use random dynamic chunk. <0: for decoding, us
(
self,
xs: torch.Tensor,
xs_lens: torch.Tensor,
decoding_chunk_size: int = 0,
num_decoding_left_chunks: int = -1,
)
| 109 | return self._output_size |
| 110 | |
| 111 | def forward( |
| 112 | self, |
| 113 | xs: torch.Tensor, |
| 114 | xs_lens: torch.Tensor, |
| 115 | decoding_chunk_size: int = 0, |
| 116 | num_decoding_left_chunks: int = -1, |
| 117 | ) -> Tuple[torch.Tensor, torch.Tensor]: |
| 118 | """Embed positions in tensor. |
| 119 | |
| 120 | Args: |
| 121 | xs: padded input tensor (B, T, D) |
| 122 | xs_lens: input length (B) |
| 123 | decoding_chunk_size: decoding chunk size for dynamic chunk |
| 124 | 0: default for training, use random dynamic chunk. |
| 125 | <0: for decoding, use full chunk. |
| 126 | >0: for decoding, use fixed chunk size as set. |
| 127 | num_decoding_left_chunks: number of left chunks, this is for decoding, |
| 128 | the chunk size is decoding_chunk_size. |
| 129 | >=0: use num_decoding_left_chunks |
| 130 | <0: use all left chunks |
| 131 | Returns: |
| 132 | encoder output tensor xs, and subsampled masks |
| 133 | xs: padded output tensor (B, T' ~= T/subsample_rate, D) |
| 134 | masks: torch.Tensor batch padding mask after subsample |
| 135 | (B, 1, T' ~= T/subsample_rate) |
| 136 | NOTE(xcsong): |
| 137 | We pass the `__call__` method of the modules instead of `forward` to the |
| 138 | checkpointing API because `__call__` attaches all the hooks of the module. |
| 139 | https://discuss.pytorch.org/t/any-different-between-model-input-and-model-forward-input/3690/2 |
| 140 | """ |
| 141 | T = xs.size(1) |
| 142 | masks = ~make_pad_mask(xs_lens, T).unsqueeze(1) # (B, 1, T) |
| 143 | if self.global_cmvn is not None: |
| 144 | xs = self.global_cmvn(xs) |
| 145 | xs, pos_emb, masks = self.embed(xs, masks) |
| 146 | mask_pad = masks # (B, 1, T/subsample_rate) |
| 147 | chunk_masks = add_optional_chunk_mask(xs, masks, |
| 148 | self.use_dynamic_chunk, |
| 149 | self.use_dynamic_left_chunk, |
| 150 | decoding_chunk_size, |
| 151 | self.static_chunk_size, |
| 152 | num_decoding_left_chunks) |
| 153 | |
| 154 | if self.gradient_checkpointing and self.training: |
| 155 | xs = self.forward_layers_checkpointed(xs, chunk_masks, pos_emb, |
| 156 | mask_pad) |
| 157 | else: |
| 158 | xs = self.forward_layers(xs, chunk_masks, pos_emb, mask_pad) |
| 159 | if self.normalize_before: |
| 160 | xs = self.after_norm(xs) |
| 161 | # Here we assume the mask is not changed in encoder layers, so just |
| 162 | # return the masks before encoder layers, and the masks will be used |
| 163 | # for cross attention with decoder later |
| 164 | return xs, masks |
| 165 | |
| 166 | def forward_layers(self, xs: torch.Tensor, chunk_masks: torch.Tensor, |
| 167 | pos_emb: torch.Tensor, |
nothing calls this directly
no test coverage detected