paddlespeech.s2t.modules.mask module

paddlespeech.s2t.modules.mask.add_optional_chunk_mask(xs: Tensor, masks: Tensor, use_dynamic_chunk: bool, use_dynamic_left_chunk: bool, decoding_chunk_size: int, static_chunk_size: int, num_decoding_left_chunks: int)[source]

Apply optional mask for encoder. Args:

xs (paddle.Tensor): padded input, (B, L, D), L for max length mask (paddle.Tensor): mask for xs, (B, 1, L) use_dynamic_chunk (bool): whether to use dynamic chunk or not use_dynamic_left_chunk (bool): whether to use dynamic left chunk for

training.

decoding_chunk_size (int): decoding chunk size for dynamic chunk, it's

0: default for training, use random dynamic chunk. <0: for decoding, use full chunk. >0: for decoding, use fixed chunk size as set.

static_chunk_size (int): chunk size for static chunk training/decoding

if it's greater than 0, if use_dynamic_chunk is true, this parameter will be ignored

num_decoding_left_chunks (int): number of left chunks, this is for decoding,

the chunk size is decoding_chunk_size. >=0: use num_decoding_left_chunks <0: use all left chunks

Returns:

paddle.Tensor: chunk mask of the input xs.

paddlespeech.s2t.modules.mask.make_non_pad_mask(lengths: Tensor) Tensor[source]

Make mask tensor containing indices of non-padded part. The sequences in a batch may have different lengths. To enable batch computing, padding is need to make all sequence in same size. To avoid the padding part pass value to context dependent block such as attention or convolution , this padding part is masked. This pad_mask is used in both encoder and decoder. 1 for non-padded part and 0 for padded part. Args:

lengths (paddle.Tensor): Batch of lengths (B,).

Returns:

paddle.Tensor: mask tensor containing indices of padded part. (B, T)

Examples:
>>> lengths = [5, 3, 2]
>>> make_non_pad_mask(lengths)
masks = [[1, 1, 1, 1 ,1],
         [1, 1, 1, 0, 0],
         [1, 1, 0, 0, 0]]
paddlespeech.s2t.modules.mask.make_pad_mask(lengths: Tensor) Tensor[source]

Make mask tensor containing indices of padded part. See description of make_non_pad_mask. Args:

lengths (paddle.Tensor): Batch of lengths (B,).

Returns:

paddle.Tensor: Mask tensor containing indices of padded part. (B, T)

Examples:
>>> lengths = [5, 3, 2]
>>> make_pad_mask(lengths)
masks = [[0, 0, 0, 0 ,0],
         [0, 0, 0, 1, 1],
         [0, 0, 1, 1, 1]]
paddlespeech.s2t.modules.mask.make_xs_mask(xs: Tensor, pad_value=0.0) Tensor[source]

Maks mask tensor containing indices of non-padded part. Args:

xs (paddle.Tensor): (B, T, D), zeros for pad.

Returns:

paddle.Tensor: Mask Tensor indices of non-padded part. (B, T)

paddlespeech.s2t.modules.mask.mask_finished_preds(pred: Tensor, flag: Tensor, eos: int) Tensor[source]

If a sequence is finished, all of its branch should be <eos> Args:

pred (paddle.Tensor): A int array with shape

(batch_size * beam_size, beam_size).

flag (paddle.Tensor): A bool array with shape

(batch_size * beam_size, 1).

Returns:

paddle.Tensor: (batch_size * beam_size).

paddlespeech.s2t.modules.mask.mask_finished_scores(score: Tensor, flag: Tensor) Tensor[source]

If a sequence is finished, we only allow one alive branch. This function aims to give one branch a zero score and the rest -inf score. Args:

score (paddle.Tensor): A real value array with shape

(batch_size * beam_size, beam_size).

flag (paddle.Tensor): A bool array with shape

(batch_size * beam_size, 1).

Returns:

paddle.Tensor: (batch_size * beam_size, beam_size).

Examples:
flag: tensor([[ True],

[False]])

score: tensor([[-0.3666, -0.6664, 0.6019],

[-1.1490, -0.2948, 0.7460]])

unfinished: tensor([[False, True, True],

[False, False, False]])

finished: tensor([[ True, False, False],

[False, False, False]])

return: tensor([[ 0.0000, -inf, -inf],

[-1.1490, -0.2948, 0.7460]])

paddlespeech.s2t.modules.mask.subsequent_chunk_mask(size: int, chunk_size: int, num_left_chunks: int = -1) Tensor[source]
Create mask for subsequent steps (size, size) with chunk size,

this is for streaming encoder

Args:

size (int): size of mask chunk_size (int): size of chunk num_left_chunks (int): number of left chunks

<0: use full chunk >=0: use num_left_chunks

Returns:

paddle.Tensor: mask, [size, size]

Examples:
>>> subsequent_chunk_mask(4, 2)
[[1, 1, 0, 0],
 [1, 1, 0, 0],
 [1, 1, 1, 1],
 [1, 1, 1, 1]]
paddlespeech.s2t.modules.mask.subsequent_mask(size: int) Tensor[source]

Create mask for subsequent steps (size, size). This mask is used only in decoder which works in an auto-regressive mode. This means the current step could only do attention with its left steps. In encoder, fully attention is used when streaming is not necessary and the sequence is not long. In this case, no attention mask is needed. When streaming is need, chunk-based attention is used in encoder. See subsequent_chunk_mask for the chunk-based attention mask. Args:

size (int): size of mask

Returns:

paddle.Tensor: mask, [size, size]

Examples:
>>> subsequent_mask(3)
[[1, 0, 0],
 [1, 1, 0],
 [1, 1, 1]]