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/ECAPA_TDNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def _compute_statistics(x, m, dim=2, eps=self.eps):
mean, std = _compute_statistics(x, mask / total)
mean = mean.unsqueeze(2).repeat(1, 1, L)
std = std.unsqueeze(2).repeat(1, 1, L)
attn = torch.cat([x, mean, std], dim=1)
attn = torch.cat([x, mean, std], dim=1).to(x.dtype)
else:
attn = x

Expand Down
39 changes: 39 additions & 0 deletions tests/unittests/test_ECAPA_TDNN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
import torch


@pytest.mark.parametrize(
"dtype", [torch.float16, torch.bfloat16, torch.float32]
)
@pytest.mark.parametrize("global_context", [True, False])
@pytest.mark.parametrize("use_lengths", [True, False])
def test_attentive_statistics_pooling_dtype(
device, dtype, global_context, use_lengths
):
from speechbrain.lobes.models.ECAPA_TDNN import AttentiveStatisticsPooling

pool = AttentiveStatisticsPooling(
channels=2, attention_channels=2, global_context=global_context
).to(device=device, dtype=dtype)
# Uniform attention gives the mean and standard deviation of valid frames.
with torch.no_grad():
pool.conv.conv.weight.zero_()
pool.conv.conv.bias.zero_()
x = torch.tensor(
[[[1, 3, 1, 3], [2, 4, 2, 4]], [[1, 3, 5, 7], [2, 4, 6, 8]]],
device=device,
dtype=dtype,
requires_grad=True,
)
lengths = torch.tensor([1.0, 0.5], device=device) if use_lengths else None

output = pool(x, lengths)

expected = [[2, 3, 1, 1], [2, 3, 1, 1]]
if not use_lengths:
expected[1] = [4, 5, 2.2360679775, 2.2360679775]
expected = torch.tensor(expected, device=device, dtype=dtype).unsqueeze(2)
torch.testing.assert_close(output, expected)
output.sum().backward()
assert torch.isfinite(x.grad).all()
assert all(torch.isfinite(param.grad).all() for param in pool.parameters())