nnAudio 2: Overcoming Dynamic Compilation Barriers and Transform Inconsistencies

Abhinaba Roy, Junyi Liang, Dorien HerremansarXiv 2026

TL;DR

nnAudio 2 delivers a targeted, conservative modernization of the popular PyTorch audio feature-extraction toolbox. By fixing TorchScript compilation errors, eliminating silent inverse-transform failures, and adding a new differentiable inverse Constant-Q Transform (iCQT), this release bridges the gap between flexible research prototyping and robust production deployment.

What Problem Does This Paper Address?

Deep learning models for audio processing (like automatic music transcription or source separation) frequently compute time-frequency representations—like the Short-Time Fourier Transform (STFT)—on the fly. The original nnAudio toolbox popularized this approach in PyTorch by wrapping these transforms into neural network layers (using 1D convolutions), allowing gradients to flow directly back to the audio processing kernels.

However, as PyTorch and the broader scientific Python ecosystem evolved, nnAudio suffered from "software decay." It faced three major hurdles:

  1. TorchScript Incompatibility: The original code relied on dynamic state mutations and dynamic submodule construction, which modern torch.jit.script compilation strictly rejects. This blocked engineers from compiling and deploying models to production environments (like mobile or embedded systems).
  2. Silent iSTFT Failures: The inverse STFT (iSTFT) module allowed users to attempt waveform reconstruction from non-uniform frequency scales (like log-spaced grids). Instead of throwing an error, it silently returned highly degraded, mathematically incorrect waveforms that superficially looked plausible but corrupted the data.
  3. Dependency Drift: Upstream updates to libraries like SciPy broke auxiliary features, such as the Combined Frequency and Periodicity (CFP) module, causing immediate runtime crashes upon import.
Figure 1: nnAudio expresses each spectrogram as a 1D convolution whose kernels are parameters of an nn.Module, allowing gradients to flow back into the analysis kernels themselves. In eager mode this composes seamlessly with the rest of a PyTorch training pipeline. Under the original code, however, attempting to deploy a model via torch.jit.script fails for three independent reasons; the fixes in this work restore that path.

Methods

Rather than introducing entirely new transforms, the authors focused on refactoring the internal architecture of nnAudio to satisfy strict static-typing and compilation constraints:

  • State-Free Scripting Paths: They removed in-forward state mutations from the STFT and iSTFT classes, replacing them with local variables and explicit arguments.
  • Functional Padding: Dynamic padding submodules (nn.ConstantPad1d and nn.ReflectionPad1d) were replaced with fully scriptable functional calls (torch.nn.functional.pad).
  • Strict API Guards: They introduced an explicit runtime check within the iSTFT inversion path. If a user attempts an inversion using a non-uniform frequency scale, the library now throws a clear RuntimeError instead of failing silently.
  • Differentiable iCQT: They implemented a brand-new inverse Constant-Q Transform (iCQT) module based on Landweber iterations. This module is implemented entirely as a differentiable nn.Module, meaning gradients can flow seamlessly through the reconstruction loop during end-to-end training.

Datasets

Because nnAudio 2 is a feature-extraction library rather than a predictive model, it does not require large training datasets. Instead, the authors evaluated their changes using synthetic and standard regression testing signals:

  • A 1-second, 440 Hz pure tone sampled at 44.1 kHz to verify iCQT reconstruction fidelity.
  • A small suite of standard test signals (sinusoids, speech, and music excerpts) to measure round-trip reconstruction errors and ensure backward compatibility.

Key Insights and Ideas

  • The Insidiousness of Silent Bugs: The paper highlights how dangerous silent failures are in research toolboxes. An incorrect waveform reconstruction that "sounds okay" can easily slip past code reviews, leading researchers to publish subtly flawed results.
  • Software Maintenance as Research Output: The authors advocate for the value of software modernization papers. In machine learning, the infrastructure supporting reproducible research is built on community-maintained libraries; keeping these tools compatible with modern deployment toolchains is a necessity, not a luxury.
Figure 2: Silent iSTFT misbehavior under non-uniform frequency scales. A clean test signal (a, chirp + tone) is analyzed and inverted using the same overlap-add procedure that nnAudio’s iSTFT applies. With freq_scale=‘no’ (b) reconstruction is essentially perfect. With freq_scale=‘log’ (d) the same procedure produces a plausible-looking but largely incorrect waveform comparable in amplitude to the signal itself; the original library returns this without any warning. Panel (c) shows per-sample error on a logarithmic scale.

Key Results

  • Successful Compilation: Running torch.jit.script on the updated STFT and iSTFT modules now succeeds perfectly, opening up clear optimization and deployment paths.
  • High-Fidelity Inversion: The newly introduced iCQT successfully reconstructs waveforms from complex CQT coefficients, achieving a Signal-to-Noise Ratio (SNR) exceeding 30 dB.
  • Full Test Compliance: The updated codebase passes the entire original repository test suite under modern Python 3.11 and PyTorch 2.x environments, while fixing the mathematical discrepancy where the Variable-Q Transform (VQT) failed to exactly reduce to a CQT when $\gamma = 0$.

Limitations and Future Work

  • Log-Frequency iSTFT Still Unsupported: The authors choose to disallow log-frequency iSTFT entirely rather than fixing the underlying mathematics, as a true overlap-add inversion for non-uniform grids does not exist in a simple form. Users must still rely on alternative methods like Griffin-Lim for log-frequency magnitude reconstruction.
  • Trainable Kernels under TorchScript: While frozen kernels are thoroughly verified and scriptable, the interaction between trainable kernels and TorchScript compilation remains less tested and is flagged as an area for future work.
  • Partial Audit: Other auxiliary modules like Gammatone and MFCC were updated to ensure eager-mode behavior remains intact, but they have not been systematically overhauled for TorchScript compatibility.

Summary updated

Abstract

nnAudio is an open-source audio feature extraction toolbox for deep learning, but its use in current environments is hindered by TorchScript incompatibilities, inverse-transform edge cases, and dependency drift. We present a targeted modernization for modern PyTorch and scientific Python. We resolve TorchScript compilation failures in STFT and iSTFT by removing dynamic state mutation and module construction from scripted code paths and tightening argument handling in inverse-related helpers. We clarify inverse-STFT behavior by restricting reliable inversion to the uniform-bin setting (freq_scale=`no') and raising explicit runtime errors for unsupported frequency scales, preventing silently degraded reconstructions. We restore CFP compatibility with modern SciPy and ensure VQT reduces to CQT when gamma = 0. Regression tests cover the new STFT/iSTFT behaviors, and the updated codebase passes the full repository test suite in a modern Python environment. These improvements provide a more robust foundation for differentiable audio analysis in research and deployment.
BibTeX
@misc{roy2026nnaudio,
  author = {Abhinaba Roy and Junyi Liang and Dorien Herremans},
  title = {{nnAudio 2: Overcoming Dynamic Compilation Barriers and Transform Inconsistencies}},
  year = {2026},
  eprint = {2606.05394},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.05394},
}

Loading related content...

Figure