fairseq icon indicating copy to clipboard operation
fairseq copied to clipboard

Speech recognition fails due to NaN if there is an input of size 1

Open mgaido91 opened this issue 6 years ago • 2 comments

🐛 Bug

If there is an input sample with a single spectrogram, the training fails because everything is converted to NaN. This is because of the normalization which is applied to each item. Despite uncommon, this situation happens in common datasets, such as mozilla-voice.

To Reproduce

Run your command on a dataset which has a sample with 1 spectrogram and if distributed the command fails in the check gradient; if not, the loss becomes always NaN.

Code sample


>>> t=torch.tensor([[-0.7661, -1.3889, -2.0972, -0.9134, -0.7071, -0.9765, -0.8700, -0.8283,
...           0.7512,  1.3211,  2.1532,  2.1174,  1.2800,  1.2633,  1.6147,  1.6322,
...           2.0723,  3.1522,  3.2852,  2.2309,  2.5569,  2.2183,  2.2862,  1.5886,
...           0.8773,  0.8725,  1.2662,  0.9899,  1.1069,  1.3926,  1.2795,  1.1199,
...           1.1477,  1.2687,  1.3843,  1.1903,  0.8355,  1.1367,  1.2639,  1.4707]])
>>> apply_mv_norm(t)
tensor([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
         nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]])

Expected behavior

Despite such input may be unexpected/wrong, the training shouldn't fail because of a single bad sample. The training should go on successfully, and the normalization should not cause NaN

Environment

  • fairseq Version (e.g., 1.0 or master): master
  • PyTorch Version (e.g., 1.0): 1.4
  • OS (e.g., Linux): linux
  • How you installed fairseq (pip, source): source
  • Build command you used (if compiling from source): pip install -e .
  • Python version: 3.7.6
  • CUDA/cuDNN version: 10
  • GPU models and configuration: 8 K80
  • Any other relevant information:

Additional context

mgaido91 avatar Mar 18 '20 16:03 mgaido91

@mgaido91 , looks like the NaN happens in mean/variance normalization (apply_mv_norm) ? I can guess this is because we are using unbiased variance estimation, i.e., \sum_{i} (x_i - mean)^2/(N-1), which naturally happens to NaN when N=1. To confirm that, could you please point me which apply_mv_norm you are using ?

yqwangustc avatar May 26 '20 19:05 yqwangustc

Hi @yqwangustc, yes, I definitely agree with your analysis. Please see the PR I opened for this issue to check the function being used (please notice that in the current codebase there is no way to skip this normalization). Thanks.

mgaido91 avatar May 26 '20 22:05 mgaido91