RuntimeError for contrastive loss if audio duration is not long enough for num_negatives
State: closed | Pull request: no
Labels: name: bug

name: stale

**Describe the bug**

If the duration of an audio file is <8s (unsure exactly, need further testing), then `sample_negatives` will throw the following error:

```sh
File "/opt/conda/lib/python3.8/site-packages/nemo/collections/asr/models/ssl_models.py", line 491, in validation_step                                                                                                      
  loss_value, _ = self.decoder_loss_step(spectrograms, spec_masks, encoded, encoded_len, targets, target_lengths)                                                                                                          
File "/opt/conda/lib/python3.8/site-packages/nemo/collections/asr/models/ssl_models.py", line 450, in decoder_loss_step                                                                                                    
  current_loss_value = current_loss(                                                                                                                                                                                       
File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1129, in _call_impl                                                                                                                         
  return forward_call(*input, **kwargs)                                                                                                                                                                                    
File "/opt/conda/lib/python3.8/site-packages/nemo/core/classes/common.py", line 963, in __call__                                                                                                                           
  outputs = wrapped(*args, **kwargs)                                                                                                                                                                                       
File "/opt/conda/lib/python3.8/site-packages/nemo/collections/asr/losses/ssl_losses/contrastive.py", line 205, in forward                                                                                                  
  negatives, _ = self.sample_negatives(targets_masked_only, targets_masked_only.size(0))  # T'xBxC  # T'                                                                                                                   
File "/opt/conda/lib/python3.8/site-packages/nemo/collections/asr/losses/ssl_losses/contrastive.py", line 139, in sample_negatives                                                                                         
  neg_idxs = torch.multinomial(torch.ones((num, high), device=y.device), self.num_negatives)                                                                                                                               
RuntimeError: cannot sample n_sample > prob_dist.size(-1) samples without replacement  
```

If you haven't read the docs or understand the code/loss function, then it might not be clear that you need to adjust the `min_duration` or `num_negatives`

**Steps/Code to reproduce bug**

Run any of the examples in [`speech_pretraining`](https://github.com/NVIDIA/NeMo/tree/main/examples/asr/speech_pretraining) with `train/validation_ds.min_duration < 8.0`

**Expected behavior**

While this is expected to fail, I would like to propose that we catch this error and replace with something more...constructive. 
I can open a PR, but wanted to see if the maintainers are open to the suggestion.

My suggestion is to re-raise with a `NeMoBaseException` (or make a new one? `LossMisconfigurationError`?) with the suggestion to either adjust `num_negative` or the `min_duration`.

Edit: I realized I opened this as a bug, but it should've been a feature suggestion.