MCPcopy Create free account
hub / github.com/ASLP-lab/OSUM / forward

Method forward

OSUM/wenet/efficient_conformer/encoder.py:244–298  ·  view source on GitHub ↗

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, use

(
        self,
        xs: torch.Tensor,
        xs_lens: torch.Tensor,
        decoding_chunk_size: int = 0,
        num_decoding_left_chunks: int = -1,
    )

Source from the content-addressed store, hash-verified

242 return factor
243
244 def forward(
245 self,
246 xs: torch.Tensor,
247 xs_lens: torch.Tensor,
248 decoding_chunk_size: int = 0,
249 num_decoding_left_chunks: int = -1,
250 ) -> Tuple[torch.Tensor, torch.Tensor]:
251 """Embed positions in tensor.
252 Args:
253 xs: padded input tensor (B, T, D)
254 xs_lens: input length (B)
255 decoding_chunk_size: decoding chunk size for dynamic chunk
256 0: default for training, use random dynamic chunk.
257 <0: for decoding, use full chunk.
258 >0: for decoding, use fixed chunk size as set.
259 num_decoding_left_chunks: number of left chunks, this is for decoding,
260 the chunk size is decoding_chunk_size.
261 >=0: use num_decoding_left_chunks
262 <0: use all left chunks
263 Returns:
264 encoder output tensor xs, and subsampled masks
265 xs: padded output tensor (B, T&#x27; ~= T/subsample_rate, D)
266 masks: torch.Tensor batch padding mask after subsample
267 (B, 1, T&#x27; ~= T/subsample_rate)
268 """
269 T = xs.size(1)
270 masks = ~make_pad_mask(xs_lens, T).unsqueeze(1) # (B, 1, T)
271 if self.global_cmvn is not None:
272 xs = self.global_cmvn(xs)
273 xs, pos_emb, masks = self.embed(xs, masks)
274 mask_pad = masks # (B, 1, T/subsample_rate)
275 chunk_masks = add_optional_chunk_mask(xs, masks,
276 self.use_dynamic_chunk,
277 self.use_dynamic_left_chunk,
278 decoding_chunk_size,
279 self.static_chunk_size,
280 num_decoding_left_chunks)
281 index = 0 # traverse stride
282 for i, layer in enumerate(self.encoders):
283 # layer return : x, mask, new_att_cache, new_cnn_cache
284 xs, chunk_masks, _, _ = layer(xs, chunk_masks, pos_emb, mask_pad)
285 if i in self.stride_layer_idx:
286 masks = masks[:, :, ::self.stride[index]]
287 chunk_masks = chunk_masks[:, ::self.stride[index], ::self.
288 stride[index]]
289 mask_pad = masks
290 pos_emb = pos_emb[:, ::self.stride[index], :]
291 index = index + 1
292
293 if self.normalize_before:
294 xs = self.after_norm(xs)
295 # Here we assume the mask is not changed in encoder layers, so just
296 # return the masks before encoder layers, and the masks will be used
297 # for cross attention with decoder later
298 return xs, masks
299
300 def forward_chunk(
301 self,

Callers

nothing calls this directly

Calls 2

make_pad_maskFunction · 0.90
add_optional_chunk_maskFunction · 0.90

Tested by

no test coverage detected