NeMo icon indicating copy to clipboard operation
NeMo copied to clipboard

DALI dataset silently accepts invalid window names: validation is unreachable dead code (dict.get in try/except)

Open udsy19 opened this issue 3 months ago • 1 comments

Describe the bug

In _DALIDataloader.__init__, the validation of the preprocessor's window config value can never fire:

# nemo/collections/asr/data/audio_to_text_dali.py:262-270
try:
    window_fn = torch_windows.get(window_name, None)
except:
    raise ValueError(
        f"{self} received '{window_name}' for the window parameter."
        f" It must be one of: ('hann', 'ones', 'hamming', 'blackman', 'bartlett', None)."
        f" None is equivalent to 'hann'."
    )
window_tensor = window_fn(self.window_size, periodic=False) if window_fn else None

dict.get(key, default) does not raise on a missing key, so the except block is unreachable dead code. An unrecognised window name silently produces window_fn = None → self.window = None, which is then passed to dali.fn.spectrogram(..., window_fn=None) (line 428), where DALI substitutes its own default Hann window.

So window: hanning, window: Hann, or window: blackmanharris in a DALI dataset config are all silently accepted and produce Hann-windowed features, instead of the error the code clearly intends to raise. A user who misspells the window name gets no feedback and trains on features that differ from the ones they configured.

The non-DALI path validates the same value correctly at nemo/collections/asr/modules/audio_preprocessing.py:393-397.

Steps/Code to reproduce bug

This mirrors lines 258-271 exactly and needs no DALI install:

import torch

torch_windows = {'hann': torch.hann_window, 'hamming': torch.hamming_window,
                 'blackman': torch.blackman_window, 'bartlett': torch.bartlett_window,
                 'none': None}

for window_name in ("hann", "hanning", "Hann"):
    raised = False
    try:
        window_fn = torch_windows.get(window_name, None)
    except Exception:
        raised = True
    window_tensor = window_fn(320, periodic=False) if window_fn else None
    print(window_name, "raised:", raised, "->",
          "real window" if window_tensor is not None else "None (DALI default Hann)")

Output:

hann raised: False -> real window
hanning raised: False -> None (DALI default Hann)
Hann raised: False -> None (DALI default Hann)

Expected behavior

An unrecognised window value should raise the ValueError the code already defines, e.g. by checking window_name not in torch_windows explicitly instead of wrapping dict.get in try/except.

The message text is also slightly off: it advertises None, but the actual dict key is the string 'none', so a config saying window: none is valid while the message suggests None.

Environment overview (please complete the following information)

  • Environment location: Bare-metal (macOS, arm64)
  • Method of NeMo install: from source, current main (d0a07c056)

Environment details

  • PyTorch 2.7.1
  • Python 3.12

Additional context

I have a fix ready and would be happy to open a PR.

udsy19 avatar Aug 18 '26 02:08 udsy19

Small correction to my own report while implementing the fix in #16075: calling the except branch strictly unreachable is slightly too strong.

dict.get doesn't raise on a missing key, so it is unreachable for any string window name — which is the case that matters in practice and the one the reproduction above covers. But an unhashable value (a plain list or dict reaching that code) does raise TypeError from the lookup itself, and the bare except converts it into the intended ValueError. So the branch is reachable in that narrow corner.

Worth noting because a naive fix that just swaps the try/except for a membership check regresses that case into a raw TypeError. The PR keeps it a ValueError with an explicit type guard, and adds a test for it.

Separately, while tracing this I noticed FilterbankFeatures.__init__ (nemo/collections/asr/parts/preprocessing/features.py:317) uses the same unvalidated torch_windows.get(window, None) pattern with no validation at all. Since AudioToMelSpectrogramPreprocessor delegates there, the mel path has the same silent-fallback behaviour as the DALI one — the validation I referenced above lives in AudioToMFCCPreprocessor, not the mel preprocessor. Happy to address that separately if maintainers would like it in scope.

udsy19 avatar Aug 18 '26 02:08 udsy19