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
12 changes: 12 additions & 0 deletions src/torchaudio/functional/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,18 @@ def vad(
"""
device = waveform.device

if not bool(torch.isfinite(waveform).all()):
# The trigger measure is a running mean, so one non-finite sample poisons it
# permanently, and every later comparison against `trigger_level` is False.
# Nothing ever triggers and the whole waveform is reported as silence, which
# returns an empty tensor with no warning. Fail here instead.
raise ValueError(
"waveform must be finite everywhere, but it contains NaN or infinite values. "
"Vad tracks a running measure of the signal, and a single non-finite sample "
"makes every later comparison against trigger_level false, so the whole input "
"would be discarded as silence."
)

if waveform.ndim > 2:
warnings.warn(
"Expected input tensor dimension of 1 for single channel"
Expand Down
15 changes: 15 additions & 0 deletions test/torchaudio_unittest/transforms/transforms_test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,18 @@ def test_vad_on_zero_audio(self, input_shape, output_shape, sample_rate: int, pr
expected_output = torch.zeros(output_shape, dtype=self.dtype, device=self.device)
result = T.Vad(sample_rate, pre_trigger_time=pre_trigger_time)(inpt)
self.assertEqual(result, expected_output)

@parameterized.expand([(float("nan"),), (float("inf"),), (float("-inf"),)])
def test_vad_rejects_non_finite_audio(self, bad_value: float):
"""VAD should raise on non-finite input rather than report the signal as silence.

The trigger measure is a running mean, so one non-finite sample poisons it and
every later comparison against trigger_level is false. Nothing triggers and the
whole waveform is discarded, returning an empty Tensor. See
https://github.com/pytorch/audio/issues/4216.
"""
sample_rate = 16000
waveform = torch.zeros(sample_rate, dtype=self.dtype, device=self.device)
waveform[100] = bad_value
with self.assertRaisesRegex(ValueError, "finite"):
T.Vad(sample_rate)(waveform)