Skip to content

FIX FilterBankLayer TorchScript with PyTorch 2.15 (master CI) - #1501

Merged
bruAristimunha merged 3 commits into
masterfrom
fix/torchscript-lfilter-torch215
Oct 30, 2026
Merged

bruAristimunha merged 3 commits into
masterfrom
fix/torchscript-lfilter-torch215

Conversation

@bruAristimunha

Copy link
Copy Markdown
Collaborator

Master's tests job has been red since CI started resolving PyTorch 2.15.0 (with torchaudio 2.11.0): test_torch_script fails for FBCNet, FBMSNet, FBLightConvNet and IFNet. The last green run of the same commit range used torch 2.14.1.

Cause

PyTorch 2.15 added a property to torch.autograd.Function:

@property
def input_grad_buffers(self) -> tuple[torch.Tensor | None, ...]:

TorchScript cannot parse the ... (Expression of type dots cannot be used in a type expression). Scripting FilterBankLayer compiles torchaudio.functional.lfilter, whose DifferentiableFIR / DifferentiableIIR are autograd.Function subclasses, so TorchScript compiles them as classes and hits that property. This breaks torch.jit.script on these four models for every user on torch 2.15, not only in CI.

Fix

braindecode/functional/_lfilter.py (private) provides lfilter / filtfilt (clamp=False, 1-D coefficients):

  • eager: calls torchaudio exactly as before (autograd unchanged), behind @torch.jit.unused;
  • scripted: runs the ops torchaudio's own scripted path runs (its ts_apply calls forward directly when scripting): the FIR part as conv1d, the IIR recursion through torchaudio's _lfilter_core_loop. Scripted outputs are unchanged.

It is a separate module because torch.jit.unused needs real annotations and filter.py uses from __future__ import annotations (same reason as _real_dft.py). FilterBankLayer._apply_iir now calls lfilter / filtfilt in an if/else instead of through a function-valued variable.

Evidence (CPU Jobs on Voyager)

  • torch 2.15.0 + torchaudio 2.11.0: master fails exactly the 4 tests; this branch passes all test_torch_script cases (90 passed, 1 skipped).
  • torch 2.14.1: this branch passes all test_torch_script cases (90 passed, 1 skipped) and test_modules.py.
  • Outputs: for FBCNet, FBMSNet, FBLightConvNet, IFNet and FIR FilterBankLayer (zero-phase and causal), every scripted output is bit-identical to master's scripted output, and every eager output is bit-identical to master's, except FBCNet eager, which already differs by up to 1.2e-5 between three identical runs on master itself (run-to-run nondeterminism on its FFT path, untouched here).
  • New tests: the scripted lfilter / filtfilt equal torchaudio's eager output bit for bit (torch.equal, FIR and IIR); FIR FilterBankLayer (zero-phase and causal) scripts and matches eager.

Not changed: an IIR FilterBankLayer does not script on master either (an unrelated empty-list typing error in the FIR-bank branch), before and after this PR.

PyTorch 2.15 added torch.autograd.Function.input_grad_buffers, annotated
tuple[Tensor | None, ...], which TorchScript cannot parse. Scripting
FilterBankLayer compiles torchaudio's lfilter, whose DifferentiableFIR and
DifferentiableIIR are autograd.Function subclasses, so FBCNet, FBMSNet,
FBLightConvNet and IFNet no longer script.

Eager filtering keeps calling torchaudio. The scripted path runs the ops
torchaudio's own scripted path runs (FIR conv1d, then its IIR core loop),
so scripted outputs are unchanged. _apply_iir calls lfilter or filtfilt in
an if/else, since the helpers no longer take torchaudio's clamp argument.
Copilot AI balanced review requested due to automatic review settings October 30, 2026 19:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the TorchScript filtering changes.

No open issues 🎉

What changed in this PR

This PR addresses the PyTorch 2.15 TorchScript failure affecting FIR FilterBankLayer instances and models that use them, while preserving eager filtering through torchaudio.

Changes:

  • Adds private scriptable filtering helpers that bypass torchaudio’s incompatible autograd classes.
  • Routes FilterBankLayer through the helpers with explicit causal and bidirectional branches.
  • Adds output-parity and scripting regression tests, plus a changelog entry.
File Description
test/​unit_tests/​models/​test_modules.py Tests filtering parity with torchaudio and FIR filter-bank scripting.
docs/​whats_new.rst Documents the PyTorch 2.15 scripting fix and affected models.
braindecode/​modules/​filter.py Uses the private filtering helpers and explicit IIR branches.
braindecode/​functional/​_lfilter.py Implements scriptable filtering paths while retaining torchaudio for eager execution.

Review effort: Balanced

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.07843% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.20%. Comparing base (05b764d) to head (f959e86).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1501      +/-   ##
==========================================
+ Coverage   91.19%   91.20%   +0.01%     
==========================================
  Files         168      169       +1     
  Lines       21862    21916      +54     
==========================================
+ Hits        19937    19989      +52     
- Misses       1925     1927       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Coverage cannot trace code that only runs inside TorchScript. Call the
scripted ops directly as well and check them against torchaudio bit for
bit, and check the Python core-loop fallback against torchaudio's loop.
Copilot AI balanced review requested due to automatic review settings October 30, 2026 20:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@bruAristimunha
bruAristimunha merged commit 9db6051 into master Oct 30, 2026
10 checks passed
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 2, 2026
Patch release of the 1.8 series: version 1.8.2 and its whats_new section.
Backports braindecode#1501 (TorchScript of the filter-bank models on PyTorch 2.15) and
braindecode#1164 (MNE >= 1.13 montage renames: FutureWarning at import, error from
MNE 1.14).
bruAristimunha added a commit to bruAristimunha/braindecode that referenced this pull request Nov 2, 2026
Patch release of the 1.8 series: version 1.8.2 and its whats_new section.
Backports braindecode#1501 (TorchScript of the filter-bank models on PyTorch 2.15) and
braindecode#1164 (MNE >= 1.13 montage renames: FutureWarning at import, error from
MNE 1.14).
bruAristimunha added a commit that referenced this pull request Nov 2, 2026
Backports #1501 (TorchScript-safe lfilter/filtfilt in FilterBankLayer) and #1164 (MNE >= 1.13 montage names) to 1.8.x; scripted-lfilter test tolerance on torchaudio < 2.11 (as #1527); version 1.8.2 and whats_new.
@bruAristimunha
bruAristimunha deleted the fix/torchscript-lfilter-torch215 branch November 22, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants