Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion speechbrain/lobes/models/transformer/TransformerASR.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def make_transformer_src_tgt_masks(
# mask out audio beyond the length of audio for each batch
if wav_len is not None:
abs_len = torch.round(wav_len * src.shape[1])
src_key_padding_mask = ~length_to_mask(abs_len).bool()
src_key_padding_mask = ~length_to_mask(abs_len, max_len=src.shape[1]).bool()

# mask out the source
src_mask = make_transformer_src_mask(
Expand Down
24 changes: 24 additions & 0 deletions tests/unittests/test_transformer_src_tgt_masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
import torch.nn


def test_src_key_padding_mask_matches_padded_width(device):
from speechbrain.lobes.models.transformer.TransformerASR import (
make_transformer_src_tgt_masks,
)

src = torch.randn(3, 100, 80, device=device)
wav_len = torch.tensor([0.50, 0.75, 0.90], device=device)

src_key_padding_mask, _, _, _ = make_transformer_src_tgt_masks(src, wav_len=wav_len)

assert src_key_padding_mask.shape == (3, src.shape[1])

attention = torch.nn.MultiheadAttention(
embed_dim=src.shape[-1], num_heads=1, batch_first=True
).to(device)
output, _ = attention(
src,
src,
src,
key_padding_mask=src_key_padding_mask,
)
assert output.shape == src.shape


def test_make_transformer_src_tgt_masks(device):
from numpy import inf

Expand Down