Skip to content

Latest commit

 

History

History
709 lines (440 loc) · 21.6 KB

File metadata and controls

709 lines (440 loc) · 21.6 KB

nnAudio Migration Log

This file is append-only during the migration work. New work items, code changes, command results, and validation notes should be added as new dated entries. Existing entries should not be deleted.

2026-03-12 19:28 +08 - Initial Baseline

Scope

The working repo is the AMAAI Lab fork of nnAudio. The current effort is to prepare a maintainable "nnAudio 2.0" style update against modern PyTorch and packaging tooling.

Planned task buckets:

  1. Establish and improve compatibility with current PyTorch.
  2. Fix issue #132: TorchScript support.
  3. Handle issue #136: inverse behavior for non-uniform STFT frequency scales, especially freq_scale="log".
  4. Add a citation reminder for installed users in a way that is stable with modern packaging.

Current Environment

  • Repo root: /junyi/nnAudio
  • Current commit: 7ff8e82
  • Python: 3.11.10
  • Torch: 2.10.0+cpu
  • Installation mode: editable install from Installation/
  • Git remote: origin = git@github.com:AMAAI-Lab/nnAudio.git

Current Source Layout Notes

  • The Python package is not rooted at the repository top level. The package source and tests live under Installation/.
  • The package currently builds from Installation/setup.py.
  • The virtual environment is local to the repo at .venv/ and is excluded locally via .git/info/exclude.

Baseline Findings Before Any Source Changes

No tracked source files have been modified yet. The repository working tree is clean before code edits.

Issue #132: TorchScript support

Baseline status: reproducibly broken on current Torch.

Observed behavior:

  • torch.jit.script(STFT(...)) fails because STFT.forward() assigns self.num_samples inside forward.
  • After mentally bypassing that first blocker, the same method also contains dynamic module instantiation for padding, which TorchScript does not accept in its current form.
  • torch.jit.script(iSTFT(...)) also fails due to refresh_win == None style control flow that TorchScript does not type-infer correctly.

Relevant source locations:

  • Installation/nnAudio/features/stft.py:274-275
  • Installation/nnAudio/features/stft.py:278-287
  • Installation/nnAudio/features/stft.py:536-537

Baseline error summary:

  • STFT script error: Tried to set nonexistent attribute: num_samples
  • iSTFT script error: invalid comparison branch around refresh_win == None

Issue #136: inverse behavior for freq_scale != "no"

Baseline status: current inverse path only behaves like a near-perfect reconstruction for freq_scale="no".

Reconstruction measurements collected locally on random waveform input:

  • freq_scale="no": MSE ~= 1.46e-13, SNR ~= 128.4 dB
  • freq_scale="linear": MSE ~= 1.14, SNR ~= -0.53 dB
  • freq_scale="log": MSE ~= 1.67, SNR ~= -2.19 dB

Interpretation at baseline:

  • The current inverse implementation is appropriate for the standard uniform-bin STFT path.
  • The current inverse implementation is not numerically reliable for the non-uniform linear and log kernel variants.
  • This means issue #136 should not be treated as a simple "new Torch compatibility" bug. It is an API behavior and invertibility design problem.

Relevant source locations:

  • Installation/nnAudio/features/stft.py:15-63
  • Installation/nnAudio/features/stft.py:478-488
  • Installation/nnAudio/utils.py:319-385
  • Installation/nnAudio/utils.py:63-70

Immediate Working Strategy

Recommended execution order:

  1. Keep recording a baseline after every change in this file.
  2. Start with issue #132 because it is concrete, reproducible, and directly tied to modern Torch compatibility.
  3. After TorchScript support is cleaned up, do a wider PyTorch compatibility pass and rerun tests.
  4. Treat issue #136 as a separate design/behavior task after the Torch compatibility path is stable.
  5. Add citation messaging only after the core runtime path is stable.

Validation Policy For This Migration

After each code change batch:

  1. Run the relevant local command or tests.
  2. Append what changed.
  3. Append the observed result, including failures if any remain.

2026-03-12 19:29 +08 - Baseline Validation Run

No source changes were made in this step. This entry records the baseline validation state before implementation work starts.

Commands Run

  1. .venv/bin/pytest Installation/tests -q
  2. TorchScript smoke test for STFT and iSTFT
  3. STFT/iSTFT reconstruction comparison for freq_scale in {"no", "linear", "log"}

Results

Test suite

Pytest result:

  • 44 passed
  • 3 failed
  • Runtime about 44.27s

Current failing tests:

  1. Installation/tests/test_cfp.py::test_cfp_original[cpu]
  2. Installation/tests/test_cfp.py::test_cfp_new[cpu]
  3. Installation/tests/test_vqt.py::test_vqt_gamma_zero[cpu]

Failure summary:

  • The two CFP failures are caused by scipy.signal.blackmanharris no longer being exposed at the location used by current code.
  • The VQT failure shows a numerical mismatch between CQT1992v2(..., gamma=0 equivalent) and VQT(..., gamma=0) under the current dependency set.

Interpretation:

  • There are already broader modern dependency compatibility issues beyond issues #132 and #136.
  • This confirms that the migration should track a wider "current PyTorch and dependency compatibility" stream in addition to the two named GitHub issues.

TorchScript smoke test

Current result:

  • STFT: failed
  • iSTFT: failed

This remains consistent with the earlier issue #132 analysis.

Reconstruction comparison

Observed reconstruction quality:

  • no: mse=1.4596e-13, snr_db=128.3974
  • linear: mse=1.14027, snr_db=-0.5304
  • log: mse=1.67143, snr_db=-2.1912

This remains consistent with the earlier issue #136 analysis.

Updated Practical Task View

Based on the baseline test run, the migration currently has at least these active workstreams:

  1. Current PyTorch and dependency compatibility.
  2. Issue #132 TorchScript support.
  3. Issue #136 inverse behavior for non-uniform STFT frequency scales.
  4. Citation reminder behavior for users after installation or first use.

Current Repo State

  • Tracked source changes: none
  • New working note file added: MIGRATION_LOG.md

2026-03-12 22:20 +08 - Code Change Batch 1: TorchScript fixes for STFT/iSTFT

This is the first source-editing batch. The focus of this batch was issue #132 only.

Files Changed

  • Installation/nnAudio/features/stft.py
  • Installation/nnAudio/utils.py
  • Installation/tests/test_stft.py

What Was Changed

Installation/nnAudio/features/stft.py

  1. Removed the dynamic self.num_samples assignment from STFT.forward() and replaced it with a local variable.
  2. Replaced dynamic construction of nn.ConstantPad1d / nn.ReflectionPad1d inside forward() with functional padding calls.
  3. Added more explicit optional argument handling for TorchScript-facing methods:
    • STFT.forward(...)
    • STFT.inverse(...)
    • iSTFT.forward(...)
  4. Updated inverse_stft(...) so that scripted execution uses local window-normalization tensors instead of mutating module state during scripting.
  5. Preserved the eager-mode cache behavior for inverse normalization when not scripting.

Installation/nnAudio/utils.py

  1. Adjusted fold(..., stride=...) calls to pass the stride as a 2D tuple rather than a bare integer.
  2. Added explicit tensor/int annotations to:
    • torch_window_sumsquare(...)
    • overlap_add(...)

These helper updates were needed because iSTFT TorchScript compilation moved on to these functions after the first blocker in stft.py was fixed.

Installation/tests/test_stft.py

Added TorchScript regression tests for:

  1. STFT
  2. iSTFT

The new tests compare scripted output against eager-mode output.

Validation Run After Batch 1

Commands run:

  1. .venv/bin/python TorchScript smoke test for STFT and iSTFT
  2. .venv/bin/pytest Installation/tests/test_stft.py -q
  3. .venv/bin/pytest Installation/tests -q

Before vs After

TorchScript smoke test

Before this batch:

  • STFT: failed
  • iSTFT: failed

After this batch:

  • STFT: script_ok
  • iSTFT: script_ok

Installation/tests/test_stft.py

Before this batch:

  • No TorchScript regression tests were present.

After this batch:

  • 39 passed
  • No failures in test_stft.py

Full test suite

Before this batch:

  • 44 passed
  • 3 failed

After this batch:

  • 46 passed
  • 3 failed

Interpretation:

  • The two additional passes are the new TorchScript tests added in this batch.
  • No new global test failures were introduced by the TorchScript fixes.
  • The remaining failures are still the pre-existing CFP / VQT issues observed in the baseline.

Remaining Known Failures After Batch 1

Still failing in the full suite:

  1. Installation/tests/test_cfp.py::test_cfp_original[cpu]
  2. Installation/tests/test_cfp.py::test_cfp_new[cpu]
  3. Installation/tests/test_vqt.py::test_vqt_gamma_zero[cpu]

Failure summary remains:

  • CFP: scipy.signal.blackmanharris compatibility issue
  • VQT: gamma=0 numerical mismatch against CQT baseline

Status After Batch 1

  • Issue #132: fixed at the STFT / iSTFT level and now covered by tests
  • Issue #136: not addressed in this batch
  • Wider dependency compatibility: still pending

2026-03-12 22:21 +08 - Code Change Batch 2: CFP compatibility with modern SciPy

This batch focused on the next concrete compatibility failure exposed by the baseline suite: CFP initialization under the current SciPy version.

Files Changed

  • Installation/nnAudio/features/cfp.py

What Was Changed

Updated the Blackman-Harris window construction in both CFP implementations:

  1. Combined_Frequency_Periodicity
  2. CFP

The code previously called:

  • scipy.signal.blackmanharris(...)

The code now calls:

  • scipy.signal.windows.blackmanharris(...)

This matches the modern SciPy location for that window function.

Validation Run After Batch 2

Commands run:

  1. .venv/bin/pytest Installation/tests/test_cfp.py -q
  2. .venv/bin/pytest Installation/tests -q

Before vs After

CFP tests

Before this batch:

  • Installation/tests/test_cfp.py::test_cfp_original[cpu]: failed
  • Installation/tests/test_cfp.py::test_cfp_new[cpu]: failed

Both failures were due to:

  • AttributeError: module 'scipy.signal' has no attribute 'blackmanharris'

After this batch:

  • 2 passed
  • No CFP test failures remain

Full test suite

Before this batch:

  • 46 passed
  • 3 failed

After this batch:

  • 48 passed
  • 1 failed

Interpretation:

  • The CFP compatibility issue is resolved under the current dependency set.
  • No new regressions were introduced by the CFP change.
  • The remaining failing test is now only Installation/tests/test_vqt.py::test_vqt_gamma_zero[cpu].

Remaining Known Failure After Batch 2

Still failing:

  1. Installation/tests/test_vqt.py::test_vqt_gamma_zero[cpu]

Current failure description:

  • VQT(gamma=0) does not exactly match CQT1992v2(...) under the current test expectation.

Status After Batch 2

  • Issue #132: fixed and covered by tests
  • CFP / SciPy compatibility: fixed
  • Issue #136: still pending
  • Remaining full-suite blocker: VQT gamma=0 mismatch

2026-03-12 22:24 +08 - Code Change Batch 3: VQT gamma=0 compatibility

This batch addressed the last remaining full-suite failure: the mismatch between VQT(gamma=0) and CQT1992v2.

Files Changed

  • Installation/nnAudio/features/vqt.py

What Was Changed

Added an explicit gamma == 0 compatibility path inside VQT:

  1. VQT.__init__(...) now creates an internal CQT1992v2 module when gamma == 0.
  2. VQT.forward(...) now delegates to that CQT1992v2 instance when gamma == 0.

Rationale:

  • The failing test was not caused by tiny floating-point noise.
  • The measured difference before this batch had:
    • max_abs ~= 0.0894
    • mean_abs ~= 0.00167
  • That is large enough to treat as a genuine path mismatch rather than a tolerance issue.
  • Since VQT with gamma = 0 should reduce to CQT behavior, delegating to the CQT implementation is the most reliable way to restore expected behavior.

Validation Run After Batch 3

Commands run:

  1. .venv/bin/pytest Installation/tests/test_vqt.py -q
  2. .venv/bin/pytest Installation/tests -q

Before vs After

test_vqt.py

Before this batch:

  • Installation/tests/test_vqt.py::test_vqt_gamma_zero[cpu]: failed

After this batch:

  • 2 passed
  • No VQT test failures remain

Full test suite

Before this batch:

  • 48 passed
  • 1 failed

After this batch:

  • 49 passed
  • 0 failed

