Repository: pytorch/audio

Issue:
F.vad on zero valued tensor returns nonempty tensor unexpectly.
### 🐛 Describe the bug

Here is a simple reproducible example.
```python
import torch
import torchaudio.functional as F

x = torch.zeros([16000])
y = F.vad(x, 16000)

print(f"input_size={x.size()}, ysize={y.size()}")
```

```
input_size=torch.Size([16000]), ysize=torch.Size([2400])
```


### Versions

Collecting environment information...
PyTorch version: 2.1.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 10 (buster) (x86_64)
GCC version: (Debian 8.3.0-6) 8.3.0
Clang version: 7.0.1-8+deb10u2 (tags/RELEASE_701/final)
CMake version: version 3.26.3
Libc version: glibc-2.28

Python version: 3.10.11 (main, Apr 20 2023, 00:02:35) [GCC 8.3.0] (64-bit runtime)
Python platform: Linux-5.4.56.bsk.10-amd64-x86_64-with-glibc2.28
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
Address sizes:       40 bits physical, 48 bits virtual
CPU(s):              32
On-line CPU(s) list: 0-31
Thread(s) per core:  1
Core(s) per socket:  16
Socket(s):           2
NUMA node(s):        2
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Platinum 8260 CPU @ 2.40GHz
Stepping:            7
CPU MHz:             2399.998
BogoMIPS:            4799.99
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            36608K
NUMA node0 CPU(s):   0-15
NUMA node1 CPU(s):   16-31
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat umip pku ospke avx512_vnni md_clear arch_capabilities

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] onnx==1.13.1
[pip3] onnxruntime==1.14.1
[pip3] torch==2.1.0+cpu
[pip3] torchaudio==2.1.0+cpu
[pip3] torchvision==0.16.0+cpu
[pip3] triton==2.0.0
[conda] Could not collect


Discussion:

Resolving patch:
diff --git a/src/torchaudio/functional/filtering.py b/src/torchaudio/functional/filtering.py
--- a/src/torchaudio/functional/filtering.py
+++ b/src/torchaudio/functional/filtering.py
@@ -1660,6 +1660,9 @@ def vad(
             flushedLen_ns = (measures_len - num_measures_to_flush) * measure_period_ns
             break
     # end for window
+    if not has_triggered:
+        return waveform[..., :0].view(shape[:-1] + torch.Size([0]))
+
     res = waveform[:, pos - samplesLen_ns + flushedLen_ns :]
     # unpack batch
     return res.view(shape[:-1] + res.shape[-1:])


Test patch:
diff --git a/test/torchaudio_unittest/transforms/transforms_test_impl.py b/test/torchaudio_unittest/transforms/transforms_test_impl.py
--- a/test/torchaudio_unittest/transforms/transforms_test_impl.py
+++ b/test/torchaudio_unittest/transforms/transforms_test_impl.py
@@ -478,3 +478,18 @@ def test_specaugment(self, n_time_masks, time_mask_param, n_freq_masks, freq_mas
             self.assertTrue(diff > 0)
         else:
             self.assertTrue(diff == 0)
+
+    @parameterized.expand(
+        [
+            ((32000,), (0,), 16000),
+            ((1, 32000), (1, 0), 32000),
+            ((2, 44100), (2, 0), 32000),
+            ((2, 2, 44100), (2, 2, 0), 32000),
+        ]
+    )
+    def test_vad_on_zero_audio(self, input_shape, output_shape, sample_rate: int):
+        """VAD should return zero when input is zero Tensor"""
+        inpt = torch.zeros(input_shape, dtype=self.dtype, device=self.device)
+        expected_output = torch.zeros(output_shape, dtype=self.dtype, device=self.device)
+        result = T.Vad(sample_rate)(inpt)
+        self.assertEqual(result, expected_output)