Interpretation:

  • The repository now passes the full available test suite under the current dependency set used in this environment.
  • The previous CFP, TorchScript, and VQT blockers are all cleared.

Status After Batch 3

  • Current PyTorch / dependency baseline: passing available tests
  • Issue #132: fixed and covered by tests
  • CFP / SciPy compatibility: fixed
  • VQT gamma=0 compatibility: fixed
  • Issue #136: still pending
  • Citation reminder feature: still pending

2026-03-12 22:34 +08 - Code Change Batch 4: Safe handling for issue #136

This batch addressed issue #136 by changing the behavior of inverse STFT on non-uniform frequency scales from "silently produce poor reconstructions" to "explicitly reject unsupported inverse use".

Files Changed

  • Installation/nnAudio/features/stft.py
  • Installation/tests/test_stft.py
  • README.md
  • Sphinx/source/intro.rst
  • Sphinx/source/index.rst

What Was Changed

Installation/nnAudio/features/stft.py

  1. Added explicit inverse-support tracking:
    • self.freq_scale
    • self.supports_inverse
  2. Added a shared inverse guard in STFTBase that raises when inverse STFT is requested with a non-uniform frequency scale.
  3. Added user-facing warnings during initialization when inverse-capable STFT/iSTFT objects are created with non-uniform frequency scales.
  4. Strengthened docstrings to state that reliable inverse STFT currently requires freq_scale='no'.

Behavioral rule after this batch:

  • freq_scale='no': inverse remains supported
  • freq_scale in {'linear', 'log', 'log2'}: inverse is rejected with a clear runtime error

Installation/tests/test_stft.py

Added regression tests for:

  1. STFT.inverse(...) with non-uniform frequency scales raising a clear error
  2. iSTFT(...) with non-uniform frequency scales raising a clear error
  3. Initialization warnings for those unsupported inverse configurations

Documentation

Added explicit notes in:

  1. README.md
  2. Sphinx/source/intro.rst
  3. Sphinx/source/index.rst

These notes explain that non-uniform STFT frequency scales are analysis-only and should not be used with inverse STFT.

Why This Fix Path Was Chosen

The earlier measurements already showed that the current inverse implementation was not just slightly inaccurate for non-uniform STFT bins. The mismatch was severe:

  • freq_scale='no': near-perfect reconstruction
  • freq_scale='linear': poor reconstruction
  • freq_scale='log': poor reconstruction

This means a "keep returning audio anyway" behavior is misleading. A safe and explicit rejection is better than silently returning a bad inverse.

Validation Run After Batch 4

Commands run:

  1. .venv/bin/pytest Installation/tests/test_stft.py -q
  2. Direct behavior check for:
    • successful inverse with freq_scale='no'
    • explicit runtime error with freq_scale='log'
  3. .venv/bin/pytest Installation/tests -q

Before vs After

Non-uniform inverse behavior

Before this batch:

  • iSTFT with freq_scale='linear' or freq_scale='log' returned numerically poor reconstructions
  • Example baseline values:
    • linear: mse ~= 1.14027, snr_db ~= -0.5304
    • log: mse ~= 1.67143, snr_db ~= -2.1912

After this batch:

  • Non-uniform inverse calls now raise:
    • RuntimeError: Inverse STFT is only supported for freq_scale='no' ...

Standard inverse behavior

After this batch, the standard uniform-bin path still works:

  • direct check with freq_scale='no': uniform_mse ~= 4.39e-14

Test suite

Before this batch:

  • 49 passed
  • 0 failed

After this batch:

  • 54 passed
  • 0 failed

Interpretation:

  • Five additional tests were added in this batch.
  • The suite remains fully passing after the behavior change.

Status After Batch 4

  • Current PyTorch / dependency baseline: passing available tests
  • Issue #132: fixed and covered by tests
  • Issue #136: addressed with an explicit safe failure mode and documentation
  • Citation reminder feature: still pending

2026-03-12 22:38 +08 - Code Change Batch 5: Citation reminder behavior

This batch implemented the citation reminder requested for users after installation/use.

Files Changed

  • Installation/nnAudio/__init__.py
  • Installation/tests/test_package.py

What Was Changed

Installation/nnAudio/__init__.py

Added package-level citation support:

  1. __citation__ constant with the nnAudio paper reference
  2. cite() helper returning the citation string
  3. show_citation() helper printing the citation string
  4. CitationReminderWarning custom warning class
  5. Import-time citation reminder, shown once per process by default
  6. NNAUDIO_DISABLE_CITATION_REMINDER=1 environment-variable escape hatch for users who want to suppress the reminder

Design choice:

  • A pure pip install build-time message is not consistently visible across modern packaging flows.
  • An import-time reminder is more stable and reliably seen by real users.
  • The package now also exposes the citation programmatically, so downstream projects can surface it however they want.

Installation/tests/test_package.py

Added tests to verify:

  1. Citation metadata exists and contains the DOI
  2. The citation warning appears on import by default
  3. The warning can be suppressed with NNAUDIO_DISABLE_CITATION_REMINDER=1

Validation Run After Batch 5

Commands run:

  1. .venv/bin/pytest Installation/tests/test_package.py -q
  2. direct import check:
    • package version
    • citation DOI presence
    • cite() returning the same string as __citation__
    • import-time warning visibility
  3. .venv/bin/pytest Installation/tests -q

Before vs After

Before this batch:

  • The package README asked users to cite the paper, but the installed package itself did not expose or surface that reminder programmatically.

After this batch:

  • Importing nnAudio shows a citation reminder by default
  • The full citation can be accessed through:
    • nnAudio.__citation__
    • nnAudio.cite()
    • nnAudio.show_citation()

Targeted test results:

  • Installation/tests/test_package.py: 3 passed

Full test suite:

Before this batch:

  • 54 passed
  • 0 failed

After this batch:

  • 57 passed
  • 0 failed

Interpretation:

  • The citation reminder feature is now implemented and covered by tests.
  • The suite remains fully passing after the packaging-facing change.

Status After Batch 5

  • Current PyTorch / dependency baseline: passing available tests
  • Issue #132: fixed and covered by tests
  • Issue #136: addressed with an explicit safe failure mode and documentation
  • Citation reminder feature: implemented and covered by tests

2026-03-13 02:22 +08 - Scope Clarification Against Professor Request

The implementation scope should be judged against the professor's explicit request only.

Professor's Explicit Requirements

From the original request, the explicit required scope is:

  1. Work from the AMAAI Lab fork rather than the original repository.
  2. Update nnAudio to be compatible with current Torch / current supporting libraries.
  3. Address issue #132.
  4. Address issue #136.
  5. Provide a citation reminder for users if possible.

Not Explicitly Requested By The Professor

The following were engineering suggestions or support actions, not separate professor requirements:

  1. README cleanup as a standalone task
  2. Packaging modernization as a standalone task
  3. Version bump / nnAudio 2.0 metadata cleanup as a standalone task
  4. Release-process or publication-prep cleanup as a standalone task

These should not be counted as missing professor tasks unless later requested explicitly.

Scope Interpretation For Work Already Done

  1. Code and validation were done in the AMAAI Lab fork working copy.
  2. Modern Torch / dependency compatibility was improved until the available local test suite passed.
  3. Issue #132 was fixed and covered by tests.
  4. Issue #136 was handled by changing unsafe inverse behavior into an explicit supported/unsupported boundary, which is now documented and tested.
  5. A citation reminder was implemented at import time, with programmatic access via the package API.

Important Note About The Citation Requirement

The professor's wording was:

  • "if they pip install and a message is shown to please cite the paper if they use it that would be beneficial"

What is implemented now:

  • a reminder shown on import nnAudio
  • citation metadata exposed through the package itself

What is not implemented literally:

  • a guaranteed build/install-time pip install console message

Reason:

  • modern packaging flows do not reliably expose setup-time print output to end users
  • import-time reminder is much more stable

Conclusion for scope judgment:

  • the citation requirement is substantially addressed
  • if the professor later insists on a literal install-time terminal message, that would still be a follow-up refinement

2026-03-18 00:00 +08 - Documentation Add-on: English Summary

This step added an English-facing summary document:

  • MIGRATION_SUMMARY.md

Purpose:

  • provide an English version of the migration summary
  • keep the technical content from the Chinese summary
  • trim most of the original "document purpose" and "scope" framing sections as requested

Notes:

  • no runtime logic was changed in this step
  • no new tests were required for this documentation-only addition